IBM®
跳转到主要内容
    中国 [选择]    使用条款
 
 
Select a scope: Search for:    
    首页    产品    服务与解决方案     支持与下载    个性化服务    
跳转到主要内容

developerWorks 中国  >  Java technology | Open source  >

针对 Java 移动设备的 3D 图形,第 1 部分: M3G 的快速模式

使用 JSR 184 创建 3D 场景

developerWorks


返回文章


/*
 * 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 defined by eight vertices which are connected 
 * by triangles.
 *
 * @author Claus Hoefele
 */
public class VerticesSample 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
  };
  /** 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;
  /** 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);
    // 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});
    // 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,
        new Appearance(), null);
    _graphics3d.releaseTarget();
  }
  /**
   * 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 "Cube Vertices";
  }
}

返回文章


    关于 IBM 隐私条约 联系 IBM 使用条款