Sign in to follow this  
TheCrow33

OpenGL Everything renders upside down in Perspective

Recommended Posts

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?

[code]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();
}[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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.
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='TheCrow33' timestamp='1307640502' post='4821393']
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()...
[/quote]
Well, using orthogonal projection isn't wrong in itself. It is at most wrong for your desire.

[quote name='TheCrow33' timestamp='1307640502' post='4821393']
... 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?
[/quote]
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.

Share this post


Link to post
Share on other sites
[quote name='TheCrow33' timestamp='1307643262' post='4821409']
[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.
[/quote]

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.

Share this post


Link to post
Share on other sites
[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.
[/quote]
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.

Share this post


Link to post
Share on other sites
[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!?
[/quote]

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 name='haegarr' timestamp='1307643584' post='4821412']
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.)
[/quote]

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.

Share this post


Link to post
Share on other sites
[quote name='Brother Bob' timestamp='1307644684' post='4821425']
[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.
[/quote]
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='TheCrow33' timestamp='1307644744' post='4821426']
[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!?
[/quote]

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 [i]relevant[/i] 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?

Share this post


Link to post
Share on other sites
[quote name='haegarr' timestamp='1307645477' post='4821432']
Of course, you're right. However, the [i]relevant[/i] 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?
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='TheCrow33' timestamp='1307645943' post='4821437']
Yeah I used to set up the orthogonal projection as such:
[code]glOrtho(0, 640, 480, 0, 0.1f, 100.0f);[/code][/quote]
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 [url="http://www.opengl.org/sdk/docs/man/xhtml/glOrtho.xml"]this manual page[/url]. 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.

Share this post


Link to post
Share on other sites
[quote name='haegarr' timestamp='1307646399' post='4821438']
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 [url="http://www.opengl.org/sdk/docs/man/xhtml/glOrtho.xml"]this manual page[/url]. 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 view. But because the now used gluPerspective is not upside down, the MODELVIEW set-up (or the model itself) causes the model to appear upside down.
[/quote]

Ahh alright, thanks for all the help guys. I've never run across any graphical programming where bottom<top so this one stumped me. Anyway I flipped all the y's and everything looks great.

Share this post


Link to post
Share on other sites
[quote name='TheCrow33' timestamp='1307646959' post='4821442']
...I've never run across any graphical programming where bottom<top so this one stumped me. ...
[/quote]
Many windowing systems (i.e. systems responsible for GUIs) use a co-ordinate system where y runs from top to bottom. glOrtho is used typically to render a GUI, and your selection of arguments (I mean the 0, 640, 480, 0) hints at that you've copied from such a use case. However, in the given use case you are rendering a 3D scene. In a 3D scene it is typical that y runs from bottom to top. Although you may have not seen that because most tutorials use perspective projection when rendering 3D scenes. That's probably the whole mystery here...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628333
    • Total Posts
      2982130
  • Similar Content

    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Thanks, 
      Dejay Hextrix 
    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By aejt
      I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
      I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
      This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
      I have these classes:
      For GPU resources:
      Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
      Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
      ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).  
      And my plan is to define everything into these XML documents to abstract away files:
      Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
      Factory classes for resources:
      For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
      Factory classes for assets:
      Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).
       
       
      Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
      Thanks!
    • By nedondev
      I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
      Thanks.
    • By Abecederia
      So I've recently started learning some GLSL and now I'm toying with a POM shader. I'm trying to optimize it and notice that it starts having issues at high texture sizes, especially with self-shadowing.
      Now I know POM is expensive either way, but would pulling the heightmap out of the normalmap alpha channel and in it's own 8bit texture make doing all those dozens of texture fetches more cheap? Or is everything in the cache aligned to 32bit anyway? I haven't implemented texture compression yet, I think that would help? But regardless, should there be a performance boost from decoupling the heightmap? I could also keep it in a lower resolution than the normalmap if that would improve performance.
      Any help is much appreciated, please keep in mind I'm somewhat of a newbie. Thanks!
  • Popular Now