Sign in to follow this  
twig_monster

OpenGL glOrtho problem

Recommended Posts

twig_monster    122
Hi folks I'm trying to convert my simple 2D tile engine (originally in C#/MDX) over to a 3D solution, but I'm pretty much a newbie. Having decided on OpenGL I set about getting a checkerboard to display in an orthogonal projection (since I don't want perspective correction). That was fun, since all the tutorials for ortho are for 2D drawing and I wanted a 45 degree view where y and z both run vertically (like an isometric projection but with no rotation around the y axis) and the squares are all rendered on the 'floor' (ie every y value is 0). Got that working eventually (had trouble getting the 'tiles' to appear exactly half their height in screen coordinates as you would expect from the viewing angle) and did a quick C++ conversion to test speed differences since the framerate seemed rather low (got a 50% improvement using C++ btw). I then decided to add some geometry with height, and that's where I'm stuck. My problem is that the top/bottom clipping planes are not behaving as I expected. Assuming values of -500/500 respectvely (the range is a bit wider than that really), I cannot see all of an object which is 500 units tall. Furthermore, the clipping plane is wonky! I rendered a plane where one side was closer to the viewer than it's opposite. The near side gets clipped earlier (ie the lines forming the top/bottom of the plane do not run parallel, and I've double-checked that they should be) - if I decrease the y values of the 'top' vertices so that it is much shorter, clipping works correctly. How am I to get tall objects rendering correctly? This engine will be used in an indie RPG and I need to have tall buildings and such in view. At the moment, no matter what I try, the tallest fully-visible object cannot even by half a screen tall. Sorry for my long first post, so I'll say thanks in advance and leave you with the render function from my C++ source (the buffers are swapped elsewhere btw): int DrawGLScene(GLvoid) { glViewport(0,0,_width,_height); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Although Z values from -768 to +768 are visible, we need to shrink these due to the 45 degree camera orientation // I don't quite understand this, and the value of 543.32 was found by trial and error: // C++ fix: the top/bottom need to equal the znear and zfar or it causes the view to change // (in C# you can use any values where one is the negated value of the other) glOrtho(-512.0f, 512.0f, -543.32f, 543.32f, -543.32f, 543.32f); // Rotate camera eye 45 degrees along the x axis towards a birds-eye view glRotatef(45.0f, 1.0f, 0.0f, 0.0f); glPushMatrix(); // Render the checkerboard int xstep = 64; int zstep = 64; glBegin(GL_QUADS); for (int z = -12; z < 12; z++) { for (int x = -8; x < 8; x++) { float f = abs((x + z) % 2) * 1.0f; glColor3f(f, f, f); int fx = x * xstep; int fz = z * zstep; glVertex3i(fx, 0, fz); glVertex3i(fx + xstep, 0, fz); glVertex3i(fx + xstep, 0, fz + zstep); glVertex3i(fx, 0, fz + zstep); } } // Render a red plane which has some height glColor3f(0.5f,0.0f,0.0f); glVertex3f(-35, 0.0, 303); glVertex3f(-35, 500, 303); glVertex3f(60, 500, 398); glVertex3f(60, 0, 398); // Despite the high y values fitting within the view volume, they get clipped....wonkily // TODO : Seek help! glEnd(); glPopMatrix(); return TRUE; }

Share this post


Link to post
Share on other sites
haegarr    7372
Currently I'm not sure what is causing your problem, but ...

(1) zNear/zFar and top/bottom are not interrelated.
(2) For a "normal" camera zNear should be close to 0, or else you can see also stuff behind the camera and waste z resolution. (But perhaps you _want_ to see behind the cam, then it would be okay.)
(3) If you alter glViewport (your code snippet hints at this since you invoke glViewport every time when drawing the scene) you also have to alter glScissor.
(4) If all is set-up well, then the left/right/top/bottom clipping planes restrict the viewing volume just at the borders of your viewport (e.g. window content area). So you shall not see clipping of geometry (besides near/far) inside the viewport. If you do, then glScissor may be the problem.
(5) The ratio (right-left)/(top-bottom) should normally match the rows/lines ratio of pixels of the window (assuming square pixels), or else the scene will be displayed distorted.

Share this post


Link to post
Share on other sites
twig_monster    122
Quote:
Original post by haegarr
Currently I'm not sure what is causing your problem, but ...

(1) zNear/zFar and top/bottom are not interrelated.
(2) For a "normal" camera zNear should be close to 0, or else you can see also stuff behind the camera and waste z resolution. (But perhaps you _want_ to see behind the cam, then it would be okay.)
(3) If you alter glViewport (your code snippet hints at this since you invoke glViewport every time when drawing the scene) you also have to alter glScissor.
(4) If all is set-up well, then the left/right/top/bottom clipping planes restrict the viewing volume just at the borders of your viewport (e.g. window content area). So you shall not see clipping of geometry (besides near/far) inside the viewport. If you do, then glScissor may be the problem.
(5) The ratio (right-left)/(top-bottom) should normally match the rows/lines ratio of pixels of the window (assuming square pixels), or else the scene will be displayed distorted.


1) Thanks, that's what I thought. But my top/bottom values affect the viewable z range in my C++ implementation - I use Colin Fahey's wrapper for C# and in my C# code the values don't have this effect (as long as each one is the negated value of the other). My guess is that they need to be the same because the 45 degree view results in the y/z axes having exactly the same scale.
2) Ah, I'm using this to control the vertical viewing distance. I admit I don't really understand ortho projection when not used in the traditional '2D' context, but unfortunately all tutorials I've found use it solely for this purpose. I just used these values since they gave me the correct viewing area with my angle of rotation along the x-axis!!
3) Just changes if the screen is resized.
4) The scissor is fine - it's not a problem with the screen being clipped prematurely, as the problem occurs in the middle of the view (you can copy my render function if you want to see what I meant in my post). I guess since glOrtho is primarily for 2D operations such as GUI/HUD elements that a view not looking directly forward is hard to describe properly?
5) Makes sense. But I do want to double the number of units along the z axis if I'm using a 45 degree top/down view don't I? I was using -768 and 768 (the view is supposed to be 1024x768 as both values are divisble by all powers of 2 up to 256), but had to fiddle with these so the correct number of squares (well, rectangles due to the camera orientation) were displayed vertically.

