Skip to main content

3D graphics for Java mobile devices, Part 1: M3G's immediate mode

Create 3D scenes with JSR 184


Return to article


/*
 * Sample code for M3G article on IBM developerWorks.
 * http://www.ibm.com/developerworks/
 */

package m3gsamples1;

import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;

/**
 * Sample displaying a cube with per-vertex colors.
 *
 * @author Claus Hoefele
 */
public class VertexColorsSample extends Canvas implements Sample
{
  /** The cube"s vertex positions (x, y, z). */
  private static final byte[] VERTEX_POSITIONS = {
    -1, -1,  1,    1, -1,  1,   -1,  1,  1,    1,  1,  1,
    -1, -1, -1,    1, -1, -1,   -1,  1, -1,    1,  1, -1
  };

  /** The cube"s vertex colors (R, G, B). */
  private static final byte[] VERTEX_COLORS = {
    0, (byte) 255, 0,             0, (byte) 255, (byte) 255,
    (byte) 255, 0, 0,             (byte) 255, 0, (byte) 255,
    (byte) 255, (byte) 255, 0,    (byte) 255, (byte) 255, (byte) 255,
    0, 0, (byte) 128,             0, 0, (byte) 255,
  };

  /** Indices that define how to connect the vertices to build 
   * triangles. */
  private static int[] TRIANGLE_INDICES = {
    0, 1, 2, 3, 7, 1, 5, 4, 7, 6, 2, 4, 0, 1
  };

  /** The cube"s vertex data. */
  private VertexBuffer _cubeVertexData;

  /** The cube"s triangles defined as triangle strips. */
  private TriangleStripArray _cubeTriangles;

  /** The cube"s appearance. */
  private Appearance _cubeAppearance;

  /** Polygon mode component of appearance that encapsulates per-face
   * attributes. */
  private PolygonMode _polygonMode;

  /** Graphics singleton used for rendering. */
  private Graphics3D _graphics3d;

  /**
   * Called when this sample is displayed.
   */
  public void showNotify()
  {
    init();
  }

  /**
   * Initializes the sample.
   */
  protected void init()
  {
    // Get the singleton for 3D rendering.
    _graphics3d = Graphics3D.getInstance();

    // Create vertex data.
    _cubeVertexData = new VertexBuffer();

    VertexArray vertexPositions =
        new VertexArray(VERTEX_POSITIONS.length/3, 3, 1);
    vertexPositions.set(0, VERTEX_POSITIONS.length/3, VERTEX_POSITIONS);
    _cubeVertexData.setPositions(vertexPositions, 1.0f, null);

    VertexArray vertexColors =
        new VertexArray(VERTEX_COLORS.length/3, 3, 1);
    vertexColors.set(0, VERTEX_COLORS.length/3, VERTEX_COLORS);
    _cubeVertexData.setColors(vertexColors);

    // Create the triangles that define the cube; the indices point to
    // vertices in VERTEX_POSITIONS.
    _cubeTriangles = new TriangleStripArray(TRIANGLE_INDICES,
        new int[] {TRIANGLE_INDICES.length});

    // Define an appearance object and set the polygon mode. The 
    // default values are: SHADE_SMOOTH, CULL_BACK, and WINDING_CCW.
    _cubeAppearance = new Appearance();
    _polygonMode = new PolygonMode();
    _cubeAppearance.setPolygonMode(_polygonMode);

    // Create a camera with perspective projection.
    Camera camera = new Camera();
    float aspect = (float) getWidth() / (float) getHeight();
    camera.setPerspective(30.0f, aspect, 1.0f, 1000.0f);
    Transform cameraTransform = new Transform();
    cameraTransform.postTranslate(0.0f, 0.0f, 10.0f);
    _graphics3d.setCamera(camera, cameraTransform);
  }

  /**
   * Renders the sample on the screen.
   *
   * @param graphics the graphics object to draw on.
   */
  protected void paint(Graphics graphics)
  {
    _graphics3d.bindTarget(graphics);
    _graphics3d.clear(null);
    _graphics3d.render(_cubeVertexData, _cubeTriangles,
        _cubeAppearance, null);
    _graphics3d.releaseTarget();

    drawMenu(graphics);
  }

  /**
   * Draws a menu on the screen.
   *
   * @param graphics graphics context.
   */
  protected void drawMenu(Graphics graphics)
  {
    graphics.setColor(0xFFFFFFFF);
    int fontHeight = graphics.getFont().getHeight();

    // Upper half menu.
    int height = 0;
    graphics.drawString(getKeyName(getKeyCode(FIRE)) +
        ": Reset", 0, height, 0);

    // Lower half menu.
    height = getHeight() - fontHeight;
    if (_polygonMode.getWinding() == PolygonMode.WINDING_CCW)
    {
      graphics.drawString(getKeyName(getKeyCode(GAME_C)) +
          ": Winding: CCW", 0, height, 0);
    }
    else
    {
      graphics.drawString(getKeyName(getKeyCode(GAME_C)) +
          ": Winding: CW", 0, height, 0);
    }

    height -= fontHeight;
    if (_polygonMode.getCulling() == PolygonMode.CULL_BACK)
    {
      graphics.drawString(getKeyName(getKeyCode(GAME_B)) +
          ": Culling: back", 0, height, 0);
    }
    else
    {
      graphics.drawString(getKeyName(getKeyCode(GAME_B)) +
          ": Culling: front", 0, height, 0);
    }

    height -= fontHeight;
    if (_polygonMode.getShading() == PolygonMode.SHADE_FLAT)
    {
      graphics.drawString(getKeyName(getKeyCode(GAME_A)) +
          ": Shading: flat", 0, height, 0);
    }
    else
    {
      graphics.drawString(getKeyName(getKeyCode(GAME_A)) +
          ": Shading: smooth", 0, height, 0);
    }
  }

  /**
   * Handles key presses.
   *
   * @param keyCode key code.
   */
  protected void keyPressed(int keyCode)
  {
    switch (getGameAction(keyCode))
    {
      case FIRE:
        init();
        break;

      case GAME_A:
        if (_polygonMode.getShading() == PolygonMode.SHADE_FLAT)
        {
          _polygonMode.setShading(PolygonMode.SHADE_SMOOTH);
        }
        else
        {
          _polygonMode.setShading(PolygonMode.SHADE_FLAT);
        }
        break;

      case GAME_B:
        if (_polygonMode.getCulling() == PolygonMode.CULL_BACK)
        {
          _polygonMode.setCulling(PolygonMode.CULL_FRONT);
        }
        else
        {
          _polygonMode.setCulling(PolygonMode.CULL_BACK);
        }
        break;

      case GAME_C:
        if (_polygonMode.getWinding() == PolygonMode.WINDING_CCW)
        {
          _polygonMode.setWinding(PolygonMode.WINDING_CW);
        }
        else
        {
          _polygonMode.setWinding(PolygonMode.WINDING_CCW);
        }

        break;

      // no default
    }

    repaint();
  }

  /**
   * Returns the <code>Displayable</code> used to display this sample.
   *
   * @return display
   */
  public Displayable getDisplayable()
  {
    return this;
  }

  /**
   * Returns the display name of this sample.
   *
   * @return name
   */
  public String getName()
  {
    return "Vertex Colors";
  }
}

Return to article