m3gac
Class OpenGLSample

java.lang.Object
  extended by javax.microedition.lcdui.Displayable
      extended by javax.microedition.lcdui.Canvas
          extended by javax.microedition.lcdui.game.GameCanvas
              extended by m3gac.OpenGLSample
All Implemented Interfaces:
java.lang.Runnable

public class OpenGLSample
extends javax.microedition.lcdui.game.GameCanvas
implements java.lang.Runnable

Demonstrates 3D rendering using the Java Binding for the OpenGL ES API (JSR 239). This sample uses OpenGL ES 1.0 features with fixed-point variants of functions. To run this sample, the device must support a texture size of 256x256 pixels.

Author:
Claus Hoefele

Field Summary
private  int angle
          The cube's current rotation angle.
private  javax.microedition.khronos.egl.EGL10 egl
          Object for executing EGL operations.
private  javax.microedition.khronos.egl.EGLContext eglContext
          EGL context used for rendering.
private  javax.microedition.khronos.egl.EGLDisplay eglDisplay
          EGL Display used for rendering.
private  javax.microedition.khronos.egl.EGLSurface eglWindowSurface
          EGL surface used for rendering.
private static byte[] FACE_NORMALS
          Face normals.
private  javax.microedition.khronos.opengles.GL10 gl
          Object for executing OpenGL operations.
private  javax.microedition.lcdui.Graphics graphics
          2D graphics context for rendering.
private  boolean isRunning
          Flag whether thread is running.
private static byte[] STRIPS
          Triangle strip information.
private static byte[] VERTEX_POSITIONS
          Vertex positions.
private static byte[] VERTEX_TEX_COORDS
          Vertex texture coordinates (origin at the lower left corner).
 
Fields inherited from class javax.microedition.lcdui.game.GameCanvas
DOWN_PRESSED, FIRE_PRESSED, GAME_A_PRESSED, GAME_B_PRESSED, GAME_C_PRESSED, GAME_D_PRESSED, LEFT_PRESSED, RIGHT_PRESSED, UP_PRESSED
 
Fields inherited from class javax.microedition.lcdui.Canvas
DOWN, FIRE, GAME_A, GAME_B, GAME_C, GAME_D, KEY_NUM0, KEY_NUM1, KEY_NUM2, KEY_NUM3, KEY_NUM4, KEY_NUM5, KEY_NUM6, KEY_NUM7, KEY_NUM8, KEY_NUM9, KEY_POUND, KEY_STAR, LEFT, RIGHT, UP
 
Constructor Summary
OpenGLSample()
          Constructor.
 
Method Summary
 void destroy()
          Stops the animation.
 void init()
          Starts the animation.
private  void initCamera(float fovy, float aspectRatio, float near, float far)
          Initializes the camera with a perspective projection matrix.
private  void initCube()
          Initializes the cube's vertex data.
private  void initGL()
          Initializes the EGL, GL, and Graphics contexts.
private  void initLight()
          Creates a directional light.
private  void initTexture(java.lang.String resource)
          Loads an image from the given resource and uses it as texture.
private  void render(javax.microedition.lcdui.Graphics graphics)
          Renders the sample.
 void run()
          Animation loop.
private static int toX(float value)
          Converts a float to a fixed-point value (GLfixed).
private static int toX(int value)
          Converts an int to a fixed-point value (GLfixed).
 
Methods inherited from class javax.microedition.lcdui.game.GameCanvas
flushGraphics, flushGraphics, getGraphics, getKeyStates, paint
 
Methods inherited from class javax.microedition.lcdui.Canvas
getGameAction, getKeyCode, getKeyName, hasPointerEvents, hasPointerMotionEvents, hasRepeatEvents, hideNotify, isDoubleBuffered, keyPressed, keyReleased, keyRepeated, pointerDragged, pointerPressed, pointerReleased, repaint, repaint, serviceRepaints, setCommandListener, setFullScreenMode, showNotify, sizeChanged
 
Methods inherited from class javax.microedition.lcdui.Displayable
addCommand, getHeight, getTicker, getTitle, getWidth, isShown, removeCommand, setTicker, setTitle
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VERTEX_POSITIONS

private static final byte[] VERTEX_POSITIONS
Vertex positions.


VERTEX_TEX_COORDS

private static final byte[] VERTEX_TEX_COORDS
Vertex texture coordinates (origin at the lower left corner).


FACE_NORMALS

private static final byte[] FACE_NORMALS
Face normals.


STRIPS

private static final byte[] STRIPS
Triangle strip information.


angle

private int angle
The cube's current rotation angle.


isRunning

private boolean isRunning
Flag whether thread is running.


graphics

private javax.microedition.lcdui.Graphics graphics
2D graphics context for rendering.


egl

private javax.microedition.khronos.egl.EGL10 egl
Object for executing EGL operations.


gl

private javax.microedition.khronos.opengles.GL10 gl
Object for executing OpenGL operations.


eglDisplay

private javax.microedition.khronos.egl.EGLDisplay eglDisplay
EGL Display used for rendering.


eglContext

private javax.microedition.khronos.egl.EGLContext eglContext
EGL context used for rendering.


eglWindowSurface

private javax.microedition.khronos.egl.EGLSurface eglWindowSurface
EGL surface used for rendering.

Constructor Detail

OpenGLSample

public OpenGLSample()
Constructor.

Method Detail

init

public void init()
Starts the animation.


destroy

public void destroy()
Stops the animation.


run

public void run()
Animation loop.

Specified by:
run in interface java.lang.Runnable

initGL

private void initGL()
Initializes the EGL, GL, and Graphics contexts.


initCube

private void initCube()
Initializes the cube's vertex data. The vertex arrays created in this method are later used for rendering.


initLight

private void initLight()
Creates a directional light.


initTexture

private void initTexture(java.lang.String resource)
Loads an image from the given resource and uses it as texture. This method automatically adjusts the y axis so that the image's coordinate system has its origin at the lower left corner. The image's transparency channel is ignored.

Parameters:
resource - named resource.

initCamera

private void initCamera(float fovy,
                        float aspectRatio,
                        float near,
                        float far)
Initializes the camera with a perspective projection matrix.

Parameters:
fovy - field of view in the vertical direction, in degrees.
aspectRatio - the viewport's width divided by its height.
near - distance to the front clipping plane.
far - distance to the back clipping plane.

render

private void render(javax.microedition.lcdui.Graphics graphics)
Renders the sample.

Parameters:
graphics - graphics context for drawing.

toX

private static int toX(int value)
Converts an int to a fixed-point value (GLfixed).

Parameters:
value - conversion value.
Returns:
result.

toX

private static int toX(float value)
Converts a float to a fixed-point value (GLfixed).

Parameters:
value - conversion value.
Returns:
result.