Reputation: 6555
I have a strange problem which I guess gets caused by qt. I'm using the qt OpenGL widget and my redraw routine and the MouseMove event look like this:
void RenderGraphFrame::redraw()
{
GLdouble objX, objY, objZ;
GLdouble ModelViewMatrixvector[16], ProjectionViewMatrixVector[16];
GLint ViewportVector[4];
glGetIntegerv (GL_VIEWPORT, ViewportVector);
glGetDoublev (GL_MODELVIEW_MATRIX, ModelViewMatrixvector);
glGetDoublev (GL_PROJECTION_MATRIX, ProjectionViewMatrixVector);
gluUnProject(mousepositionX, mousepositionY, 0,
ModelViewMatrixvector,
ProjectionViewMatrixVector,
ViewportVector,
&objX, &objY, &objZ);
qDebug() << "objX: " << objX << " objY: " << objY << " objZ: " << objZ;
qglColor(Qt::red);
glBegin(GL_TRIANGLES);
glNormal3f(0,-1,0.707);
glVertex3f(objX-1,objY-1,0);
glVertex3f(objX+1,objY-1,0);
glVertex3f(objX,objY,1.2);
glEnd();
}
void RenderGraphFrame::mouseMoveEvent (QMouseEvent *event)
{
mousepositionX = event->x();
mousepositionY = event->y();
redraw();
return;
}
When I'm moving the mouse over the widget I get a whole stream of output through qDebug()
so it is sure the Even triggers and redraw()
gets executed.
For testing I want a sqaure consiting of 4 triangles to be rendered where 1 of them follows the cursor.
But there are only 3 shapes the 4th is missing... Thats at least what I was thinking. While I was dbeugging I figgured out while I was alt+tabbing out of the window, that when I'm pressing the alt
-key the redraw()
method gets invoked again with exactly the same output of the last mouse move but now the 4th shape gets rendered (not on the position I want, but that is probabbly just error in my 3d-logic)
How can that be? Is there anything mysterious what is qt doing in the widget that causes this? And if it is not my fault, what can I do that qt lets me do as it is desired?
edit:
-I noticed that just pressing the alt
-key to to cause a desired rendering is hard to reproduce (tabbing into other applicantions while clicking on different on the alt+tab menu some times work). But what is reproducable is:
When I tab out of the application and then tab in again, the redraw works as it should. But not while invoked from the mousemove event.
-I deleted the static shapes which are not the problem, so the code is more easy to read.
Upvotes: 1
Views: 229
Reputation: 6555
I fixed this behaving by changing in the event listener the method call of redraw()
to updateGL()
. This acts probably in respect to own acting of qt, as described by Lukas Boersma in his answer.
the now as intended running version has this implementation of the method:
void RenderGraphFrame::mouseMoveEvent (QMouseEvent *event)
{
mousepositionX = event->x();
mousepositionY = event->y();
updateGL();
return;
}
note:
updateGL()
is an external function!
Upvotes: 1
Reputation: 1082
Your mistake is to assume that the OpenGL context is in the same state at each function call. However, Qt modifies a lot of states while rendering its own stuff. That is why you get different results even when you do exactly the same GL calls in your code every time.
Quoting the Qt documentation:
[...] the state of the current OpenGL context will be altered by the paint engine to reflect its needs. Applications should not rely upon the OpenGL state being reset to its original conditions, particularly the current shader program, OpenGL viewport, texture units, and drawing modes.
So, a lot of states like bound textures, polygon mode, blending, depth tests, etc. are basically in a random state every time redraw()
is called. You have to reset all of them at the beginning of redraw()
to make sure you get the same result every time.
Upvotes: 2