Problem with gluLookAt

Hi everyone,

I’m coming from the world of Direct3D programming, as I’m trying to port my project over to OpenGL/SDL…

All I’m trying to do, is to move the camera a few units back, and up while pointed at 0,0,-10 (in OpenGL).

When I begin my scene rendering, I set the view matrix using:

//move our camera 10 units up, 5 units back,
//and point to 0,0,-10. 
//Our up vector should be(?) 0,1,0
gluLookAt(0,10,5,0,0,-10,0,1,0)

//render a simple damn triangle! :(
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -10.0f);
glBegin(GL_TRIANGLES); 
 glColor3f(1.0f, 1.0f, 1.0f);
 glVertex3f(-5.0f, 0.0f, 5.0f);
 glVertex3f(-5.0f, 0.0f, -5.0f);
 glVertex3f(5.0f, 0.0f, 5.0f);
glEnd();
glPopMatrix();

But my vertices do not come across properly! The
triangle looks flipped somehow…

I’ve tried re-ordering the vertices, but I still roughly end up with the same result…

Am I using gluLookAt wrong??

thanks for any help…

Hi !

What do you mean with “flipped” if it is flipped left/right then that would mean that you have swapped the Z axis from what you expected I guess.

Mikael

Also your code for setting up the matrix doesn’t seem right. Normally your “display” or “render” function should look like:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0, 10, 5, 0, 0, -10, 0, 1, 0);
//we've set up the modelview matrix now draw the damn triangle ;)
glBegin(GL_TRIANGLES);
glTranslatef(0.0f, 0.0f, -10.0f);
glBegin(GL_TRIANGLES); 
 glColor3f(1.0f, 1.0f, 1.0f);
 glVertex3f(-5.0f, 0.0f, 5.0f);
 glVertex3f(-5.0f, 0.0f, -5.0f);
 glVertex3f(5.0f, 0.0f, 5.0f);
glEnd();

glFlush(); // send data over to the graphics pipeline for display

Most of the time in OpenGL we’re in MODELVIEW matrix. When you want to make a projection transformation (usually at the “resize” function then you use:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//set the projection matrix
//glOrtho - glFrustum, etc.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

So your pushing and popping of the matrix before rendering the triangle and then calling glLoadIdentity() actually cancels out gluLookAt. If you want to make a specific transformation to the triangle but keep the effects of gluLookAt do:

glPushMatrix();
glTranslatef(blabla...);
glBegin(GL_TRIANGLES);
//blabla
glEnd();
glPopMatrix();

I don’t know how much of help I was but hey… Unfortunately I don’t know any D3D to clarify more. Good luck!

Thanks for the replies!

Moucard: sorry, I snipped out a bunch of code. I was assuming that the depth/color buffer bits were cleared, etc…(ie. a “standard” opengl rendering beginning)…

My projection matrix has been set to 1.0f, 100.0f for the near/far clip planes, and the matrix mode is switched over to MODELVIEW before calling gluLookAt, etc…

I push the matrix after the gluLookAt calculation, in order to be able to properly position my vertices…then pop it, leaving the View Matrix right on the stack…

Here’s the whole function, just so people see what’s going on:

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();

//okay now the gluLookAt affects the MODELVIEW
//matrix
gluLookAt(0,10,5, 0,0,-10, 0,1,0);

glPushMatrix();
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -10.0f);
glBegin(GL_TRIANGLES);
  glColor3f(1.0f, 1.0f, 1.0f);
  glVertex3f(-5.0f, 0.0f, 5.0f);
  glVertex3f(-5.0f, 0.0f, -5.0f);
  glVertex3f(5.0f, 0.0f, 5.0f);
glEnd();
glPopMatrix();

glFlush();
SDL_GL_SwapBuffers();

Does that help?

Thanks everyone for trying to help!

Nope, I’ve already taken account of the whole right-handed, left-handed thing (that D3D uses)… :wink:

Thanks though!

Originally posted by mikael_aronsson:
[b]Hi !

What do you mean with “flipped” if it is flipped left/right then that would mean that you have swapped the Z axis from what you expected I guess.

Mikael[/b]

From the documentation on this site (in the FAQ section), it appears as if you’re not supposed to use a pushMatrix call before rendering your models…(after a gluLookAt)

ie.

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();

//okay now the gluLookAt affects the MODELVIEW
//matrix
gluLookAt(0,10,5, 0,0,-10, 0,1,0);

//if we use PushMatrix, it ruins our 
//gluLookAt calucation?? *shrug*
//glPushMatrix();
//glLoadIdentity();
glTranslatef(0.0f, 0.0f, -10.0f);
glBegin(GL_TRIANGLES);
  glColor3f(1.0f, 1.0f, 1.0f);
  glVertex3f(-5.0f, 0.0f, 5.0f);
  glVertex3f(-5.0f, 0.0f, -5.0f);
  glVertex3f(5.0f, 0.0f, 5.0f);
glEnd();
//glPopMatrix();

glFlush();
SDL_GL_SwapBuffers();

I’ve re-compiled my scene, and the triangle now appears properly! (hurrah!)

But this leads me to ask, how then do we “properly” render scene objects if I can’t use a push/pop Matrix call to re-orient everything in the scene?? (Did that make sense?)

Not much :slight_smile:
Ok, here’s the thing from my point of view. As I’ve told you we’re supposedly always on modelview matrix. When we need to make a change to the other two matrices, we change to the appropriate matrix, load identity (to know where we’re going) and make the change. Then we switch back to the modelview matrix.

You really shouldn’t push-pop anything unless you have to. Consider this “render” function:

void render()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();
  gluLookAt(...);  // set the camera

  //draw triangle
  glBegin(GL_TRIANGLES);
  // draw vertices
  glEnd();

  glFlush();
  //swap buffers etc.
}

This code is perfectly valid. You don’t need to push pop anything for the triangle to show. A valid push pop case would be if we had 2 triangles and say we want one to rotate left and the other right. Then we would do:

void render()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();
  gluLookAt(...);  // set the camera

  glPushMatrix();
  glRotatef(angle, 0, 1, 0);  // suppose we have a constrantly changing angle variable
  //draw triangle
  glBegin(GL_TRIANGLES);
  // draw vertices
  glEnd();
  glPopMatrix();
  //now we have the old matrix (the one after gluLookAt
  glRotatef(-angle, 0, 1, 0);
  // draw another triangle....

  glFlush();
  //swap buffers etc.
}

As you can see in the above code snippet I draw two triangles. The first I want to rotate left and I don’t want the transformation to stay on the matrix, so I include it in a push pop block. If you need further clarification post again.

There must be something wrong with my brain then Moucard, as what you’re suggesting is the FIRST thing I tried out!! (see the code I’ve posted at the beginning of the thread)…

I’ll play with it some more…it’s just highly annoying to waste so much time on such a (relatively) simple thing IMHO.

thanks for the offer of help though…I do appreciate it, even if I sound frustrated.

I’m really sorry if I’ve offended you. I was only kidding. Also my apologies for not reading the whole of your posts. Ever since playing too much Baldur’s Gate I’m tired of reading stuff (honestly, since every time you read the shame sh*t and you had to press 1 or 2 for the “good” answer) You can sure make fun of me as well :slight_smile:

With OpenGL and default projection the axis orientation is +X points to the right, +Y points up the screen and +Z points out of the screen.

I hope this sorts things out for you.