
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.m3g.*;
public class My3DClass extends GameCanvas implements Runnable {
private Graphics3D v3DInstance = Graphics3D.getInstance();
private Appearance vAppearance;
private Material vMaterial;
private VertexBuffer vVBuffer;
private IndexBuffer vIBuffer;
private Camera vCamera;
private Background vBackground;
private Transform vTransform;
private Light vLight;
private float vAngle;
boolean vRunning;
My3DClass()
{
//call the parent
super(true);
//initiate the variables
vAppearance = new Appearance();
vMaterial = new Material();
vVBuffer = new VertexBuffer();
vCamera = new Camera();
vBackground = new Background();
vLight = new Light();
vTransform = new Transform();
vRunning = false;
}
public void init()
{
//set the vertices
vAngle=0.0f;
short vertices[]={
3, 1, 3, -3, 1, 3, 3,-1, 3, -3,-1, 3, // front
-3, 1,-3, 3, 1,-3, -3,-1,-3, 3,-1,-3, // back
-3, 1, 3, -3, 1,-3, -3,-1, 3, -3,-1,-3, // left
3, 1,-3, 3, 1, 3, 3,-1,-3, 3,-1, 3, // right
3, 1,-3, -3, 1,-3, 3, 1, 3, -3, 1, 3, // top
3,-1, 3, -3,-1, 3, 3,-1,-3, -3,-1,-3};
VertexArray v_array = new VertexArray(vertices.length/3,3,2);
v_array.set(0,vertices.length/3,vertices);
byte[] normals = {
0, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0, 127,
0, 0,-127, 0, 0,-127, 0, 0,-127, 0, 0,-127,
-127, 0, 0, -127, 0, 0, -127, 0, 0, -127, 0, 0,
127, 0, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0,
0, 127, 0, 0, 127, 0, 0, 127, 0, 0, 127, 0,
0,-127, 0, 0,-127, 0, 0,-127, 0, 0,-127, 0 };
VertexArray n_array = new VertexArray(normals.length/3, 3, 1);
n_array.set(0,normals.length/3,normals);
//now create the vertex buffer that will hold the triangle
vVBuffer.setPositions(v_array, 1.0f, null);
vVBuffer.setNormals(n_array);
//specify the index buffer and the type of rendering mode for the triangles
int[] stripLength ={ 4, 4, 4, 4, 4, 4 }; //{36};
vIBuffer=new TriangleStripArray(0,stripLength);
//create the material drawn on top of the triangle
vMaterial.setColor(Material.DIFFUSE,0xff0000);
vMaterial.setColor(Material.SPECULAR,0xff0000);
vMaterial.setShininess(30.0f);
//set the mat to the appearance class
vAppearance.setMaterial(vMaterial);
//set the background color
vBackground.setColor(0x00ee88);
//set the camera
float fov = (float)getWidth()/(float)getHeight();
vCamera.setPerspective(60.0f,fov,1.0f,300.0f);
//set the light
//vLight.setColor(0xffffff);
//vLight.setIntensity(1.25f);
}
public void start()
{
vRunning=true;
init();
Thread new_thread = new Thread(this);
new_thread.start();
}
public void stop()
{
vRunning=false;
}
public void run()
{
Graphics g = getGraphics();
while(vRunning)
{
if(isShown())
{
v3DInstance.bindTarget(g);
v3DInstance.clear(vBackground);
vTransform.setIdentity();
vTransform.postTranslate(0.0f,5.0f,20.0f);
v3DInstance.setCamera(vCamera,vTransform);
v3DInstance.resetLights();
v3DInstance.addLight(vLight, vTransform);
vAngle+=1.0f;
vTransform.setIdentity();
vTransform.postRotate(vAngle,0.0f,1.0f,0.0f);
v3DInstance.render(vVBuffer,vIBuffer,vAppearance,vTransform);
System.out.println("Render called");
v3DInstance.releaseTarget();
flushGraphics();
}
}
}
}
Comments
i ran ur code as it is on the nokia emulator 2.0 using nokia sdk, but it crashes every time and says emulator has stopped working..
please help me..