Saturday, July 30, 2016

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.



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...