Problem with Matrix stack and View position movement.

I was having a problem with manipulation matrices in GL that I was hoping someone could help me with.

I have a 3D engine, and I used to move the camera via the orbit method, that is rotating it around the origin of the world.

I decided to play around some and move the camera more like a fly through, similar to a flight simulator, but I wasn’t able to get the behavior I want.

I want to have all delta’s to rotation apply to the current camera view orienation. That is I want forward motion to be in the direction of the camera as well as have the delta rotations apply to the camera orientation. So I can look up(rotation around camera x-axis) move forward, look to the left and move that direction, rotate the camera around it’s z axis etc.

Do do this I intitialize the matrix with gluLookat, grab the matrix and then do the following. (Currently I only include the rotation around the x and y axis…

// Get the current matrix
matrix2 = m_engine_state->GetMatrix();

// now do delta stuff…
glLoadIdentity();

glRotatef(m_engine_state->rotx180/M_PI, 1, 0, 0);glRotatef(m_engine_state->roty180/M_PI, 0, 1, 0);
glTranslatef(0,0, m_engine_state->transz);

glMultMatrixf(matrix);
// save current state for next time through
glGetFloatv(GL_MODELVIEW_MATRIX, (float *)matrix);
This doesn’t quit work, it seems the rotations are always around the origin of the world not the camera. So I figured, I have the order wrong, so I changed things around, Loaded matrix, then performed the rotations and translation, this actually was worse, I ended up not moving in the direction I was facing,

In the other way I did the following
glLoadMatrixf(matrix)
// then do rotations and translation

Then saved the current matrix for next time.

I assume I’m doing something wrong, any ideas.

One thought I had is to use the second method but change the translation to not be in the z direction but calculate the xyz components, but is seems like I should be able to do this easier. I want the following

  1. have all rotations relative to the current camera position
  2. movement will be in the direction the camera is facing.

Thanks,

First, do not use deltas and apply then to a matrix, and then use this matrix in the next frame by applying more deltas. This will accumulate roundoff errors, and after a while, these roundoff errors can get quite noticeable.

What you should do is store the direction and position of your object, and rebuild the matrix every frame. It’s as fast as using deltas and the previous matrix, but more acurate.

Do like this instead:

1: load the indetity matrix
2: setup viewpoint (with gluLookAt or whatever you use)
3: push the matrix to stack
4: rotate the object you want to draw
5: translate the object you want to draw to it’s position in world space
6: draw the object
7: pop the matrix from stack
8: repeat step 3 to 7 for each object you want to draw

And when you move foward, you just update their position based on velocity and direction. When you rotate, you change the angle.

Thanks for the info,

I understand about not apply delta’s because of round off error, but I’m still not clear on how to adjust the camera position correctly… I must be missing something, I understand how to move the objects, but in this case it is the viewpoint(camera) I’m having problems with. I can’t seem to get the viewing position to behave like a flight simulator, I want to have the view position be represented by a direction and up vector, then apply any keyboard, mouse inputs to be performed relative to the current viewing direction and up vector?

Am I just being dense here?

Thanks

elroy