Sign in to follow this  
ade-the-heat

gluLookAt + transformation don't work !

Recommended Posts

So at the start of the frame I set the camera position with gluLookAt. I then draw my object like this: gluLookAt(); ...etc glPushMatrix(); //model matrix is at the camera which is not what we want DoTransformation(position); //to the object's position DrawObject(); glPopMatrix(); IT WORKS! The object is in the correct position -but it shouldn't be as I haven't done LoadIdentity(). So if I do it properly: gluLookAt(); ...etc glPushMatrix(); glLoadIdentity(); //THIS SHOULD BE REALLY CORRECT DoTransformation(position);//to the correct position DrawObject(); glPopMatrix(); Then none of the objects drawn like this can be seen - although this is the correct way. So what is wrong with the second way of doing things ?

Share this post


Link to post
Share on other sites
Using OpenGL you have two matrix stack: GL_PROJECTION and GL_MODELVIEW. The idea is to load the projection into the first matrix and the model AND view matrix into the second one. Since the gluLookAt matrix modifies the current matrix, and it represents the view, the correct order would be similar to your first code:


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(....);

glPushMatrix();
DoTransform(...);
RenderObject();
glPopMatrix();

Share this post


Link to post
Share on other sites
You're doing it correctly the first time. When you push a matrix in OpenGL, the current top matrix takes the value of the one beneith. You'd then want to multiply that matrix with the transformation matrix of the object. In the second case, you're clearing the camera transform from the top matrix, then multiplying my your objects transformation matrix, which is like the camera transform never happened.

I don't see what the projection matrix has to do with any of this.

So, for clarity:


glMatrixMode( GL_MODELVIEW );
glLoadIdentity(); // may do this internally in gluLookAt, I can't remember
gluLookat();

// (1)

glPushMatrix();
// the value of the model view matrix is the same here as at (1)
DoTransform();
// (1) * whatever DoTransform does
RenderObject();
glPopMatrix();
// again, the matrix takes the value of (1)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this