View Full Version : Could someone explain in detail gluLookAt

I'm trying to figure out how gluLookAt works because it seems like it might be an easier way to translate/rotate my view of the world as opposed to using glTranslatef/glRotatef. The first 3 parameters are easy enough. My position. But the other 6 confuse me. I'm thinking that the x_aim/y_aim/z_aim are the coordinates of the object I'm looking at, or any point along that line... is that right? And then there's the x_up/y_up/z_up. I'm thinking that that's any point along a ray that is going directly up from my head... is THAT right? If it is, then it really confuses me because that would also determine what I'm looking at and thus seemingly render useless the x_aim/y_aim/z_aim... Can you see what I'm confused about? Please help if you can... Thanks in advance. =)

To create a matrix for viewing, OpenGL needs the coordinate system of the camera, which is 3 orthogonal axes plus an origin (the eye). To construct the 3 axes, GLU uses the LookAt parameters to create the -z_axis = (eye - at), y_axis = up, and x_axis = y_axis x z_axis. All axes are then normalized. The origin (eye) is projected into this coordinate system, and inserted, along with the axes, into a 4x4 homogeneous view matrix, which is then multiplied onto the top of the current matrix stack (GL_MODELVIEW in the case of the camera).

Does that make sense? :)

I left out a step, the orthogonalized y_axis, which may be the source of your confusion:

z_axis = eye - at

y_axis = up

x_axis = y_axis x z_axis

y_axis = x_axis x z_axis.

:)

:eek: Phew... Homogenous view matrix... I think my brain just exploded. =). I only vaguely remember matrices from from Algebra and I never did like them. I remember they reminded me alot of arrays and they had a weird way of being multiplied. But right now I don't have a clue how to multiply an array. And I never did have a clue what they were useful for. When I'm doing stuff in OpenGL I don't think of the matrices or however it stores and works with the data or whatever. I just think of empty 3D space. And when I'm drawing my triangles or whatever I generally used glTranslatef to translate back 5 units or so in the z and I kind of thought of myself as floating above the triangle and looking down on it (I kind of picture z axis as the up and down axis and not the y). Anyway, can you explain what the parameters of gluLookAt do without using words like 'Homogenous view matrix'? Because I don't know what homogenous means. And even if I did, I still don't think I'd know what a homogenous view matrix is =). Anyway, thx for trying. And thanks in advance again =).

It sounds to me like someone needs to bone up on the math :D Here is a tutorial on matrix basics:

http://www.math.hmc.edu/calculus/tutorials/matrixalgebra/

I strongly recommend that you learn this stuff. Your life in graphics will be a veritable hell with out it ;)

If you ignore the last bit in my first post, about the OpenGL matrix, the rest is really just vector stuff, and it's pretty straightforward. If you're having trouble with vectors and coordinate systems, I advise you to seek out books and tutorials in these areas, for your own sanity ;)

Well I'm looking at that site now. It's really boring... and I'm really not sure it will help me at all. I just don't see how matrices has anything to do with the coordinate system. And it's not the coordinate system I'm having problems with. It's the parameters of gluLookAt I'm not sure what do. And in your explanation it's kind of the words I'm having trouble with. Homogenous... eye - at... yaxis x zaxis... Here's what I know: When my OpenGL program starts before I rotate or do anything, I'm at 0, 0, 0. The positive side of the y axis goes up my screen. The negative side goes down. The negative side of the x axis goes left and the positive side goes right. And the positive side of the z axis is behind me and the negative is in front of me... If you could explain things in words like that instead of this = that, it would be alot easier to understand for me. And even if I do need to learn more math, right now I only want to know what the parameters of gluLookAt do in a way that I can understand. Again, thanks for trying.

You really should learn maths, what they told you, even if their posts and links look boring, are more than important.

Now, about your question.

gluLookAt do all the translations and rotations you would have to do if you wasn't using it. Meaning that it places the 'camera' where you want it.

If you want to use it, don't do any translations or rotations before calling it, it will just give bad results.

Now, the 3 first parameters specify the coordinates of the eye (x,y,z). The next 3 ones specify where the camera aims (where it looks at) with also (x,y,z). Finally, the last 3 ones just specify the orientation of the camera - its up vector (generally the up vector is full at the positive y coordinates: 0,1,0). If you were looking downside, it would be 0,-1,0. With some mathematics you can orientate it as you wish.

Hope that helps.

I am sorry that you did not find my answer revealing . I can do no better than that. It's difficult for me to communicate certain concepts without using a common language like math. I hope that you understand, and I wish you the best of luck.

BTW, I like Jide's explanation better than mine :)

