ModelView matrix in OpenGL

Good morning,

I’m trying to rotate my object using quaternions.
I deifned a quaternion, then normalized it and calculated the rotation matrix. I used glMultMatrix function to perform the rotation. According to my researchs, glMultMatrix multiplies the modelview matrix by the rotation matrix. You only have to type glMultMatrix(rotation matrix) and the object rotates.

Now I’m trying to do the same steps but using Qt QQuaternion class.
So I defined the quaternion, normalize it. I wanted to use this function:
void QMatrix4x4::rotate(const QQuaternion &quaternion). All I have to do is write this line:
m.rotate(quaternion) where m is the modelview matrix. However I didn’t know how to make the function understand that by m, I mean the modelview matrix. I mean I didn’t know how to make the function call that matrix.

I know that this is not an OpenGL question but since my problem is related to the modelview matrix, I thought that maybe someone could help me.

I shared my question on a programming forum but no one replied. I know that people may be very busy but I didn’t know what to do and I really need some help.

Thank you.

Legacy OpenGL (nowadays available in compatibility profile OpenGL contexts, see context types) has the built in matrix stacks (one for modelview and one for projection) that you manipulate with glMultMatrix, glPushMatrix, glPopMatrix, etc.

In modern OpenGL (i.e. when using a core profile context) there is no built in matrix stack, the transformation of vertex data to clip space is handled by the vertex shader. How it accomplishes this is entirely up to the developer, but a very common way is to pass in relevant matrices as uniform values and apply them to incoming attributes.

I don’t know much about QMatrix4x4, but I would guess its purpose is to allow you to define and combine matrices to calculate the values for such uniforms to be passed to your shaders.

As for what the value of the modelview matrix should be: what is its value before you call glMultMatrix? If that’s the very first OpenGL matrix function you call it would have its initial value (identity matrix) otherwise its the result of your other calls. In either case you should initialize m to the same value.

@carsten_neumann First of all, Thank you for your reply. The function that I’ve mentioned multiplies the current matrix which is the modelview matrix by a rotation matrix that correponds to my quaternion. It does the same thing as glMultMatrixf => m.rotate(quaternion) where m is supposed to be the modelview matrix.
gluLookAt(6.0, 6.0, 6.0, 3.0,3.5,0.0, 0.0,1.0,0.0);
glTranslatef(1.1, 2.1, -3.1);

How can I specify that m is actually the modelview matrix. I mean how can I ensure that m has the exact same values as the modelview matrix or how can the code recognize it as the modelview matrix.
Since I was out of solution, I tried something which is totally wrong and I knew it was wrong:
I typed GL_MODELVIEW_MATRIX.rotate(quaternion). And I knew it wouldn’t work cause it doesn’t make any sense.

Hmm, you seem to want to mix using the OpenGL matrix stacks for some calculations and do some others with QMatrix4x4. IMHO it would be better to pick one and stick with it for all matrix math.
You can get the current value of the top of the modelview matrix stack with glGetFloatv(GL_MODELVIEW_MATRIX, ...) and then use those values to initialize a QMatrix4x4 that represents the same transformation.
Alternatively, in the snippet you’ve posted your modelview matrix contains the product of a look at matrix and a translation matrix, so you could use QMatrix4x4::lookAt and QMatrix4x4::translate to construct the same matrix.
Since QMatrix4x4 does not know anything about OpenGL there is no automatic way to create one that matches the modelview matrix. Likewise, once you have a QMatrix4x4 you’ll have to use glLoadMatrixf to pass its values back to OpenGL.

@carsten_neumann okay thank you,
I have another question, does glGetFloatv (GL_MODELVIEW_MATRIX, m) returns a 4x4 matrix or an array of 16 elements ?


A 4x4 matrix and an array of 16 elements are functionally identical and as far as C is concerned, utterly indistinguishable from an API perspective.

@Alfonse_Reinheart this means that I can use the result of the function as a matrix and multiply it by another matrix ? [I’m coding with c++]

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.