View Full Version : 2D to 3D and back (Ortho and Perspective)
Phate
2003.11.30, 12:17 AM
Hello, I'm working on a game at the moment, where I would like to draw 2D elements, as well as 3D. So I need to be able to change from a perspective 3D one, to an orthographic 2D one, and then switch back.
I can set it up one way or the other, but my attempts to allow it to switch between the two have been unsuccessful. Can anyone provide any sample code for this?
thanks
Phate
Mark Levin
2003.11.30, 01:46 AM
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60,currentWidth/currentHeight, ME_NEAR_CLIP_DISTANCE,1000);
[draw 3D objects]
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,1000,0,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
[draw 2D objects];
Phate
2003.11.30, 12:08 PM
Thanks Mark,
Is there a way to draw some 2d objects before the 3d stuff as well? I've tried simply setting up the perspective matrix after drawing the first round of 2d stuff, but I been getting some crazy results.
Thanks,
Phate
Mark Levin
2003.11.30, 12:22 PM
It shouldn't matter which order they're drawn in... The important part is to have all the steps (switch to projection, load identity, set perspective or ortho matrix) before you try drawing either category.
Originally posted by Mark Levin
It shouldn't matter which order they're drawn in... The important part is to have all the steps (switch to projection, load identity, set perspective or ortho matrix) before you try drawing either category.
Wrong. The important step is to disable the depth buffer, or clear the depth buffer. When you switch the projection matrix, the depth buffer stays the same, but the values become meaningless in the new projection.
AnotherJake
2003.11.30, 01:56 PM
I wouldn't go so far as to say it's wrong, cause you can ignore the depth buffer depending on what you're doing. But yeah, if you don't disable the depth buffer, the 2D stuff will have a depth value. This means your 2D stuff might intersect with, let's say, a spinning cube or something close to the viewer (depending on the depth values of course).
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.