Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Examples

share-lists.cc

Simple display list sharing example.
// -*- C++ -*- /* * share-lists.cc: * Simple display list sharing example. * * written by Naofumi Yasufuku <naofumi@users.sourceforge.net> */ #include <iostream> #include <cstdlib> #include <gtkmm.h> #include <gtkglmm.h> #ifdef G_OS_WIN32 #define WIN32_LEAN_AND_MEAN 1 #include <windows.h> #endif #include <GL/gl.h> #include <GL/glu.h> // // Simple OpenGL scene base class. // class SimpleGLScene : public Gtk::GL::DrawingArea { public: explicit SimpleGLScene(const Glib::RefPtr<const Gdk::GL::Config>& config); SimpleGLScene(const Glib::RefPtr<const Gdk::GL::Config>& config, const Glib::RefPtr<const Gdk::GL::Context>& share_list); virtual ~SimpleGLScene(); protected: virtual void init_gl(); protected: virtual bool on_configure_event(GdkEventConfigure* event); virtual bool on_expose_event(GdkEventExpose* event); public: // Gtk::Widget::realize() is protected, so that ... void realize() { Gtk::GL::DrawingArea::realize(); } public: void set_light_diffuse(GLfloat r, GLfloat g, GLfloat b, GLfloat a) { m_LightDiffuse[0] = r; m_LightDiffuse[1] = g; m_LightDiffuse[2] = b; m_LightDiffuse[3] = a; } void set_light_position(GLfloat x, GLfloat y, GLfloat z, GLfloat w) { m_LightPosition[0] = x; m_LightPosition[1] = y; m_LightPosition[2] = z; m_LightPosition[3] = w; } protected: GLfloat m_LightDiffuse[4]; GLfloat m_LightPosition[4]; }; SimpleGLScene::SimpleGLScene(const Glib::RefPtr<const Gdk::GL::Config>& config) : Gtk::GL::DrawingArea(config) { set_light_diffuse(1.0, 1.0, 1.0, 1.0); set_light_position(1.0, 1.0, 1.0, 0.0); } SimpleGLScene::SimpleGLScene(const Glib::RefPtr<const Gdk::GL::Config>& config, const Glib::RefPtr<const Gdk::GL::Context>& share_list) : Gtk::GL::DrawingArea(config, share_list) { set_light_diffuse(1.0, 1.0, 1.0, 1.0); set_light_position(1.0, 1.0, 1.0, 0.0); } SimpleGLScene::~SimpleGLScene() { } void SimpleGLScene::init_gl() { glLightfv(GL_LIGHT0, GL_DIFFUSE, m_LightDiffuse); glLightfv(GL_LIGHT0, GL_POSITION, m_LightPosition); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); glClearColor(1.0, 1.0, 1.0, 1.0); glClearDepth(1.0); glViewport(0, 0, get_width(), get_height()); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(40.0, 1.0, 1.0, 10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glTranslatef(0.0, 0.0, -3.0); } bool SimpleGLScene::on_configure_event(GdkEventConfigure* event) { // // Get GL::Window. // Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window(); // // GL calls. // // *** OpenGL BEGIN *** if (!glwindow->gl_begin(get_gl_context())) return false; glViewport(0, 0, get_width(), get_height()); glwindow->gl_end(); // *** OpenGL END *** return true; } bool SimpleGLScene::on_expose_event(GdkEventExpose* event) { // // Get GL::Window. // Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window(); // // GL calls. // // *** OpenGL BEGIN *** if (!glwindow->gl_begin(get_gl_context())) return false; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glCallList(1); // Swap buffers. if (glwindow->is_double_buffered()) glwindow->swap_buffers(); else glFlush(); glwindow->gl_end(); // *** OpenGL END *** return true; } // // SimpleGLSceneMain (creates display lists) // class SimpleGLSceneMain : public SimpleGLScene { public: explicit SimpleGLSceneMain(const Glib::RefPtr<const Gdk::GL::Config>& config); virtual ~SimpleGLSceneMain(); protected: virtual void on_realize(); }; SimpleGLSceneMain::SimpleGLSceneMain(const Glib::RefPtr<const Gdk::GL::Config>& config) : SimpleGLScene(config) { } SimpleGLSceneMain::~SimpleGLSceneMain() { } void SimpleGLSceneMain::on_realize() { // We need to call the base on_realize() SimpleGLScene::on_realize(); // // Get GL::Window. // Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window(); // // GL calls. // // *** OpenGL BEGIN *** if (!glwindow->gl_begin(get_gl_context())) return; // Create display list #1 GLUquadricObj* qobj = gluNewQuadric(); gluQuadricDrawStyle(qobj, GLU_FILL); glNewList(1, GL_COMPILE); gluSphere(qobj, 1.0, 20, 20); glEndList(); // Initialize rendering context init_gl(); glwindow->gl_end(); // *** OpenGL END *** } // // SimpleGLSceneSub (shares display lists) // class SimpleGLSceneSub : public SimpleGLScene { public: SimpleGLSceneSub(const Glib::RefPtr<const Gdk::GL::Config>& config, const Glib::RefPtr<const Gdk::GL::Context>& share_list); virtual ~SimpleGLSceneSub(); protected: virtual void on_realize(); }; SimpleGLSceneSub::SimpleGLSceneSub(const Glib::RefPtr<const Gdk::GL::Config>& config, const Glib::RefPtr<const Gdk::GL::Context>& share_list) : SimpleGLScene(config, share_list) { } SimpleGLSceneSub::~SimpleGLSceneSub() { } void SimpleGLSceneSub::on_realize() { // We need to call the base on_realize() SimpleGLScene::on_realize(); // // Get GL::Window. // Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window(); // // GL calls. // // *** OpenGL BEGIN *** if (!glwindow->gl_begin(get_gl_context())) return; // Initialize rendering context init_gl(); glwindow->gl_end(); // *** OpenGL END *** } // // The application class. // class Simple : public Gtk::Window { public: explicit Simple(const Glib::RefPtr<const Gdk::GL::Config>& config); virtual ~Simple(); protected: // signal handlers: void on_button_quit_clicked(); protected: // member widgets: Gtk::VBox m_VBox; SimpleGLScene *m_GLScene1; SimpleGLScene *m_GLScene2; SimpleGLScene *m_GLScene3; Gtk::Button m_ButtonQuit; }; Simple::Simple(const Glib::RefPtr<const Gdk::GL::Config>& config) : m_VBox(false, 10), m_ButtonQuit("Quit") { // // Top-level window. // set_title("share-lists"); // Get automatically redrawn if any of their children changed allocation. set_reallocate_redraws(true); // Set border width. set_border_width(10); add(m_VBox); // // OpenGL scene #1 (SimpleGLSceneMain: creates display lists) // m_GLScene1 = new SimpleGLSceneMain(config); m_GLScene1->set_size_request(120, 120); m_GLScene1->set_light_diffuse(1.0, 0.0, 0.0, 0.0); // red m_VBox.pack_start(*m_GLScene1); // // Get OpenGL rendering context. // m_GLScene1->realize(); Glib::RefPtr<Gdk::GL::Context> glcontext = m_GLScene1->get_gl_context(); // // OpenGL scene #2 (SimpleGLSceneSub: shares display lists) // m_GLScene2 = new SimpleGLSceneSub(config, glcontext); m_GLScene2->set_size_request(120, 120); m_GLScene2->set_light_diffuse(1.0, 1.0, 0.0, 0.0); // yellow m_VBox.pack_start(*m_GLScene2); // // OpenGL scene #3 (SimpleGLSceneSub: shares display lists) // m_GLScene3 = new SimpleGLSceneSub(config, glcontext); m_GLScene3->set_size_request(120, 120); m_GLScene3->set_light_diffuse(0.0, 1.0, 0.0, 0.0); // green m_VBox.pack_start(*m_GLScene3); // // Simple quit button. // m_ButtonQuit.signal_clicked().connect( sigc::mem_fun(*this, &Simple::on_button_quit_clicked)); m_VBox.pack_start(m_ButtonQuit, Gtk::PACK_SHRINK, 0); // // Show window. // show_all(); } Simple::~Simple() { delete m_GLScene1; delete m_GLScene2; delete m_GLScene3; } void Simple::on_button_quit_clicked() { Gtk::Main::quit(); } // // Main. // int main(int argc, char** argv) { Gtk::Main kit(argc, argv); // // Init gtkglextmm. // Gtk::GL::init(argc, argv); // // Configure OpenGL-capable visual. // Glib::RefPtr<Gdk::GL::Config> glconfig; // Try double-buffered visual glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB | Gdk::GL::MODE_DEPTH | Gdk::GL::MODE_DOUBLE); if (!glconfig) { std::cerr << "*** Cannot find the double-buffered visual.\n" << "*** Trying single-buffered visual.\n"; // Try single-buffered visual glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB | Gdk::GL::MODE_DEPTH); if (!glconfig) { std::cerr << "*** Cannot find any OpenGL-capable visual.\n"; std::exit(1); } } // // Instantiate and run the application. // Simple simple(glconfig); kit.run(simple); return 0; }
00001 // -*- C++ -*- 00002 /* 00003 * share-lists.cc: 00004 * Simple display list sharing example. 00005 * 00006 * written by Naofumi Yasufuku <naofumi@users.sourceforge.net> 00007 */ 00008 00009 #include <iostream> 00010 #include <cstdlib> 00011 00012 #include <gtkmm.h> 00013 00014 #include <gtkglmm.h> 00015 00016 #ifdef G_OS_WIN32 00017 #define WIN32_LEAN_AND_MEAN 1 00018 #include <windows.h> 00019 #endif 00020 00021 #include <GL/gl.h> 00022 #include <GL/glu.h> 00023 00024 00026 // 00027 // Simple OpenGL scene base class. 00028 // 00030 00031 class SimpleGLScene : public Gtk::GL::DrawingArea 00032 { 00033 public: 00034 explicit SimpleGLScene(const Glib::RefPtr<const Gdk::GL::Config>& config); 00035 00036 SimpleGLScene(const Glib::RefPtr<const Gdk::GL::Config>& config, 00037 const Glib::RefPtr<const Gdk::GL::Context>& share_list); 00038 00039 virtual ~SimpleGLScene(); 00040 00041 protected: 00042 virtual void init_gl(); 00043 00044 protected: 00045 virtual bool on_configure_event(GdkEventConfigure* event); 00046 virtual bool on_expose_event(GdkEventExpose* event); 00047 00048 public: 00049 // Gtk::Widget::realize() is protected, so that ... 00050 void realize() { Gtk::GL::DrawingArea::realize(); } 00051 00052 public: 00053 void set_light_diffuse(GLfloat r, GLfloat g, GLfloat b, GLfloat a) 00054 { m_LightDiffuse[0] = r; m_LightDiffuse[1] = g; m_LightDiffuse[2] = b; m_LightDiffuse[3] = a; } 00055 00056 void set_light_position(GLfloat x, GLfloat y, GLfloat z, GLfloat w) 00057 { m_LightPosition[0] = x; m_LightPosition[1] = y; m_LightPosition[2] = z; m_LightPosition[3] = w; } 00058 00059 protected: 00060 GLfloat m_LightDiffuse[4]; 00061 GLfloat m_LightPosition[4]; 00062 00063 }; 00064 00065 SimpleGLScene::SimpleGLScene(const Glib::RefPtr<const Gdk::GL::Config>& config) 00066 : Gtk::GL::DrawingArea(config) 00067 { 00068 set_light_diffuse(1.0, 1.0, 1.0, 1.0); 00069 set_light_position(1.0, 1.0, 1.0, 0.0); 00070 } 00071 00072 SimpleGLScene::SimpleGLScene(const Glib::RefPtr<const Gdk::GL::Config>& config, 00073 const Glib::RefPtr<const Gdk::GL::Context>& share_list) 00074 : Gtk::GL::DrawingArea(config, share_list) 00075 { 00076 set_light_diffuse(1.0, 1.0, 1.0, 1.0); 00077 set_light_position(1.0, 1.0, 1.0, 0.0); 00078 } 00079 00080 SimpleGLScene::~SimpleGLScene() 00081 { 00082 } 00083 00084 void SimpleGLScene::init_gl() 00085 { 00086 glLightfv(GL_LIGHT0, GL_DIFFUSE, m_LightDiffuse); 00087 glLightfv(GL_LIGHT0, GL_POSITION, m_LightPosition); 00088 glEnable(GL_LIGHTING); 00089 glEnable(GL_LIGHT0); 00090 glEnable(GL_DEPTH_TEST); 00091 00092 glClearColor(1.0, 1.0, 1.0, 1.0); 00093 glClearDepth(1.0); 00094 00095 glViewport(0, 0, get_width(), get_height()); 00096 00097 glMatrixMode(GL_PROJECTION); 00098 glLoadIdentity(); 00099 gluPerspective(40.0, 1.0, 1.0, 10.0); 00100 00101 glMatrixMode(GL_MODELVIEW); 00102 glLoadIdentity(); 00103 gluLookAt(0.0, 0.0, 3.0, 00104 0.0, 0.0, 0.0, 00105 0.0, 1.0, 0.0); 00106 glTranslatef(0.0, 0.0, -3.0); 00107 } 00108 00109 bool SimpleGLScene::on_configure_event(GdkEventConfigure* event) 00110 { 00111 // 00112 // Get GL::Window. 00113 // 00114 00115 Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window(); 00116 00117 // 00118 // GL calls. 00119 // 00120 00121 // *** OpenGL BEGIN *** 00122 if (!glwindow->gl_begin(get_gl_context())) 00123 return false; 00124 00125 glViewport(0, 0, get_width(), get_height()); 00126 00127 glwindow->gl_end(); 00128 // *** OpenGL END *** 00129 00130 return true; 00131 } 00132 00133 bool SimpleGLScene::on_expose_event(GdkEventExpose* event) 00134 { 00135 // 00136 // Get GL::Window. 00137 // 00138 00139 Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window(); 00140 00141 // 00142 // GL calls. 00143 // 00144 00145 // *** OpenGL BEGIN *** 00146 if (!glwindow->gl_begin(get_gl_context())) 00147 return false; 00148 00149 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 00150 00151 glCallList(1); 00152 00153 // Swap buffers. 00154 if (glwindow->is_double_buffered()) 00155 glwindow->swap_buffers(); 00156 else 00157 glFlush(); 00158 00159 glwindow->gl_end(); 00160 // *** OpenGL END *** 00161 00162 return true; 00163 } 00164 00165 00167 // 00168 // SimpleGLSceneMain (creates display lists) 00169 // 00171 00172 class SimpleGLSceneMain : public SimpleGLScene 00173 { 00174 public: 00175 explicit SimpleGLSceneMain(const Glib::RefPtr<const Gdk::GL::Config>& config); 00176 virtual ~SimpleGLSceneMain(); 00177 00178 protected: 00179 virtual void on_realize(); 00180 00181 }; 00182 00183 SimpleGLSceneMain::SimpleGLSceneMain(const Glib::RefPtr<const Gdk::GL::Config>& config) 00184 : SimpleGLScene(config) 00185 { 00186 } 00187 00188 SimpleGLSceneMain::~SimpleGLSceneMain() 00189 { 00190 } 00191 00192 void SimpleGLSceneMain::on_realize() 00193 { 00194 // We need to call the base on_realize() 00195 SimpleGLScene::on_realize(); 00196 00197 // 00198 // Get GL::Window. 00199 // 00200 00201 Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window(); 00202 00203 // 00204 // GL calls. 00205 // 00206 00207 // *** OpenGL BEGIN *** 00208 if (!glwindow->gl_begin(get_gl_context())) 00209 return; 00210 00211 // Create display list #1 00212 GLUquadricObj* qobj = gluNewQuadric(); 00213 gluQuadricDrawStyle(qobj, GLU_FILL); 00214 glNewList(1, GL_COMPILE); 00215 gluSphere(qobj, 1.0, 20, 20); 00216 glEndList(); 00217 00218 // Initialize rendering context 00219 init_gl(); 00220 00221 glwindow->gl_end(); 00222 // *** OpenGL END *** 00223 } 00224 00225 00227 // 00228 // SimpleGLSceneSub (shares display lists) 00229 // 00231 00232 class SimpleGLSceneSub : public SimpleGLScene 00233 { 00234 public: 00235 SimpleGLSceneSub(const Glib::RefPtr<const Gdk::GL::Config>& config, 00236 const Glib::RefPtr<const Gdk::GL::Context>& share_list); 00237 virtual ~SimpleGLSceneSub(); 00238 00239 protected: 00240 virtual void on_realize(); 00241 00242 }; 00243 00244 SimpleGLSceneSub::SimpleGLSceneSub(const Glib::RefPtr<const Gdk::GL::Config>& config, 00245 const Glib::RefPtr<const Gdk::GL::Context>& share_list) 00246 : SimpleGLScene(config, share_list) 00247 { 00248 } 00249 00250 SimpleGLSceneSub::~SimpleGLSceneSub() 00251 { 00252 } 00253 00254 void SimpleGLSceneSub::on_realize() 00255 { 00256 // We need to call the base on_realize() 00257 SimpleGLScene::on_realize(); 00258 00259 // 00260 // Get GL::Window. 00261 // 00262 00263 Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window(); 00264 00265 // 00266 // GL calls. 00267 // 00268 00269 // *** OpenGL BEGIN *** 00270 if (!glwindow->gl_begin(get_gl_context())) 00271 return; 00272 00273 // Initialize rendering context 00274 init_gl(); 00275 00276 glwindow->gl_end(); 00277 // *** OpenGL END *** 00278 } 00279 00280 00282 // 00283 // The application class. 00284 // 00286 00287 class Simple : public Gtk::Window 00288 { 00289 public: 00290 explicit Simple(const Glib::RefPtr<const Gdk::GL::Config>& config); 00291 virtual ~Simple(); 00292 00293 protected: 00294 // signal handlers: 00295 void on_button_quit_clicked(); 00296 00297 protected: 00298 // member widgets: 00299 Gtk::VBox m_VBox; 00300 SimpleGLScene *m_GLScene1; 00301 SimpleGLScene *m_GLScene2; 00302 SimpleGLScene *m_GLScene3; 00303 Gtk::Button m_ButtonQuit; 00304 }; 00305 00306 Simple::Simple(const Glib::RefPtr<const Gdk::GL::Config>& config) 00307 : m_VBox(false, 10), m_ButtonQuit("Quit") 00308 { 00309 // 00310 // Top-level window. 00311 // 00312 00313 set_title("share-lists"); 00314 00315 // Get automatically redrawn if any of their children changed allocation. 00316 set_reallocate_redraws(true); 00317 // Set border width. 00318 set_border_width(10); 00319 00320 add(m_VBox); 00321 00322 // 00323 // OpenGL scene #1 (SimpleGLSceneMain: creates display lists) 00324 // 00325 00326 m_GLScene1 = new SimpleGLSceneMain(config); 00327 m_GLScene1->set_size_request(120, 120); 00328 m_GLScene1->set_light_diffuse(1.0, 0.0, 0.0, 0.0); // red 00329 00330 m_VBox.pack_start(*m_GLScene1); 00331 00332 // 00333 // Get OpenGL rendering context. 00334 // 00335 00336 m_GLScene1->realize(); 00337 Glib::RefPtr<Gdk::GL::Context> glcontext = m_GLScene1->get_gl_context(); 00338 00339 // 00340 // OpenGL scene #2 (SimpleGLSceneSub: shares display lists) 00341 // 00342 00343 m_GLScene2 = new SimpleGLSceneSub(config, glcontext); 00344 m_GLScene2->set_size_request(120, 120); 00345 m_GLScene2->set_light_diffuse(1.0, 1.0, 0.0, 0.0); // yellow 00346 00347 m_VBox.pack_start(*m_GLScene2); 00348 00349 // 00350 // OpenGL scene #3 (SimpleGLSceneSub: shares display lists) 00351 // 00352 00353 m_GLScene3 = new SimpleGLSceneSub(config, glcontext); 00354 m_GLScene3->set_size_request(120, 120); 00355 m_GLScene3->set_light_diffuse(0.0, 1.0, 0.0, 0.0); // green 00356 00357 m_VBox.pack_start(*m_GLScene3); 00358 00359 // 00360 // Simple quit button. 00361 // 00362 00363 m_ButtonQuit.signal_clicked().connect( 00364 sigc::mem_fun(*this, &Simple::on_button_quit_clicked)); 00365 00366 m_VBox.pack_start(m_ButtonQuit, Gtk::PACK_SHRINK, 0); 00367 00368 // 00369 // Show window. 00370 // 00371 00372 show_all(); 00373 } 00374 00375 Simple::~Simple() 00376 { 00377 delete m_GLScene1; 00378 delete m_GLScene2; 00379 delete m_GLScene3; 00380 } 00381 00382 void Simple::on_button_quit_clicked() 00383 { 00384 Gtk::Main::quit(); 00385 } 00386 00387 00389 // 00390 // Main. 00391 // 00393 00394 int main(int argc, char** argv) 00395 { 00396 Gtk::Main kit(argc, argv); 00397 00398 // 00399 // Init gtkglextmm. 00400 // 00401 00402 Gtk::GL::init(argc, argv); 00403 00404 // 00405 // Configure OpenGL-capable visual. 00406 // 00407 00408 Glib::RefPtr<Gdk::GL::Config> glconfig; 00409 00410 // Try double-buffered visual 00411 glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB | 00412 Gdk::GL::MODE_DEPTH | 00413 Gdk::GL::MODE_DOUBLE); 00414 if (!glconfig) 00415 { 00416 std::cerr << "*** Cannot find the double-buffered visual.\n" 00417 << "*** Trying single-buffered visual.\n"; 00418 00419 // Try single-buffered visual 00420 glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB | 00421 Gdk::GL::MODE_DEPTH); 00422 if (!glconfig) 00423 { 00424 std::cerr << "*** Cannot find any OpenGL-capable visual.\n"; 00425 std::exit(1); 00426 } 00427 } 00428 00429 // 00430 // Instantiate and run the application. 00431 // 00432 00433 Simple simple(glconfig); 00434 00435 kit.run(simple); 00436 00437 return 0; 00438 }

Generated on Sun Jun 20 16:59:43 2004 for gtkglextmm by doxygen 1.3.7