#### Archived

This topic is now archived and is closed to further replies.

# Camera Rotation

This topic is 5961 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Can anybody tell me a good way to do camera rotation using gluLookAt()? All tutorials I''ve seen did a terrible job and didn''t work. Also no one ever really explains gluLookAt(). Also it would help if it were done with the camera''s Position X, Y, Z and Look X, Y, Z, and Up X, Y, Z. And if possible can you include how to move in the direction the camera is looking at. Thanks.

##### Share on other sites
very similar to a post below, must be the new topic in class

if u want to do rotation on 3 axises (6DOF?)then u will need another method than eular angles eg angle/axis + quaternion/matrices which is much more complicated (check on the net for a lot of examples/tutorials).
if all u want to do is a quakelike movement FPS then u can get away with 1.5 axises using eulars

gluLookAt( cam.x, cam.y, cam.z, cam.x+camDIR.x, cam.y+camDIR.y, cam.z+camDIR.z, 0,1,0 )

cam is where the camera is
camDIR is the direction the camera is looking

the player can rotate on the ground plane 0..360 deg (like a compass) this is called the heading.
the player can also look up and down +90 (at sky) .. -90 (at feet) this is the pitch.(note u prolly want to limit that to 89 .. -89deg

ok to find the camera direction use the following

camDIR.y = sin(pitch);

now to move foward u ignore the pitch and just concentrate on the XZ plane

(foward key pressed)

piece of piss right?

note above u need to supply heading/pitch in radians and not degrees to convert degrees to radians multiple the degrees by (PI/180) about 0.017

##### Share on other sites
Wow, Thanks that helped me too!

_BUSTER_

www.dotspot.cjb.net
______________________________
Check out my for sale domain name!

http://www.theatermonkey.com

##### Share on other sites
It still doesn''t work....
Here''s what I tried to find the direction:

right and up are the distance up or right for rotation.

camDIR.x = sin((camDIR.x+right)*(180/0.017))*cos((camDIR.y+up)*(180/0.17));
camDIR.y = sin((camDIR.y+up)*(180/0.17));
camDIR.z = sin((camDIR.x+right)*(180/0.017))*cos((camDIR.y+up)*(180/0.17));

##### Share on other sites
You''ll find your problem is that the computer works in radians. You are sending the angle in degrees and of course this wont work. Here are 2 inline functions that i use lots.

inline float cosd(float p_angle)
{
return cos(p_angle * (3.141592654 / 180));
}

inline float sind(float p_angle)
{
return sin(p_angle * (3.141592654 / 180));
}

This will allow you to enter a degrees angle and get the correct results back. Hope this helps =)

##### Share on other sites
Something is really messed up in the code but I can''t
figure out what! I used to be able to move around in the world
with glTranslatef() and the objects looked truley 3D(They still
do but...) and they would shrink as you got further away
and grow larger as you got closer as they should but now you
have to get real close to be able to see them... If you aren''t
real close it doesn''t draw them. The rotation still just spins
all the objects and shoves them either down or left(Even
if you rotate the opposite direction).
This is really starting to make me mad.
Anybody got any ideas? (Mabye if someone actually SHOWED
how to apply the suggestions because no one really gave
too detailed info)

##### Share on other sites
>>Something is really messed up in the code but I can''t
figure out what! I used to be able to move around in the world
with glTranslatef() and the objects looked truley 3D(They still
do but...) and they would shrink as you got further away
and grow larger as you got closer as they should but now you
have to get real close to be able to see them... If you aren''t
real close it doesn''t draw them. <<

perhaps try this look in your code for gluPerspective() or glFrustum() and make the last number 100x larger.

##### Share on other sites
Are you using glLoadIdentity() before you do all the rotations/translations?

DeathWish

##### Share on other sites
inline float cosd(float p_angle)
{
return cos(p_angle * (3.141592654 / 180));
}

wouldnt it be better to precalculate 3.141592654 / 180
this would speed the function up!

