Everything renders upside down in Perspective

Started by
10 comments, last by haegarr 12 years, 10 months ago
So I just recently realized I had been using glOrtho when initializing Opengl to render 3D objects. You guys pointed out to me that was wrong, and now I've been converting all my 3d projects to using perspective via gluPerspective(). Everything has been going fine, and I've been sieving through tutorials and code on the subject, but it would seem that I have done something mundane terribly wrong. Whenever I render an object it is upside down from what I expect (even textures are mapped upside down). Moving an object in the negative Y direction moves it down (from top to bottom) on the screen, however moving in positive X moves the object to the right (left to right) as I would expect. Can any of you see something terribly wrong with my opengl initialization?

private void openglInit() {


glViewport(0, 0, 640, 480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45f, 640 / 480, .1f, 100.0f);
glMatrixMode(GL_MODELVIEW);

// Shade the model
glShadeModel(GL_SMOOTH);

glClearColor(0f, 0f, 0f, 0f); // Set the background?
glClearDepth(1f); // Enables clearing of the depth buffer?

// Sets the z buffer testing
glEnable(GL_DEPTH_TEST);

glDepthFunc(GL_LEQUAL); // Type of depth test
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Pretty perspective calculations

// Enables culling (only drawing polygons facing the correct way)
glEnable(GL_CULL_FACE);
// Cull the frontside
glCullFace(GL_FRONT);

glEnable(GL_LIGHTING);

glEnable(GL_LIGHT0);

glEnable(GL_TEXTURE_2D);

// Light up the world
getLit();
}
Advertisement
Negative Y-axis is commonly pointing down. You have probably set up your orthographic projection with positive Y-axis going down which is why you expect the opposite. You just have to learn how axes are set up and design your application from there.

You have probably set up your orthographic projection with positive Y-axis going down which is why you expect the opposite.


So I am supposed to be using both glOrtho and gluPerspective when I initialize opengl in the function above?

So I just recently realized I had been using glOrtho when initializing Opengl to render 3D objects. You guys pointed out to me that was wrong, and now I've been converting all my 3d projects to using perspective via gluPerspective()...

Well, using orthogonal projection isn't wrong in itself. It is at most wrong for your desire.


... Everything has been going fine, and I've been sieving through tutorials and code on the subject, but it would seem that I have done something mundane terribly wrong. Whenever I render an object it is upside down from what I expect (even textures are mapped upside down). Moving an object in the negative Y direction moves it down (from top to bottom) on the screen, however moving in positive X moves the object to the right (left to right) as I would expect. Can any of you see something terribly wrong with my opengl initialization?

The OP doesn't show how the MODELVIEW matrix is set-up. I assume that you don't expect the mistake to be buried therein!?

Maybe it is okay for the used programming language (C# ?), but in C/C++ the expression 640/480 as is used as argument for gluPerspective is an integer expression and computes to 1. That wouldn't cause the reported issue but may nevertheless be not what was intended. (Further, e.g. gcc wouldn't be happy with 45f, 0f, and 1f, but I assume that your compiler doesn't reject that.)

Besides that, AFAIS the shown code snippet is okay. I would expect the mistake to be located elsewhere.

[quote name='Brother Bob' timestamp='1307642987' post='4821408']
You have probably set up your orthographic projection with positive Y-axis going down which is why you expect the opposite.


So I am supposed to be using both glOrtho and gluPerspective when I initialize opengl in the function above?
[/quote]
Mixing orthogonal and perspective projection for the same rendering would not be meaningful. Brother Bob meant that your previous set-up using glOrtho was wrong (in OpenGL's typical use), and that you have compensated that mistake at another place (presumable the MODELVIEW matrix) instead of correcting it.That would support my assumption, too, that the mistake is located elsewhere.

Brother Bob meant that your previous set-up using glOrtho was wrong (in OpenGL's typical use), and that you have compensated that mistake at another place (presumable the MODELVIEW matrix) instead of correcting it.

Not saying it's wrong, just that it's different and that you have to learn to handle the differences. Many people prefer Y-down for 2D.

The OP doesn't show how the MODELVIEW matrix is set-up. I assume that you don't expect the mistake to be buried therein!?


As far as I know this is all the setting-up of the matrices I have done (besides a gltranslatef and two glRotatef calls right before I draw the model). The code in question is 3 classes, I didn't really want to flood the post with several hundred lines of code as it might be rather tedious to sort through.


Maybe it is okay for the used programming language (C# ?), but in C/C++ the expression 640/480 as is used as argument for gluPerspective is an integer expression and computes to 1. That wouldn't cause the reported issue but may nevertheless be not what was intended. (Further, e.g. gcc wouldn't be happy with 45f, 0f, and 1f, but I assume that your compiler doesn't reject that.)


This was a good point, I wasn't thinking about integer division when I wrote that line, thanks for pointing that out. By the way my code is in Java using the LWJGL.

[quote name='haegarr' timestamp='1307643833' post='4821415']
Brother Bob meant that your previous set-up using glOrtho was wrong (in OpenGL's typical use), and that you have compensated that mistake at another place (presumable the MODELVIEW matrix) instead of correcting it.

Not saying it's wrong, just that it's different and that you have to learn to handle the differences. Many people prefer Y-down for 2D.
[/quote]
Yep, that's the reason I've added "in OpenGL's typical use" with the meaning in comparison to how tutorials and books AFAIK commonly use the y axis.


[quote name='haegarr' timestamp='1307643584' post='4821412']
The OP doesn't show how the MODELVIEW matrix is set-up. I assume that you don't expect the mistake to be buried therein!?


As far as I know this is all the setting-up of the matrices I have done (besides a gltranslatef and two glRotatef calls right before I draw the model). The code in question is 3 classes, I didn't really want to flood the post with several hundred lines of code as it might be rather tedious to sort through.
[/quote]
Of course, you're right. However, the relevant lines would be just a hand full. E.g. the formerly set-up using glOrtho ... do you have it still at hand? Can you post it for comparison?

Of course, you're right. However, the relevant lines would be just a hand full. E.g. the formerly set-up using glOrtho ... do you have it still at hand? Can you post it for comparison?


Yeah I used to set up the orthogonal projection as such:
glOrtho(0, 640, 480, 0, 0.1f, 100.0f);

Yeah I used to set up the orthogonal projection as such:
glOrtho(0, 640, 480, 0, 0.1f, 100.0f);

So you've used bottom=480 and top=0, hence bottom>top, hence an "upside down" (term not to be interpreted as "wrong" but compared to the common use in tutorials and the like ;)) projection as Brother Bob has guessed.
(See e.g this manual page. The usual set-up with glOrtho looks something like glOrtho(-width/2, +width/2, -height/2, +height/2, near, far), so that bottom<top.)

Your MODELVIEW matrix (or the model itself) is defined to match the upside down projection, i.e. you previously used a double "upside down" and hence "correct" view. But because the now used gluPerspective is not upside down, the same MODELVIEW set-up (or the model itself) causes the model to appear upside down.

This topic is closed to new replies.

Advertisement