I am coding a graphic application in Qt (4.8.2)(VS 2008), using QGLWidget and therefore OpenGL. Here is short description of the application: It's a physics simulation. It consists of 2 threads. Main application thread handles scene drawing (QGLWidget subclass) and events. Computing thread is in a loop computing next steps of the simulation. Now... I would like to interact with the simulation using cursor (dragging objects or similar stuff). Since I decided not to do intelligent ray shooting in the scene along with some spatial space decomposition (maybe I'll have to do it afterall), I would like the computing thread to execute something like this:
glGetDoublev(GL_MODELVIEW_MATRIX, modelviewMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix);
glGetIntegerv(GL_VIEWPORT, viewport);
gluProject(px, py, pz, modelviewMatrix, projectionMatrix, viewport, &x, &y, &z);
in order to get screen [x;y] coordinates of the [px;py;pz] point and use them in computing next step of a simulation. Well, turns out main thread always gets correct modelviewMatrix and projectionMatrix arrays, but when this is executed by compute thread, it gets trash matrix data. I tried many things ... running makeCurrent() before querying OpenGL, locking any OpenGL actions so matrices should be untouched, but no success. I wonder, is this even possible ? Does OpenGL preserve matrices after drawing of a scene ? Is there any way how to make this piece of code thread safe ?
Do the GL queries in the main thread and pass the results to your compute thread.