Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Is it possible to mix 2D and 3D graphics in a single OpenGL ES 2.0 game, please?

I have plenty of 2D graphics in my game. The 2D graphics is represented by two triangular polygons (making up a rectangle) with texture on them. I use orthographic matrix to render the whole scene.

However, I need to add some 3D effects into my game. Threfore, I wish to use perspective camera to render the meshes.

Is it possible to mix orthographic and perspective camera in one scene? If yes, is there going to be a large performance cost for this? Is there any recommended approach to do this effectively? I wil have 90% of 2D graphics and only 10% of 3D.

Target platform is OpenGL ES 2.0 (iOS, Android). I use C++ to develop.

Thank you.

share|improve this question
    
Fixed the spurious openg tag for you. :) –  ChrisE Apr 29 '11 at 21:10
    
@ChrisE: thanks for that little adjustment. :-) –  Bunkai.Satori Apr 29 '11 at 21:19

2 Answers 2

up vote 8 down vote accepted

This is easily done.

Set the view transform to orthographic for the 2D stuff, and render it. Then, before clearing the framebuffer, draw the effects with a perspective projection. The projection will only effect the geometry drawn after it, so just set the desired mode prior to drawing.

This is the same way we handle HUDs in our FPS. :)

The only performance impact is that your are changing a uniform variable (your project matrix), right? So, just try to batch things to minimize state changes--same old stuff.

share|improve this answer
1  
+1 for good answer. I think, the question was clearly answerred, giving me solid information to start experimenting on my own. I am marking this as the Accepted Answer. –  Bunkai.Satori Apr 29 '11 at 21:21

There will only be a slight performance hit if you calculate the orthographic and perspective projection matrix each time you render to the frame buffer. I suggest caching them in a variable, calculating and storing them once in the initialization code, and then using glLoadMatrix to load the matrix. The rendering method would look like this in pseudo-code:

// Draw 3D things first
glMatrixMode(GL_PROJECTION);
glLoadMatrix(perspective_projection_matrix);
glMatrixMode(GL_MODELVIEW);
Draw3DObjects();
// Draw 2D things second
glMatrixMode(GL_PROJECTION);
glLoadMatrix(orthographics_projection_matrix);
glMatrixMode(GL_MODELVIEW);
Draw2DObjects();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.