songho

11-21-2005, 11:10 AM

gluLookAt() is kind of 3-point camera operator.

First point is the origin of camera and the second point is the target(lookat) point. These two points are enough to translate or rotate camera(view) if you assume the camera is always straight up. (0,1,0)

If not, you need the third point(vector) to tell which direction is camera's up vector, so you can roll the camera. This up vector does represent only direction, not absolute position in the 3D space.

gluLookAt() is usually used to construct the "view" matrix (rotate and translate with given 3 points) and it will be multiplied with "model" matrix in order to build the final GL_MODELVIEW matrix. (= Mv * Mm)

Or you may use this function an object keep following a certain target, for example, a enemy in the scene keeps looking at you.

==song==

Eh. I kind of get it. As a test, I did this:

gluLookAt(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -5.0f, 0.0f, -1.0f, 0.0f);

glBegin(GL_TRIANGLES);

glColor3f(0.0f, 0.0f, 1.0f);

glVertex3f(-1.0f, -1.0f, -5.0f);

glVertex3f(1.0f, -1.0f, -5.0f);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3f(0.0f, 1.0f, -5.0f);

glEnd();

And that sets me so I'm at 0, 0, 0 and looking at the triangle at 0, 0, -5. And I'm upside down so the red point of the triangle that is pointing up looks to me like it's pointing down. But what I was wanting to know is about the up. If say my 'camera' is in a mailbox with the door down, taped to the bottom, and the flag on the mailbox is up, then the flag on that mailbox is pointing to my 'up' right? And for the x, y, and z of the up, I can put the coordinates of any point that is on that line right? If that's true, then setting my 'up' would also set my view wouldn't it? Do you see what I'm trying to say? Let me kind of rephrase this...

The 4th, 5th, and 6th parameters are the x, y, and z of the object im looking at right? Either that or any point along that line (the line from my eyes/camera to the object) would work too right? And the purpose of my up is for my orientation. Like if I'm upside down or rightside up, right? But my local x and y axes are already set so that I'm looking along the line from my eyes to the point I specified in the 4th, 5th, and 6th parameters. Then the only axis I could rotate on would be my z axis wouldnt it? Which would determine whether I'm upside down or rightside up or turned at a 90 degree angle or whatever. But what if in the example above, where I'm upside down looking at the triangle, I set my up to 0, 0, 1. Then my up would be a line going out the back of my head wouldn't it? So how could that coexist with my view being set to 0, 0, -5?

Ehh, I hope you can figure out what I'm asking from all that but I probably just confused you more didn't I? I'm just not sure how to word it. Anyway, thanks for trying yall. =)

Ah okay forget that last post. I didn't notice songho's post there for a while because it was posted while I was typing that last post of mine. I think I understand. I played around with the function a little, but I got one question left which was kind of the source of my confusion. What if I'm say at 0, 0, 0 and looking at 0, 0, -5, but I set my up to 0, 0, 5. Then, as I said in the last part of my last post, my up would be pointing out the back of my head/back of my camera, and if that's so, it's not possible that I'm still looking at 0, 0, -5 is it? If instead of an x, y, and z, the function had of took a single float for my camera's rotation, that would have made more sense to me.

songho

11-21-2005, 12:46 PM

The UP vector must not be parallel to the line of sight from the eye point to the lookat point, because gluLookAt() performs cross product internally to build matrix.

The result of cross product of 2 identical vectors or opposite direction vector is 0 (think about sin(0) and sin(180)).

GLU library is just supplement over GL lib, so you don't have to use gluLookAt(). But I think it is easier than just translate and rotate. You can build your own lookat function based on your idea that you mentioned.

==song==

K. Thanks everyone for trying to help. =)

i was asking the same exact questions last week. here's what i think i figured out that might help:

the matrices you learned in high school and the matrixes they are talking about work the same, but the rows and cols are swapped.

the opengl( et al) ones are like this:

[][][][x]

[][][][y]

[][][][z]

[][][][w]

but in math class it was like this:

[x][y][z][w]

[][][][]

[][][][]

[][][][]

they still work the same though.

check this page

Article on Matrixes[morrowland.com] (http://www.morrowland.com/apron/article/technical/matrix/index.php)

songho

12-09-2005, 06:46 AM

<shozma>,

I think the order of matrix elements is not correct.

OpenGL uses column major but normal math is row-major.

Therfore, the matrix in OpenGL looks like this;

| 0 4 8 12 |

| 1 5 9 13 |

| 2 6 10 14 |

| 3 7 11 15 |3: x coord

7: y coord

11: z coord

15: w coord

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