View Full Version : rotation of camera around a cube

sunnymonk

02-23-2005, 10:02 AM

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 = 150*sin(angLR);

z = 150*cos(angLR);

lx = -sin(angLR);

lz = -cos(angLR);

glLoadIdentity();

gluLookAt(x,y,z,

lx, ly, lz,

upX,upY,upZ);

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!

Aeluned

02-23-2005, 10:34 AM

Edit: saw your post.

Why can't you just use gluLookAt()?

gluLookAt(camX,camY,camZ,cubeX,cubeY,cubeZ,0,1,0);

MMMovania

02-23-2005, 06:04 PM

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

DrawCube();

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

glRotatef(xAng,1,0,0);

glRotatef(zAng,0,0,1);

Hope it helps thanx.

sunnymonk

02-25-2005, 10:58 PM

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 :(

MMMovania

02-26-2005, 12:03 AM

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.

sunnymonk

02-26-2005, 07:24 PM

Hi,

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

Cheers

monk

sunnymonk

02-28-2005, 07:43 PM

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

Ominae392

03-02-2005, 06:18 AM

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:

glLoadIdentity();

glTranslatef(0,0,z);

glRotatef(x,y,0);

DrawCube();

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

sunnymonk

03-02-2005, 07:15 AM

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?

Ominae392

03-02-2005, 09:31 AM

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: http://rush3d.com/reference/opengl-redbook-1.1/chapter03.html

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

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