import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.m3g.Camera;
import javax.microedition.m3g.Graphics3D;
import javax.microedition.m3g.Light;
import javax.microedition.m3g.World;
import java.io.IOException;
import javax.microedition.m3g.Object3D;
import javax.microedition.m3g.Loader;
/**
*
* 绘制一个对象模型
* @author 吴佳峻
* @version 2011-1-27
*/
public class BeginCanvas extends Canvas implements Runnable {
World myWorld;// 世界
Camera cam;// 镜头
Graphics3D g3d;// 画笔
int currentTime = 0;// 当前帧
// camera rotation
float camRot = 0.0f;
double camSine = 0.0f;
double camCosine = 0.0f;
// Head bobbing
float headDeg = 0.0f;
// key array
boolean[] key = new boolean[5];
// key constants
public static final int FIRE = 0;
public static final int UP = FIRE + 1;
public static final int DOWN = UP + 1;
public static final int LEFT = DOWN + 1;
public static final int RIGHT = LEFT + 1;
public BeginCanvas(int fps) throws IOException {
loadWorld();
loadCamera();
g3d = Graphics3D.getInstance();
new Thread(this).start();
}
/**
* 加载世界
*/
public void loadWorld() {
Object3D[] objects;
try {
objects = Loader.load("/terrerist1.m3g");
for (int i = 0; i < objects.length; i++) {
if (objects[i] instanceof World) {
myWorld = (World) objects[i];
}
}
objects = null;
} catch (IOException e) {
e.printStackTrace();
System.out.println("Loading error!");
}
}
/**
* 加载镜头
*/
public void loadCamera() {
if (myWorld == null) {
return;
}
cam = myWorld.getActiveCamera();// get the active camera
Light l = new Light();// create a light
l.setMode(Light.AMBIENT);
l.setIntensity(3.0f);
myWorld.addChild(l);
}
/**
* 移动镜头
*/
public void moveCamera() {
if (key[LEFT]) {
camRot += 0.5f;
} else if (key[RIGHT]) {
camRot -= 0.5f;
}
// set rotation
cam.setOrientation(camRot, 0.0f, 1.0f, 0.0f);
// calculate trigonometry for camera movement
double rads = Math.toRadians(camRot);
camSine = Math.sin(rads);
camCosine = Math.cos(rads);
if (key[UP]) {
// move forword
cam.translate(-0.1f * (float) camSine, 0.0f, -0.1f
* (float) camCosine);
// bob head
headDeg += 0.5f;
// a simple way to "bob" the camera as the user moves
cam.translate(0.0f, (float) Math.sin(headDeg) / 40.f, 0.0f);
} else if (key[DOWN]) {
// move backWord
cam.translate(0.1f * (float) camSine, 0.0f,
0.1f * (float) camCosine);
// bob head
headDeg -= 0.5f;
// a simple way to "bob" the camera as the user moves
cam.translate(0.0f, (float) Math.sin(headDeg) / 40.f, 0.0f);
}
if (key[FIRE]) {
}
}
protected void paint(Graphics g) {
try {
moveCamera();
g3d.bindTarget(g);// 绑定目标
g3d.render(myWorld); // render a view from the active camera
} catch (Exception e) {
e.printStackTrace();
} finally {
g3d.releaseTarget(); // flush the rendered image
}
}
public void run() {
while (true) {
this.repaint();
}
}
protected void keyPressed(int keyCode) {
keyCode = Math.abs(keyCode);
switch(keyCode){
case 1:
key[UP] = true;
break;
case 2:
key[DOWN] = true;
break;
case 3:
key[LEFT] = true;
break;
case 4:
key[RIGHT] = true;
break;
}
}
protected void keyReleased(int keyCode) {
keyCode = Math.abs(keyCode);
switch(keyCode){
case 1:
key[UP] = false;
break;
case 2:
key[DOWN] = false;
break;
case 3:
key[LEFT] = false;
break;
case 4:
key[RIGHT] = false;
break;
}
}
}
效果如下: