gluLookAt

Hello. I am confused of how to use gluLookAt() properly.

For instance, why do I need to call glLoadIdentity() before calling gluLookAt()?
Or do I need to call that at all?

The reason I ask this is b/c I have a problem and here it is.

In the init(GLDrawable drawable) method I initialize the camera’s position with the gluLookAt method.
If I don’t ever call gluLookAt again elsewhere, then everything gets rendered properly; however, if I call gluLookAt again with the same parameters, then all the graphics that were previously on the screen disapear.

If I call glLoadIdentity before gluLookAt then stuff still disapears. I then tried to modify the z-axis of the camera position and I have to zoom like 10x out to get the polys looking the same as when it was first set.

So, any suggestions of how to use gluLookAt?

Thanks

Typically you will load the identity before calling gluLookAt. This function creates the lookat matrix then multiplies the created matrix onto the current matrix. This means that if the current matrix isn’t the identity, your lookat matrix will probably not be correct.

You can get all this from the awesome Blue Book or just google the function name. Using google, I got this right off the bat:
http://pyopengl.sourceforge.net/documentation/manual/gluLookAt.3G.html

P.S. – One thing to consider is writing your own code for the view matrix. It is quite simple. Just treat the camera/viewer as any other object with its own transformation matrix. At the beginning of your rendering pass, multiply the inverse of this matrix onto the matrix stack. That’s all there is to it.

In my init()


      gl.glLoadIdentity();
      glu.gluLookAt(eye.x,eye.y,eye.z,dir.x,dir.y,dir.z,up.x,up.y,up.z);

In my display() method I have the following


         gl.glMatrixMode(GL.GL_MODELVIEW);
         gl.glLoadIdentity();
        glu.gluLookAt(eye.x,eye.y,eye.z,dir.x,dir.y,dir.z,up.x,up.y,up.z);

         gl.pushMatrix();
             ... draw some stuff
         gl.popMatrix();

Now, if I comment out the glLoadIdentity and LookAt in display, everything works. But if I leave that in there, then the camera looks to be zoomed in even though I haven’t changed the paramaters to gluLookAt.

Is there a function I need to call before drawing the geometry in the scene?

Are you sure you’re applying lookAt to the modelview matrix when you call it in your init, or have you left it set to projection?

it was set to GL_PROJECTION in init; however, setting it to PROJECT or MODELVIEW still makes things wrong.

maybe the reshape method is causing some problems particulary the last 3 statements. Could this be foobarring up my transformations of the camera?


   public void reshape(GLDrawable drawable, int x, int y, int width, int height) {
      System.out.println("=== Reshape ===");
      float h = (float)height / (float)width;
                                                                                                                                                            
      gl.glMatrixMode(GL.GL_PROJECTION);
      System.err.println("GL_VENDOR: " + gl.glGetString(GL.GL_VENDOR));
      System.err.println("GL_RENDERER: " + gl.glGetString(GL.GL_RENDERER));
      System.err.println("GL_VERSION: " + gl.glGetString(GL.GL_VERSION));
      System.err.println();
      System.err.println("glLoadTransposeMatrixfARB() supported: " +
                         gl.isFunctionAvailable("glLoadTransposeMatrixfARB"));
      if (!gl.isFunctionAvailable("glLoadTransposeMatrixfARB")) {
        // --- not using extensions
        gl.glLoadIdentity();
      }
      else {
        // --- using extensions
        final float[] identityTranspose = new float[] {
          1, 0, 0, 0,
          0, 1, 0, 0,
          0, 0, 1, 0,
          0, 0, 0, 1
        };
        gl.glLoadTransposeMatrixfARB(identityTranspose);
      }
      gl.glFrustum(-1.0f, 1.0f, -h, h, 5.0f, 60.0f);
                                                                                                                                                            
      gl.glMatrixMode(GL.GL_MODELVIEW);
      gl.glLoadIdentity();
      gl.glTranslatef(0.0f, 0.0f, -40.0f);
   }

I forget exactly how .reshape behaves, but I seem to remember it being called at least once when a canvas becomes visible. In which case the bottom bit nukes over your lookAt because you’ve replaced it with a z translate.

I doubt those last couple of lines really should be there, if you want to move the view then set it just after your lookAt (preferably at the start of your display method).

I’m not sure, I got that bit of code from a sample I believe.

Conzar,

I created a camera using glLookAt() that seems to work fine, and use it in my pitiful newbie experiments :slight_smile:

If you would like to see the camera and my application template, which uses the camera, I would be happy to e-mail the files to you. Maybe you will have some advice on how I can improve the thing.

My email is kmyers@bardstowncable.net.

Z

I think what was confusing me was the reshape method.
It was changing the camera in the scene by moving the camera out by 40. So really, the initialize gluLookAt call was nullified by the glTranslate call in the reshape method.

I got everything to “look right” by changing my z value in my gnuLookAt to 40.

Thanks for your help though.

Hello All,
I’ve been struggling with this same problem and now think I understand it.
I think it stems from us newbies cut/pasting code out of different examples that aren’t compatible. Some example code is placing the camera at the origin and drawing some widget at another location. For myself, if I use gluLookAt() I’m trying to place the camera at some other location and draw my widget at the origin. So, when we run our code we call init, then reshape then display. For my purposes I call gluLookAt in init and then I don’t want to ever call loadIdentity for GL.GL_MODELVIEW again. But in some of the samples I’ve looked at they call loadIdentity in reshape, which would override what I did in init. They’re just shooting for a different effect. ANother option would be to call gluLookAt in display which would then override anything done in init or reshape, of course you get the overhead of making that call even if the camera doesn’t move

Dave