// -*- C++ -*- /* * simple-mixed.cc: * Simple gtkglextmm example for mixing OpenGL and GDK rendering. * * 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> // // OpenGL frame buffer configuration utilities. // struct GLConfigUtil { static void print_gl_attrib(const Glib::RefPtr<const Gdk::GL::Config>& glconfig, const char* attrib_str, int attrib, bool is_boolean); static void examine_gl_attrib(const Glib::RefPtr<const Gdk::GL::Config>& glconfig); }; // // Print a configuration attribute. // void GLConfigUtil::print_gl_attrib(const Glib::RefPtr<const Gdk::GL::Config>& glconfig, const char* attrib_str, int attrib, bool is_boolean) { int value; if (glconfig->get_attrib(attrib, value)) { std::cout << attrib_str << " = "; if (is_boolean) std::cout << (value == true ? "true" : "false") << std::endl; else std::cout << value << std::endl; } else { std::cout << "*** Cannot get " << attrib_str << " attribute value\n"; } } // // Print configuration attributes. // void GLConfigUtil::examine_gl_attrib(const Glib::RefPtr<const Gdk::GL::Config>& glconfig) { std::cout << "\nOpenGL visual configurations :\n\n"; std::cout << "glconfig->is_rgba() = " << (glconfig->is_rgba() ? "true" : "false") << std::endl; std::cout << "glconfig->is_double_buffered() = " << (glconfig->is_double_buffered() ? "true" : "false") << std::endl; std::cout << "glconfig->is_stereo() = " << (glconfig->is_stereo() ? "true" : "false") << std::endl; std::cout << "glconfig->has_alpha() = " << (glconfig->has_alpha() ? "true" : "false") << std::endl; std::cout << "glconfig->has_depth_buffer() = " << (glconfig->has_depth_buffer() ? "true" : "false") << std::endl; std::cout << "glconfig->has_stencil_buffer() = " << (glconfig->has_stencil_buffer() ? "true" : "false") << std::endl; std::cout << "glconfig->has_accum_buffer() = " << (glconfig->has_accum_buffer() ? "true" : "false") << std::endl; std::cout << std::endl; print_gl_attrib(glconfig, "Gdk::GL::USE_GL", Gdk::GL::USE_GL, true); print_gl_attrib(glconfig, "Gdk::GL::BUFFER_SIZE", Gdk::GL::BUFFER_SIZE, false); print_gl_attrib(glconfig, "Gdk::GL::LEVEL", Gdk::GL::LEVEL, false); print_gl_attrib(glconfig, "Gdk::GL::RGBA", Gdk::GL::RGBA, true); print_gl_attrib(glconfig, "Gdk::GL::DOUBLEBUFFER", Gdk::GL::DOUBLEBUFFER, true); print_gl_attrib(glconfig, "Gdk::GL::STEREO", Gdk::GL::STEREO, true); print_gl_attrib(glconfig, "Gdk::GL::AUX_BUFFERS", Gdk::GL::AUX_BUFFERS, false); print_gl_attrib(glconfig, "Gdk::GL::RED_SIZE", Gdk::GL::RED_SIZE, false); print_gl_attrib(glconfig, "Gdk::GL::GREEN_SIZE", Gdk::GL::GREEN_SIZE, false); print_gl_attrib(glconfig, "Gdk::GL::BLUE_SIZE", Gdk::GL::BLUE_SIZE, false); print_gl_attrib(glconfig, "Gdk::GL::ALPHA_SIZE", Gdk::GL::ALPHA_SIZE, false); print_gl_attrib(glconfig, "Gdk::GL::DEPTH_SIZE", Gdk::GL::DEPTH_SIZE, false); print_gl_attrib(glconfig, "Gdk::GL::STENCIL_SIZE", Gdk::GL::STENCIL_SIZE, false); print_gl_attrib(glconfig, "Gdk::GL::ACCUM_RED_SIZE", Gdk::GL::ACCUM_RED_SIZE, false); print_gl_attrib(glconfig, "Gdk::GL::ACCUM_GREEN_SIZE", Gdk::GL::ACCUM_GREEN_SIZE, false); print_gl_attrib(glconfig, "Gdk::GL::ACCUM_BLUE_SIZE", Gdk::GL::ACCUM_BLUE_SIZE, false); print_gl_attrib(glconfig, "Gdk::GL::ACCUM_ALPHA_SIZE", Gdk::GL::ACCUM_ALPHA_SIZE, false); std::cout << std::endl; } // // Simple OpenGL scene. // class SimpleGLScene : public Gtk::DrawingArea, public Gtk::GL::Widget<SimpleGLScene> { public: SimpleGLScene(); virtual ~SimpleGLScene(); protected: virtual void on_realize(); virtual bool on_configure_event(GdkEventConfigure* event); virtual bool on_expose_event(GdkEventExpose* event); }; SimpleGLScene::SimpleGLScene() { // // Configure OpenGL-capable visual. // Glib::RefPtr<Gdk::GL::Config> glconfig; // Try single-buffered visual glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB | Gdk::GL::MODE_DEPTH | Gdk::GL::MODE_SINGLE); if (!glconfig) { std::cerr << "*** Cannot find any OpenGL-capable visual.\n"; std::exit(1); } // print frame buffer attributes. GLConfigUtil::examine_gl_attrib(glconfig); // // Set OpenGL-capability to the widget. // set_gl_capability(glconfig); } SimpleGLScene::~SimpleGLScene() { } void SimpleGLScene::on_realize() { // We need to call the base on_realize() Gtk::DrawingArea::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; GLUquadricObj* qobj = gluNewQuadric(); gluQuadricDrawStyle(qobj, GLU_FILL); glNewList(1, GL_COMPILE); gluSphere(qobj, 1.0, 20, 20); glEndList(); static GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0}; static GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_POSITION, light_position); 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); glwindow->gl_end(); // *** OpenGL END *** } 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); // Sync. glwindow->wait_gl(); // GDK rendering. glwindow->draw_rectangle(get_style()->get_fg_gc(get_state()), true, get_width()/10, get_height()/10, get_width()*8/10, get_height()*8/10); // Sync. glwindow->wait_gdk(); glCallList(1); glFlush(); glwindow->gl_end(); // *** OpenGL END *** return true; } // // The application class. // class Simple : public Gtk::Window { public: Simple(); virtual ~Simple(); protected: // signal handlers: void on_button_quit_clicked(); protected: // member widgets: Gtk::VBox m_VBox; SimpleGLScene m_SimpleGLScene; Gtk::Button m_ButtonQuit; }; Simple::Simple() : m_VBox(false, 0), m_ButtonQuit("Quit") { // // Top-level window. // set_title("Simple"); // Get automatically redrawn if any of their children changed allocation. set_reallocate_redraws(true); add(m_VBox); // // Simple OpenGL scene. // m_SimpleGLScene.set_size_request(200, 200); m_VBox.pack_start(m_SimpleGLScene); // // 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() {} 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); // // Query OpenGL extension version. // int major, minor; Gdk::GL::query_version(major, minor); std::cout << "OpenGL extension version - " << major << "." << minor << std::endl; // // Instantiate and run the application. // Simple simple; kit.run(simple); return 0; }
00001 // -*- C++ -*- 00002 /* 00003 * simple-mixed.cc: 00004 * Simple gtkglextmm example for mixing OpenGL and GDK rendering. 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 // OpenGL frame buffer configuration utilities. 00028 // 00030 00031 struct GLConfigUtil 00032 { 00033 static void print_gl_attrib(const Glib::RefPtr<const Gdk::GL::Config>& glconfig, 00034 const char* attrib_str, 00035 int attrib, 00036 bool is_boolean); 00037 00038 static void examine_gl_attrib(const Glib::RefPtr<const Gdk::GL::Config>& glconfig); 00039 }; 00040 00041 // 00042 // Print a configuration attribute. 00043 // 00044 void GLConfigUtil::print_gl_attrib(const Glib::RefPtr<const Gdk::GL::Config>& glconfig, 00045 const char* attrib_str, 00046 int attrib, 00047 bool is_boolean) 00048 { 00049 int value; 00050 00051 if (glconfig->get_attrib(attrib, value)) 00052 { 00053 std::cout << attrib_str << " = "; 00054 if (is_boolean) 00055 std::cout << (value == true ? "true" : "false") << std::endl; 00056 else 00057 std::cout << value << std::endl; 00058 } 00059 else 00060 { 00061 std::cout << "*** Cannot get " 00062 << attrib_str 00063 << " attribute value\n"; 00064 } 00065 } 00066 00067 // 00068 // Print configuration attributes. 00069 // 00070 void GLConfigUtil::examine_gl_attrib(const Glib::RefPtr<const Gdk::GL::Config>& glconfig) 00071 { 00072 std::cout << "\nOpenGL visual configurations :\n\n"; 00073 00074 std::cout << "glconfig->is_rgba() = " 00075 << (glconfig->is_rgba() ? "true" : "false") 00076 << std::endl; 00077 std::cout << "glconfig->is_double_buffered() = " 00078 << (glconfig->is_double_buffered() ? "true" : "false") 00079 << std::endl; 00080 std::cout << "glconfig->is_stereo() = " 00081 << (glconfig->is_stereo() ? "true" : "false") 00082 << std::endl; 00083 std::cout << "glconfig->has_alpha() = " 00084 << (glconfig->has_alpha() ? "true" : "false") 00085 << std::endl; 00086 std::cout << "glconfig->has_depth_buffer() = " 00087 << (glconfig->has_depth_buffer() ? "true" : "false") 00088 << std::endl; 00089 std::cout << "glconfig->has_stencil_buffer() = " 00090 << (glconfig->has_stencil_buffer() ? "true" : "false") 00091 << std::endl; 00092 std::cout << "glconfig->has_accum_buffer() = " 00093 << (glconfig->has_accum_buffer() ? "true" : "false") 00094 << std::endl; 00095 00096 std::cout << std::endl; 00097 00098 print_gl_attrib(glconfig, "Gdk::GL::USE_GL", Gdk::GL::USE_GL, true); 00099 print_gl_attrib(glconfig, "Gdk::GL::BUFFER_SIZE", Gdk::GL::BUFFER_SIZE, false); 00100 print_gl_attrib(glconfig, "Gdk::GL::LEVEL", Gdk::GL::LEVEL, false); 00101 print_gl_attrib(glconfig, "Gdk::GL::RGBA", Gdk::GL::RGBA, true); 00102 print_gl_attrib(glconfig, "Gdk::GL::DOUBLEBUFFER", Gdk::GL::DOUBLEBUFFER, true); 00103 print_gl_attrib(glconfig, "Gdk::GL::STEREO", Gdk::GL::STEREO, true); 00104 print_gl_attrib(glconfig, "Gdk::GL::AUX_BUFFERS", Gdk::GL::AUX_BUFFERS, false); 00105 print_gl_attrib(glconfig, "Gdk::GL::RED_SIZE", Gdk::GL::RED_SIZE, false); 00106 print_gl_attrib(glconfig, "Gdk::GL::GREEN_SIZE", Gdk::GL::GREEN_SIZE, false); 00107 print_gl_attrib(glconfig, "Gdk::GL::BLUE_SIZE", Gdk::GL::BLUE_SIZE, false); 00108 print_gl_attrib(glconfig, "Gdk::GL::ALPHA_SIZE", Gdk::GL::ALPHA_SIZE, false); 00109 print_gl_attrib(glconfig, "Gdk::GL::DEPTH_SIZE", Gdk::GL::DEPTH_SIZE, false); 00110 print_gl_attrib(glconfig, "Gdk::GL::STENCIL_SIZE", Gdk::GL::STENCIL_SIZE, false); 00111 print_gl_attrib(glconfig, "Gdk::GL::ACCUM_RED_SIZE", Gdk::GL::ACCUM_RED_SIZE, false); 00112 print_gl_attrib(glconfig, "Gdk::GL::ACCUM_GREEN_SIZE", Gdk::GL::ACCUM_GREEN_SIZE, false); 00113 print_gl_attrib(glconfig, "Gdk::GL::ACCUM_BLUE_SIZE", Gdk::GL::ACCUM_BLUE_SIZE, false); 00114 print_gl_attrib(glconfig, "Gdk::GL::ACCUM_ALPHA_SIZE", Gdk::GL::ACCUM_ALPHA_SIZE, false); 00115 00116 std::cout << std::endl; 00117 } 00118 00119 00121 // 00122 // Simple OpenGL scene. 00123 // 00125 00126 class SimpleGLScene : public Gtk::DrawingArea, 00127 public Gtk::GL::Widget<SimpleGLScene> 00128 { 00129 public: 00130 SimpleGLScene(); 00131 virtual ~SimpleGLScene(); 00132 00133 protected: 00134 virtual void on_realize(); 00135 virtual bool on_configure_event(GdkEventConfigure* event); 00136 virtual bool on_expose_event(GdkEventExpose* event); 00137 00138 }; 00139 00140 SimpleGLScene::SimpleGLScene() 00141 { 00142 // 00143 // Configure OpenGL-capable visual. 00144 // 00145 00146 Glib::RefPtr<Gdk::GL::Config> glconfig; 00147 00148 // Try single-buffered visual 00149 glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB | 00150 Gdk::GL::MODE_DEPTH | 00151 Gdk::GL::MODE_SINGLE); 00152 if (!glconfig) 00153 { 00154 std::cerr << "*** Cannot find any OpenGL-capable visual.\n"; 00155 std::exit(1); 00156 } 00157 00158 // print frame buffer attributes. 00159 GLConfigUtil::examine_gl_attrib(glconfig); 00160 00161 // 00162 // Set OpenGL-capability to the widget. 00163 // 00164 00165 set_gl_capability(glconfig); 00166 } 00167 00168 SimpleGLScene::~SimpleGLScene() 00169 { 00170 } 00171 00172 void SimpleGLScene::on_realize() 00173 { 00174 // We need to call the base on_realize() 00175 Gtk::DrawingArea::on_realize(); 00176 00177 // 00178 // Get GL::Window. 00179 // 00180 00181 Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window(); 00182 00183 // 00184 // GL calls. 00185 // 00186 00187 // *** OpenGL BEGIN *** 00188 if (!glwindow->gl_begin(get_gl_context())) 00189 return; 00190 00191 GLUquadricObj* qobj = gluNewQuadric(); 00192 gluQuadricDrawStyle(qobj, GLU_FILL); 00193 glNewList(1, GL_COMPILE); 00194 gluSphere(qobj, 1.0, 20, 20); 00195 glEndList(); 00196 00197 static GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0}; 00198 static GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; 00199 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); 00200 glLightfv(GL_LIGHT0, GL_POSITION, light_position); 00201 glEnable(GL_LIGHTING); 00202 glEnable(GL_LIGHT0); 00203 glEnable(GL_DEPTH_TEST); 00204 00205 glClearColor(1.0, 1.0, 1.0, 1.0); 00206 glClearDepth(1.0); 00207 00208 glViewport(0, 0, get_width(), get_height()); 00209 00210 glMatrixMode(GL_PROJECTION); 00211 glLoadIdentity(); 00212 gluPerspective(40.0, 1.0, 1.0, 10.0); 00213 00214 glMatrixMode(GL_MODELVIEW); 00215 glLoadIdentity(); 00216 gluLookAt(0.0, 0.0, 3.0, 00217 0.0, 0.0, 0.0, 00218 0.0, 1.0, 0.0); 00219 glTranslatef(0.0, 0.0, -3.0); 00220 00221 glwindow->gl_end(); 00222 // *** OpenGL END *** 00223 } 00224 00225 bool SimpleGLScene::on_configure_event(GdkEventConfigure* event) 00226 { 00227 // 00228 // Get GL::Window. 00229 // 00230 00231 Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window(); 00232 00233 // 00234 // GL calls. 00235 // 00236 00237 // *** OpenGL BEGIN *** 00238 if (!glwindow->gl_begin(get_gl_context())) 00239 return false; 00240 00241 glViewport(0, 0, get_width(), get_height()); 00242 00243 glwindow->gl_end(); 00244 // *** OpenGL END *** 00245 00246 return true; 00247 } 00248 00249 bool SimpleGLScene::on_expose_event(GdkEventExpose* event) 00250 { 00251 // 00252 // Get GL::Window. 00253 // 00254 00255 Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window(); 00256 00257 // 00258 // GL calls. 00259 // 00260 00261 // *** OpenGL BEGIN *** 00262 if (!glwindow->gl_begin(get_gl_context())) 00263 return false; 00264 00265 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 00266 00267 // Sync. 00268 glwindow->wait_gl(); 00269 00270 // GDK rendering. 00271 glwindow->draw_rectangle(get_style()->get_fg_gc(get_state()), 00272 true, 00273 get_width()/10, 00274 get_height()/10, 00275 get_width()*8/10, 00276 get_height()*8/10); 00277 00278 // Sync. 00279 glwindow->wait_gdk(); 00280 00281 glCallList(1); 00282 00283 glFlush(); 00284 00285 glwindow->gl_end(); 00286 // *** OpenGL END *** 00287 00288 return true; 00289 } 00290 00291 00293 // 00294 // The application class. 00295 // 00297 00298 class Simple : public Gtk::Window 00299 { 00300 public: 00301 Simple(); 00302 virtual ~Simple(); 00303 00304 protected: 00305 // signal handlers: 00306 void on_button_quit_clicked(); 00307 00308 protected: 00309 // member widgets: 00310 Gtk::VBox m_VBox; 00311 SimpleGLScene m_SimpleGLScene; 00312 Gtk::Button m_ButtonQuit; 00313 }; 00314 00315 Simple::Simple() 00316 : m_VBox(false, 0), m_ButtonQuit("Quit") 00317 { 00318 // 00319 // Top-level window. 00320 // 00321 00322 set_title("Simple"); 00323 00324 // Get automatically redrawn if any of their children changed allocation. 00325 set_reallocate_redraws(true); 00326 00327 add(m_VBox); 00328 00329 // 00330 // Simple OpenGL scene. 00331 // 00332 00333 m_SimpleGLScene.set_size_request(200, 200); 00334 00335 m_VBox.pack_start(m_SimpleGLScene); 00336 00337 // 00338 // Simple quit button. 00339 // 00340 00341 m_ButtonQuit.signal_clicked().connect( 00342 sigc::mem_fun(*this, &Simple::on_button_quit_clicked)); 00343 00344 m_VBox.pack_start(m_ButtonQuit, Gtk::PACK_SHRINK, 0); 00345 00346 // 00347 // Show window. 00348 // 00349 00350 show_all(); 00351 } 00352 00353 Simple::~Simple() 00354 {} 00355 00356 void Simple::on_button_quit_clicked() 00357 { 00358 Gtk::Main::quit(); 00359 } 00360 00361 00363 // 00364 // Main. 00365 // 00367 00368 int main(int argc, char** argv) 00369 { 00370 Gtk::Main kit(argc, argv); 00371 00372 // 00373 // Init gtkglextmm. 00374 // 00375 00376 Gtk::GL::init(argc, argv); 00377 00378 // 00379 // Query OpenGL extension version. 00380 // 00381 00382 int major, minor; 00383 Gdk::GL::query_version(major, minor); 00384 std::cout << "OpenGL extension version - " 00385 << major << "." << minor << std::endl; 00386 00387 // 00388 // Instantiate and run the application. 00389 // 00390 00391 Simple simple; 00392 00393 kit.run(simple); 00394 00395 return 0; 00396 }