// -*- C++ -*- /* * simple.cc: * Simple Gtk::GL::DrawingArea 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> // // 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::GL::DrawingArea { public: SimpleGLScene(const Glib::RefPtr<const Gdk::GL::Config>& config); virtual ~SimpleGLScene(); protected: virtual void on_realize(); virtual bool on_configure_event(GdkEventConfigure* event); virtual bool on_expose_event(GdkEventExpose* event); }; SimpleGLScene::SimpleGLScene(const Glib::RefPtr<const Gdk::GL::Config>& config) : Gtk::GL::DrawingArea(config) { } SimpleGLScene::~SimpleGLScene() { } void SimpleGLScene::on_realize() { // We need to call the base on_realize() Gtk::GL::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); glCallList(1); // Swap buffers. if (glwindow->is_double_buffered()) glwindow->swap_buffers(); else glFlush(); glwindow->gl_end(); // *** OpenGL END *** return true; } // // The application class. // class Simple : public Gtk::Window { public: 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_SimpleGLScene; Gtk::Button m_ButtonQuit; }; Simple::Simple(const Glib::RefPtr<const Gdk::GL::Config>& config) : m_VBox(false, 0), m_SimpleGLScene(config), m_ButtonQuit("Quit") { // // Top-level window. // set_title("Gtk::GL::DrawingArea"); // 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; // // 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); } } // print frame buffer attributes. GLConfigUtil::examine_gl_attrib(glconfig); // // Instantiate and run the application. // Simple simple(glconfig); kit.run(simple); return 0; }
00001 // -*- C++ -*- 00002 /* 00003 * simple.cc: 00004 * Simple Gtk::GL::DrawingArea 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 // 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::GL::DrawingArea 00127 { 00128 public: 00129 SimpleGLScene(const Glib::RefPtr<const Gdk::GL::Config>& config); 00130 virtual ~SimpleGLScene(); 00131 00132 protected: 00133 virtual void on_realize(); 00134 virtual bool on_configure_event(GdkEventConfigure* event); 00135 virtual bool on_expose_event(GdkEventExpose* event); 00136 00137 }; 00138 00139 SimpleGLScene::SimpleGLScene(const Glib::RefPtr<const Gdk::GL::Config>& config) 00140 : Gtk::GL::DrawingArea(config) 00141 { 00142 } 00143 00144 SimpleGLScene::~SimpleGLScene() 00145 { 00146 } 00147 00148 void SimpleGLScene::on_realize() 00149 { 00150 // We need to call the base on_realize() 00151 Gtk::GL::DrawingArea::on_realize(); 00152 00153 // 00154 // Get GL::Window. 00155 // 00156 00157 Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window(); 00158 00159 // 00160 // GL calls. 00161 // 00162 00163 // *** OpenGL BEGIN *** 00164 if (!glwindow->gl_begin(get_gl_context())) 00165 return; 00166 00167 GLUquadricObj* qobj = gluNewQuadric(); 00168 gluQuadricDrawStyle(qobj, GLU_FILL); 00169 glNewList(1, GL_COMPILE); 00170 gluSphere(qobj, 1.0, 20, 20); 00171 glEndList(); 00172 00173 static GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0}; 00174 static GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; 00175 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); 00176 glLightfv(GL_LIGHT0, GL_POSITION, light_position); 00177 glEnable(GL_LIGHTING); 00178 glEnable(GL_LIGHT0); 00179 glEnable(GL_DEPTH_TEST); 00180 00181 glClearColor(1.0, 1.0, 1.0, 1.0); 00182 glClearDepth(1.0); 00183 00184 glViewport(0, 0, get_width(), get_height()); 00185 00186 glMatrixMode(GL_PROJECTION); 00187 glLoadIdentity(); 00188 gluPerspective(40.0, 1.0, 1.0, 10.0); 00189 00190 glMatrixMode(GL_MODELVIEW); 00191 glLoadIdentity(); 00192 gluLookAt(0.0, 0.0, 3.0, 00193 0.0, 0.0, 0.0, 00194 0.0, 1.0, 0.0); 00195 glTranslatef(0.0, 0.0, -3.0); 00196 00197 glwindow->gl_end(); 00198 // *** OpenGL END *** 00199 } 00200 00201 bool SimpleGLScene::on_configure_event(GdkEventConfigure* event) 00202 { 00203 // 00204 // Get GL::Window. 00205 // 00206 00207 Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window(); 00208 00209 // 00210 // GL calls. 00211 // 00212 00213 // *** OpenGL BEGIN *** 00214 if (!glwindow->gl_begin(get_gl_context())) 00215 return false; 00216 00217 glViewport(0, 0, get_width(), get_height()); 00218 00219 glwindow->gl_end(); 00220 // *** OpenGL END *** 00221 00222 return true; 00223 } 00224 00225 bool SimpleGLScene::on_expose_event(GdkEventExpose* 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 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 00242 00243 glCallList(1); 00244 00245 // Swap buffers. 00246 if (glwindow->is_double_buffered()) 00247 glwindow->swap_buffers(); 00248 else 00249 glFlush(); 00250 00251 glwindow->gl_end(); 00252 // *** OpenGL END *** 00253 00254 return true; 00255 } 00256 00257 00259 // 00260 // The application class. 00261 // 00263 00264 class Simple : public Gtk::Window 00265 { 00266 public: 00267 Simple(const Glib::RefPtr<const Gdk::GL::Config>& config); 00268 virtual ~Simple(); 00269 00270 protected: 00271 // signal handlers: 00272 void on_button_quit_clicked(); 00273 00274 protected: 00275 // member widgets: 00276 Gtk::VBox m_VBox; 00277 SimpleGLScene m_SimpleGLScene; 00278 Gtk::Button m_ButtonQuit; 00279 }; 00280 00281 Simple::Simple(const Glib::RefPtr<const Gdk::GL::Config>& config) 00282 : m_VBox(false, 0), m_SimpleGLScene(config), m_ButtonQuit("Quit") 00283 { 00284 // 00285 // Top-level window. 00286 // 00287 00288 set_title("Gtk::GL::DrawingArea"); 00289 00290 // Get automatically redrawn if any of their children changed allocation. 00291 set_reallocate_redraws(true); 00292 00293 add(m_VBox); 00294 00295 // 00296 // Simple OpenGL scene. 00297 // 00298 00299 m_SimpleGLScene.set_size_request(200, 200); 00300 00301 m_VBox.pack_start(m_SimpleGLScene); 00302 00303 // 00304 // Simple quit button. 00305 // 00306 00307 m_ButtonQuit.signal_clicked().connect( 00308 sigc::mem_fun(*this, &Simple::on_button_quit_clicked)); 00309 00310 m_VBox.pack_start(m_ButtonQuit, Gtk::PACK_SHRINK, 0); 00311 00312 // 00313 // Show window. 00314 // 00315 00316 show_all(); 00317 } 00318 00319 Simple::~Simple() 00320 {} 00321 00322 void Simple::on_button_quit_clicked() 00323 { 00324 Gtk::Main::quit(); 00325 } 00326 00327 00329 // 00330 // Main. 00331 // 00333 00334 int main(int argc, char** argv) 00335 { 00336 Gtk::Main kit(argc, argv); 00337 00338 // 00339 // Init gtkglextmm. 00340 // 00341 00342 Gtk::GL::init(argc, argv); 00343 00344 // 00345 // Query OpenGL extension version. 00346 // 00347 00348 int major, minor; 00349 Gdk::GL::query_version(major, minor); 00350 std::cout << "OpenGL extension version - " 00351 << major << "." << minor << std::endl; 00352 00353 // 00354 // Configure OpenGL-capable visual. 00355 // 00356 00357 Glib::RefPtr<Gdk::GL::Config> glconfig; 00358 00359 // Try double-buffered visual 00360 glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB | 00361 Gdk::GL::MODE_DEPTH | 00362 Gdk::GL::MODE_DOUBLE); 00363 if (!glconfig) 00364 { 00365 std::cerr << "*** Cannot find the double-buffered visual.\n" 00366 << "*** Trying single-buffered visual.\n"; 00367 00368 // Try single-buffered visual 00369 glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB | 00370 Gdk::GL::MODE_DEPTH); 00371 if (!glconfig) 00372 { 00373 std::cerr << "*** Cannot find any OpenGL-capable visual.\n"; 00374 std::exit(1); 00375 } 00376 } 00377 00378 // print frame buffer attributes. 00379 GLConfigUtil::examine_gl_attrib(glconfig); 00380 00381 // 00382 // Instantiate and run the application. 00383 // 00384 00385 Simple simple(glconfig); 00386 00387 kit.run(simple); 00388 00389 return 0; 00390 }