#include #include "gpudataviz.h" #include "boring_parts.h" #include "gtk_win_main.h" #include "my_gtk_gl_scene.h" // Macro to make things readable in main() function #define EXIT_IF_FAIL(val, expr) do { \ if ( ! (expr) ) { \ std::cerr << "Init error " << val << std::endl; \ exit(val); \ } \ } while(0) #define CALL_TRACE do { \ std::cout << "trace " <<__PRETTY_FUNCTION__ << std::endl; \ } while(0) int main(int argc, char* argv[]) { CALL_TRACE; int meshWidth=64, meshHeight=64; // TODO : those vars should not be hardcoded // Initialize GTK Gtk::Main gtkKit(argc, argv); // gtk itself Gtk::GL::init(argc, argv); // gtkglextmm // Query and print OpenGL version int glVersionMajor, glVersionMinor; EXIT_IF_FAIL(1, Gdk::GL::query_version(glVersionMajor, glVersionMinor) ); std::cout << "OpenGL extension version - " << glVersionMajor << "." << glVersionMinor << std::endl; // Initialize OpenGL Gdk::GL::ConfigMode glMode = Gdk::GL::MODE_RGB | Gdk::GL::MODE_DEPTH | Gdk::GL::MODE_DOUBLE; Glib::RefPtr glconfig; EXIT_IF_FAIL(2, glconfig=Gdk::GL::Config::create(glMode) ); // Initialize OpenCL (if available) EXIT_IF_FAIL(3, initLibs()==0 ); // See boring_parts.cc // Initialize host work memory (array for all the vertex coordinates computed by OpenCL and displayed by OpenGL) cl_float4 *hostWorkMem = (cl_float4 *) calloc(meshWidth * meshHeight, sizeof(cl_float4)); EXIT_IF_FAIL(4, hostWorkMem); // Initialize the OpenGL scene MyGTKGLScene glScene(glconfig); // Instantiate and run the GTK app GTKWinMain gtkwinmain(glScene); gtkKit.run(gtkwinmain); return 0; } /* MyGTKGLScene implementation I want to keep interesting code part in this file in natural reading order */ MyGTKGLScene::MyGTKGLScene(Glib::RefPtr &glconfig) { CALL_TRACE; set_gl_capability(glconfig); // Set OpenGL-capability to the widget. } MyGTKGLScene::~MyGTKGLScene() { CALL_TRACE; } void MyGTKGLScene::on_size_request(Gtk::Requisition* requisition) { CALL_TRACE; *requisition = Gtk::Requisition(); requisition->width = 320; requisition->height = 240; } void MyGTKGLScene::on_realize() { CALL_TRACE; // This run once at window creation time Gtk::DrawingArea::on_realize(); Glib::RefPtr glwindow = get_gl_window(); // *** OpenGL BEGIN *** if (!glwindow->gl_begin(get_gl_context())) { std::cerr << "Oups : glwindow->gl_begin(get_gl_context())" << std::endl; 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 MyGTKGLScene::on_configure_event(GdkEventConfigure* event) { CALL_TRACE ; // This run mainly when GTK GL Widget is resized Glib::RefPtr glwindow = get_gl_window(); // *** OpenGL BEGIN *** if (!glwindow->gl_begin(get_gl_context())) { std::cerr << "Oups : glwindow->gl_begin(get_gl_context())" << std::endl; return false; } int width = get_width(); int height = get_height(); GLdouble ratio = (GLdouble) width/height; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, ratio, 0.1, 10.0); glMatrixMode(GL_MODELVIEW); glwindow->gl_end(); // *** OpenGL END *** return true; } bool MyGTKGLScene::on_expose_event(GdkEventExpose* event) { CALL_TRACE ; // This run mainly when GTK GL Widget have to be redrawn Glib::RefPtr glwindow = get_gl_window(); // *** OpenGL BEGIN *** if (!glwindow->gl_begin(get_gl_context())) { std::cerr << "Oups : glwindow->gl_begin(get_gl_context())" << std::endl; return false; } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glCallList(1); glwindow->gl_end(); // *** OpenGL END *** // Display the rendered image glwindow->swap_buffers(); return true; }