Basically I want to draw my geometry with the correct 3D placement:
The ground plane runs along x/z with a y elevation of 0.
Objects such as buildings will have a positive y value for any vertices not anchoring them to the ground. But very tall objects need to be renderable, as I plan on having some towers and trees which might take up more than one whole screen vertically.

The camera is supposed to look down/forward at a 45 degree angle so that there is a 3D view of the 'map' without perspective correction taking place. So the y/z values should appear at half scale (ie rotating a ground square 45 degrees around it's y axis would appear to be a diamond tile - the typical dimetric '2.5D' view used in many so-called 'isometric' RPG's).
I don't want to do this in 2D, as I want to be able to freely rotate objects and avoid the 'traditional' look. Plus, character animation using sprites is a memory hog.

Share this post


Link to post
Share on other sites
haegarr    7372
Orthogonal projection means that the projectors (i.e. those line running through each pixel if thinking of a ray tracer) hit the projection plane perpendicularly. It is a special kind of parallel projection, i.e. all projectors are also parallel. As for any parallel projection you get no depth foreshortening and hence see no perspective effect. Besides this, there is no real difference between perspective and orthogonal projection. However, notice please that projection takes place in VIEW space. glOrtho always projects along the VIEW's z axis. If you want to do other orthogonal projections, you have to make the projection matrix yourself. But, as your approach already shows, a way to reach the goal is to adapt the VIEW matrix properly and using the standard projection stuff.

You've said that the clipping occurs just in the middle of the screen. Looking at your scene drawing code and assuming that it is completely posted, I miss various things. The usual way of setting up OpenGL's matrices is like this:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(...);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// set-up of VIEW portion (its the inverse of camera's local space transformation)
// set-up MODEL portion

where the projection stuff is usually done only once, while the MODELVIEW is set-up for each scene rendering round-trip. In your case, the VIEW portion IMHO should consist of 3 things: 1st pitch the camera, 2nd lift it up to the viewing height, and 3rd shift it around over the playfield. Here the 2nd and 3rd step define the camera's postion, of course. Especially, if the 2nd step is not done (and you don't do it), a sufficiently big ground plane at level 0 will be cutted horizontally on half of the screen! Maybe that is your problem.

Share this post


Link to post
Share on other sites
twig_monster    122
Thanks, I've resolved the problem now. You're right, it was my mis-interpretation of how orthogonal projection works. I'm now drawing all objects along the x/y axes instead and using the z axis for height (still find it hard remembering that y moves *up* the screen in 3D space, plus now I'll have to pre-rotate my models so that the +y axis becomes the +z axis). The 45 degree geometry rotation takes care of halving the scale of y and converting z values into a screen 'height' equal to half of z.
Still have to specify that weird floating-point value for the vertical viewport size(though I'm now using bottom/left values of 0, so it's doubled) in order to get exactly 1536 units visible along the y axis after rotation (multiply the required value by 0.7074479167). I call glTranslatef (512, 768) to center the view correctly.

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  

  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
  • Popular Now