2D to 3D and back (Ortho and Perspective)
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
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
Code:
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];
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
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
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.
Quote: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.
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).
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| OpenGL ES2 matrix setup (humbly crawling back) | Fenris | 2 | 5,329 |
Aug 31, 2011 06:47 AM Last Post: Fenris |
|
| OpenGL Perspective Screen to Obj Coordinates Mapping Question | WhatMeWorry | 5 | 7,682 |
Feb 17, 2011 05:40 PM Last Post: Holmes |
|
| The tabbing on a word playing back the recorded voice | itcreate | 0 | 2,010 |
Jun 25, 2010 02:06 PM Last Post: itcreate |
|
| 3D in Ortho | bmantzey | 5 | 3,421 |
Nov 20, 2008 02:34 AM Last Post: DoG |
|
| getting the perspective i want | dave05 | 15 | 5,290 |
May 9, 2006 10:51 AM Last Post: unknown |
|

