views:

184

answers:

2

I have the following OpenGL code in the display function:

glLoadIdentity();
gluLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz);
// called as: gluLookAt(20, 5, 5, -20, 5, 5, 0, 1, 0);

axis();
glutWireCube (1.);

glFlush ();

axis() draws lines from (0,0,0) to (10,0,0), (0,10,0) and (0,10,0), plus a line from (1,0,0) to (1,3,0).

My reshape function contains the following:

glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(45.0, (GLsizei) w / (GLsizei) h, 1.0, 100.0);
glMatrixMode (GL_MODELVIEW);

This image shows the result of running the program with 1. as the argument to glutWireCube:

glutWireCube(1.)

As you can see, the cube isn't centered around (0,0,0) as the documentation says it should be:

The cube is centered at the modeling coordinates origin (...) (source)

If I run the program with 5. as the argument, the cube is displaced even further:

glutWireCube(5.)

Why is that, and how do I place the cubes around (0,0,0)?

FURTHER INFORMATION

It doesn't matter if I switch the order of axis() and glutWireCube. Surrounding axis() with glPushMatrix() and glPopMatrix() doesn't fix it either.

SOLUTION

I modified gluPerspective to start looking further away from the camera, and now the Z-buffering works properly, so it is clear that the cubes are placed around the origin.

+1  A: 

Are you sure axis does not mess with the view matrix ?

What happens if you call it after the drawing of the cube ?

Edit to add:

Actually... Looking at the picture closer, it looks like it might be centered at the origin.

The center of the cube seems to align exactly with the intersection of the 3 axes. The only thing that looks suspicious is that the red line does not write over the white edge. do you have Z-buffering properly set up ?

Bahbar
Unfortunately, that doesn't change the result either. :(
jmoeller
I think I have Z-buffering enabled with `glEnable(GL_DEPTH_TEST)` and `glDepthMask(GL_TRUE)`. The result is the same if the lines are present or not.
jmoeller
Actually, you are right. If I view the image with `glOrtho` instead of `gluLookAt` and `gluPerspective` the cubes are centered around the origin.How would I enable z-buffering correctly?
jmoeller
I increased `znear` in `gluPerspective` and now the Z-buffering works. Now I just have to figure out how to see more of the image :)
jmoeller
how about reducing zfar then ? the ratio of the 2 is what really affects your Z-buffer precision
Bahbar
A: 

It might be right, I think it's hard to determine due to the perspective ... But I guess it isn't from staring a bit more at it.

To quickly rule out that axis() isn't modifying the model view matrix, surround the call with matrix push/pops:

glPushMatrix();
axis();
glPopMatrix();

Things to investigate/check:

  1. Is this the entire window? It seems odd that the view is down in one corner.
  2. Does it help if you add an increasing rotation before the rendering? That can make it easier to determine the perspective, by giving more clues.
  3. You can also try moving the "camera" around, by changing the arguments to gluLookAt() dynamically.
unwind
Unfortunately, that doesn't change the result.
jmoeller
> Is this the entire window? It seems odd that the view is down in one corner.Yes, it's the entire window. I'm forced to do a front-perspective with a eye-position at (20,5,5).
jmoeller