rotation of camera around a cube

Hi ppl, i had some difficulties rotating my camera around a cube.

My camera is in this initial settings below and i have a cube situated at the origin.

float x=150.0f,y=150.0f,z=150.0f;
float lx=-1.0f,ly=-1.0f,lz=-1.0f;
float upX = 0.0f, upY = 1.0f, upZ = 0.0f;

If i rotate my camera about the y-axis ard the cube, i have this formula and it works alright so far. angLR is the total angle, obtained by adding all the delta angles moved through key pressed.

x = 150sin(angLR);
z = 150

lx = -sin(angLR);
lz = -cos(angLR);

lx, ly, lz,

My question is, how can i rotate my camera about the x-axis around the cube, with the camera always pointing to the center of the cube(the origin)?
I am trying to rotate the camera abt the cube in 2 axis, with the camera always pointing to the origin. Would anyone help me? Thanks alot!

Edit: saw your post.

Why can’t you just use gluLookAt()?


Hello there,
You don’t hveto calculate angls just use glRotatef(amount,xDisp, yDisp, zDisp) and then do your drawing.
Assuming that your cube code draws a unit cube t origin.Simply do this in your render function.

glTranslate3f(0,0,-10); //Move the camera 10 units
glRotatef(yAng,0,1,0); //Rotate about the y Axis

Now if you want to rotate around x or z axis use

Hope it helps thanx.

Hi ppl,

Thanx for replying, if i use gluLookAt for rotation about the x-axis around the cube, the up-vector will not always be in (1,0,0) since i have to keep pointing to the cube origin everytime it rotates. That’s why i do not know how to calculate the upVector and the probably also the line of sight. Anyone can help?

If i use the glRotate instead of gluLookAt(), am i changing the vertices of the cube as i call the glRotate? I do not want my cube vertices to change in 3D, i want to rotate the camera around the stationary cube…

Anyone pls enlighten me somehow…i just started openGl…and i am stuck with this rotation thing for quite some time since :frowning:

Hello there
As far as i know , using glRotate and other such transformation functions transformthe world coordinates no the object coordinates so if you draw a cube at origin it remians there.


after using the glRotate, i realized actually i want the object coordinates to be transformed instead of the world… how should i do that?


The situation now is this…at first my scene is initialize such that my camera is located at
gluLookAt(x, y, z, lx, ly, lz, upX, upY, upZ);

float x=150.0f,y=150.0f,z=150.0f;
float lx=-1.0f,ly=-1.0f,lz=-1.0f;
float upX = 0.0f, upY = 1.0f, upZ = 0.0f;

when user press an arrrow key, the camera will rotate using glRotatef() about either the y-axis or the x-axis. My cube will be situated at the origin.

However, i want to have a zoom in and zoom out function. I do not want to zoom in or out from the world coordinates, since it always change whenever user press arrow key. I want it to zoom in and out of the screen…just like there is an imaginary axis on screen…with the z-axis pointing out of the screen. I wonder if i make sense to any of you all… I jus don’t wanna have the zoom in/out from the transformed world coor… I want it from a fix frame located at the center of the screen.

Anyone to help me? I am getting crazy about transformation…

Maybe i’m reading what you’re saying wrong so take what I say with a grain of salt.

If you want your object to stay fixed at the center of the screen for zooming just do a glTranslatef(0,0,z) where z changes depending on which arrow key is pressed.

Since the order in which you do rotations and translations matters do something like:



That will rotate the cube about the origin first then translate along the z-axis, thus giving you a zoom.

What if before that the user has rotate the cube several times about y-axis and several times in x-axis, and the order of rotations is random?

How do i trace back all the rotations done to be able to translate first in gltranslatef(0,0,z) then do the rotations back again?

Is there other ways?

You don’t have to trace back the rotations if you do it in the order I mentioned. The vertices of your cube are multiplied by the rotational matrix. This gives you the newly rotated cube. Now you multiply these new vertices by the translation matrix giving you the rotated/translated cube.

*Edit: I’d actually suggest reading the RedBook’s section on rotations and translations:

This will give you a better idea of what is happening to the data and it explains how to think about the opengl scene(ie, whether your moving the object or moving your view.)