How to Translate Camera after Rotating the Scene?

Hello all,

I am new here and new to OpenGL. For a school project, I am trying to use OpenGL and GLUT to render a city scene with multiple buildings in it. Then, I have to do a camera “fly through” around the city and capture the whole process.

I have the city properly rendered. But I have ran into a issue with coding proper control for my camera.

I will try to explain my approach here as clear as possible without going on forever:

What I am trying to do is to control my camera, or more accurately the movement of the scene, in ways similar to a first person video game.

Here is what I mean:

‘w’ - Move Forward
‘s’ - Move Backward
‘a’ - Turn Left
‘d’ - Turn Right
‘q’ - Strafe Left
‘e’ - Strafe Right

I implemented this by changing the parameters inside gluLookAt()and by rotating the scene as needed.

Here is how I set up my gluLookAt():
gluLookAt(xPos,yPos,zPos, xLookAt,yLookAt,zLookAt, 0,1,0);

So, for example, every time I press, say ‘w’(Forward), zPos and zLookAt are decremented for a certain amount. Likewise, if I press ‘q’(Strafe Right), xPos and xLookAt are decremented for a set amount, so on and so forth.

To turn and face a different direction, I rotate the scene instead of rotating the camera (Mainly because I don’t know how to rotate the camera). So, when ‘a’ is pressed,I decrements a valuable call angle_x and perform the rotation like so: glRotatef(Transformations::angle_x, 0.0, 1.0, 0.0);

To make the rotation seems like it is rotating about the camera, I do the following:

glRotatef(Transformations::angle_x, 0.0, 1.0, 0.0);

Everything works perfect except for one thing. I cannot change the orientation of the camera after the rotation. So imagine forward is pointing North initially and I make a 90 degree rotation and I am now appeared to be looking toward East. If I press forward on my keyboard now my camera still moves toward north, which now would appears to be moving to the left and not what I want.

I have tried many ways of messing with the order of translation and rotation and I still couldn’t figure out a solution. I looked through the internet and found people talking about updating the direction vector of the camera or something like that but I couldn’t understand their approach posted.

I am hoping someone here would be able to give me some idea as to what I can do. Any comment would be greatly appreciated!

Thank you very much for reading so much!

Just rotate the look at vector

float xPos = cameraPosition.x;
float yPos = cameraPosition.y;
float zPos = cameraPosition.z;
float xLookAt = xPos + sin(Transformations::angle_x);
float yLookAt = yPos + cos(Transformations::angle_x);
float zLookAt = zPos;

gluLookAt(xPos,yPos,zPos, xLookAt,yLookAt,zLookAt, 0,1,0);