glOrtho problem

Started by
3 comments, last by twig_monster 15 years, 12 months ago
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; }
Advertisement
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.
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.
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.
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.

This topic is closed to new replies.

Advertisement