Sunday, December 26, 2010

Rendering Blender model in OpenGL ES 2.0 - porting OpenGL ES 1.1 (fixed) to 2.0 (programmable GLSL) pipeline

The code I was able to find in SIO2 and oolongengine (blender reader copied from gamekit) was rendering Blender models using fixed pipeline. I needed to get cartoonlike rendering, so decided to go with OpenGL ES 2.0 and GLSL.
Oolongengine (3Dlabs Inc) includes this OpenGL ES 2.0 example - rendering rotating teapot:
  

The shader on video is simple toon shader. Decided to use it when implementing this example.

Couldn't find an OpenGL ES 2.0 example that would render 3d model loaded from .blend or .pod file - found only examples with OpenGL ES 1.1 code. One of those OpenGL ES 1.1 examples, ReadBlend from oolongengine looks like this:


I'll try to explain the approach to combine them:



Code is available here.

Class Piper, with PiperGL11 and PiperGL20 subclasses is a simple work in progress, intended to abstract differences in fixed and rendering code to one place. Now, it includes push&pop&set&multMatrix operations.
e.g. in the code, it was just required to replace ES 1.1 call glMultMatrix() with call to Piper::instance()->multMatrix() and that would bring support for both OpenGL ES 1.1 and OpenGL ES 2.0.
Decided not to use pattern to first select mode with glMatrixMode() and then do matrix operation, as it looked cleaner to supply matrix mode for each operation. Of course, in PiperGL11 avoid redundant setting the same mode with setMatrixMode.

For OpenGL ES 2.0 version, it is needed to set the values for uniforms just before the call to glDrawElements. I guess it is important to optimize this, and handle the case when there were no change in uniforms between consecutive calls to Piper::instance()->glDrawElements. Then, setupChangedVariables should be able to handle this.

Plan to work on touch input and navigating through scene, maybe with camera behind the ball, check different versions of toon shaders I could find online,... figure out how to add some more color to scene - with color attribute array or textures and check if there is difference in performances...