32014, 13th Kandroid minmax - www.kandroid.org
OpenGL : What is OpenGL
– OpenGL (Open Graphics Library) is a cross-language, multi-platform application programming interface (
API) for rendering 2D and 3D vector graphics. The API is typically used to interact with a Graphics processin
g unit (GPU), to achieve hardware-accelerated rendering.
– OpenGL was developed by Silicon Graphics Inc. (SGI) from 1991 and released in January 1992 and is wid
ely used in CAD, virtual reality, scientific visualization, information visualization, flight simulation, and video g
ames. OpenGL is managed by the non-profit technology consortium Khronos Group.
• OpenGL ES
– OpenGL for Embedded Systems (OpenGL ES or GLES) is a subset of the OpenGL
– It is designed for embedded systems like smartphones, computer tablets, video game consoles and PDAs.
• OpenGL development
– In addition to the features required by the core API, GPU vendors may provide additional functionality in the f
orm of extensions. Extensions may introduce new functions and new constants, and may relax or remove re
strictions on existing OpenGL functions. Vendors can use extensions to expose custom APIs without needin
g support from other vendors or the Khronos Group as a whole, which greatly increases the flexibility of Ope
nGL. All extensions are collected in, and defined by, the OpenGL Registry.
42014, 13th Kandroid minmax - www.kandroid.org
OpenGL : OpenGL ES 2.0 Rendering Pipeline
• Fixed Function Pipeline removed at ES2.0
52014, 13th Kandroid minmax - www.kandroid.org
OpenGL : Shading and Shaders
– Shading refers to depicting depth perception in 3D models or illustrations by varying levels of
– In computer graphics, shading refers to the process of altering the color of an object/surface/
polygon in the 3D scene, based on its angle to lights and its distance from lights to create a p
hotorealistic effect. Shading is performed during the rendering process by a program called a
62014, 13th Kandroid minmax - www.kandroid.org
OpenGL : Transform and Lighting
surfaceColor = emissive + ambient + diffuse + specular
emissive = Ke
ambient = Ka x globalAmbient
diffuse = Kd x lightColor x max(N · L, 0)
specular = Ks x lightColor x facing x (max(N · H, 0)) s
82014, 13th Kandroid minmax - www.kandroid.org
OpenGL : What is GLSL
– OpenGL Shading Language (abbreviated: GLSL or GLslang), is a high-level shading langu
age based on the syntax of the C programming language. It was created by the OpenGL AR
B (OpenGL Architecture Review Board) to give developers more direct control of the graphic
s pipeline without having to use ARB assembly language or hardware-specific languages.
– Supports OpenGL ES(Android, iOS, and etc.)
92014, 13th Kandroid minmax - www.kandroid.org
OpenGL : GLSL – Simple Example
ftransform() is used for fixed function pipeline.
102014, 13th Kandroid minmax - www.kandroid.org
OpenGL : GLSL keywords – attribute, uniform, varying
• (Vertex) Attribute
– Vertex attributes are used to communicate from outside to the vertex shader.
• Unlike uniform variables, values are provided per vertex (and not globally for all vertices).
• There are built-in vertex attributes like the normal or the position, or you can specify your own vertex at
tribute like a tangent or another custom value.
• Attributes can't be defined in the fragment shader.
– Uniform variables are used to communicate with your vertex or fragment shader from "outsid
e". In your shader you use the uniform qualifier to declare the variable
• Uniform variables are read-only and have the same value among all processed vertices. You can only
change them within your C++ program.
– Varying variables provide an interface between Vertex and Fragment Shader.
• Vertex Shaders compute values per vertex and fragment shaders compute values per fragment.
• If you define a varying variable in a vertex shader, its value will be interpolated (perspective-correct) ov
er the primitive being rendered and you can access the interpolated value in the fragment shader.
232014, 13th Kandroid minmax - www.kandroid.org
EGL : OpenGL ARB and Khronos Group
• OpenGL ES
– OpenGL ES is a lightweight graphics API which is designed for Embedded System from Ope
nGL which is designed for Work Station.
– OpenGL is maintained by OpenGL ARB(Architecture Review Board), OpenGL ES is
maintained Khronos Group.
252014, 13th Kandroid minmax - www.kandroid.org
EGL : WGL
• OpenGL Native Interface for Windows
– WGL or Wiggle is an API between OpenGL and the windowing system interface of Microsoft
262014, 13th Kandroid minmax - www.kandroid.org
EGL : EGL Overview
• Native Platform Interface
– EGL™ is an interface between Khronos rendering APIs such as OpenGL ES or OpenVG and
the underlying native platform window system. It handles graphics context management, surf
ace/buffer binding, and rendering synchronization and enables high-performance, accelerate
d, mixed-mode 2D and 3D rendering using other Khronos APIs. EGL also provides interop ca
pability between Khronos to enable efficient transfer of data between APIs – for example bet
ween a video subsystem running OpenMAX AL and a GPU running OpenGL ES.
• Portable Layer for Graphics Resource Management
– EGL can be implemented on multiple operating systems (such as Android and Linux) and
native window systems (such as X and Microsoft Windows). Implementations may also
choose to allow rendering into specific types of EGL surfaces via other supported native
rendering APIs, such as Xlib or GDI. EGL provides:
• Mechanisms for creating rendering surfaces (windows, pbuffers, pixmaps) onto which client APIs can d
raw and share
• Methods to create and manage graphics contexts for client APIs
• Ways to synchronize drawing by client APIs as well as native platform rendering APIs
272014, 13th Kandroid minmax - www.kandroid.org
EGL : EGL Features
• Specifically EGL is a wrapper over the following subsystems;
– WGL: Windows GL-the Windows-OpenGL interface (pronounced wiggle)
– CGL: the Mac OS X-OpenGL interface (the AGL layer sits on top of CGL)
– GLX: the equivalent X11-OpenGL interface
• EGL not only provides a convenient binding between the operating system resources
and the OpenGL subsystem, but also provides the hooks to the operating system to i
nform it when you require something, such as;
1. Iterating, selecting, and initializing an OpenGL context.
2. This can be the OGL API level, software vs. hardware rendering, etc.
3. Requesting a surface or memory resource.
4. The OS services requests for system or video memory.
5. Iterating through the available surface formats (to pick an optimal one)
6. You can find out properties of the video card(s) from the OS – the surfaces presented will re
sides on the video card(s) or software renderer interface.
7. Selecting the desired surface format.
8. Informing the OS you are done rendering and it’s time to show the scene.
9. Informing the OS to use a different OpenGL context.
10. Informing the OS you are done with the resources.
282014, 13th Kandroid minmax - www.kandroid.org
EGL : EGLWindow, EGLDisplay, EGLSurface
– Abstract display on which graphics are drawn
– Single physical screen
– Initialize by querying a default display
– All EGL objects are associated with an EGLDisplay
• EGLContext(Rendering Contexts)
– EGLContext is a state machine defined by a client API
– EGLContext is associated with EGLSurfaces
• EGLSurface(Drawing Surfaces)
– Types: windows, pbuffers, pixmaps
• Windows, pixmaps: tied to native window system
– Created with EGLConfig
• Describes depth of color buffer component and types, quantities, and sizes of the ancillary
buffers(depth, multisample, stencil buffers)
– Ancillary buffers are associated with an EGLSurface
• Not EGLContext
292014, 13th Kandroid minmax - www.kandroid.org
EGL : EGL Operation(1/2)
• Interaction with native rendering
– Native rendering will always be supported by pixmap surfaces
• Pixmap surfaces have restricted capabilities and performance relative to window and pbuffer surfaces
– Native rendering will not be supported by pbuffer surfaces, since the color buffers of pbuffers are allocated internally
by EGL and are not accessible through any other means.
– Native rendering may be supported by window surfaces, but only if the native window system has a compatible
rendering model allowing it to share the back color buffer, or if single buffered rendering to the window surface is
– When both native rendering APIs and client APIs are drawing into the same underlying surface, no guarantees are
placed on the relative order of completion of operations in the different rendering streams other than those provided by
the synchronization primitives discussed in section 3.8
• Shared State
– OpenGL and OpenGL ES Texture Objects
• OpenGL and OpenGL ES makes no attempt to synchronize access to texture objects. If a texture object is bound to
more than one context, then it is up to the programmer to ensure that the contents of the object are not being
changed via one context while another context is using the texture object for rendering. The results of changing a
texture object while another context is using it are undefined.
– OpenGL and OpenGL ES Buffer Objects
• hen it is up to the programmer to ensure that the contents of the object are not being changed via one context while
another context is using the buffer object for rendering. The results of changing a buffer object while another context
is using it are undefined.
• Multiple Threads
– EGL guarantees sequentiality within a command stream for each of its client APIs , but not between these APIs and
native APIs which may also be rendering into the same surface.
– Client API commands are not guaranteed to be atomic.
• Synchronization is in the hands of the client.
• It can be maintained at moderate cost with the judicious use of commands such as glFinish, vgFinish, eglWait-Client,
and eglWaitNative, as well as (if they exist) synchronization commands present in native rendering APIs.
302014, 13th Kandroid minmax - www.kandroid.org
EGL : EGL Operation(2/2)
• Power Management
– Power management events can occur synchronously while an application is running. When
the system returns from the power management event the EGLContext will be invalidated,
and all subsequent client API calls will have no effect (as if no context is bound).
– Following a power management event, calls to eglSwapBuffers, eglCopy-Buffers, or
eglMakeCurrent will indicate failure by returning EGL_FALSE. The error EGL_CONTEXT_LOST
will be returned if a power management event has occurred.
• On detection of this error, the application must destroy all contexts (by calling eglDestroyContext for each
context). To continue rendering the application must recreate any contexts it requires, and subsequently restore
any client API state and objects it wishes to use.
• Any EGLSurfaces that the application has created need not be destroyed following a power management event,
but their contents will be invalid.
312014, 13th Kandroid minmax - www.kandroid.org
EGL : OpenGL Framebuffer
– A Framebuffer is a collection of buffers that can be used as the destination for rendering.
Color Buffer Double buffering, stereo buffering
Depth Buffer Shadow map, internal rendering
Stencil Buffer Shadow volume, color masking, reflection
Accum Buffer Motion blur, anti-aliasing
glEnable GL_DEPTH_TEST, GL_STENCIL_TEST
322014, 13th Kandroid minmax - www.kandroid.org
EGL : EGL Basic Usage
• The basic usage of EGL and similar API are the following;
1. (Android) Obtain the EGL interface.
• So you can make EGL calls
2. Obtain a display that’s associated with an app or physical display
3. Initialize the display
4. Configure the display
5. Create surfaces
• Front, back, offscreen buffers, etc.
6. Create a context associated with the display
• This holds the “state” for the OpenGL calls
7. Make the context “current”
• This selects the active state
8. Render with OpenGL (OpenGL not EGL calls, the OpenGL state is held by EGL context)
9. Flush or swap the buffers so EGL tells the OS to display the rendered scene. Repeat rend
ering till done.
10. Make the context “not current”
11. Clean up the EGL resources
332014, 13th Kandroid minmax - www.kandroid.org
EGL : EGL API(1/3)
Initializing Funtion Description
EGLBoolean eglInitialize(EGLDisplay dpy, EGLint
*major, EGLint *minor);
the values of *major and *minor would be 1 and 2, respectively).
major and minor are not updated if they are specified as NULL.
EGLBoolean eglTerminate(EGLDisplay dpy);
const char *eglQueryString(EGLDisplay dpy, EGLint
EGL_CLIENT_APIS, EGL_EXTENSIONS, EGL_VENDOR, or EGL_
Rendering to Textures Funtion Description
EGLBoolean eglBindTexImage(EGLDisplay dpy,
EGLSurface surface, EGLint buffer);
Bind OpenGL ES Texture
EGLBoolean eglReleaseTexImage(EGLDisplay dpy,
EGLSurface surface, EGLint buffer);
Release OpenGL ES Texture
Posting the color buffer Funtion Description
EGLBoolean eglSwapBuffers(EGLDisplay dpy,
Posting to a Window
When native window resizing, called automatically
EGLBoolean eglCopyBuffers(EGLDisplay dpy,
EGLSurface surface, EGLNativePixmapType
Copying to a Native Pixmap
EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint
Control swap buffer interval
EGL Extension Funtion Description
void (*eglGetProcAddress(const char *procname))(void); eglQueryString(dpy, EGL_EXTENSIONS)