# Thread: rotating a camera around the origin (using glutLookAt)

1. ## rotating a camera around the origin (using glutLookAt)

Hi!

I am trying to rotate my camera around a cube I have at the origin.

I am currently using
gluLookAt (camera_x, camera_y, camera_z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

to postion the camera from different points along the 3 axes.

I was told to use:

GLfloat m[16];
glGetFloatv(GL_MODELVIEW_MATRIX,GLfloat m);
glRotatef(45,0,0,1);
glMultMatrix(m);

In order to Pre-Multiply the rotation, but how does this help me supply parameters to gluLookAt()?

I have looked into this and cannot find the answer.

Also, I know that calls to glGetFloatv() are costly, I just want to do this in the simplist way possible.

Thanks

2. ## Re: rotating a camera around the origin (using glutLookAt)

Well...the things you were told to do will rotate the cube at 45 degrees about z-axis and I doubt that it will have any effect on the gluLookAt () call.
If you are trying to make a cube place at the origin and rotated at 45 degrees than a much better approach would be

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glRotatef (45, 0, 0, 1);
//Draw the cube here
glPopMatrix();

But this code is only needed if you want to ratate the cube by 45 degrees about z-axis, otherwise, you just need to draw the cube.

[This message has been edited by EycEwinD (edited 02-26-2001).]

3. ## Re: rotating a camera around the origin (using glutLookAt)

In case I wasn't clear, I do not want the cube to rotate, I want the camera to rotate around the cube (centered at the original).

To further clarify, here is what my teacher specified for this part of the assignment.

Start quote
/*

Sometimes, we want to rotate the scene about the line
of sight. In this situation, we need pre-multiple a
transformation matrix with the modelview matrix. To be
more specific, let M be the modelview matrix, and let
T be the transformation based on the eye coordinates.
The pre-multiplication gives TM. Thus, when the
product is applied to a vertex v, the result is TM v.
That is, the modelview matrix M is applied to v first,
and then the matrix T.

If you want to rotate a scene 45 degrees about the
line of sight, and that the modelview matrix is on the
top of the modelview matrix stack, you should use:

GLfloat m[16];
glGetFloatv(GL_MODELVIEW_MATRIX,GLfloat m);
glRotatef(45,0,0,1);
glMultMatrix(m);

End quote
/*

4. ## Re: rotating a camera around the origin (using glutLookAt)

It's easier to use glTranslate & glRotate than gluLookAt for your problem.
I think you can do it in this way:

...
angle=angle+1.0;
glRotatef(angle,0.0,1.0,0.0);
glTranslatef(0.0,0.0,-dist);
drawTheCube();
...

Transformations always make me cry, but I think this is the right order of xform. First you are in 0.0,0.0,0.0 and you rotate. Then you move backward: any object you'll draw in the origin will be in front of you.
That works?
Bye!
tFz

5. ## Re: rotating a camera around the origin (using glutLookAt)

Hi!

another way when using gluLookAt:

some pseudo-c-code:

float counter=0; //should be global

void display_scene()
{
glClear(blah...)
t+=0.01;
gluLookAt(0,0,0, //position of cam
10*sin(t), 10*cos(t), 0,
//this is a circle function
0,1,0); //the up-vector

and so son

swapbuffers();
}

hope I could help.

Bastian.

6. ## Re: rotating a camera around the origin (using glutLookAt)

Thank you all for the reply, you gave a lot of helpful hints but I am still confused by all of this!

I am using:

gluLookAt (camera_x, camera_y, camera_z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

the camera_x, y and z are global variable that are modified by different functions.

I am looking for a way for my camera to rotate around the Y axis (so my camera will circles AROUND the origin at a radius of camera_z (the radius of the circle will be equal to whatever the 'z' distance of the camera is).

I have a cube drawn at the origin and I DO NOT want to rotate the cube at all. I want to camera to ALWAYS face the cube and rotate around it.

Teofuzz's suggestion
(using glTranslate & glRotate instead of gluLookAt() ) but I already implemented some other functions using gluLookAt() and do not wnat to change my whole program...

McBastian suggestion doesn't seem like it does the right thing (didn't work).

Can't I use

gluLookAt (camera_x, camera_y, camera_z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

and change the value of my camera variables?
camera_x and camera_z should change according to the rotation and camera_y should stay the same?

I am not up to date on trig functions. What valut of cos, sin should I use to accomplish this?

Thanks

7. ## Re: rotating a camera around the origin (using glutLookAt)

Moving the camera or moving the object is all relative. Moving the camera away from an object gives the same visual effect as moving the object away from the camera. Rotating an object one way is the same as rotating the camera the oposite way.. (In this case, anyway.) I agree that using glTranslate/glRotate will greatly simplify what you want to do. gluLookAt is really nothing more than a glorified set of glTranslate/glRotate calls... If you want to keep your gluLookAt, you can also do this..

glRotatef(45, 0, 0, 1);
gluLookAt(camera_x, camera_y, camera_z, 0, 0, 0, 0, 1, 0);

In this case gluLookAt does exactly the same thing as
glTranslatef(-camera_x, -camera_y, -camera_z);

You CAN use trig functions and just change camera_x, camera_y, camera_z, but it'll be less efficient and I wouldn't do it unless you need to know the exact x,y,z values at all times.

8. ## Re: rotating a camera around the origin (using glutLookAt)

Moving the camera and an object is _NOT_ the same thing.

However moving the camera and moving the _SCENE_ is.

9. ## Re: rotating a camera around the origin (using glutLookAt)

If you just want to use gluLookAt, it's not so difficult.
This function takes the point where the viewer is, the viewing direction, the up vector.
Now you want to follow a circle around the origin and look always at the origin, ok? Am I wrong?
The position of the viewer has these coords:
xp=ray*cos(theta)
yp=0
zp=ray*sin(theta)
where ray is the distance from the origin and theta is the current angle.
The direction is toward the origin: the vector between the point where we are and 0,0,0:
xd=-cos(theta)
yd=0
zd=-sin(theta)
Finally, the up vector is very simple: the viewer head points always in the y dir:
xu=0
yu=1
zu=0
So you can incremet every frame theta and display with:
gluLookAt(xp,yp,zp,xd,yd,zd,xu,yu,zu);

That might work...
Let me know!
bye
tFz

10. ## Re: rotating a camera around the origin (using glutLookAt)

Teofuzz:

Thank you, that's exactly what I want to do.

What do you mean when you say that theta is the current angle?

Isn't theta the angle that I want to rotate by?

What will be the result of ray * cos (theta)? will it be the increment or the actual new postion?

Thanks

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•