Channels ▼
RSS

Mobile

OpenGL & Mobile Devices

Source Code Accompanies This Article. Download It Now.


Using the EGL Interface

The EGL API was designed to be a platform-independent way to interface OpenGL ES with the underlying window or display system. A single evening was all I needed to get an OpenGL ES buffer up and ready for rendering. Dave Astle and Dave Durnil's book OpenGL ES Game Development (PTR, 2004) was invaluable, covering many aspects of OpenGL ES game development, and NVidia provided the headers and libraries:

#include <gl/egl.h>
#include <gl/egltypes.h>
#include <gl/gl.h>

for bringing in the EGL interface and OpenGL ES functions.

The EGL interface works much like desktop binding interfaces, in that you need handles to the display contexts, arrays of attribute flags for your buffer, and so on. Here are the variable declarations needed for these settings:

EGLDisplay hDisplay = NULL;
EGLint  attrs[3] = { EGL_DEPTH_SIZE, 16, EGL_NONE };
EGLint  nConfigs;
EGLConfig *pConfigs;
EGLSurface hSurface;
EGLContext hContext;

Listing One is the skeleton code for initialization when the window is created, and the cleanup code for when the window is destroyed. The SetupRC() and ShutdownRC() functions are my own for regular one-time OpenGL setup and cleanup, such as loading and freeing textures, and the like. Also included are the WM_PAINT and WM_SIZE message handlers. These handle the standard tasks of setting up the projection and rendering. The paint handler renders, does the buffer swap, and then invalidates the window, creating a looping behavior useful for animation on Windows' event-driven architecture. (The complete code for this project is available at http://www.ddj.com/code/.)

Listing One

case WM_CREATE:
    {
    hDisplay = eglGetDisplay(EGL_NO_DISPLAY);
    eglInitialize(hDisplay, NULL, NULL);
    eglChooseConfig(
        hDisplay, attrs, NULL, 0, &nConfigs);
    pConfigs = (EGLConfig *)malloc(nConfigs * sizeof(EGLConfig));
    eglChooseConfig(hDisplay, attrs, pConfigs, nConfigs, &nConfigs);
    hSurface = e
        glCreateWindowSurface(hDisplay, *pConfigs, hWnd, NULL);hContext = 
            eglCreateContext(hDisplay, *pConfigs, 0, 0);
    eglMakeCurrent
        (hDisplay, hSurface, hSurface, hContext);
    SetupRC();
    }
case WM_DESTROY:
    ShutdownRC();
    eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, 
         EGL_NO_SURFACE, EGL_NO_CONTEXT);
    eglDestroyContext(hDisplay, hContext);
    eglDestroySurface(hDisplay, hSurface);
    eglTerminate(hDisplay);
    free(pConfigs);
    PostQuitMessage(0);
    break;
case WM_SIZE:
         {
    int iWidth = LOWORD(lParam);
    int iHeight = HIWORD(lParam);
    ChangeSize(iWidth, iHeight);
    }
    break;
case WM_PAINT:
    {
    Render();
    eglSwapBuffers(hDisplay, hSurface);
    InvalidateRect(hWnd, NULL, FALSE);
    }
    break;


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video