From dcbaaf5bb09caf07f27c03f3c3db196542668b4a Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Sun, 3 Mar 2013 13:56:50 +0000 Subject: Standardisation des extensions. Avancee sur l'initialisation des VBO mais je galere a trouver un (bon) moyen de charger les fonctions OpenGL qui vont bien. git-svn-id: file:///var/svn/2013-gpudataviz/trunk@17 371a6b4a-a258-45f8-9dcc-bdd82ce0ac9d --- src/boring_parts.cc | 179 ------------------------ src/boring_parts.cpp | 179 ++++++++++++++++++++++++ src/boring_parts.h | 27 ---- src/boring_parts.hpp | 27 ++++ src/compil.sh | 7 +- src/gpudataviz.cc | 283 -------------------------------------- src/gpudataviz.cpp | 301 +++++++++++++++++++++++++++++++++++++++++ src/gpudataviz.h | 0 src/gpudataviz.hpp | 0 src/gtk_includes.h | 16 --- src/gtk_includes.hpp | 19 +++ src/gtk_win_main.cc | 30 ---- src/gtk_win_main.cpp | 30 ++++ src/gtk_win_main.h | 21 --- src/gtk_win_main.hpp | 21 +++ src/my_gtk_gl_scene_widget.h | 29 ---- src/my_gtk_gl_scene_widget.hpp | 29 ++++ 17 files changed, 610 insertions(+), 588 deletions(-) delete mode 100644 src/boring_parts.cc create mode 100644 src/boring_parts.cpp delete mode 100644 src/boring_parts.h create mode 100644 src/boring_parts.hpp delete mode 100644 src/gpudataviz.cc create mode 100644 src/gpudataviz.cpp delete mode 100644 src/gpudataviz.h create mode 100644 src/gpudataviz.hpp delete mode 100644 src/gtk_includes.h create mode 100644 src/gtk_includes.hpp delete mode 100644 src/gtk_win_main.cc create mode 100644 src/gtk_win_main.cpp delete mode 100644 src/gtk_win_main.h create mode 100644 src/gtk_win_main.hpp delete mode 100644 src/my_gtk_gl_scene_widget.h create mode 100644 src/my_gtk_gl_scene_widget.hpp (limited to 'src') diff --git a/src/boring_parts.cc b/src/boring_parts.cc deleted file mode 100644 index 27ead8b..0000000 --- a/src/boring_parts.cc +++ /dev/null @@ -1,179 +0,0 @@ -#include "boring_parts.h" - -// TODO : only need OpenGL things, not GTK ones for now -//#include "gtk_includes.h" - -#define RETURN_IF_FAIL(expr) do { \ - int res=(expr); \ - if ( res != 0 ) return res; \ -} while(0) - -// TODO : print streamsdk::getOpenCLErrorCodeStr(res) -#define CL_RETURN_VAL_IF_FAIL(val, expr) do { \ - cl_int res=(expr); \ - if ( res != CL_SUCCESS ) { \ - std::cerr << "file " << __FILE__ << ": line " << __LINE__ << " (" << __PRETTY_FUNCTION__ \ - << "): '" << "expr" << "' failed (return code : " << res << ")" << std::endl; \ - return val; \ - } \ -} while(0) - - -/* From http://stackoverflow.com/questions/4317062/opengl-how-to-check-if-the-user-supports-glgenbuffers -#ifndef STRINGIFY - #define STRINGIFY(x) #x -#endif -#ifdef WIN32 - #include - #define glGetProcAddress(a) wglGetProcAddress(a) -#endif -#ifdef X11 - #define glGetProcAddress(a) glXGetProcAddress ( \ - reinterpret_cast(a) \ - ) -#endif - -#ifndef GetExtension - #define GetExtension(Type, ExtenName) \ - ExtenName = (Type) \ - glGetProcAddress(STRINGIFY(ExtenName)); \ - if(!ExtenName) \ - { \ - std:cout << "Your Computer Does Not " \ - << "Support GL Extension: " \ - << STRINGIFY(ExtenName) \ - << std::endl; \ - exit(1); \ - } \ - else \ - { \ - std::cout << "Loaded Extension: " \ - << STRINGIFY(ExtenName) \ - << std::endl; \ - } -#endif -*/ - -#ifdef HAS_OPENCL -int initOpenCL(intptr_t gl_display, intptr_t gl_context, intptr_t gl_vbo) { - cl_uint id, numPlatforms, numDevices; - char pbuf[100]; - std::string dTypeStr; - cl_platform_id *platforms, platform; - cl_device_id /* *devices, */device; - cl_context cl_ctx; - cl_command_queue cl_commandQueue; - bool usableDeviceFound=false; - - // Get platform count - CL_RETURN_VAL_IF_FAIL(1, - clGetPlatformIDs(0, NULL, &numPlatforms) - ); - - std::cout << "Detected " << numPlatforms << " platform(s)" << std::endl; - if ( ! ( numPlatforms > 0 ) ) return 2; - - // Allocate room for all platform IDs - platforms = new cl_platform_id[numPlatforms]; - - // Get platform IDs - CL_RETURN_VAL_IF_FAIL(3, - clGetPlatformIDs(numPlatforms, platforms, &numPlatforms) - ); - - // Enumerate platforms and grab informations - for(id=0;id glCtx, Glib::RefPtr glWin, int width, int height) { - - GLdouble aspect = (GLdouble) width/height; - - // *** OpenGL BEGIN *** - if (!glWin->gl_begin(glCtx)) return false; - - glViewport(0, 0, width, height); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - gluPerspective(60.0, aspect, 0.1, 10.0); - glMatrixMode(GL_MODELVIEW); - - glWin->gl_end(); - // *** OpenGL END *** - - return true; - -} - diff --git a/src/boring_parts.cpp b/src/boring_parts.cpp new file mode 100644 index 0000000..0f4ee1a --- /dev/null +++ b/src/boring_parts.cpp @@ -0,0 +1,179 @@ +#include "boring_parts.hpp" + +// TODO : only need OpenGL things, not GTK ones for now +//#include "gtk_includes.hpp" + +#define RETURN_IF_FAIL(expr) do { \ + int res=(expr); \ + if ( res != 0 ) return res; \ +} while(0) + +// TODO : print streamsdk::getOpenCLErrorCodeStr(res) +#define CL_RETURN_VAL_IF_FAIL(val, expr) do { \ + cl_int res=(expr); \ + if ( res != CL_SUCCESS ) { \ + std::cerr << "file " << __FILE__ << ": line " << __LINE__ << " (" << __PRETTY_FUNCTION__ \ + << "): '" << "expr" << "' failed (return code : " << res << ")" << std::endl; \ + return val; \ + } \ +} while(0) + + +/* From http://stackoverflow.com/questions/4317062/opengl-how-to-check-if-the-user-supports-glgenbuffers +#ifndef STRINGIFY + #define STRINGIFY(x) #x +#endif +#ifdef WIN32 + #include + #define glGetProcAddress(a) wglGetProcAddress(a) +#endif +#ifdef X11 + #define glGetProcAddress(a) glXGetProcAddress ( \ + reinterpret_cast(a) \ + ) +#endif + +#ifndef GetExtension + #define GetExtension(Type, ExtenName) \ + ExtenName = (Type) \ + glGetProcAddress(STRINGIFY(ExtenName)); \ + if(!ExtenName) \ + { \ + std:cout << "Your Computer Does Not " \ + << "Support GL Extension: " \ + << STRINGIFY(ExtenName) \ + << std::endl; \ + exit(1); \ + } \ + else \ + { \ + std::cout << "Loaded Extension: " \ + << STRINGIFY(ExtenName) \ + << std::endl; \ + } +#endif +*/ + +#ifdef HAS_OPENCL +int initOpenCL(intptr_t gl_display, intptr_t gl_context, intptr_t gl_vbo) { + cl_uint id, numPlatforms; + char pbuf[100]; + std::string dTypeStr; + cl_platform_id *platforms, platform; + cl_device_id /* *devices, */device; + cl_context cl_ctx; + cl_command_queue cl_commandQueue; + bool usableDeviceFound=false; + + // Get platform count + CL_RETURN_VAL_IF_FAIL(1, + clGetPlatformIDs(0, NULL, &numPlatforms) + ); + + std::cout << "Detected " << numPlatforms << " platform(s)" << std::endl; + if ( ! ( numPlatforms > 0 ) ) return 2; + + // Allocate room for all platform IDs + platforms = new cl_platform_id[numPlatforms]; + + // Get platform IDs + CL_RETURN_VAL_IF_FAIL(3, + clGetPlatformIDs(numPlatforms, platforms, &numPlatforms) + ); + + // Enumerate platforms and grab informations + for(id=0;id glCtx, Glib::RefPtr glWin, int width, int height) { + + GLdouble aspect = (GLdouble) width/height; + + // *** OpenGL BEGIN *** + if (!glWin->gl_begin(glCtx)) return false; + + glViewport(0, 0, width, height); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(60.0, aspect, 0.1, 10.0); + glMatrixMode(GL_MODELVIEW); + + glWin->gl_end(); + // *** OpenGL END *** + + return true; + +} + diff --git a/src/boring_parts.h b/src/boring_parts.h deleted file mode 100644 index b4cd58d..0000000 --- a/src/boring_parts.h +++ /dev/null @@ -1,27 +0,0 @@ -#include - -#include /* Must included before X11/Xlib.h */ -//#include /* Seems included by GL/glxew.h */ -//#include /* For GLXContext, must appear before gl.h, includes X11/Xlib.h */ -#include /* Includes a GL/gl.h (the right one ??) */ - -//#include -#include -//#include - -int initLibs(); - -#ifdef HAS_OPENCL -#include -int initOpenCL(intptr_t gl_display, intptr_t gl_context, intptr_t gl_vbo); -#else -// Quick and dirty cl_float4 replacement -typedef union { - float s[4]; - struct{ float x, y, z, w; }; - struct{ float s0, s1, s2, s3; }; -} cl_float4; -#endif /*HAS_OPENCL*/ - -bool updateGLProjectionMatrix(Glib::RefPtr glCtx, Glib::RefPtr glWin, int width, int height); - diff --git a/src/boring_parts.hpp b/src/boring_parts.hpp new file mode 100644 index 0000000..b4cd58d --- /dev/null +++ b/src/boring_parts.hpp @@ -0,0 +1,27 @@ +#include + +#include /* Must included before X11/Xlib.h */ +//#include /* Seems included by GL/glxew.h */ +//#include /* For GLXContext, must appear before gl.h, includes X11/Xlib.h */ +#include /* Includes a GL/gl.h (the right one ??) */ + +//#include +#include +//#include + +int initLibs(); + +#ifdef HAS_OPENCL +#include +int initOpenCL(intptr_t gl_display, intptr_t gl_context, intptr_t gl_vbo); +#else +// Quick and dirty cl_float4 replacement +typedef union { + float s[4]; + struct{ float x, y, z, w; }; + struct{ float s0, s1, s2, s3; }; +} cl_float4; +#endif /*HAS_OPENCL*/ + +bool updateGLProjectionMatrix(Glib::RefPtr glCtx, Glib::RefPtr glWin, int width, int height); + diff --git a/src/compil.sh b/src/compil.sh index 84ed7c4..f3ca2a0 100755 --- a/src/compil.sh +++ b/src/compil.sh @@ -45,9 +45,10 @@ function link_cxx() { rm -v $BUILD_PATH/* || true -build_cxx gpudataviz.o gpudataviz.cc -build_cxx boring_parts.o boring_parts.cc -build_cxx gtk_win_main.o gtk_win_main.cc +build_cxx gl_core_1_5_vbo.o gl_core_1_5_vbo.cpp +build_cxx gpudataviz.o gpudataviz.cpp +build_cxx boring_parts.o boring_parts.cpp +build_cxx gtk_win_main.o gtk_win_main.cpp link_cxx gpudataviz $BUILD_PATH/*.o diff --git a/src/gpudataviz.cc b/src/gpudataviz.cc deleted file mode 100644 index fbc1852..0000000 --- a/src/gpudataviz.cc +++ /dev/null @@ -1,283 +0,0 @@ -#include - -#include "gpudataviz.h" -#include "boring_parts.h" - -//#include "gtk_includes.h" -#include "gtk_win_main.h" -#include "my_gtk_gl_scene_widget.h" - -#include // X11 specific - -// 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) - -// For understanding the event sequences -#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 the OpenGL scene - MyGTKGLSceneWidget glScene(glconfig); - - // Instantiate the GTK app - GTKWinMain gtkwinmain(glScene); - - // Initialize OpenCL (only after the MyGTKGLSceneWidget realize) - //EXIT_IF_FAIL(3, initLibs()==0 ); // See boring_parts.cc - - gtkKit.run(gtkwinmain); - - return 0; -} - -/* MyGTKGLSceneWidget implementation - I want to keep interesting code part in this file - in natural reading order -*/ -MyGTKGLSceneWidget::MyGTKGLSceneWidget(Glib::RefPtr &glconfig) { - set_gl_capability(glconfig); - Gdk::EventMask mask = Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_MOTION_MASK | Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK; - set_events(mask); // The containing window should have those attributes too - this->camera.rx = this->camera.ry = 0.0f; this->camera.tz = -3.0f; -} - -MyGTKGLSceneWidget::~MyGTKGLSceneWidget() { } - -void MyGTKGLSceneWidget::on_size_request(Gtk::Requisition* requisition) { - CALL_TRACE; // Technical stuff : GTK call this to ask the widget minimal size - *requisition = Gtk::Requisition(); - requisition->width = 320; requisition->height = 240; -} - -void MyGTKGLSceneWidget::on_realize() { - CALL_TRACE; // This one runs once at window creation time - // It's time to setup GL things that don't change on each frame - - 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; - } - - // TODO : GL_ARB_vertex_buffer_object - intptr_t gl_vbo=0; - -#ifdef HAS_OPENCL - static bool isOpenCLInitialized=false; - - if (! isOpenCLInitialized) { -// #ifdef X11 - intptr_t gl_context = (intptr_t)glXGetCurrentContext(); - intptr_t gl_display = (intptr_t)glXGetCurrentDisplay(); - - std::cerr << "DEBUG : begin initOpenCL()" << std::endl; - initOpenCL(gl_display, gl_context, gl_vbo); /* See boring_parts.cc */ - isOpenCLInitialized=true; -// #else -// #error initOpenCL works only for X11 systems for now -// #endif - } -#endif - - - // Programmatically create rendering lists : opengl will able to replay that efficiently - GLUquadricObj* qobj = gluNewQuadric(); - gluQuadricDrawStyle(qobj, GLU_FILL); - glNewList(1, GL_COMPILE); - //gluSphere(qobj, 1.0, 20, 20); - gluSphere(qobj, 1.0, 5, 5); - glEndList(); - - // Setup scene envrionnement - 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); - - // Projection setup is done at on_configure_event - // Camera setup (ie initial MODELVIEW matrix) is done at on_expose_event - - glwindow->gl_end(); - // *** OpenGL END *** -} - -bool MyGTKGLSceneWidget::on_configure_event(GdkEventConfigure* event) { - CALL_TRACE ; // This one runs mainly when GTK GL Widget is resized - // See boring_parts.cc. In short : gluPerspective(60.0, aspect, 0.1, 10.0); - return updateGLProjectionMatrix(get_gl_context(), get_gl_window(), get_width(), get_height()); -} - -bool MyGTKGLSceneWidget::on_expose_event(GdkEventExpose* event) { -// CALL_TRACE ; // This one runs 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; - } - - //Camera position update - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glTranslatef(0.0, 0.0, this->camera.tz); - glRotatef(this->camera.rx, 1.0, 0.0, 0.0); - glRotatef(this->camera.ry, 0.0, 1.0, 0.0); - - // Drawing all the stuff - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glCallList(1); - - - glwindow->gl_end(); - // *** OpenGL END *** - - glwindow->swap_buffers(); // Display the rendered image - - return true; -} - -bool MyGTKGLSceneWidget::on_motion_notify_event (GdkEventMotion *event) { - return do_mouse_logic(event->type, event->state, event->x, event->y); -} - -bool MyGTKGLSceneWidget::on_button_press_event(GdkEventButton *event) { - return do_mouse_logic(event->type, event->state | 1<<(7+event->button) , event->x, event->y); -} - -bool MyGTKGLSceneWidget::on_button_release_event(GdkEventButton *event) { - return do_mouse_logic(event->type, event->state, event->x, event->y); -} - -#define ALL_KEYBOARD_MODIFIERS ( GDK_SHIFT_MASK | GDK_CONTROL_MASK \ - | GDK_SUPER_MASK | GDK_HYPER_MASK | GDK_META_MASK ) - -#define MOUSE_CLIC(button, required_modifier_mask, allowed_extra_modifier_mask) if ( \ - type == GDK_BUTTON_RELEASE \ - && ( state & button ) == button \ - && ( state & required_modifier_mask ) == required_modifier_mask \ - && ( ( state & ALL_KEYBOARD_MODIFIERS ) & ~( required_modifier_mask | allowed_extra_modifier_mask ) ) == 0 \ -) -#define MOUSE_DOUBLECLIC(state_mask) if ( \ - type == GDK_BUTTON_RELEASE \ - && ( state & state_mask ) == state_mask \ - && prev_type == GDK_2BUTTON_PRESS \ -) -#define MOUSE_DRAG_START(state_mask) if ( \ - type == GDK_BUTTON_PRESS \ - && ( state & state_mask ) == state_mask \ -) -#define MOUSE_DRAGING(state_mask) if ( \ - ( type == GDK_MOTION_NOTIFY ) \ - && ( state & state_mask ) == state_mask \ -) -#define MOUSE_DRAG_END(state_mask) if ( \ - type == GDK_BUTTON_RELEASE \ - && ( state & state_mask ) == state_mask \ -) - -bool MyGTKGLSceneWidget::do_mouse_logic(GdkEventType type, guint state, guint x, guint y) { -/* - * type : the type of the event. - * Simple motion : GDK_MOTION_NOTIFY (3) - * Simple clic : GDK_BUTTON_PRESS then GDK_BUTTON_RELEASE (4 then 7) - * Double clic : GDK_BUTTON_PRESS, GDK_BUTTON_RELEASE, GDK_BUTTON_PRESS, GDK_2BUTTON_PRESS, GDK_BUTTON_RELEASE (4 7 4 5 7) - * - * stat : a bit-mask representing the state of the modifier keys and the pointer buttons. - * GDK_BUTTON1_MASK, ... , GDK_BUTTON5_MASK (mouse buttons) - * GDK_SHIFT_MASK, GDK_LOCK_MASK, GDK_CONTROL_MASK (keyboard standard modifier keys) - * GDK_MOD1_MASK, ... (normally MOD1 it is the Alt key) - * GDK_SUPER_MASK, GDK_HYPER_MASK, GDK_META_MASK (extra keybord modifier keys) - */ - // Static variables to hold previous mouse button event - static GdkEventType prev_type = GDK_NOTHING; - /*static guint prev_state=0; UNUSED FOR NOW */ - static guint drag_x=0, drag_y=0; - - bool redraw=false; // Setting it to true will queue a redraw to the widget (invalidate) - -// std::cout << "event type " << type << " state " << state << " on (" << x << "," << y << ") " << std::endl; - - /* *** BEGIN event filtering *** */ - MOUSE_DRAG_START(GDK_BUTTON2_MASK) { - drag_x=x; drag_y=y; - } - - MOUSE_DRAGING(GDK_BUTTON2_MASK) { - float mouse_sensivity = 0.2f; - gint dx = drag_x - x; // Delta movement (since last event) - gint dy = drag_y - y; // Not unsigned ! - // Yes dy for camera.rx, and -= operator - // GTK mouse coords and Opengl are not on the same coords system - this->camera.rx -= mouse_sensivity * dy; - this->camera.ry -= mouse_sensivity * dx; - drag_x = x; drag_y = y; - redraw=true; - } - - MOUSE_CLIC(GDK_BUTTON1_MASK, 0, 0) { - //TODO - } - - - // Demo filters - MOUSE_CLIC(GDK_BUTTON1_MASK, GDK_SHIFT_MASK, GDK_CONTROL_MASK) { - std::cout << "Mouse 1 clic with shift or control-shift" << std::endl; - } - - MOUSE_DOUBLECLIC(GDK_BUTTON3_MASK) { - std::cout << "Mouse 1 double clic" << std::endl; - } - /* *** END event filtering *** */ - - - // Previous button event retention for double-clic filtering - switch(type) { - case GDK_BUTTON_PRESS: - case GDK_BUTTON_RELEASE: - case GDK_2BUTTON_PRESS: - prev_type=type; -// prev_state=state; - break; - default: - break; - } - - if ( redraw ) queue_draw(); - return true; -} diff --git a/src/gpudataviz.cpp b/src/gpudataviz.cpp new file mode 100644 index 0000000..a9fdb8e --- /dev/null +++ b/src/gpudataviz.cpp @@ -0,0 +1,301 @@ +#include + + +//#include "gtk_includes.h" +#include "gtk_win_main.hpp" +//#include "my_gtk_gl_scene_widget.hpp" +#include "boring_parts.hpp" + +#include // X11 specific + +// 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) + +// For understanding the event sequences +#define CALL_TRACE do { \ + std::cout << "trace " <<__PRETTY_FUNCTION__ << std::endl; \ +} while(0) + +int main(int argc, char* argv[]) { + CALL_TRACE; + + // 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 the OpenGL scene widget (realization came later) + MyGTKGLSceneWidget glScene(glconfig); + + // Instantiate the GTK app (and realize glScene) + // Could exit() the program if problem with OpenGL or OpenCL + GTKWinMain gtkwinmain(glScene); + + // Initialize OpenCL (only after the MyGTKGLSceneWidget realize) + //EXIT_IF_FAIL(3, initLibs()==0 ); // See boring_parts.cc + + gtkKit.run(gtkwinmain); + + return 0; +} + +/* MyGTKGLSceneWidget implementation + I want to keep interesting code part in this file + in natural reading order +*/ +MyGTKGLSceneWidget::MyGTKGLSceneWidget(Glib::RefPtr &glconfig) { + set_gl_capability(glconfig); + Gdk::EventMask mask = Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_MOTION_MASK | Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK; + set_events(mask); // The containing window should have those attributes too + this->camera.rx = this->camera.ry = 0.0f; this->camera.tz = -3.0f; +} + +MyGTKGLSceneWidget::~MyGTKGLSceneWidget() { } + +void MyGTKGLSceneWidget::on_size_request(Gtk::Requisition* requisition) { + CALL_TRACE; // Technical stuff : GTK call this to ask the widget minimal size + *requisition = Gtk::Requisition(); + requisition->width = 320; requisition->height = 240; +} + +void MyGTKGLSceneWidget::on_realize() { + CALL_TRACE; // This one runs once at window creation time + // It's time to setup GL things that don't change on each frame + + GLenum gl_res; + + 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; + } + + EXIT_IF_FAIL(3, Gdk::GL::query_gl_extension("GL_ARB_vertex_buffer_object") ); + + size_t mesh_width=64; + size_t mesh_height=64; + GLsizeiptr gl_vbo_data_size = mesh_width * mesh_height * sizeof(cl_float4); + intptr_t gl_vbo=0; + + glGenBuffers(1, &gl_vbo); + glBindBuffer(GL_ARRAY_BUFFER, gl_vbo); + /* + STREAM : The data store contents will be modified once and used at most a few times. + STATIC : The data store contents will be modified once and used many times. + DYNAMIC : The data store contents will be modified repeatedly and used many times. + */ + glBufferData(GL_ARRAY_BUFFER, gl_vbo_data_size, NULL, GL_DYNAMIC_DRAW); + if ( gl_res=glGetError() ) { + std::cerr << "glBufferData(). Unable to allocate " << gl_vbo_data_size << "bytes in VRAM"; + std::cerr << gluErrorString(gl_res); + } + +#ifdef HAS_OPENCL +// static bool isOpenCLInitialized=false; + +// if (! isOpenCLInitialized) { +// #ifdef X11 + intptr_t gl_context = (intptr_t)glXGetCurrentContext(); + intptr_t gl_display = (intptr_t)glXGetCurrentDisplay(); + +// std::cerr << "DEBUG : begin initOpenCL()" << std::endl; + initOpenCL(gl_display, gl_context, gl_vbo); /* See boring_parts.cc */ +// isOpenCLInitialized=true; +// #else +// #error initOpenCL works only for X11 systems for now +// #endif +// } +#endif + + + // Programmatically create rendering lists : opengl will able to replay that efficiently + GLUquadricObj* qobj = gluNewQuadric(); + gluQuadricDrawStyle(qobj, GLU_FILL); + glNewList(1, GL_COMPILE); + //gluSphere(qobj, 1.0, 20, 20); + gluSphere(qobj, 1.0, 5, 5); + glEndList(); + + // Setup scene envrionnement + 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); + + // Projection setup is done at on_configure_event + // Camera setup (ie initial MODELVIEW matrix) is done at on_expose_event + + glwindow->gl_end(); + // *** OpenGL END *** +} + +bool MyGTKGLSceneWidget::on_configure_event(GdkEventConfigure* event) { + CALL_TRACE ; // This one runs mainly when GTK GL Widget is resized + // See boring_parts.cc. In short : gluPerspective(60.0, aspect, 0.1, 10.0); + return updateGLProjectionMatrix(get_gl_context(), get_gl_window(), get_width(), get_height()); +} + +bool MyGTKGLSceneWidget::on_expose_event(GdkEventExpose* event) { +// CALL_TRACE ; // This one runs 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; + } + + //Camera position update + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0, 0.0, this->camera.tz); + glRotatef(this->camera.rx, 1.0, 0.0, 0.0); + glRotatef(this->camera.ry, 0.0, 1.0, 0.0); + + // Drawing all the stuff + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glCallList(1); + + + glwindow->gl_end(); + // *** OpenGL END *** + + glwindow->swap_buffers(); // Display the rendered image + + return true; +} + +bool MyGTKGLSceneWidget::on_motion_notify_event (GdkEventMotion *event) { + return do_mouse_logic(event->type, event->state, event->x, event->y); +} + +bool MyGTKGLSceneWidget::on_button_press_event(GdkEventButton *event) { + return do_mouse_logic(event->type, event->state | 1<<(7+event->button) , event->x, event->y); +} + +bool MyGTKGLSceneWidget::on_button_release_event(GdkEventButton *event) { + return do_mouse_logic(event->type, event->state, event->x, event->y); +} + +#define ALL_KEYBOARD_MODIFIERS ( GDK_SHIFT_MASK | GDK_CONTROL_MASK \ + | GDK_SUPER_MASK | GDK_HYPER_MASK | GDK_META_MASK ) + +#define MOUSE_CLIC(button, required_modifier_mask, allowed_extra_modifier_mask) if ( \ + type == GDK_BUTTON_RELEASE \ + && ( state & button ) == button \ + && ( state & required_modifier_mask ) == required_modifier_mask \ + && ( ( state & ALL_KEYBOARD_MODIFIERS ) & ~( required_modifier_mask | allowed_extra_modifier_mask ) ) == 0 \ +) +#define MOUSE_DOUBLECLIC(state_mask) if ( \ + type == GDK_BUTTON_RELEASE \ + && ( state & state_mask ) == state_mask \ + && prev_type == GDK_2BUTTON_PRESS \ +) +#define MOUSE_DRAG_START(state_mask) if ( \ + type == GDK_BUTTON_PRESS \ + && ( state & state_mask ) == state_mask \ +) +#define MOUSE_DRAGING(state_mask) if ( \ + ( type == GDK_MOTION_NOTIFY ) \ + && ( state & state_mask ) == state_mask \ +) +#define MOUSE_DRAG_END(state_mask) if ( \ + type == GDK_BUTTON_RELEASE \ + && ( state & state_mask ) == state_mask \ +) + +bool MyGTKGLSceneWidget::do_mouse_logic(GdkEventType type, guint state, guint x, guint y) { +/* + * type : the type of the event. + * Simple motion : GDK_MOTION_NOTIFY (3) + * Simple clic : GDK_BUTTON_PRESS then GDK_BUTTON_RELEASE (4 then 7) + * Double clic : GDK_BUTTON_PRESS, GDK_BUTTON_RELEASE, GDK_BUTTON_PRESS, GDK_2BUTTON_PRESS, GDK_BUTTON_RELEASE (4 7 4 5 7) + * + * stat : a bit-mask representing the state of the modifier keys and the pointer buttons. + * GDK_BUTTON1_MASK, ... , GDK_BUTTON5_MASK (mouse buttons) + * GDK_SHIFT_MASK, GDK_LOCK_MASK, GDK_CONTROL_MASK (keyboard standard modifier keys) + * GDK_MOD1_MASK, ... (normally MOD1 it is the Alt key) + * GDK_SUPER_MASK, GDK_HYPER_MASK, GDK_META_MASK (extra keybord modifier keys) + */ + // Static variables to hold previous mouse button event + static GdkEventType prev_type = GDK_NOTHING; + /*static guint prev_state=0; UNUSED FOR NOW */ + static guint drag_x=0, drag_y=0; + + bool redraw=false; // Setting it to true will queue a redraw to the widget (invalidate) + +// std::cout << "event type " << type << " state " << state << " on (" << x << "," << y << ") " << std::endl; + + /* *** BEGIN event filtering *** */ + MOUSE_DRAG_START(GDK_BUTTON2_MASK) { + drag_x=x; drag_y=y; + } + + MOUSE_DRAGING(GDK_BUTTON2_MASK) { + float mouse_sensivity = 0.2f; + gint dx = drag_x - x; // Delta movement (since last event) + gint dy = drag_y - y; // Not unsigned ! + // Yes dy for camera.rx, and -= operator + // GTK mouse coords and Opengl are not on the same coords system + this->camera.rx -= mouse_sensivity * dy; + this->camera.ry -= mouse_sensivity * dx; + drag_x = x; drag_y = y; + redraw=true; + } + + MOUSE_CLIC(GDK_BUTTON1_MASK, 0, 0) { + //TODO + } + + + // Demo filters + MOUSE_CLIC(GDK_BUTTON1_MASK, GDK_SHIFT_MASK, GDK_CONTROL_MASK) { + std::cout << "Mouse 1 clic with shift or control-shift" << std::endl; + } + + MOUSE_DOUBLECLIC(GDK_BUTTON3_MASK) { + std::cout << "Mouse 1 double clic" << std::endl; + } + /* *** END event filtering *** */ + + + // Previous button event retention for double-clic filtering + switch(type) { + case GDK_BUTTON_PRESS: + case GDK_BUTTON_RELEASE: + case GDK_2BUTTON_PRESS: + prev_type=type; +// prev_state=state; + break; + default: + break; + } + + if ( redraw ) queue_draw(); + return true; +} diff --git a/src/gpudataviz.h b/src/gpudataviz.h deleted file mode 100644 index e69de29..0000000 diff --git a/src/gpudataviz.hpp b/src/gpudataviz.hpp new file mode 100644 index 0000000..e69de29 diff --git a/src/gtk_includes.h b/src/gtk_includes.h deleted file mode 100644 index 4a6632f..0000000 --- a/src/gtk_includes.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef GTK_INCLUDES_H -#define GTK_INCLUDES_H - -#include -#include - -#ifdef G_OS_WIN32 -#define WIN32_LEAN_AND_MEAN 1 -#include -#endif - -#include -#include - -#endif /* GTK_INCLUDES_H */ - diff --git a/src/gtk_includes.hpp b/src/gtk_includes.hpp new file mode 100644 index 0000000..5a60f04 --- /dev/null +++ b/src/gtk_includes.hpp @@ -0,0 +1,19 @@ +#ifndef GTK_INCLUDES_H +#define GTK_INCLUDES_H + + +#include +//#include "gl_core_3_1.hpp" +#include +//#include "gl_core_1_5_vbo.hpp" + +#ifdef G_OS_WIN32 +#define WIN32_LEAN_AND_MEAN 1 +#include +#endif + +//#include +#include + +#endif /* GTK_INCLUDES_H */ + diff --git a/src/gtk_win_main.cc b/src/gtk_win_main.cc deleted file mode 100644 index 8997b56..0000000 --- a/src/gtk_win_main.cc +++ /dev/null @@ -1,30 +0,0 @@ -#include "gtk_win_main.h" - - -GTKWinMain::GTKWinMain(MyGTKGLSceneWidget &glScene) - : m_VBox(false, 0), m_ButtonQuit("Quit"), m_GLScene(glScene) -{ - set_title(WIN_MAIN_TITLE); - set_reallocate_redraws(true); // Get automatically redrawn if any of their children changed allocation. - - // Add components to the window - add(m_VBox); - m_VBox.pack_start(m_GLScene); - m_VBox.pack_start(m_ButtonQuit, Gtk::PACK_SHRINK, 0); - - // Link signal handlers on events - m_ButtonQuit.signal_clicked().connect( sigc::mem_fun(*this, >KWinMain::on_button_quit_clicked) ); - Gdk::EventMask mask = Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_MOTION_MASK | Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK; - set_events(mask); - - show_all(); -} - -GTKWinMain::~GTKWinMain() -{} - -void GTKWinMain::on_button_quit_clicked() -{ - Gtk::Main::quit(); -} - diff --git a/src/gtk_win_main.cpp b/src/gtk_win_main.cpp new file mode 100644 index 0000000..465b828 --- /dev/null +++ b/src/gtk_win_main.cpp @@ -0,0 +1,30 @@ +#include "gtk_win_main.hpp" + + +GTKWinMain::GTKWinMain(MyGTKGLSceneWidget &glScene) + : m_VBox(false, 0), m_ButtonQuit("Quit"), m_GLScene(glScene) +{ + set_title(WIN_MAIN_TITLE); +// set_reallocate_redraws(true); // Get automatically redrawn if any of their children changed allocation. + + // Add components to the window + add(m_VBox); + m_VBox.pack_start(m_GLScene); + m_VBox.pack_start(m_ButtonQuit, Gtk::PACK_SHRINK, 0); + + // Link signal handlers on events + m_ButtonQuit.signal_clicked().connect( sigc::mem_fun(*this, >KWinMain::on_button_quit_clicked) ); + Gdk::EventMask mask = Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_MOTION_MASK | Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK; + set_events(mask); + + show_all(); +} + +GTKWinMain::~GTKWinMain() +{} + +void GTKWinMain::on_button_quit_clicked() +{ + Gtk::Main::quit(); +} + diff --git a/src/gtk_win_main.h b/src/gtk_win_main.h deleted file mode 100644 index c377040..0000000 --- a/src/gtk_win_main.h +++ /dev/null @@ -1,21 +0,0 @@ -#include "gtk_includes.h" -#include "my_gtk_gl_scene_widget.h" - -#define WIN_MAIN_TITLE "GPU Data Viz v0.1" - -class GTKWinMain : public Gtk::Window -{ - public: - GTKWinMain(MyGTKGLSceneWidget &glScene); - virtual ~GTKWinMain(); - - protected: - // signal handlers - void on_button_quit_clicked(); - - protected: - // member widgets - Gtk::VBox m_VBox; - Gtk::Button m_ButtonQuit; - MyGTKGLSceneWidget & m_GLScene; -}; diff --git a/src/gtk_win_main.hpp b/src/gtk_win_main.hpp new file mode 100644 index 0000000..2a42beb --- /dev/null +++ b/src/gtk_win_main.hpp @@ -0,0 +1,21 @@ +#include "gtk_includes.hpp" +#include "my_gtk_gl_scene_widget.hpp" + +#define WIN_MAIN_TITLE "GPU Data Viz v0.1" + +class GTKWinMain : public Gtk::Window +{ + public: + GTKWinMain(MyGTKGLSceneWidget &glScene); + virtual ~GTKWinMain(); + + protected: + // signal handlers + void on_button_quit_clicked(); + + protected: + // member widgets + Gtk::VBox m_VBox; + Gtk::Button m_ButtonQuit; + MyGTKGLSceneWidget & m_GLScene; +}; diff --git a/src/my_gtk_gl_scene_widget.h b/src/my_gtk_gl_scene_widget.h deleted file mode 100644 index bf156ab..0000000 --- a/src/my_gtk_gl_scene_widget.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef MY_GTK_GL_SCENE_H -#define MY_GTK_GL_SCENE_H - -#include "gtk_includes.h" - -// Class that will contain all the OpenGL logic for displaying the OpenCL computed data -// Implementation is kept in gpudataviz.cc (I want to keep interesting code part in this file) - -class MyGTKGLSceneWidget : public Gtk::DrawingArea, public Gtk::GL::Widget -{ - public: - MyGTKGLSceneWidget(Glib::RefPtr &glconfig); - virtual ~MyGTKGLSceneWidget(); - - protected: - virtual void on_size_request(Gtk::Requisition* requisition); - virtual void on_realize(); - virtual bool on_configure_event(GdkEventConfigure* event); - virtual bool on_expose_event(GdkEventExpose* event); - virtual bool on_motion_notify_event (GdkEventMotion *event); - virtual bool on_button_press_event(GdkEventButton *event); - virtual bool on_button_release_event(GdkEventButton *event); - - bool do_mouse_logic(GdkEventType type, guint state, guint x, guint y); - private: - struct camera_params { float rx; float ry; float tz; } camera; -}; - -#endif /*MY_GTK_GL_SCENE_H*/ diff --git a/src/my_gtk_gl_scene_widget.hpp b/src/my_gtk_gl_scene_widget.hpp new file mode 100644 index 0000000..212f346 --- /dev/null +++ b/src/my_gtk_gl_scene_widget.hpp @@ -0,0 +1,29 @@ +#ifndef MY_GTK_GL_SCENE_H +#define MY_GTK_GL_SCENE_H + +#include "gtk_includes.hpp" + +// Class that will contain all the OpenGL logic for displaying the OpenCL computed data +// Implementation is kept in gpudataviz.cc (I want to keep interesting code part in this file) + +class MyGTKGLSceneWidget : public Gtk::DrawingArea, public Gtk::GL::Widget +{ + public: + MyGTKGLSceneWidget(Glib::RefPtr &glconfig); + virtual ~MyGTKGLSceneWidget(); + + protected: + virtual void on_size_request(Gtk::Requisition* requisition); + virtual void on_realize(); + virtual bool on_configure_event(GdkEventConfigure* event); + virtual bool on_expose_event(GdkEventExpose* event); + virtual bool on_motion_notify_event (GdkEventMotion *event); + virtual bool on_button_press_event(GdkEventButton *event); + virtual bool on_button_release_event(GdkEventButton *event); + + bool do_mouse_logic(GdkEventType type, guint state, guint x, guint y); + private: + struct camera_params { float rx; float ry; float tz; } camera; +}; + +#endif /*MY_GTK_GL_SCENE_H*/ -- cgit v1.2.3