OpenGL. gluLookAt function not working

Tag: glulookat Author: hyr88 Date: 2014-04-23

This is an image which I draw with my program:

[IMG] http://i62.tinypic.com/j163j8.png [/IMG]

It is supposed to be 3D. When I try to check it with gluLookAt:

GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);  
GL.glLoadIdentity();
GL.glMatrixMode(GL.GL_MODELVIEW);

glu.gluLookAt(
5.0, 2.0, 2.0,
0.0, 0.0, 0.0,
0.0, 0.0, 0.0);

figura(drawable);   //Drawing figure 
GL.glFlush();

It only shows white screen, or a messed up figure for a second, then it goes off. If I undestand good first 3 coords shows point of view, second 3 shows viewing destination and third 3 shows rotation axis. But this function only messes everything up.

Thanks for answers.

Third 3 are the "up" vector, try glu.gluLookAt(5.0, 2.0, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
@AnthonyVallée-Dubois That would be a valid up vector if the eye was on the same XZ plane as the target, ie. {5, 0, 2} instead of {5, 2, 2}.
@schmop Maybe I'm wrong as it's been a while but it's my understanding that the up vector is recomputed by gluLookAt() to be perpendicular to the direction vector such as side = cross(dir, up); up = cross(side, dir);. See :opengl.org/wiki/GluLookAt_code
@schmop In any case computing it yourself isn't wrong and your answer is spot on.
@AnthonyVallée-Dubois Oh. That's very handy! Haven't used gluLookAt in 10 years myself...

Other Answer1

The last three parameters aren't exactly the rotation, but the "up" vector. If you do not want a distorted view, the up vector should be perpendicular to your direction vector, which here is {-5, -2, -2}. Among all the perpendicular vectors, the one you will choose will define the rotation as you call it.

In your example, the most upward perpendicular vector (the one you would use in a first person game for instance) would be {-.323, .937, -.129} after normalization. I computed it first by finding the "left" vector (cross product between absolute up {0, 1, 0} and the direction), and then as the cross product between the direction and "left".

comments:

gluLookAt() ortho-normalizes the up vector relative to the eye vector. There's no need to do that manually. See manpagez.com/man/3/gluLookAt.
@RetoKoradi Okay. That wasn't so clear from the OpenGL reference pages.