From fc4daa1fe40d0127dbd1e9fdbd4031bade1f7522 Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Mon, 4 Mar 2013 22:38:40 +0000 Subject: Ok, première version qui affiche le mesh correctement initializé avec OpenCL et qui affiche même un sinus radial sur un clic de souris mouse1. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reste a capturer les evenements mouse wheel et/ou clavier. git-svn-id: file:///var/svn/2013-gpudataviz/trunk@19 371a6b4a-a258-45f8-9dcc-bdd82ce0ac9d --- src/gpudataviz.cpp | 129 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 83 insertions(+), 46 deletions(-) (limited to 'src/gpudataviz.cpp') diff --git a/src/gpudataviz.cpp b/src/gpudataviz.cpp index 72ee472..2b2386a 100644 --- a/src/gpudataviz.cpp +++ b/src/gpudataviz.cpp @@ -1,12 +1,7 @@ #include - #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 @@ -59,7 +54,7 @@ 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 = 0.0f; this->camera.ry = 0.0f; this->camera.tz = -3.0f; + this->camera.rx = -64.0f; this->camera.ry = -16.0f; this->camera.tz = -1.0f; } MyGTKGLSceneWidget::~MyGTKGLSceneWidget() { } @@ -97,49 +92,54 @@ void MyGTKGLSceneWidget::on_realize() { glBufferData(GL_ARRAY_BUFFER, gl_vbo_data_size, NULL, GL_DYNAMIC_DRAW); gl_res=glGetError(); if ( gl_res != GL_NO_ERROR ) { - std::cerr << "glBufferData(). Unable to allocate " << gl_vbo_data_size << "bytes in VRAM" << std::endl; + std::cerr << "glBufferData(). Unable to allocate " << gl_vbo_data_size << " bytes in VRAM" << std::endl; std::cerr << gluErrorString(gl_res); EXIT_IF_FAIL(5, false); } -//#ifdef HAS_OPENCL -// #ifdef X11 +//#ifdef X11 intptr_t gl_context = (intptr_t)glXGetCurrentContext(); intptr_t gl_display = (intptr_t)glXGetCurrentDisplay(); - - int cl_res = clKit.initCL(gl_display, gl_context, gl_vbo, mesh_width, mesh_height, group_size); - EXIT_IF_FAIL(cl_res, cl_res==0); - -// std::cerr << "DEBUG : begin initOpenCL()" << std::endl; -// int cl_res = initOpenCL(gl_display, gl_context, gl_vbo); /* See boring_parts.cpp */ -// EXIT_IF_FAIL(cl_res, cl_res==0); -// #else -// #error initOpenCL works only for X11 systems for now -// #endif -// } +//#else +// #error initOpenCL works only for X11 systems for now //#endif + int cl_res = this->clKit.initCL(gl_display, gl_context, gl_vbo, mesh_width, mesh_height, group_size); + EXIT_IF_FAIL(cl_res, cl_res==0); + this->clKit.resetVBO(); // XXX Just for displaying a flat mesh at start + + const char source[]=STRINGIFY( + /* This is OpenCL kernel code (Syntax like C but it's a different language) */ + __kernel void water1(__global float4 *pos, unsigned int width, unsigned int height, float time) { + unsigned int nx = get_global_id(0); + unsigned int ny = get_global_id(1); + /* calculate uv coordinates of the mesh point [0.0;1.0] */ + float u = nx / (float) width; + float v = ny / (float) height; + /* calculate centered coordinates [-1.0;1.0] */ + float x = u*2.0-1.0; + float y = v*2.0-1.0; + /* We only use normalized quaterinons here */ + float w = 1.0; + /* Calculate the desirated value of the mesh point */ + float freq = 8.0 * 3.14; + float amp = 1.0 / 10.0; /* 0.1 does NOT works ! WTF !!! */ + float speed = 1.0; + float dist = sqrt(x*x+y*y); + float z = amp * sin( freq * dist - speed * time ) / dist ; + + /* Write output vertex (centered) */ + pos[ny*width+nx] = (float4)(x, y, z, w); + } + ); + + std::list knames; + knames.push_back("water1"); + cl_res = this->clKit.compileKernels(knames, source, sizeof(source)); + + EXIT_IF_FAIL(6, cl_res==0); + knames.clear(); - // 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 @@ -149,8 +149,28 @@ void MyGTKGLSceneWidget::on_realize() { bool MyGTKGLSceneWidget::on_configure_event(GdkEventConfigure* event) { CALL_TRACE ; // This one runs mainly when GTK GL Widget is resized - // See boring_parts.cpp. In short : gluPerspective(60.0, aspect, 0.1, 10.0); - return updateGLProjectionMatrix(get_gl_context(), get_gl_window(), get_width(), get_height()); + + float h=this->get_height(); + float w=this->get_width(); + Glib::RefPtr glwindow = get_gl_window(); + + // *** OpenGL BEGIN *** + + //FIXME could segfault if get_gl_window() has failed + if (!glwindow->gl_begin(get_gl_context())) { + std::cerr << "Oups : glwindow->gl_begin(get_gl_context())" << std::endl; + return false; + } + + glViewport(0, 0, w, h); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(60.0, w/h, 0.1, 10.0); + + glwindow->gl_end(); + // *** OpenGL END *** + + return true; } bool MyGTKGLSceneWidget::on_expose_event(GdkEventExpose* event) { @@ -169,12 +189,18 @@ bool MyGTKGLSceneWidget::on_expose_event(GdkEventExpose* event) { 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); + glRotatef(this->camera.ry, 0.0, 0.0, 1.0); - // Drawing all the stuff glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glCallList(1); + // Drawing all the stuff + glColor4f(0.40f,0.78f,0.97f,1.0f); + glBindBuffer(GL_ARRAY_BUFFER, this->clKit.getGLVBO()); + glVertexPointer(4, GL_FLOAT, 0, (GLvoid *) 0); + glEnableClientState(GL_VERTEX_ARRAY); + glDrawArrays(GL_POINTS, 0, this->clKit.getMeshItemCount()); + glDisableClientState(GL_COLOR_ARRAY); + glBindBuffer(GL_ARRAY_BUFFER, 0); glwindow->gl_end(); // *** OpenGL END *** @@ -243,11 +269,12 @@ bool MyGTKGLSceneWidget::do_mouse_logic(GdkEventType type, guint state, guint x, 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; + 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; + //std::cout << "this->camera == {" << this->camera.rx << ", " << this->camera.ry << ", " << this->camera.tz << "}" << std::endl; } MOUSE_DRAGING(GDK_BUTTON2_MASK) { @@ -262,8 +289,18 @@ bool MyGTKGLSceneWidget::do_mouse_logic(GdkEventType type, guint state, guint x, redraw=true; } + MOUSE_CLIC(GDK_BUTTON4_MASK, 0, 0) { + this->camera.tz += 1.0f; + } + + MOUSE_CLIC(GDK_BUTTON5_MASK, 0, 0) { + this->camera.tz -= 1.0f; + } + MOUSE_CLIC(GDK_BUTTON1_MASK, 0, 0) { //TODO + this->clKit.execKernel("water1", 0.0f); + redraw=true; } -- cgit v1.2.3