PDA

View Full Version : Problem with glulookat/gltranslatef



gandolf23
06-17-2016, 10:47 AM
I have a wierd issue that Im sure has a simple solution.

Quick history : I usually use lwjgl, but I am converting some stuff to C++.
I have a class (p) that keeps proper track of my 3 control vectors (right,up,view/forward)

Here is my issue:

I basically have the following display code that looks like :
<code>
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glLoadIdentity();
p->getView(&ploc,&uVec,&vDir);
gluLookAt(ploc.x,ploc.y,ploc.z,vDir.x+ploc.x,vDir. y+ploc.y,vDir.z+ploc.z,uVec.x,uVec.y,uVec.z);

float x= 0;
float y = 2;
float z = 2;
float n =1 ;
glBegin(GL_QUADS);

glVertex3f( x+n, y-n,z); // Top Right Of The Quad (Top)
glVertex3f(x-n, y-n,z); // Top Left Of The Quad (Top)
glVertex3f(x-n, y+n, z); // Bottom Left Of The Quad (Top)
glVertex3f( x+n, y+n, z); // Bottom Right Of The Quad (Top)
glEnd();
glPopMatrix();

</code>

Now, when uVec == 0,1,0 everything works fine. That is to say, when ploc.y increases, my box "moves down". Beautiful.

In addition, I can change my uVec within class p and the view of my box rotates beautifully. It also "rotates" up and down when my view direction changes.
In this example VDir = 0,0,1 (looking straight ahead) and never changes.

It all works.

Here is the issue. When uVec == 1,0,0 (ie, I have rotated my camera 90 degrees), as ploc.y increases, my box does not "move down". It "moves to the right". In other words, the gltranslatef part of gluLookat is translating the y coordinate along the up Vector, rather than just translating to the world y location.

This was wierd, so I quickly wrote my own gluLookAt function. I won't go into the detail, but I basically created the rotation matrix using https://www.opengl.org/sdk/docs/man2/xhtml/gluLookAt.xml.

Again, it works flawlessly with all my rotation tests, but as soon as I do a gltranslatef(), it wants to use the rotational axis for the direction of the gltranslatef rather than the actual real location. In other words, uVec=1,0,0 and any changes to ploc.y makes the box move left and right, not up and down.

The wierd thing is, I do this all the time using lwjgl and it is not an issue. GL11.gltranslatef does not use the rotational matrix to determine where to translate to with gluLookat, or when my make my own matrix.

The only thing I can think of is in lwjgl, I am specficially using glut 1.1 (GL11 calls) where as in C++ on linux, I am using freeglut3 libraries. Is there a setting I need to set to use real coordinates? Otherwise it would be a nightmare to keep track of where your camera is in world space, and where you are viewing from.

GClements
06-17-2016, 12:56 PM
Here is the issue. When uVec == 1,0,0 (ie, I have rotated my camera 90 degrees), as ploc.y increases, my box does not "move down". It "moves to the right". In other words, the gltranslatef part of gluLookat is translating the y coordinate along the up Vector, rather than just translating to the world y location.

No, it translates along the world's Y axis. Which is aligned with the screen's X axis because of uVec.



The wierd thing is, I do this all the time using lwjgl and it is not an issue. GL11.gltranslatef does not use the rotational matrix to determine where to translate to with gluLookat, or when my make my own matrix.

LWJGL is just a Java binding for OpenGL. There is no difference between using LWJGL and using the C API directly.

One thing which might make a difference in the case where construct the matrix yourself is that you might be using row-major order where column-major order is required. But that isn't an issue for gluLookAt().



The only thing I can think of is in lwjgl, I am specficially using glut 1.1 (GL11 calls) where as in C++ on linux, I am using freeglut3 libraries. Is there a setting I need to set to use real coordinates?
No. You just need to convert your LWJGL code faithfully to C++ without accidentally changing something.

gandolf23
06-17-2016, 02:06 PM
Bah!! Never mind. This is what I get for testing without a horizon line. You are obviously correct and I had a brain shut down. Never mind, my bad :-|. It is acting as it completely should. Thanks for just telling me the obvious :-).