Doja平台上的3D机器人程序
这个机器人程序实现了常见的3D基础操作:移动,旋转,光照,纹理,动画等等。说明:这个程序参考了j2medev网上的一篇文章,原文链接:
精通Micro3D v3基础技术
程序中所使用的资源文件也来自该文章附带的文件中。我只是将其移植到doja平台罢了,在此过程中学习3D知识。程序代码及效果图:
下载
飘飘白云
2006-01-26
import com.nttdocomo.ui.*;
import com.nttdocomo.opt.ui.j3d.*;
import javax.microedition.io.*;
//
/**
* @author ppby 2006-01-26
* Doja 3D examples.
*/
public class Robot_3D extends IApplication {
private Canvas3D canv;
public void start() {
try {
canv = new Canvas3D();
Display.setCurrent(canv);
Thread thread = new Thread(canv);
thread.start();
} catch (IllegalThreadStateException itse) {
itse.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Canvas class
*/
class Canvas3D extends Canvas implements Runnable {
Graphics g = null;
private String[] msg = {
"Move :up,down,left,right key.",
"Light :KEY_7,KEY_9.",
"Rotate:KEY_1,KEY_3.",
"Zoom :KEY_2,KEY_8.",
"Actions :KEY_5.",
};
// 3D affine transformation matrix
private AffineTrans viewT = new AffineTrans();
private AffineTrans viewBGT = new AffineTrans();
private AffineTrans affineT = new AffineTrans();
private AffineTrans tempTrans = new AffineTrans();
private int rotY; // Variable for rotation
private int nScale; // Variable for zoom/scale
private int nTransX,nTransY;// Variable for translation
private int nAmLight = 4096 * 7/8 ;//ambient light intensity
private Figure figureBG; //background figure
private Figure figure; //robot figure
private Texture texture; // texture for figures.
private ActionTable[] action = new ActionTable[2];
private int frame; //frame of action
private int actNo; //No. of action
public Canvas3D() {
//init data
g = this.getGraphics();
actNo = 0;
frame = 0;
try {
// Set a camera:(position,look at,up direction
//background view port
viewBGT.lookAt(
new Vector3D(0, 0, 1800),
new Vector3D(0, 0, 200),
new Vector3D(0, 4096, 0));
//robot view port
viewT.lookAt(
new Vector3D(0, 0, 800),
new Vector3D(0, 0, 200),
new Vector3D(0, 4096, 0));
//Init resource
figureBG = new Figure(Connector.openDataInputStream("resource:///test_model_haikei.mbac"));
figure = new Figure(Connector.openDataInputStream("resource:///test_model_robo.mbac"));
texture = new Texture(Connector.openDataInputStream("resource:///tex_001.bmp"), false);
action[0] = new ActionTable(Connector.openInputStream("resource:///action_01.mtra"));
action[1] = new ActionTable(Connector.openInputStream("resource:///action_02.mtra"));
figure.setTexture(texture);
figureBG.setTexture(texture);
} catch (Exception e) {
System.out.println("Init data failed");
e.printStackTrace();
}
}
public void run() {
while (true) {
update(); //update
render(); //draw
try {
Thread.sleep(100); // wait 100ms
} catch (InterruptedException ie) {
System.out.println("Run error~~");
}
}
}
public void update()
{
affineT.setIdentity();
//tranlate
tempTrans.setIdentity();
tempTrans.m03 += nTransX;
tempTrans.m13 += nTransY;
affineT.mul(tempTrans);
//rotate y axis
tempTrans.setIdentity();
tempTrans.setRotateY(rotY);
affineT.mul(tempTrans);
//zoom (scale)
tempTrans.setIdentity();
tempTrans.setElement(0, 0, tempTrans.m00 + nScale);
tempTrans.setElement(1, 1, tempTrans.m11 + nScale);
affineT.mul(tempTrans);
//action
frame += action[actNo].getMaxFrame(0)/10;
if( frame >= action[actNo].getMaxFrame(0) ){
frame = 0;
actNo = 0;
}
figure.setPosture(action[actNo], 0, frame);
}
public void render()
{
try {
g.lock();
// Background Clear
g.setColor(Graphics.getColorOfRGB(0, 0, 0));
g.fillRect(0, 0, getWidth(), getHeight());
Graphics3D g3d = (Graphics3D) g;
g3d.setClipRect3D(0, 0, getWidth(),getHeight());
g3d.setScreenCenter(getWidth()>>1, getHeight()>>1);
g3d.setPerspective(200, 10000, 4096*50/360);
//set light
g3d.setAmbientLight(nAmLight);
g3d.setDirectionLight(new Vector3D(1,1,0),4096/2);
g3d.enableLight(true);
//background figure
g3d.setViewTrans(viewBGT); // Set view coordinate matrix
g3d.renderFigure(figureBG);
g3d.drawFigure(figureBG);
//robot figure
affineT.mul(viewT,affineT);
g3d.setViewTrans(affineT); // //set viewport
g3d.renderFigure(figure);
g3d.drawFigure(figure);
g3d.flush();
g.setColor(Graphics.getColorOfRGB(0, 255, 0xff));
for(int i = 0;i < msg.length; i++){
g.drawString(msg[i], 5, (i + 1)*( 2 + Font.getDefaultFont().getHeight()));
}
g.unlock(true);
} catch (Exception e) {
System.out.println("Paint() err");
e.printStackTrace();
}
}
/**
* Key event process.
*/
public void processEvent(int type, int param) {
try {
if (type == Display.KEY_PRESSED_EVENT) {
switch (param) {
//translate
case Display.KEY_UP://move up
nTransY += 15;
break;
case Display.KEY_DOWN://move down
nTransY -= 15;
break;
case Display.KEY_LEFT://move left
nTransX -= 15;
break;
case Display.KEY_RIGHT://move right
nTransX += 15;
break;
//rotate
case Display.KEY_1://rotate left
rotY -= 128;
rotY %= 4096;
break;
case Display.KEY_3://rotate right
rotY += 128;
rotY %= 4096;
break;
//zoom/scale
case Display.KEY_8://zoom in
nScale -= 128;
break;
case Display.KEY_2://zoom out
nScale += 128;
break;
//change action
case Display.KEY_5:
actNo = 1;
frame = 0;
break;
//change light intensity
case Display.KEY_7://weak light
nAmLight -= 512;
nAmLight %= 4096;
nAmLight = (nAmLight<0? 1:nAmLight);
break;
case Display.KEY_9://strong light
nAmLight += 512;
nAmLight = (nAmLight>=4096 ? 4095:nAmLight);
nAmLight %= 4096;
break;
}
}
} catch (Exception e) {
System.out.println("Set attribute value err");
e.printStackTrace();
}
}
public void paint(Graphics _g) {
}
}