##### Share on other sites
appears to be somewhat closer!
There''s still that problem with distance.(You have to be REAL close)(And also note that when rotating the camera I can
see that object wireframes will get cut off near the camera
and at the ends that are far from the camera.
I did mess with gluPerspective with no sucess.
Currently I can''t tell if it really looks like it is rotating
the camera or rotationg the objects themselves.
It also appears the everythings rotating the opposite direction they''re supposed to.
Also the camera doesn''t move/translate the direction it is facing
And I did put the CamDIR.x/y/z+x/y/z in gluLookAt()

##### Share on other sites
Personally I''ve never used gluLookAt(), I hardly use the glu library except for mipmaps and quadratic shapes. You could try it using just the standard glRotatef() and glTranslatef(), or you could do what I do in my basic 3D engine and translate all the co-ordinates by generating my own translation matrix and keeping the camera at 0,0,0.

Or, one thing you could try:

glPushMatrix();
// translate, rotate, render
glPopMatrix();

I''m not too sure about it though, so I may be totally wrong on the matrix push/pops... I hardly use them

(BTW, if you want to use your own translation matrix then I''m more than happy to help you if you don''t know how.)

DeathWish

##### Share on other sites
Another thought... you could try reverse transforming your camera. (Transform -x -y -z, rotate -z -y -x)

DeathWish

##### Share on other sites
quote:
Original post by Anonymous Poster
inline float cosd(float p_angle)
{
return cos(p_angle * (3.141592654 / 180));
}

wouldnt it be better to precalculate 3.141592654 / 180
this would speed the function up!

The compiler will do that for you. The only real optimization with sine and cosine is to precalculate them (they are horrendously expensive functions):
  float Sine[360], Cosine[360];const float M_PI = 3.141592654;void CreateTrigLUTs( void ){ for( int i = 0; i < 360; i++ ) { Sine[i] = (float)sin( i * M_PI / 180 ); Cosine[i] = (float)cos( i * M_PI / 180 ); }}

EDIT: i as array index.

Edited by - Oluseyi on September 17, 2001 1:10:35 PM

##### Share on other sites
using a LUT for a couple of sin''s cos''s will most likely be slower than using sin(A).
the LUT really is only benificial when youre doing heaps of sin''s at a time

http://members.xoom.com/myBollux

##### Share on other sites
Hey, none of that was really a reply to my last post.
Does anybody got any clues about the distance problem?

##### Share on other sites
Ok, I''ve got an idea:

glMatrixMode(GL_PERSPECTIVE);
gluPerspective(...);

glMatrixMode(GL_MODELVIEW);
gluLookAt(...);
glBegin(...);
...
glEnd();

##### Share on other sites
Actually I believe it''s GL_PROJECTION, not GL_PERSPECTIVE.
And that didn''t help at ALL.
You can''t use gluLookAt() in the GL_MODELVIEW matrix.
And I already do use gluPerspective in GL_PROJECTION and it can only be used in that matrix.

##### Share on other sites
>>There''s still that problem with distance.(You have to be REAL close)(And also note that when rotating the camera I can
see that object wireframes will get cut off near the camera<<

decrease your near planes depth value + increase the far planes value
in gluPerspective they are the 3rd+4th variable

http://members.xoom.com/myBollux

##### Share on other sites
zedzeek, that is very obvious and it keeps getting repeated here. I have tried that 3 times! I''ve tried a near distance
of 0.0f and a far distance of 100000.0f, no effect.

##### Share on other sites
Are you doing...

glLookAt(...)
push
glRotate() or glTranslate etc...
pop

make sure that the values to glLookAt are not huge numbers...

##### Share on other sites
um.... where exactly were you planning to use gluLookAt if not on the modelview matrix? That is exactly where it should be used, regardless of your projection.

Follow Ansi2000''s structure (rendering objects between the matrix push and pop), making sure that all these operations are done on the modelview matrix.

##### Share on other sites
a near value of 0.0 is not allowed try something larger

##### Share on other sites
OK! I added gluPerspective to the rotation and movement
functions, and it seems to work!!!
Though the camera still doesn''t want to move in the
direction it is facing
(And I DO use camDIR.x/y/z+x/y/z in gluLookAt center params)

ANSI2000, no I don''t and no I don''t use large numbers.