Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Currently I am using gluLook at like so:

gluLookAt(position.x, position.y, position.z,
          viewPoint.x, viewPoint.y, viewPoint.z,
          upVector.x, upVector.y, upVector.z);

with the above, don't know if you need more information, how could I change it so that the camera acts like its rotating around itself, instead rotating around its viewpoint. You can see the current code at https://github.com/dchhetri/OpenGL-City/blob/master/opengl_camera.cpp, that class was adapted from codecolony.com.

share|improve this question
add comment

2 Answers

I'm not so sure if I understood your problem right, but I'll try to answer anyways.

gluLookAt(), as its name implies, creates a view matrix for a camera, and takes three vectors as parameters.

The first one is the camera's position. This is where your camera will be located. If you want your camera to rotate around itself, you most likely don't want this parameter to change.

The second one is the location of the point of interest, or where your camera will be pointed at.

The third one is a vector that represents where 'up' is.

So, as you mentioned, you want your camera to rotate around itself. There are actually three meanings of "rotation" in three-dimensional space:

Suppose this lovely plane donated to us by Wikipedia is your camera:

Plane

Doing yaw (yawing?) is pretty simple. You keep your 'up' vector to (0, 1, 0), and change the point of view to rotate around the XZ plane, by setting it to (cos(theta), 0, sin(theta)).

For simplicity, I'm supposing your camera is located at (0, 0, 0), but if not, you can simply add the location of your camera to your point of interest.

Rolling is also quite simple. You keep the point of interest static, and change the 'up' vector to rotate around the XY plane, by setting it to (cos(theta), sin(theta), 0).

Once again, for simplicity, I'm supposing you're looking at (0, 0, 1) or (0, 0, -1).

Finally, pitching is slightly more complicated, since you will be simultaneously changing your point of interest and 'up' vectors, but it's not so complicated either.

Let's start by changing the point of interest to rotate around the YZ plane, by setting it to (0, sin(theta), cos(theta)). This will take care of rotating the camera.

Now, to stabilize the camera, we need to set the 'up' vector to be orthogonal to both the point of interest vector, and the X axis. You can also build it with trigonometric functions, or you can use the facilities of the cross product, which does exactly that. So you set the 'up' vector to cross(point_of_interest, (1, 0, 0)). If your camera ends up upside down, reverse the parameters or do the cross product with (-1, 0, 0) instead.

This covers the three main rotations. Doing more complicated rotations involves using quaternions. Quaternions is a slightly more complicated topic I can't cover in a simple answer, but I'm sure there are many tutorials around online, so go ahead and google it.

Have a nice day.

share|improve this answer
add comment

The simple example is for rotating in xy-plane (up = z).

In order to circle around the object (at origin) at distance R, you use LookAt = 0,0,0; Position = R*cos(a),R*sin(a),0;

To rotate camera instead, the equations are LookAt = x + cos(a), y + sin(a), 0; Position = x,y,0;`

It's basically vector math, where the vector origin and target change place.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.