00001 /** 00002 \file Robots/LoBot/ui/LoDrawable.C 00003 \brief A base class for rendering things in the Robolocust UI. 00004 */ 00005 00006 // //////////////////////////////////////////////////////////////////// // 00007 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00008 // by the University of Southern California (USC) and the iLab at USC. // 00009 // See http://iLab.usc.edu for information about this project. // 00010 // //////////////////////////////////////////////////////////////////// // 00011 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00012 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00013 // in Visual Environments, and Applications'' by Christof Koch and // 00014 // Laurent Itti, California Institute of Technology, 2001 (patent // 00015 // pending; application number 09/912,225 filed July 23, 2001; see // 00016 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00017 // //////////////////////////////////////////////////////////////////// // 00018 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00019 // // 00020 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00021 // redistribute it and/or modify it under the terms of the GNU General // 00022 // Public License as published by the Free Software Foundation; either // 00023 // version 2 of the License, or (at your option) any later version. // 00024 // // 00025 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00026 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00027 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00028 // PURPOSE. See the GNU General Public License for more details. // 00029 // // 00030 // You should have received a copy of the GNU General Public License // 00031 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00032 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00033 // Boston, MA 02111-1307 USA. // 00034 // //////////////////////////////////////////////////////////////////// // 00035 // 00036 // Primary maintainer for this file: mviswana usc edu 00037 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/LoBot/ui/LoDrawable.C $ 00038 // $Id: LoDrawable.C 13958 2010-09-17 11:45:00Z mviswana $ 00039 // 00040 00041 //---------------------- ALTERNATIVE DEFINITION ------------------------- 00042 00043 // In case OpenGL and/or GLUT are missing 00044 // 00045 // NOTE: Don't really need to check INVT_HAVE_LIBGL and INVT_HAVE_LIBGLU 00046 // as well because it ought to be a pretty rare/broken installation that 00047 // has GLUT but not the OpenGL libraries... 00048 #ifndef INVT_HAVE_LIBGLUT 00049 00050 #include "Robots/LoBot/ui/LoDrawable.H" 00051 #include "Robots/LoBot/misc/LoExcept.H" 00052 00053 namespace lobot { 00054 00055 Drawable::Drawable() 00056 { 00057 throw missing_libs(MISSING_OPENGL) ; 00058 } 00059 00060 // Empty API 00061 void Drawable::gl_init(){} 00062 void Drawable::add_hook(const Drawable::RenderHook&){} 00063 void Drawable::render(){} 00064 void Drawable::render_me(){} 00065 void Drawable::zoom_by(float){} 00066 void Drawable::pan(int, int, int, int){} 00067 void Drawable::reset_zoom_pan(){} 00068 void Drawable::keypress(unsigned char){} 00069 void Drawable::unit_view_volume() const {} 00070 void Drawable::text_view_volume() const {} 00071 void Drawable::restore_view_volume() const {} 00072 00073 void Drawable::gl_cleanup(){} 00074 Drawable::~Drawable(){} 00075 00076 } // end of namespace encapsulating above empty definition 00077 00078 #else // pthreads, OpenGL and GLUT available ==> the real McCoy 00079 00080 //------------------------------ HEADERS -------------------------------- 00081 00082 // lobot headers 00083 #include "Robots/LoBot/ui/LoDrawable.H" 00084 #include "Robots/LoBot/config/LoConfigHelpers.H" 00085 #include "Robots/LoBot/misc/LoExcept.H" 00086 #include "Robots/LoBot/util/LoString.H" 00087 00088 // OpenGL headers 00089 #include <GL/glu.h> 00090 #include <GL/gl.h> 00091 00092 // Standard C++ headers 00093 #include <vector> 00094 00095 //----------------------------- NAMESPACE ------------------------------- 00096 00097 namespace lobot { 00098 00099 //-------------------------- INITIALIZATION ----------------------------- 00100 00101 Drawable::Drawable(const std::string& name, const Geometry& g) 00102 : m_name(name), m_geometry(g), 00103 m_border(true), m_border_color(1.0f, 1.0f, 0.0f) 00104 {} 00105 00106 Drawable::Geometry::Geometry(int xx, int yy, int w, int h) 00107 : x(xx), y(yy), width(w), height(h) 00108 {} 00109 00110 Drawable::Geometry::Geometry(const std::string& geometry) 00111 { 00112 std::vector<int> g = string_to_vector<int>(geometry) ; 00113 if (g.size() < 4) 00114 throw customization_error(BAD_GEOMETRY_SPEC) ; 00115 00116 x = g[0] ; 00117 y = g[1] ; 00118 width = g[2] ; 00119 height = g[3] ; 00120 } 00121 00122 void Drawable::gl_init(){} 00123 00124 void Drawable::add_hook(const Drawable::RenderHook& h) 00125 { 00126 AutoMutex M(m_hooks_mutex) ; 00127 m_hooks.push_back(h) ; 00128 } 00129 00130 //----------------------------- RENDERING ------------------------------- 00131 00132 void Drawable::render() 00133 { 00134 render_me() ; 00135 00136 // Render overlays 00137 AutoMutex M(m_hooks_mutex) ; 00138 std::for_each(m_hooks.begin(), m_hooks.end(), trigger_hook) ; 00139 } 00140 00141 void Drawable::trigger_hook(const Drawable::RenderHook& h) 00142 { 00143 h.first(h.second) ; 00144 } 00145 00146 //---------------------------- DEFAULT API ------------------------------ 00147 00148 void Drawable::render_me(){} 00149 void Drawable::zoom_by(float){} 00150 void Drawable::pan(int, int, int, int){} 00151 void Drawable::reset_zoom_pan(){} 00152 void Drawable::keypress(unsigned char){} 00153 00154 //------------------------------ HELPERS -------------------------------- 00155 00156 // Setup the OpenGL 2D view volume so that it goes from -1 to +1 in both 00157 // x- and y-directions. 00158 void Drawable::unit_view_volume() const 00159 { 00160 glMatrixMode(GL_PROJECTION) ; 00161 glPushMatrix() ; 00162 glLoadIdentity() ; 00163 gluOrtho2D(-1, 1, -1, 1) ; 00164 00165 glMatrixMode(GL_MODELVIEW) ; 00166 glPushMatrix() ; 00167 glLoadIdentity() ; 00168 glRotatef(get_conf("laser_viz", "lrf_direction", 90.0f), 0, 0, 1) ; 00169 } 00170 00171 // Setup the OpenGL 2D view volume so that it corresponds to the 00172 // drawable's geometry specification. 00173 void Drawable::text_view_volume() const 00174 { 00175 glMatrixMode(GL_PROJECTION) ; 00176 glPushMatrix() ; 00177 glLoadIdentity() ; 00178 gluOrtho2D(0, m_geometry.width, 0, m_geometry.height) ; 00179 00180 glMatrixMode(GL_MODELVIEW) ; 00181 glPushMatrix() ; 00182 glLoadIdentity() ; 00183 glRotatef(180, 0, 0, 1) ; 00184 glRotatef(180, 0, 1, 0) ; 00185 glTranslatef(0, -m_geometry.height, 0) ; 00186 } 00187 00188 // Setup the OpenGL 2D view volume using the supplied parameters 00189 void Drawable::setup_view_volume(float L, float R, float B, float T) const 00190 { 00191 glMatrixMode(GL_PROJECTION) ; 00192 glPushMatrix() ; 00193 glLoadIdentity() ; 00194 gluOrtho2D(L, R, B, T) ; 00195 00196 glMatrixMode(GL_MODELVIEW) ; 00197 glPushMatrix() ; 00198 glLoadIdentity() ; 00199 } 00200 00201 // Undo the effects of the above functions 00202 void Drawable::restore_view_volume() const 00203 { 00204 glMatrixMode(GL_PROJECTION) ; 00205 glPopMatrix() ; 00206 00207 glMatrixMode(GL_MODELVIEW) ; 00208 glPopMatrix() ; 00209 } 00210 00211 //----------------------------- CLEAN-UP -------------------------------- 00212 00213 void Drawable::gl_cleanup(){} 00214 Drawable::~Drawable(){} 00215 00216 //----------------------------------------------------------------------- 00217 00218 } // end of namespace encapsulating this file's definitions 00219 00220 #endif // #if !defined(INVT_HAVE_LIBPTHREAD) || !defined(INVT_HAVE_LIBGLUT) 00221 00222 /* So things look consistent in everyone's emacs... */ 00223 /* Local Variables: */ 00224 /* indent-tabs-mode: nil */ 00225 /* End: */