# gluLookAt - matrix ops

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

## Recommended Posts

I've understood that there are two ways to manage your view point in a 3d enviroment: 1.) You can have a camera object wich you manually move around by calculating x, y and z values for for pos, look-at and up vectors througt 3d vector math. Then you give these values to gluLookAt to set you view point. You draw your objects at the same place and move your view point by changing the values you pass to gluLookAt. 2.) But I've also understood that the best way to manage 3d graphics is to use matrix operations, using glTranslate, glRotate, glScale etc. And you could also make the view point to appeare to move through series of call to these function. So why should one use the former of latter of thouse two alternatives? It feel more logical to use 1), it's more easally imagine a camera moving around. On the other hand, it takes a lot of manual calculations and matrix operations as in 2) feels like a 'neater' way to do it, maybe easier too... Dose anyone have an idea? And does anyone know where to find articles about these kind of more design related issuse, I've never found much of that around. /Lidestro

##### Share on other sites
gluLookAt is a utility function (glu stands for "GL utility" library) which constructs the transformation matrix for you. in short, number 1 and 2 are pretty much the same thing, unless you have some interesting circumstances... and I think you will know if and when you have those extenuating circumstances.

FOR WHAT ITS WORTH edit: gluLookAt subtracts your camera position from the "look-at" position, and gets a view vector. It then crosses that vector with the "up" vector (which usually is just given as <0, 1, 0> if you aren't upside down), to get the "right" vector. It then crosses the "right" vector with the view vector to get the true up vector. It normalizes all of them, and using those three othronormal vectors it constructs the correct transformation.

##### Share on other sites
Interesting!

But in the camera management I've seen using gluLookAt they have to use quite complex math to calculate their new look-at vectors. So seems easier just to do it with glRotate and glTransform. But maybe you have to calculate these vetors anyway to know which way to move...

/Jens

##### Share on other sites
Quote:
 Original post by LidestroBut in the camera management I've seen using gluLookAt they have to use quite complex math to calculate their new look-at vectors. So seems easier just to do it with glRotate and glTransform. But maybe you have to calculate these vetors anyway to know which way to move...

Well, I think it goes without saying that the nature of your camera system comes into play. If you aren't using a camera that is "looking at" particular places or things, then you may not want to use gluLookAt. Of course, you could always just keep a "lookat" view vector for your view orientation and rotate that vector around when your view moves, and still use gluLookAt.

##### Share on other sites
Hi,
I also have a problem with gluLookAt()
Im trying to make the camera move in all three axis(like the old "descent" game).

so I wrote the following code,
But after I rotate on too much axis it start to do wrong things.
(if I look 90 deg to the left I cant roll correctly ect.)

--------------------------------------

upX = sin(z_angle * pi /180);
upY = cos(z_angle * pi /180) * cos(x_angle * pi /180);
upZ = -sin(x_angle * pi /180);

lookX = x_loc + sin(y_angle * pi /180);
lookY = y_loc - sin(x_angle * pi /180);
lookZ = z_loc - cos(y_angle * pi /180) * cos(x_angle * pi /180);

gluLookAt(x_loc,y_loc,z_loc,lookX,lookY,lookZ,upX,upY,upZ);
// Rotating
{
z_angle += 1.5f;
}
{
z_angle -= 1.5f;
}
{
x_angle += 1.5f;
}
{
x_angle -= 1.5f;
}
{
y_angle += 1.5f;
}
{
y_angle -= 1.5f;
}
// Moving
// move fowards
if(keys[65])
{
z_loc -= 0.1 * cos(y_angle * pi /180) * cos(x_angle * pi /180);
x_loc += 0.1 * sin(y_angle * pi /180);
y_loc -= 0.1 * sin(x_angle * pi /180);
}
// move backwards
if(keys[90])
{
z_loc += 0.1 * cos(y_angle * pi /180) * cos(x_angle * pi /180);
x_loc -= 0.1 * sin(y_angle * pi /180);
y_loc += 0.1 * sin(x_angle * pi /180);
}
// Slides sides
{
z_loc -= 0.1 * sin(y_angle * pi /180);
x_loc -= 0.1 * cos(y_angle * pi /180) * cos(z_angle * pi /180);
y_loc += 0.1 * sin(z_angle * pi /180);
}
{
z_loc += 0.1 * sin(y_angle * pi /180);
x_loc += 0.1 * cos(y_angle * pi /180) * cos(z_angle * pi /180);
y_loc -= 0.1 * sin(z_angle * pi /180);
}
//Slides up/Down
{
y_loc += 0.1 * cos(z_angle * pi /180) * cos(x_angle * pi /180);
x_loc += 0.1 * sin(z_angle * pi /180);
z_loc -= 0.1 * sin(x_angle * pi /180);
}
if(keys[VK_RETURN])
{
y_loc -= 0.1 * cos(z_angle * pi /180) * cos(x_angle * pi /180);
x_loc -= 0.1 * sin(z_angle * pi /180);
z_loc += 0.1 * sin(x_angle * pi /180);
}

##### Share on other sites
See:
http://www.gamedev.net/reference/articles/article1095.asp

##### Share on other sites
This wont be easy as I Hoped...

##### Share on other sites
Hi.. I a beginner...

I want to use gluLookAt.. But I was confuse for fill the parameter. First x,y,z is for a position of camera i guess. But in second and third x,y,z what for?? I have read the MSDN help..but still confuse to implement. Can you help me to explain because i want to implement it..

Thanks

##### Share on other sites
Whenever you want to move or rotate your camera, just glRotatef() or glTranslatef() in the opposite. Or you could multiply the MODEL_VIEW matrix yourself with glMultMatrix().

Simple really, but make sure you translate last, otherwise you'll just rotate the whole world around 0, 0, 0

Using this method makes it easy to rotate on the Z axis, as you can use an angle instead of the Up vector thing in gluLookAt()

That's the way my camera works anyway..

##### Share on other sites
Quote:
 Original post by coba2Hi.. I a beginner...I want to use gluLookAt.. But I was confuse for fill the parameter. First x,y,z is for a position of camera i guess. But in second and third x,y,z what for?? I have read the MSDN help..but still confuse to implement. Can you help me to explain because i want to implement it..Thanks

In gluLookAt the first xyz is for the camera location,
the second XYZ is for the location of the thing you are looking at and the last is for a vector that says were is up.

##### Share on other sites
The last xyz is for the vector that says were is up????

##### Share on other sites
Think you are in an airplane located in a x,y,z location and you are looking at an island in a x,y,z location.

You still need to know were up is (the up side of the airplane) because the view will be diffrent if the air plane is upside down.

##### Share on other sites

But I have some problem with my project. In my project I draw a camera in position 1,1,3. After that I using glLookAt (1,1,3,0,0,0,,1,0). And the result is so weird. My object camera that i was draw.. still on there but viewing has been change...

I was confuse.. what wrong from my project???

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628706
• Total Posts
2984309

• 23
• 10
• 9
• 13
• 13