Tutorials on how to write shaders for the Android platform are a little difficult to come by on the Internet, so I decided to work on a small but simple program which hopefully provides a simple framework to get started writing programs for OpenGL ES 2.0.
I’ll first link to the apk, source code and the repository:
Source code here.
Google Code Repository here. NOTE: Branch is “default”
Here’s what my program does:
- Mesh Loading:The program loads triangular meshes in these formats (as .txt files):
- (.OFF): These consist of only vertex positions. My program will calculate per-vertex normals.
- (.OBJ): Vertex positions, normals and texture coordinates are provided. Sample textured cube mesh was exported from Blender.
- Gouraud Shading: Per-vertex lighting [with texture mapping if enabled].
- Phong Shading: Per-pixel lighting [with texture mapping if enabled].
- Normal Mapping: Gives a fake appearance of depth to a mesh. Sample normal map included.
NOTE: If your program is crashing with glError 1281 and you have a relatively new phone (a Samsung Galaxy S2 for example), change all dot() method calls in the shaders to dot2().
How it works:
The program will only work if your phone supports OpenGL ES 2.0. If it doesn’t my program quits without a warning.
When the program starts you will see an octahedron mesh with Gouraud shading enabled. There is a light rotating around the mesh. You can use one finger
to rotate the mesh, and two fingers (pinch-to-zoom) to scale the mesh (though it’s not *perfect* ). Clicking the menu button on your phone allows you to:
- Toggle Light Rotation: Light rotation can be toggled through this option. A short ‘Toast’ notification pops up.
- Shader switching: Can switch between Gouraud shading, phong shading and normal mapping.
- Mesh switching: Can switch between an Octahedron, Tetrahedron (both .off files) and Textured Cube Mesh (.obj file).
- Toggle Texturing: Can turn off texture mapping if there is an associated texture with the object (only the textured cube mesh).