View Full Version : 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);

glLoadIdentity();

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

EycEwinD

02-26-2001, 10:17 PM

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();

glLoadIdentity();

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).]

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);

glLoadIdentity();

glRotatef(45,0,0,1);

glMultMatrix(m);

End quote

/*

Teofuzz

02-27-2001, 01:45 AM

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

mcbastian

02-27-2001, 02:58 AM

Hi!

another way when using gluLookAt:

some pseudo-c-code:

float counter=0; //should be global http://www.opengl.org/discussion_boards/ubb/wink.gif

void display_scene()

{

glLoadIdentity();

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

glBegin(GL_QUADS);

and so son http://www.opengl.org/discussion_boards/ubb/smile.gif

swapbuffers();

}

hope I could help.

Bastian.

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

Deiussum

03-01-2001, 12:12 PM

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.

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

However moving the camera and moving the _SCENE_ is.

Teofuzz

03-01-2001, 01:14 PM

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

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

Thanks again for the help...

It seems to work for the first rotation, but it does nothing after that! (I can see that the camera_x, y and z don't change the second time I press the left-key

What's wrong?

Here is my display function

{

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(fov,ratio,1,200);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();;

glShadeModel(GL_FLAT);

glClear(GL_COLOR_BUFFER_BIT);

gluLookAt (camera_x,camera_y, camera_z, x_direction,y_direction,z_direction, 0.0, 1.0, 0.0);

if (lightstatus == 1)

{start_lights();}

DrawCube();

glColor3f(0.4f,0.0f,0.4f);

glTranslatef(0.0f, 2.0f, 0.0f);

glutSolidSphere(1.0,360,360);

glFlush();

}

and here is my switch where I want to rotate the camera when the left-right arrow keys are pressed

case GLUT_KEY_LEFT:

printf("The LEFT arrow has been pressed and the 3 camera point are: \n%f\n%f\n%f\n",camera_x, camera_y, camera_z);

camera_x = 10 * cos(angle);

camera_z = 10 * sin(angle);

x_direction = -cos(angle);

z_direction = -sin(angle);

angle = angle + 5;

printf("The LEFT arrow has been pressed and the 3 camera point are: \n%f\n%f\n%f\n",camera_x, camera_y, camera_z);

glutPostRedisplay();

break;

Does this make any sense at all

Ok...it reduced the increment (fro angle) to 0.1 and now is seems to work!

How would I rotate in the other direction?

I think I REALLY need to brush up on my trig and matrix math http://www.opengl.org/discussion_boards/ubb/smile.gif

Teofuzz

03-02-2001, 01:01 AM

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

r*cos(t) and r*sin(t) are the current position expressed in terms of the polar coordinates (r,t). If you consider that the first is x and the second z, using the known propery of sin and cos:

sin^2+cos^2=1

you get:

x^2+z^2=r^2

that is the equation of a circle centered in 0,0,0 on the plane xz.

How would I rotate in the other direction?

Decreasing the angle!

Bye!

tFz

Deiussum

03-02-2001, 10:56 AM

Originally posted by mr x:

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

However moving the camera and moving the _SCENE_ is.

Acording to the post, his scene consists of a cube. So in this case his _OBJECT_ _IS_ the _SCENE_.

Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.