Java Reference
In-Depth Information
Feeding M3G with Triangle Information
Examination of the code for TriangleCanvas will reveal how the triangle and rotation informa-
tion is fed to M3G for rendering. The code for the TriangleCanvas is shown in Listing 15-1.
Listing 15-1. The TriangleCanvas for Rotation of a Triangle in 3D
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.*;
import javax.microedition.m3g.*;
public class TriangleCanvas extends GameCanvas implements Runnable {
private boolean mRunning = false;
private Thread mPaintThrd = null;
private Graphics3D mGraphics3D = Graphics3D.getInstance();
private Camera mCamera = new Camera();;
private Light mLight = new Light();
private float mAngle = 0.0f;
private Transform mTransform = new Transform();
private Background mBackground = new Background();
private VertexBuffer mVertexBuffer;
private IndexBuffer mIndexBuffer;
private Appearance mAppearance = new Appearance();
private Material mMaterial = new Material();
public TriangleCanvas() {
super(true);
}
public void init() {
short[] vertices = { 0, 0, 0, 3, 0, 0, 0, 3, 0 };
VertexArray vertexArray = new VertexArray(vertices.length / 3, 3, 2);
vertexArray.set(0, vertices.length/3, vertices);
byte[] normals = { 0, 0, 127, 0, 0, 127, 0, 0, 127 };
VertexArray normalsArray = new VertexArray(normals.length / 3, 3, 1);
normalsArray.set(0, normals.length/3, normals);
VertexBuffer verbuf = mVertexBuffer = new VertexBuffer();
verbuf.setPositions(vertexArray, 1.0f, null);
verbuf.setNormals(normalsArray);
int[] stripLength = { 3 };
mIndexBuffer = new TriangleStripArray( 0, stripLength );
Search WWH ::




Custom Search