Why is my glutWireCube not placed in origin?
-
12-09-2019 - |
Question
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
:
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:
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.
Solution
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 ?
OTHER TIPS
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:
- Is this the entire window? It seems odd that the view is down in one corner.
- 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.
- You can also try moving the "camera" around, by changing the arguments to
gluLookAt()
dynamically.