Drawing your graphics

Now we're finally ready to start drawing beautiful OpenGL ES graphics! The first thing that we need to do is initialize OpenGL ES.

Initializing OpenGL ES

When working with OpenGL ES, some things won't change from frame to frame and you can set them up once at the beginning of the program. It's useful to keep all of that setup code in a single location. Because our goal is to get a empty window on the screen, we don't have much setup code. Here, in the initialize() function in main.c, we tell OpenGL ES to use a certain color any time it needs to clear the color buffer. The parameters are R,G,B, and A - where A stands for alpha and represents the transparency of the color.

glClearColor(0.0f, 0.8f, 1.0f, 1.0f);

Here, we've chosen a nice blue color.

Using OpenGL ES

Now that you have the framework in place, we hope it'll be easy to experiment with some of that content on your BlackBerry PlayBook. It's useful to organize your per-frame OpenGL ES code into a single method. We've called ours render().

Here we clear the color and depth buffers. The color buffer will be cleared using the clear color we set earlier.

void render()
{
    glClear(GL_COLOR_BUFFER_BIT);

}

OpenGL ES works by drawing to an off-screen buffer and then swapping that buffer to the screen buffer at periodic intervals. So, at the end of our per-frame code, we have to call bbutil_swap() to do that swap.

void render()
{
    glClear(GL_COLOR_BUFFER_BIT);

    bbutil_swap();

}

For the purposes of this tutorial, you can comment out or remove the remaining code in render().

Cleaning up your graphics objects

We did a lot of work to set up a native window and EGL. When we're done with the application and want to clean up, we have to be sure to do it properly. We start by disassociating our setup from our rendering thread in bbutil_terminate():

eglMakeCurrent(egl_disp, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);

Next, we destroy our EGLSurface and EGL context.

eglDestroySurface(egl_disp, egl_surf);
eglDestroyContext(egl_disp, egl_ctx);

Then we destroy the native window, terminate our EGLDisplay connection, and release all per-thread EGL state.

screen_destroy_window(screen_win);
eglTerminate(egl_disp);
eglReleaseThread();

Congratulations! You've now cleaned up all the work done to create your window and the application can exit cleanly.