   Matrix for camera in OpenGL
  Hi everyoneI am trying to manually create a matrix for the camera but I am stuck. For now, I want to avoid gluLookAt until I have a grasp of the basic math behind virtual cameras. Some code:// in init()float yRot = 0.0f; // initially, NO rotationV3D pos; // initially, camera is located at (0,0,0)// in the rendering callbackglRotatef( yRot, 0.0f, 1.0f, 0.0f ); // rotate around the Y-axisglTranslatef( pos.x, 0.0f, pos.z ); // camera is anchored to the XZ plane where y=0After I've rotated the camera, I keep translating along the original Z-axis and not the new one.1. How do I calculate the new Z-axis?2. How do I translate the camera along the new Z-axis?Thanks   
  OpenGL matrix operations are performed in reverse order (bottom to top as you see it in code) because the matrices are accumulated onto a stack which naturally reverses their order. So the translation is being applied first, followed by the rotation. If I understand you correctly the effect you want can be achieved by reversing the order of your transformations, i.e. swap them over.   ;  I reversed the *order* of the calls (glTranslate, then glRotate) and the camera translates along the new Z-axis. However, the following strange things happen:If I haven't translated the camera, then rotation around the Y-axis works as expected. Once I move the camera away from the origin, then the rotation is screwed up, like it isn't rotating around the local Y-axis anymore.[Edited by - minime9111 on May 7, 2010 9:50:47 PM];  I don't know how you're going about moving your camera, but as for your code, it would typically be written as follows:glRotatef( -yRot, 0.0f, 1.0f, 0.0f );glTranslatef( -pos.x, 0.0f, -pos.z );If this were a 'model' transform, it would look like this:glTranslatef( pos.x, 0.0f, pos.z );glRotatef( yRot, 0.0f, 1.0f, 0.0f );However, for a view transform you want the inverse of the above, which can be achieved by inverting the individual transforms (negating the rotation angle and the translation vector), and reversing their order.   ;  I am trying to achieve a viewing transformation.glRotatef( -yRot, 0.0f, 1.0f, 0.0f );glTranslatef( -pos.x, 0.0f, -pos.z );The code above has the following effect:I can rotate around the Y-axis and the focal point changes as I do that, which means I should have a new Z-axis. However, I can only move forward/backward along the initial Z-axis.Also, whose matrices' inverses should I calculate?;Quote:Original post by minime9111I am trying to achieve a viewing transformation.glRotatef( -yRot, 0.0f, 1.0f, 0.0f );glTranslatef( -pos.x, 0.0f, -pos.z );The code above has the following effect:I can rotate around the Y-axis and the focal point changes as I do that, which means I should have a new Z-axis. However, I can only move forward/backward along the initial Z-axis.Also, whose matrices' inverses should I calculate?The code that I posted takes care of the inversion(s) for you.As for the forward/backward movement problem, can you post the code where you update the camera position?   ;  A small correction to the viewing transformation I posted before:glRotatef( 0.0f, 0.0f, pos.z ); // I am only updating the Z coordinate, not the XSo, the viewing transformation:glRotatef( -yRot, 0.0f, 1.0f, 0.0f );glTranslatef( 0.0f, 0.0f, -pos.z );The input handler (where I update the angle of rotation and position):void SpecialKeys(int key, int x, int y){if(key == GLUT_KEY_F1)   exit(0);if(key == GLUT_KEY_UP)   pos.z -= 0.1f;if(key == GLUT_KEY_DOWN)   pos.z += 0.1f;if(key == GLUT_KEY_LEFT)   yRot += 1.0f;if(key == GLUT_KEY_RIGHT)   yRot -= 1.0f;glutPostRedisplay();};  Your camera only moves back and forth along the z axis because you're only ever moving it back and forward along the z axis (in other words, your program is doing exactly what you've told it to do).To move in the direction the camera is facing, you first need to add the 'x' translation back in:glTranslatef( -pos.x, 0.0f, -pos.z );Then, your 'move' code should probably look something like this (pseudocode):float angle = deg_to_rad(yRot);float c = cos(angle);float s = sin(angle);pos.z += c * speed * time_step;pos.x += s * speed * time_step;You may need to swap and/or negate 'c' and 's' in the last two lines there in order to get the behavior that you're looking for.   ;  I inserted the following code in the input handler that I posted earlier but the movement is even more erratic than before.float angle = yRot * M44_RADIAN; //convert to radiansfloat c = cos(angle);float s = sin(angle);pos.z += c * 0.1f; // removed speed/timesteppos.x += s * 0.1f; // increment by a fixed value for nowI've tried switching around cos and sin as well as their signs but it didn't work. (I tried all 8 permutations).I want to be able to do the following:1. Rotate around the Y-axis, thus looking down a new, rotated Z-axis.2. Translate along the new, rotated Z-axis.Does that make sense?   ;  Have you had a look at NEHE: Camera Class?   