OpenGL ES
on Android
OpenGL ES
●
●
●
●
●
●
●
●

subset of OpenGL for Embedded Systems
royalty free
adopted by every major handset OS
version 1.x fixed function
version 2.x fully programmable
version 3.x builds on version 2
http://www.khronos.org/opengles/
http://en.wikipedia.org/wiki/OpenGL_ES
Android GLSurfaceView
●
●
●
●
●
●

built in to Android
manages OpenGL ES surfaces
draws into the Android view system
provides dedicated render thread
handles EGL interface with window system
supports continuous or on-demand
rendering
● makes getting started relatively easy
Android Renderer
● a custom implementation
○ GLSurfaceView.Renderer

● 3 interface methods called by system
○ onSurfaceCreated
○ onSurfaceChanged
○ onDrawFrame

● define model data in renderer
○ pass to OpenGL ES in onDrawFrame

● define matrices in renderer
Model Data
● OpenGL ES version 1
○ set points and colors with direct method calls
glBegin(GL_TRIANGLES);
glVertex3f(-0.5f, -0.25f, 0.0f);
glColor3f(1.0f, 0.0f, 0.0f);
...
glEnd();

● OpenGL ES version 2
○ define arrays and buffers to draw with
final float[] triangle1VerticesData = {
-0.5f, -0.25f, 0.0f, // X, Y, Z,
1.0f, 0.0f, 0.0f, 1.0f, // R, G, B, A
0.5f, -0.25f, 0.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.559016994f, 0.0f,
0.0f, 1.0f, 0.0f, 1.0f
};
Buffers
●
●
●
●

Android is written in Java
OpenGL ES is written in C
model data is passed via buffers
model data is cached somewhere
○ client memory: FloatBuffer
○ graphics memory: VBO, IBO
Matrices
● model matrix
○ places a drawing in the world

● view matrix
○ positions drawings relative to our eye

● projection matrix
○ projects the view onto the screen
○ enables 3 dimensional perspective
Shaders
● GLSL ES: http://www.khronos.org/opengles/sdk/docs/manglsl/
● model data passes through shaders
● vertex shaders
○ perform operations on each vertex
○ pass results to fragment shaders

● fragment shaders
○ use results of vertex shaders
○ applies additional operations per pixel
○ draws to screen by sending data to OpenGL ES
Shader Program
● links vertex output to fragment input
● provides mechanism to pass model data
○ App puts model data in buffers
○ OpenGL ES passes data from buffers to shaders

● used by OpenGL ES to execute drawing
● App can have multiple shader programs
Light
● version 2 requires us to write our own lighting
○ per-vertex
○ per-fragment

● ray tracing
○ very accurate and realistic but very expensive

● rasterization
○ very good approximation
○ fast enough for real time graphics on mobile devices
Light Types
● Ambient
○ pervades entire scene, no single source
○ indirect diffuse lighting

● Diffuse
○ light from a source bounces directly off an object
○ illumination of object depends on angle to source

● Specular
○ moves as we move relative to an object
○ perceived as "shininess"
Light Sources
● Directional
○ consistent strength from undefined location
○ consistent direction no matter where you are

● Point
○ strength fades from a center point
○ travels in all directions

● Spot
○ strength fades from a point
○ travels with attenuation and direction
Textures
● Coordination
○ OpenGL y-axis is flipped relative to images
■ more expensive: flip bitmap when loaded
■ less expensive: flip texture coordinates
○ texture coordinates are called texels
○ (s,t) instead of (x,y)

● Load image bitmap into application
● Supply shaders with texture data
● Shaders draw the texture
Texture Filtering
● minification & magnification
● mipmapping
● nearest neighbor
○ blocky when magnified

● bilinear
○ smoother when magnified

● trilinear
○ smoother when multiple levels are side by side
Blending
● effect of combining colors
● OpenGL provides several modes
○ additive
○ multiplicative
○ interpolative

● occurs in fragment shader
○ after calculating final color
○ normally overwrites previous value
○ blending… blends with previous value
Vertex Buffer Objects
● instead of transferring data every frame
○ from client memory to OpenGL

● transfer once and draw from graphics cache
● generate an OpenGL buffer
○ give it the data in the client buffer
○ free client memory
Index Buffer Objects
● VBO's can contain a lot of redundant data
○ specifically, redundant vertex data

● instead, define each vertex once only in IBO
● reference each vertex by index in IBO

