Code sample: Drawing a texture on a GLField

The following code sample requires a 256-by-256 pixed .png file named bg.png in the top level of the project folder structure.

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.opengles.*;
import javax.microedition.khronos.opengles.*;
import javax.microedition.khronos.egl.*;

public final class GLFieldDemo extends UiApplication
{
    public GLFieldDemo()
    {
        pushScreen(new HomeScreen());
    }
    
    public static void main(String[] args)
    {
        GLFieldDemo app = new GLFieldDemo();
        app.enterEventDispatcher();
    }
}

class MyGLField extends GLField
{
     protected void layout(int width, int height)
     {
         setExtent(256, 256);
     }
     
     protected void initialize(GL gl)
     {
         GL11 gl11 = (GL11)gl;
         gl11.glClearColor(0, 0, 0, 1);
         
         int textureNames[] = new int[1];
         gl11.glGenTextures(1,textureNames,0);
         gl11.glBindTexture(GL11.GL_TEXTURE_2D, textureNames[0]);

         Bitmap img = Bitmap.getBitmapResource("bg.png");
         GLUtils.glTexImage2D(gl11,0,GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, img, null);
         gl11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
         gl11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
         
         gl11.glEnable(GL11.GL_TEXTURE_2D);
     }
     
     protected void render(GL gl)
     {
         GL11 gl11 = (GL11)gl;
         gl11.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

         ((GL11Ext)gl11).glDrawTexiOES(0, 0, 0, 256,256);
     }
     
     public MyGLField(int ver)
     {
         super(ver);
     }
 }
 
 
class HomeScreen extends MainScreen
 {
     public HomeScreen()
     {
         add(new MyGLField(GLField.VERSION_1_1));
         add(new LabelField("OpenGL ES field"));
     }
 }

Was this information helpful? Send us your comments.