00001
00002
00003
00004
00005
00006
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
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
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
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
00123
00125
00126
class PixmapGLScene :
public Gtk::DrawingArea
00127 {
00128
public:
00129 PixmapGLScene();
00130
virtual ~PixmapGLScene();
00131
00132
protected:
00133
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
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
00152
00153
00154
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
00165 GLConfigUtil::examine_gl_attrib(m_GLConfig);
00166
00167
00168
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
00215
00216
00217 m_Pixmap = Gdk::Pixmap::create(get_window(),
00218 get_width(), get_height(),
00219 m_GLConfig->get_depth());
00220
00221
00222
00223
00224
00225 Glib::RefPtr<Gdk::GL::Pixmap> glpixmap =
00226
Gdk::GL::ext(m_Pixmap).set_gl_capability(m_GLConfig);
00227
00228
00229
00230
00231
00232
if (!m_GLContext)
00233 m_GLContext =
Gdk::GL::Context::create(glpixmap,
false);
00234
00235
00236
00237
00238
00239
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 glCallList(1);
00255
00256 glFlush();
00257
00258 glpixmap->gl_end();
00259
00260
00261
return true;
00262 }
00263
00264
bool PixmapGLScene::on_expose_event(GdkEventExpose* event)
00265 {
00266
if (!m_Pixmap)
00267
return false;
00268
00269 get_window()->draw_drawable(get_style()->get_fg_gc(get_state()),
00270 m_Pixmap,
00271 event->area.x, event->area.y,
00272 event->area.x, event->area.y,
00273 event->area.width, event->area.height);
00274
00275
return true;
00276 }
00277
00278
00280
00281
00282
00284
00285
class Pixmap :
public Gtk::Window
00286 {
00287
public:
00288 Pixmap();
00289
virtual ~Pixmap();
00290
00291
protected:
00292
00293
void on_button_quit_clicked();
00294
00295
protected:
00296
00297 Gtk::VBox m_VBox;
00298 PixmapGLScene m_PixmapGLScene;
00299 Gtk::Button m_ButtonQuit;
00300 };
00301
00302 Pixmap::Pixmap()
00303 : m_VBox(false, 0), m_ButtonQuit("Quit")
00304 {
00305
00306
00307
00308
00309 set_title(
"Pixmap");
00310
00311 add(m_VBox);
00312
00313
00314
00315
00316
00317 m_PixmapGLScene.set_size_request(200, 200);
00318
00319 m_VBox.pack_start(m_PixmapGLScene);
00320
00321
00322
00323
00324
00325 m_ButtonQuit.signal_clicked().connect(
00326 sigc::mem_fun(*
this, &Pixmap::on_button_quit_clicked));
00327
00328 m_VBox.pack_start(m_ButtonQuit, Gtk::PACK_SHRINK, 0);
00329
00330
00331
00332
00333
00334 show_all();
00335 }
00336
00337 Pixmap::~Pixmap()
00338 {}
00339
00340
void Pixmap::on_button_quit_clicked()
00341 {
00342 Gtk::Main::quit();
00343 }
00344
00345
00347
00348
00349
00351
00352
int main(
int argc,
char** argv)
00353 {
00354 Gtk::Main kit(argc, argv);
00355
00356
00357
00358
00359
00360
Gtk::GL::init(argc, argv);
00361
00362
00363
00364
00365
00366
int major, minor;
00367
Gdk::GL::query_version(major, minor);
00368 std::cout <<
"OpenGL extension version - "
00369 << major <<
"." << minor << std::endl;
00370
00371
00372
00373
00374
00375 Pixmap pixmap;
00376
00377 kit.run(pixmap);
00378
00379
return 0;
00380 }