GLField

The GLField class enables you to use the OpenGL® ES library by including code that handles EGL™ setup. GLField also includes built-in support for refreshing the display at a specified target frame rate. It is a convenient choice for 3-D gaming and animation applications.

GLField is an abstract class. You must extend it to create a UI component that you can add to a screen. Your subclass must include a constructor and override layout(), initialize(), and render().

The constructor can call the parent class constructor by invoking super()and passing in the version of OpenGL ES that you want to use. In initialize(), you write code to perform setup tasks, such as setting the color to use to clear the screen. In layout(), you set the size of the field.

You can use setTargetFrameRate()to create a rendering thread that calls render() at the frame rate you specify. You do not have to override update(), but if you do, it is also called at the target frame rate. The rendering started by setTargetFrameRate() runs on the event thread that you create when you call enterEventDispatcher() You should set a target frame rate using setTargetFrameRate() instead of manually throttling rendering yourself. Thirty frames per second is generally the lower bound for smooth real-time graphics. Sixty frames per second is the upper limit because of the LCD refresh rate. A higher frame rate consumes more battery power. The frame rate can be set dynamically.

If you do not specify a target frame rate, it defaults to 0 and paint(), render() and update() are called from the standard UI event thread in response to invalidate(). Note that initialize() is called when the EGL context is lost and when the field is removed and added back to the screen.

Code sample: Minimal GLField implementation

The following code sample provides simple implementations for the required methods. You can create a MyGLField class and add it to a screen.

import net.rim.device.api.opengles.GLField;


public class MyGLField extends GLField
 {
     public MyGLField(int ver)
     {
         super(ver);
     }

     protected void layout(int width, int height)
     {
         // Set field dimensions
         setExtent(100, 100);
     }
     
     protected void initialize(GL gl)
     {
         GL20 gl20 = (GL20)gl;
         gl20.glClearColor(0, 0, 0, 1);
     }
     
     protected void render(GL gl)
     {
         GL20 gl20 = (GL20)gl;
         gl20.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
     }
 }

Previous topic: OpenGL ES

Was this information helpful? Send us your comments.