• Matrix - A NxN array of values
• Vertex - A point in space which act as the basic
building block for shapes e.g. line, triangle, etc.
• Mesh - A structure compromised of triangles
• Shader - Small program running on the GPU in
Background - OpenGL
• An abstract graphics API - That is it
• Written in C but not limited to it
• Bindings for most programming languages
• Low level ⬌ Close to hardware
Better control of when things are rendered - Possible to
make speciﬁc optimizations
Rendering of ambiguous 3d meshes
Custom effects, that can only be achieved with a
Why not RenderScript ?
It is platform speciﬁc which limits its usage, etc.
My preference: OpenGL + OpenCL
Android Wrapping of OpenGL
• Platform independent abstract API for setting up an
• Query graphics card for context capabilities
• GLSurfaceView wraps EGL
OpenGL ES Versions
OpenGL ES 1.x - Fixed pipeline
OpenGL ES 3.x- Extensions and Optimizations
OpenGL ES 2.x - Shader support
OpenGL in Android
- A view, set-up for rendering with OpenGL
- Runs in its own thread
- OpenGL context can be lost onPause()
- Where the actual rendering with OpenGL happens
GLSurfaceView.Renderer - Quick Look
• onSurfaceCreated() - The OpenGL context is now created
and we can start loading our resources
• onSurfaceChanged() - Called when the size has changed e.g.
• onDrawFrame() - Where the fun happens!
• Never mind the parameter GL10, for GLES20+
GLSL - OpenGL Shading Language
• A C-style language for writing OpenGL shaders
• OpenGL ES 2.0 has two types of shaders
- Vertex shader
- Fragment shader
• Vertex shader - A program that runs per vertex
• Fragment shader - A program that runs per fragment
• Uniform - A value that is the same for all vertices and/or pixels
• Attribute - A value deﬁned per vertex, only available in a vertex
• Varying - Data shared between shaders down the pipeline and
since they don’t map 1:1 they are also interpolated
Simple Fragment Shader
precision highp float; // ES Specific
varying vec3 color;
float alpha = 1.0;
gl_FragColor = vec4(color, alpha);
Calculations are done per fragment
We have a battery!
Running @60 FPS for no reason drains the battery
General Tips & Tricks
• Limited memory bandwidth - Offload memory bus with computations in shader
• If-statements - In shader is bad for parallelism, both branches are usually
• Method calls - In draw-loop may impact performance. Use direct access to data
instead. Bad for encapsulation, good for performance
• Memory allocation - In draw loop should be avoided. Allocate everything on
beforehand or on-demand
• Power of 2 textures - Good practice to use. Non-power-of-two textures has some
limitations in OpenGL ES 2.0
Sample Project Demo
Droidcon attendees notice
I was unable to host the requested shaders for the earth demo. I might post a
blogpost about it in the near future so follow me on google plus or the Jayway
blog. You are feel to ask me questions about it though.
Finally, Thanks for your questions and interest in my presentation.
Feel free to share any feedback to help me improve my presentations.