OpenGL ES 2.0: Mixing 2D with 3D

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.

• Fixed the spurious openg tag for you. :) Apr 29 '11 at 21:10

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.

• +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. 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);
glMatrixMode(GL_MODELVIEW);
Draw3DObjects();
// Draw 2D things second
glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);
Draw2DObjects();

• There is no glMatrixMode or glLoadMatrix in OpenGL ES. All CPU based matrix routines have been removed. You can do the equivalent but you would have to roll it yourself. Jul 19 '16 at 6:11