OpenGL ES on Android

  • 1.
  • 2.
    OpenGL ES ● ● ● ● ● ● ● ● subset ofOpenGL for Embedded Systems royalty free adopted by every major handset OS version 1.x fixed function version 2.x fully programmable version 3.x builds on version 2 http://www.khronos.org/opengles/ http://en.wikipedia.org/wiki/OpenGL_ES
  • 3.
    Android GLSurfaceView ● ● ● ● ● ● built into Android manages OpenGL ES surfaces draws into the Android view system provides dedicated render thread handles EGL interface with window system supports continuous or on-demand rendering ● makes getting started relatively easy
  • 4.
    Android Renderer ● acustom implementation ○ GLSurfaceView.Renderer ● 3 interface methods called by system ○ onSurfaceCreated ○ onSurfaceChanged ○ onDrawFrame ● define model data in renderer ○ pass to OpenGL ES in onDrawFrame ● define matrices in renderer
  • 5.
    Model Data ● OpenGLES version 1 ○ set points and colors with direct method calls glBegin(GL_TRIANGLES); glVertex3f(-0.5f, -0.25f, 0.0f); glColor3f(1.0f, 0.0f, 0.0f); ... glEnd(); ● OpenGL ES version 2 ○ define arrays and buffers to draw with final float[] triangle1VerticesData = { -0.5f, -0.25f, 0.0f, // X, Y, Z, 1.0f, 0.0f, 0.0f, 1.0f, // R, G, B, A 0.5f, -0.25f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.559016994f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f };
  • 6.
    Buffers ● ● ● ● Android is writtenin Java OpenGL ES is written in C model data is passed via buffers model data is cached somewhere ○ client memory: FloatBuffer ○ graphics memory: VBO, IBO
  • 7.
    Matrices ● model matrix ○places a drawing in the world ● view matrix ○ positions drawings relative to our eye ● projection matrix ○ projects the view onto the screen ○ enables 3 dimensional perspective
  • 8.
    Shaders ● GLSL ES:http://www.khronos.org/opengles/sdk/docs/manglsl/ ● model data passes through shaders ● vertex shaders ○ perform operations on each vertex ○ pass results to fragment shaders ● fragment shaders ○ use results of vertex shaders ○ applies additional operations per pixel ○ draws to screen by sending data to OpenGL ES
  • 9.
    Shader Program ● linksvertex output to fragment input ● provides mechanism to pass model data ○ App puts model data in buffers ○ OpenGL ES passes data from buffers to shaders ● used by OpenGL ES to execute drawing ● App can have multiple shader programs
  • 10.
    Light ● version 2requires us to write our own lighting ○ per-vertex ○ per-fragment ● ray tracing ○ very accurate and realistic but very expensive ● rasterization ○ very good approximation ○ fast enough for real time graphics on mobile devices
  • 11.
    Light Types ● Ambient ○pervades entire scene, no single source ○ indirect diffuse lighting ● Diffuse ○ light from a source bounces directly off an object ○ illumination of object depends on angle to source ● Specular ○ moves as we move relative to an object ○ perceived as "shininess"
  • 12.
    Light Sources ● Directional ○consistent strength from undefined location ○ consistent direction no matter where you are ● Point ○ strength fades from a center point ○ travels in all directions ● Spot ○ strength fades from a point ○ travels with attenuation and direction
  • 13.
    Textures ● Coordination ○ OpenGLy-axis is flipped relative to images ■ more expensive: flip bitmap when loaded ■ less expensive: flip texture coordinates ○ texture coordinates are called texels ○ (s,t) instead of (x,y) ● Load image bitmap into application ● Supply shaders with texture data ● Shaders draw the texture
  • 14.
    Texture Filtering ● minification& magnification ● mipmapping ● nearest neighbor ○ blocky when magnified ● bilinear ○ smoother when magnified ● trilinear ○ smoother when multiple levels are side by side
  • 15.
    Blending ● effect ofcombining colors ● OpenGL provides several modes ○ additive ○ multiplicative ○ interpolative ● occurs in fragment shader ○ after calculating final color ○ normally overwrites previous value ○ blending… blends with previous value
  • 16.
    Vertex Buffer Objects ●instead of transferring data every frame ○ from client memory to OpenGL ● transfer once and draw from graphics cache ● generate an OpenGL buffer ○ give it the data in the client buffer ○ free client memory
  • 17.
    Index Buffer Objects ●VBO's can contain a lot of redundant data ○ specifically, redundant vertex data ● instead, define each vertex once only in IBO ● reference each vertex by index in IBO