views:

51

answers:

2

We have a mostly 2D game that runs in orthographic mode, but one part shows a 3d model that is rendered in between the other 2D objects. How can I switch to perspective mode, render that model, then switch back to render the other objects in orthographic mode?

Kudos if you can show how it's done in OpenGL ES.

+2  A: 

I think this isn't exactly specified question. Are you want more views? Or you want to have 2D background, 3D game objects, 2D gui. If you want this, then:

  • render fullscreen background
  • set viewport to position=obj.pos-obj.size/2, size=obj.size, render object
  • render 2D gui

Or you want something else?

EDIT:

Here's little code:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,w,0,h,near,far);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(pos.x,...);

DrawQuads();

//if you want to keep your previus matrix
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(90,width/(float)height,0.001,1000);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(pos.x,...);
glRotate(-45.0f,1,0,0);
glTranslate(0,0,distance from object);
glRotate(90.0f,1,0,0);
// or use gluLookAt
// 0,0,1 - if you want have z as up in view
// 0,1,0 - for y
//gluLookAt(pos.x,pos.y,pos.z,cam.x,cam.y,cam.z,0,0,1);

glScale(object.width/model.width,...);
DrawModel();

// Restore old ortho
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
Miro
I'm rendering several quads in orthographic mode in the background (sprites) after this I need to render a 3d model in perspective mode, then after that i need to render some more sprites in the foreground.It's not just a full screen background. Does that make sense?
Sam Washburn
I've edited it, will this help you?
Miro
Thanks for taking the time to make the code example. I didn't realize you could push a projection matrix. Also the rotate/translate code was helpful.
Sam Washburn
+2  A: 

Well, "just do it"

  • set your projection matrix as ortho
  • set your modelview for 2D objects
  • render your 2D objects
  • set your projection matrix as projection
  • set your modelview for 3D objects
  • render your 3D objects

... and this can go on again an again

  • and swap buffers.

If you KNOW the order of your objects as you seem to do, you can also clear the z-buffer between each render.

Calvin1602