# OpenGL Modelview matrix hell...

This topic is 4701 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, i got some problem with matrix... In my game, i do my camera rotation by rotating the scene with glRotate, so i dunno where my camera is in xyz coordinate. I know how to extract the modelview matrix value from opengl but dunno how to find what i want. Any idea?

##### Share on other sites
Quote:
 Original post by SlugAny idea?
Sure, but we'll need more info. I would start by posting the code where you set up the transformation for your camera.

##### Share on other sites
ok, so here how it's done in my game:

glPushMatrix();
//Rotate around the scene
glRotatef(xRotation, 1.0f, 0.0f, 0.0f)
glRotatef(yRotation, 0.0f, 1.0f, 0.0f)
//Translate the scene
glTranslatef(xTranslation, yTranslation, zTranslation);

//more code
...

glPopMatrix();

So, i wanna know where the camera is now, in a xyz coordinate.
How did i find that?

##### Share on other sites
You need to position the camera...
glTranslatef(camera.x, camera.y, camera.z);

Then do the camera's rotations...
glRotatef(...)
...

Then push the matrix on the stack and translate/rotate for all objects.

##### Share on other sites
Actually... make that...
glTranslatef(-camera.x, -camera.y, -camera.z);

##### Share on other sites
Quote:
 Original post by Slugok, so here how it's done in my game:glPushMatrix(); //Rotate around the scene glRotatef(xRotation, 1.0f, 0.0f, 0.0f) glRotatef(yRotation, 0.0f, 1.0f, 0.0f) //Translate the scene glTranslatef(xTranslation, yTranslation, zTranslation); //more code ...glPopMatrix();So, i wanna know where the camera is now, in a xyz coordinate.How did i find that?
You usually don't need to 'find out' where the camera is in OpenGL, because you're the one who told OpenGL where it is in the first place. As suggested in the above posts, the problem may be that you're not setting up your camera transform properly: all of the 'Translation' and 'Rotation' values in the above code should be negated.

##### Share on other sites
All the above is rotate the scene

if you do not want to rotate the whole scene but just like change the view point:

glMatrixMode(GL_MODELVIEW)
gluLookAt(XXXXXXXXX)

##### Share on other sites
To give an explanation: Notice that a camera (if using such) is given in the same scene as the objects are. The belonging co-ordinate frame is normally named the "global" or "world" frame. But what you want to display is not the world itself but the cut scene as seen through the camera. So you don't have to render in the global frame but in the local frame of the camera. To come from the global to the camera local frame, you have to apply the _inverse_ transformation of the camera frame (since the frame describes how to come from the local to the global frame), i.e. the inverse (negated) translation and inverse rotation. Notice please that also the typical order is reversed. This inverse transformation is what is meant with the VIEW portion of the MODELVIEW matrix.

1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633700
• Total Posts
3013420
×