Luca
Luca

Reputation: 11016

QOpenGLWidget overriding projection matrix

I am using Qt 5.4 and setting up the projection matrix and viewport as follows in my resizeGL function override:

glViewport(_off_x, _off_y, _width, _height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, _width, 0, _height, -1, 1);

I can verify this and when I print out the projection matrix as follows, it shows the correct value:

GLdouble projection[16];
glGetDoublev(GL_PROJECTION_MATRIX, projection );
// printing this shows the correct projection matrix.

However, somewhere this is getting overridden. When I print the projection matrix in the paintGL() function, it shows it as identity.

Interestingly, I switched to the old QGLWidget and it performs as expected.

Upvotes: 0

Views: 914

Answers (1)

datenwolf
datenwolf

Reputation: 162327

However, somewhere this is getting overridden. When I print the projection matrix in the paintGL() function, it shows it as identity.

And you're surprised exactly why? Qt5 may use OpenGL for drawing its stuff. Which means that Qt will have to set the state of the OpenGL context according to its needs.

What you observed is what is to be expected, so don't be surprised.

I am using Qt 5.4 and setting up the projection matrix and viewport as follows in my resizeGL function override:

You should not be doing that. As with every state based system it's essential to set the state right when you need it to what you need it – or keep track of all of the state changes, which is much more difficult.

Do the right thing and move everything you did in resizeGL to where it belongs: paintGL. The sole purpose of resizeGL is to update resources like FBO renderbuffers and to reflect the new size. But don't use it to set drawing related OpenGL state.

Upvotes: 4

Related Questions