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

pixmap-mixed.cc

Simple offscreen rendering example for mixing OpenGL and GDK rendering.
// -*- C++ -*- /* * pixmap-mixed.cc: * Simple off-screen rendering 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 using GL::Pixmap. // class PixmapGLScene : public Gtk::DrawingArea { public: PixmapGLScene(); virtual ~PixmapGLScene(); protected: // init OpenGL context void init_gl(); protected: virtual bool on_configure_event(GdkEventConfigure* event); virtual bool on_expose_event(GdkEventExpose* event); protected: // OpenGL rendering stuff: Glib::RefPtr<Gdk::GL::Config> m_GLConfig; Glib::RefPtr<Gdk::GL::Context> m_GLContext; Glib::RefPtr<Gdk::Pixmap> m_Pixmap; }; PixmapGLScene::PixmapGLScene() : m_GLConfig(0), m_GLContext(0), m_Pixmap(0) { // // Configure OpenGL-capable visual. // // Try single-buffered visual m_GLConfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB | Gdk::GL::MODE_DEPTH | Gdk::GL::MODE_SINGLE); if (!m_GLConfig) { std::cerr << "*** Cannot find any OpenGL-capable visual.\n"; std::exit(1); } // print frame buffer attributes. GLConfigUtil::examine_gl_attrib(m_GLConfig); // // Set OpenGL-capable colormap. // set_colormap(m_GLConfig->get_colormap()); } PixmapGLScene::~PixmapGLScene() { } void PixmapGLScene::init_gl() { 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); } bool PixmapGLScene::on_configure_event(GdkEventConfigure* event) { // // Create an OpenGL off-screen rendering area. // m_Pixmap = Gdk::Pixmap::create(get_window(), get_width(), get_height(), m_GLConfig->get_depth()); // // Set OpenGL-capability to the pixmap (invoke extension method). // Glib::RefPtr<Gdk::GL::Pixmap> glpixmap = Gdk::GL::ext(m_Pixmap).set_gl_capability(m_GLConfig); // // Create OpenGL rendering context (not direct). // if (!m_GLContext) m_GLContext = Gdk::GL::Context::create(glpixmap, false); // // GL calls. // // *** OpenGL BEGIN *** if (!glpixmap->gl_begin(m_GLContext)) return false; static bool is_initialized = false; if (!is_initialized) { init_gl(); is_initialized = true; } glViewport(0, 0, get_width(), get_height()); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Sync. glpixmap->wait_gl(); // GDK rendering. glpixmap->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. glpixmap->wait_gdk(); glCallList(1); glFlush(); glpixmap->gl_end(); // *** OpenGL END *** return true; } bool PixmapGLScene::on_expose_event(GdkEventExpose* event) { if (!m_Pixmap) return false; get_window()->draw_drawable(get_style()->get_fg_gc(get_state()), m_Pixmap, event->area.x, event->area.y, event->area.x, event->area.y, event->area.width, event->area.height); return true; } // // The application class. // class Pixmap : public Gtk::Window { public: Pixmap(); virtual ~Pixmap(); protected: // signal handlers: void on_button_quit_clicked(); protected: // member widgets: Gtk::VBox m_VBox; PixmapGLScene m_PixmapGLScene; Gtk::Button m_ButtonQuit; }; Pixmap::Pixmap() : m_VBox(false, 0), m_ButtonQuit("Quit") { // // Top-level window. // set_title("Pixmap"); add(m_VBox); // // Simple OpenGL scene using GL::Pixmap. // m_PixmapGLScene.set_size_request(200, 200); m_VBox.pack_start(m_PixmapGLScene); // // Simple quit button. // m_ButtonQuit.signal_clicked().connect( sigc::mem_fun(*this, &Pixmap::on_button_quit_clicked)); m_VBox.pack_start(m_ButtonQuit, Gtk::PACK_SHRINK, 0); // // Show window. // show_all(); } Pixmap::~Pixmap() {} void Pixmap::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. // Pixmap pixmap; kit.run(pixmap); return 0; }
00001 // -*- C++ -*- 00002 /* 00003 * pixmap-mixed.cc: 00004 * Simple off-screen rendering 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 using GL::Pixmap. 00123 // 00125 00126 class PixmapGLScene : public Gtk::DrawingArea 00127 { 00128 public: 00129 PixmapGLScene(); 00130 virtual ~PixmapGLScene(); 00131 00132 protected: 00133 // init OpenGL context 00134 void init_gl(); 00135 00136 protected: 00137 virtual bool on_configure_event(GdkEventConfigure* event); 00138 virtual bool on_expose_event(GdkEventExpose* event); 00139 00140 protected: 00141 // OpenGL rendering stuff: 00142 Glib::RefPtr<Gdk::GL::Config> m_GLConfig; 00143 Glib::RefPtr<Gdk::GL::Context> m_GLContext; 00144 Glib::RefPtr<Gdk::Pixmap> m_Pixmap; 00145 }; 00146 00147 PixmapGLScene::PixmapGLScene() 00148 : m_GLConfig(0), m_GLContext(0), m_Pixmap(0) 00149 { 00150 // 00151 // Configure OpenGL-capable visual. 00152 // 00153 00154 // Try single-buffered visual 00155 m_GLConfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB | 00156 Gdk::GL::MODE_DEPTH | 00157 Gdk::GL::MODE_SINGLE); 00158 if (!m_GLConfig) 00159 { 00160 std::cerr << "*** Cannot find any OpenGL-capable visual.\n"; 00161 std::exit(1); 00162 } 00163 00164 // print frame buffer attributes. 00165 GLConfigUtil::examine_gl_attrib(m_GLConfig); 00166 00167 // 00168 // Set OpenGL-capable colormap. 00169 // 00170 00171 set_colormap(m_GLConfig->get_colormap()); 00172 } 00173 00174 PixmapGLScene::~PixmapGLScene() 00175 { 00176 } 00177 00178 void PixmapGLScene::init_gl() 00179 { 00180 GLUquadricObj* qobj = gluNewQuadric(); 00181 gluQuadricDrawStyle(qobj, GLU_FILL); 00182 glNewList(1, GL_COMPILE); 00183 gluSphere(qobj, 1.0, 20, 20); 00184 glEndList(); 00185 00186 static GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0}; 00187 static GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; 00188 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); 00189 glLightfv(GL_LIGHT0, GL_POSITION, light_position); 00190 glEnable(GL_LIGHTING); 00191 glEnable(GL_LIGHT0); 00192 glEnable(GL_DEPTH_TEST); 00193 00194 glClearColor(1.0, 1.0, 1.0, 1.0); 00195 glClearDepth(1.0); 00196 00197 glViewport(0, 0, get_width(), get_height()); 00198 00199 glMatrixMode(GL_PROJECTION); 00200 glLoadIdentity(); 00201 gluPerspective(40.0, 1.0, 1.0, 10.0); 00202 00203 glMatrixMode(GL_MODELVIEW); 00204 glLoadIdentity(); 00205 gluLookAt(0.0, 0.0, 3.0, 00206 0.0, 0.0, 0.0, 00207 0.0, 1.0, 0.0); 00208 glTranslatef(0.0, 0.0, -3.0); 00209 } 00210 00211 bool PixmapGLScene::on_configure_event(GdkEventConfigure* event) 00212 { 00213 // 00214 // Create an OpenGL off-screen rendering area. 00215 // 00216 00217 m_Pixmap = Gdk::Pixmap::create(get_window(), 00218 get_width(), get_height(), 00219 m_GLConfig->get_depth()); 00220 00221 // 00222 // Set OpenGL-capability to the pixmap (invoke extension method). 00223 // 00224 00225 Glib::RefPtr<Gdk::GL::Pixmap> glpixmap = 00226 Gdk::GL::ext(m_Pixmap).set_gl_capability(m_GLConfig); 00227 00228 // 00229 // Create OpenGL rendering context (not direct). 00230 // 00231 00232 if (!m_GLContext) 00233 m_GLContext = Gdk::GL::Context::create(glpixmap, false); 00234 00235 // 00236 // GL calls. 00237 // 00238 00239 // *** OpenGL BEGIN *** 00240 if (!glpixmap->gl_begin(m_GLContext)) 00241 return false; 00242 00243 static bool is_initialized = false; 00244 if (!is_initialized) 00245 { 00246 init_gl(); 00247 is_initialized = true; 00248 } 00249 00250 glViewport(0, 0, get_width(), get_height()); 00251 00252 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 00253 00254 // Sync. 00255 glpixmap->wait_gl(); 00256 00257 // GDK rendering. 00258 glpixmap->draw_rectangle(get_style()->get_fg_gc(get_state()), 00259 true, 00260 get_width()/10, 00261 get_height()/10, 00262 get_width()*8/10, 00263 get_height()*8/10); 00264 00265 // Sync. 00266 glpixmap->wait_gdk(); 00267 00268 glCallList(1); 00269 00270 glFlush(); 00271 00272 glpixmap->gl_end(); 00273 // *** OpenGL END *** 00274 00275 return true; 00276 } 00277 00278 bool PixmapGLScene::on_expose_event(GdkEventExpose* event) 00279 { 00280 if (!m_Pixmap) 00281 return false; 00282 00283 get_window()->draw_drawable(get_style()->get_fg_gc(get_state()), 00284 m_Pixmap, 00285 event->area.x, event->area.y, 00286 event->area.x, event->area.y, 00287 event->area.width, event->area.height); 00288 00289 return true; 00290 } 00291 00292 00294 // 00295 // The application class. 00296 // 00298 00299 class Pixmap : public Gtk::Window 00300 { 00301 public: 00302 Pixmap(); 00303 virtual ~Pixmap(); 00304 00305 protected: 00306 // signal handlers: 00307 void on_button_quit_clicked(); 00308 00309 protected: 00310 // member widgets: 00311 Gtk::VBox m_VBox; 00312 PixmapGLScene m_PixmapGLScene; 00313 Gtk::Button m_ButtonQuit; 00314 }; 00315 00316 Pixmap::Pixmap() 00317 : m_VBox(false, 0), m_ButtonQuit("Quit") 00318 { 00319 // 00320 // Top-level window. 00321 // 00322 00323 set_title("Pixmap"); 00324 00325 add(m_VBox); 00326 00327 // 00328 // Simple OpenGL scene using GL::Pixmap. 00329 // 00330 00331 m_PixmapGLScene.set_size_request(200, 200); 00332 00333 m_VBox.pack_start(m_PixmapGLScene); 00334 00335 // 00336 // Simple quit button. 00337 // 00338 00339 m_ButtonQuit.signal_clicked().connect( 00340 sigc::mem_fun(*this, &Pixmap::on_button_quit_clicked)); 00341 00342 m_VBox.pack_start(m_ButtonQuit, Gtk::PACK_SHRINK, 0); 00343 00344 // 00345 // Show window. 00346 // 00347 00348 show_all(); 00349 } 00350 00351 Pixmap::~Pixmap() 00352 {} 00353 00354 void Pixmap::on_button_quit_clicked() 00355 { 00356 Gtk::Main::quit(); 00357 } 00358 00359 00361 // 00362 // Main. 00363 // 00365 00366 int main(int argc, char** argv) 00367 { 00368 Gtk::Main kit(argc, argv); 00369 00370 // 00371 // Init gtkglextmm. 00372 // 00373 00374 Gtk::GL::init(argc, argv); 00375 00376 // 00377 // Query OpenGL extension version. 00378 // 00379 00380 int major, minor; 00381 Gdk::GL::query_version(major, minor); 00382 std::cout << "OpenGL extension version - " 00383 << major << "." << minor << std::endl; 00384 00385 // 00386 // Instantiate and run the application. 00387 // 00388 00389 Pixmap pixmap; 00390 00391 kit.run(pixmap); 00392 00393 return 0; 00394 }

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