Android & OpenGL ES 2+
from an app developers point of view
Droidcon Berlin, 2014
Andreas Nilsson
Software specialist @Jayway
!
Broad interest in graphics, both 2D and 3D
!
www.jayway.com/blog
Inspiration
AndreasEarth
BACKUP PLAN(ET)
Terminology
• Matrix - A NxN array of values
• Vertex - A point in space which act as the basic
building block for shapes ...
Background - OpenGL
• An abstract graphics API - That is it
• Written in C but not limited to it
• Bindings for most progr...
Better control of when things are rendered - Possible to
make specific optimizations
Why OpenGL?
Control
Control
Rendering of ambiguous 3d meshes
Models
Why OpenGL?
Control ModelsEffects
Why OpenGL?
Custom effects, that can only be achieved with a
shader
Why not RenderScript	?
It is platform specific which limits its usage, etc.

My preference: OpenGL + OpenCL
Android Wrapping of OpenGL
Model
float[…]
FloatBuffer
Drawing
OpenGL
GLES20
Abstract
Android
Native
Context
GLSurfaceView
...
EGL
• Platform independent abstract API for setting up an
OpenGL context/surface
• Query graphics card for context capabil...
OpenGL ES Versions
OpenGL ES 1.x - Fixed pipeline
OpenGL ES 3.x- Extensions and Optimizations
OpenGL ES 2.x - Shader suppo...
OpenGL in Android
GLSurfaceView
GLSurfaceView.Renderer
- A view, set-up for rendering with OpenGL
- Runs in its own thread...
GLSurfaceView.Renderer - Quick Look
• onSurfaceCreated() - The OpenGL context is now created
and we can start loading our ...
1. Create an OpenGL Context
2. Initial OpenGL Setup

-clear color, cull mode, etc
3. Load Resources

-textures, models, sh...
GLSL ES
The OpenGL Shading Language
GLSL - OpenGL Shading Language
• A C-style language for writing OpenGL shaders
• OpenGL ES 2.0 has two types of shaders

-...
Rendering Pipeline
5.929688
4.125000
0.000000
5.832031
4.494141
0.000000
5.945313
0.000000
6.429688
4.125000
0.000000
5.38...
GLSL Qualifiers
• Uniform - A value that is the same for all vertices and/or pixels
• Attribute - A value defined per vertex...
Simple Vertex Shader
attribute vec4 a_position;

attribute vec3 a_color;

uniform mat4 mvp;

varying vec3 color;

void mai...
Simple Fragment Shader
precision highp float; // ES Specific

varying vec3 color;

void main()

{

float alpha = 1.0;

gl_...
We have a battery! 

Running @60 FPS for no reason drains the battery

Solution: RENDERMODE_WHEN_DIRTY
General Tips & Tric...
Sample Project Demo
github.com/andreasnilsson/OGLHelloWorld
Example Project:
Questions?
plus.google.com/+AndreasNilsson1985
se.linkedin.com/pub/andreas-nilsson/14/685/966
Droidcon attendees notice

I...
Upcoming SlideShare
Loading in...5
×

Android open gl2_droidcon_2014

231

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
231
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Android open gl2_droidcon_2014

  1. 1. Android & OpenGL ES 2+ from an app developers point of view Droidcon Berlin, 2014
  2. 2. Andreas Nilsson Software specialist @Jayway ! Broad interest in graphics, both 2D and 3D ! www.jayway.com/blog
  3. 3. Inspiration AndreasEarth BACKUP PLAN(ET)
  4. 4. Terminology • 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 parallell.
  5. 5. 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
  6. 6. Better control of when things are rendered - Possible to make specific optimizations Why OpenGL? Control
  7. 7. Control Rendering of ambiguous 3d meshes Models Why OpenGL?
  8. 8. Control ModelsEffects Why OpenGL? Custom effects, that can only be achieved with a shader
  9. 9. Why not RenderScript ? It is platform specific which limits its usage, etc.
 My preference: OpenGL + OpenCL
  10. 10. Android Wrapping of OpenGL Model float[…] FloatBuffer Drawing OpenGL GLES20 Abstract Android Native Context GLSurfaceView EGL
  11. 11. EGL • Platform independent abstract API for setting up an OpenGL context/surface • Query graphics card for context capabilities • GLSurfaceView wraps EGL
  12. 12. OpenGL ES Versions OpenGL ES 1.x - Fixed pipeline OpenGL ES 3.x- Extensions and Optimizations OpenGL ES 2.x - Shader support OpenGL OpenGL ES ⇎ ⇕
  13. 13. OpenGL in Android GLSurfaceView GLSurfaceView.Renderer - 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
  14. 14. 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. device rotation • onDrawFrame() - Where the fun happens! • Never mind the parameter GL10, for GLES20+
  15. 15. 1. Create an OpenGL Context 2. Initial OpenGL Setup
 -clear color, cull mode, etc 3. Load Resources
 -textures, models, shaders, etc 4. Compile and link shaders 5. Upload Resources to GPU 6. Clear Screen 7. Draw Draw Loop Rendering: Step-By-Step
  16. 16. GLSL ES The OpenGL Shading Language
  17. 17. 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
  18. 18. Rendering Pipeline 5.929688 4.125000 0.000000 5.832031 4.494141 0.000000 5.945313 0.000000 6.429688 4.125000 0.000000 5.387188 4.125000 2.747500 5.297100 4.494141 … x.xxxxxx ][ Vertex Shader Fragment Shader Simplified!
  19. 19. GLSL Qualifiers • Uniform - A value that is the same for all vertices and/or pixels • Attribute - A value defined per vertex, only available in a vertex shader • Varying - Data shared between shaders down the pipeline and since they don’t map 1:1 they are also interpolated
  20. 20. Simple Vertex Shader attribute vec4 a_position;
 attribute vec3 a_color;
 uniform mat4 mvp;
 varying vec3 color;
 void main()
 {
 color = a_color
 gl_Position = mvp * a_position;
 } Calculations are done per vertex
  21. 21. Simple Fragment Shader precision highp float; // ES Specific
 varying vec3 color;
 void main()
 {
 float alpha = 1.0;
 gl_FragColor = vec4(color, alpha);
 } Calculations are done per fragment
  22. 22. We have a battery! 
 Running @60 FPS for no reason drains the battery
 Solution: RENDERMODE_WHEN_DIRTY 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 evaluated • 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
  23. 23. Sample Project Demo github.com/andreasnilsson/OGLHelloWorld Example Project:
  24. 24. Questions? plus.google.com/+AndreasNilsson1985 se.linkedin.com/pub/andreas-nilsson/14/685/966 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.

  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×