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

simple.cc

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

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