2D to 3D and back (Ortho and Perspective)

Phate
Unregistered
 
Post: #1
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
Quote this message in a reply
Member
Posts: 177
Joined: 2002.08
Post: #2
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];
Quote this message in a reply
Phate
Unregistered
 
Post: #3
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
Quote this message in a reply
Member
Posts: 177
Joined: 2002.08
Post: #4
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 this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #5
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.
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #6
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).
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGLES video play back prathap 0 901 May 9, 2014 03:45 AM
Last Post: prathap
  OpenGL ES2 matrix setup (humbly crawling back) Fenris 2 6,429 Aug 31, 2011 06:47 AM
Last Post: Fenris
  OpenGL Perspective Screen to Obj Coordinates Mapping Question WhatMeWorry 5 8,796 Feb 17, 2011 05:40 PM
Last Post: Holmes
  The tabbing on a word playing back the recorded voice itcreate 0 2,373 Jun 25, 2010 02:06 PM
Last Post: itcreate
  3D in Ortho bmantzey 5 3,792 Nov 20, 2008 02:34 AM
Last Post: DoG