Strange behavior with gluLookAt(), am I doing something wrong?

Hi,

My platform is Windows XP, Java 1.5, and the latest JOGL.

I made a camera class, and I am calling gluLookAt() to change the view on the scene.

I checked my math for the transformations on the “look at” vectors, and it seems okay.

However, when I pan or turn the camera, the view changes consistently in the wrong way. For example, a pan to the left will spiral away.

Are there any “gotchas” with gluLookAt()? Maybe some coordinate conversion?

To describe the code briefly, there is a loop for rendering. These happen in the loop:

  1. Event may change the Camera object
  2. Camera object calls gluLookAt()
  3. push matrix
    model is built and transformed
    pop matrix

{ 3) happens multiple times }

That’s about it. I built this based on code from gamedev.

I removed all calls to glLoadIdentity(), since they seemed to make the program ignore my view transformations. I added the push and pop calls around the models’ code.

You removed ALL calls to glLoadIdentity?
Really?

gluLookAt modifies the current transformation.

That means that if you call it twice, it will add up.

What you need to do is something like this:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(...);
// now render stuff
glPushMatrix();
glTranslatef(...);  // set object's location
glRotatef(...);     // set object's rotation
glPopMatrix();
// render next object

If the rendering loop’s code starts with a call to glLoadIdentity(), then it appears that nothing gets moved when gluLookAt() gets called by the Camera (cam). :frowning: That’s why I removed the call to glLoadIdentity().

If you would like to see it, the source code can be found here:

http://210.209.186.12/Lesson05changeview.java

gluLookAt() gets called by cam.execute().

The file is not very helpful if the most interesting piece of code is in cam.execute(). :rolleyes:

Sorry,

All the Camera object, cam, does is store and calculate math (positions of view vectors) and call gluLookAt().

I verified that the math is okay: all translations and rotations from the origin yield the right coordinates for the view vectors. It’s just that the coordinates doesn’t match what I see in the OpenGL window.

I verified that the math is okay: all translations and rotations from the origin yield the right coordinates for the view vectors. It’s just that the coordinates doesn’t match what I see in the OpenGL window.
Kind of contradictory, don’t you think? :wink:
Nobody will be able to spot the error if you don’t show the code and explain precisely what numerical behaviour you expected and what you got.
There are plenty of possibilites to make a mistake with OpenGL matrices and coordinate systems and it’s probably simple to correct.

Okay,

I condensed some code I was trying. It can be found here: [Simple Test.zip](http://210.209.186.12/Simple Test.zip)

In simpletest , the right and left arrow keys should pan the view right and left. However, the view turns on its side and pans up, it seems.

All the while, the view vectors’ coordinates are printed onscreen, and they seem to be correct. :confused:

( By the way, I did add a call to glLoadIdentity() to the beginning of the loop. I noticed that without it, the previous transformations get applied over and over! :stuck_out_tongue: )