Doja平台3D基础示例
这个示例在前面示例的基础上进行了改进,实现了doja平台上3D常见的操作,比如偏移,缩放,旋转,光照等效果,不过由于没有使用纹理,光照效果不明显~~。 飘飘白云
import com.nttdocomo.ui.*;
import com.nttdocomo.opt.ui.j3d.*;
//
/**
* @author ppby 2006-01-25
* Doja3D foundation
*/
public class Doja3D 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 {
// Command list array
private int[] cmdListFunc_;
private int[] cmdListPrim_;
// 3D affine transformation matrix
private AffineTrans viewT = new AffineTrans();
private AffineTrans affineT = new AffineTrans();
private AffineTrans rotateT = new AffineTrans();
private AffineTrans scaleT = new AffineTrans();
private AffineTrans translateT = new AffineTrans();
private int rotX, rotY; // Variable for rotation
private int nScale; // Variable for scale
private int nTransX,nTransY;//Variable for translation
private Vector3D vLightDirection ;//direction of directing light
private boolean bAmbientLight = false;//enable ambient light flag
private int nLight; //intensity of ambient light
public Canvas3D() {
// Set a camera:(eye position,look at position,up direction)
viewT.lookAt(
new Vector3D(0, 0, 800),
new Vector3D(0, 0, 0),
new Vector3D(0, 4096, 0)
);
vLightDirection = new Vector3D(4096/4, 4096/4, 4096);//direction of Light
// Set a command list
setupCmdListFunc();
setupCmdListPrim();
}
public void run() {
while (true) {
UpdateAffineTrans();
repaint();
try {
Thread.sleep(100); // wait 100ms
} catch (InterruptedException ie) {
}
}
}
public void UpdateAffineTrans()
{
affineT.setIdentity();
/*
* Translate
*/
translateT.setIdentity();
translateT.m03 += nTransX;
translateT.m13 += nTransY;
affineT.mul(translateT);
/*
* Rotate
*/
//rotate y axis
rotateT.setIdentity();
rotateT.setRotateY(rotY);
affineT.mul(rotateT);
//rotate x axis
rotateT.setIdentity();
rotateT.setRotateX(rotX);
affineT.mul(rotateT,affineT);//same as affineT.mul(rotateT);
/*
* Scale:What will happen if scaleT.mXX < 0?~~
*/
scaleT.setIdentity();
scaleT.setElement(0, 0, scaleT.m00 + nScale);
scaleT.setElement(1, 1, scaleT.m11 + nScale);
//The follow two lines have the same effect.
//
scaleT.m00 += nScale;
//
scaleT.m11 += nScale;
affineT.mul(scaleT,affineT);
}
/**
* Rendering method.
*/
public void paint(Graphics g) {
if (g == null)
return;
try {
g.lock();
// Background Clear
g.setColor(Graphics.getColorOfRGB(0, 0, 0));
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Graphics.getColorOfRGB(150, 150, 120));
Graphics3D g3d = (Graphics3D) g;
//Method 1
g3d.setClipRect3D(0, 0, getWidth(),getHeight());
g3d.setScreenCenter(getWidth()>>1, getHeight()>>1);
g3d.setPerspective(200, 10000, 4096*50/360);
//Method 2:The effect is the same s method 1.
//g3d.executeCommandList(cmdListFunc_); // Excute the command list
//light: The lighting effect is not obvious in this program.
//If i use texture ,it maybe obviously to see lighting effect.
g3d.setDirectionLight(vLightDirection, nLight );
if( bAmbientLight )
g3d.setAmbientLight(nLight);
g3d.enableLight(true);
//set viewport
affineT.mul(viewT,affineT);
g3d.setViewTrans(affineT); // Set view coordinate matrix
setupCmdListPrim();
g3d.executeCommandList(cmdListPrim_); // Excute a command list
//g3d.flush();//It has been done when executeCommandList.
g.setColor(Graphics.getColorOfRGB(0, 255, 0xff));
g.drawString("Rotate:up,down,left,right key.", 5, Font.getDefaultFont().getHeight());
g.drawString("Scale :KEY_1,KEY_3", 5, 5+2*Font.getDefaultFont().getHeight());
g.drawString("Translate :KEY_4,6,8,2", 5, 10+3*Font.getDefaultFont().getHeight());
g.drawString("Light :KEY_0,7,9", 5, 15+4*Font.getDefaultFont().getHeight());
g.unlock(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Key event process.
*/
public void processEvent(int type, int param) {
try {
if (type == Display.KEY_PRESSED_EVENT) {
switch (param) {
//rotate
case Display.KEY_UP:
rotX -= 128;
rotX %= 4096;
break;
case Display.KEY_DOWN:
rotX += 128;
rotX %= 4096;
break;
case Display.KEY_LEFT:
rotY -= 128;
rotY %= 4096;
break;
case Display.KEY_RIGHT:
rotY += 128;
rotY %= 4096;
break;
//scale
case Display.KEY_1:
nScale += 128;
break;
case Display.KEY_3:
nScale -= 128;
break;
//translate
case Display.KEY_4:
nTransX -= 15;
break;
case Display.KEY_6:
nTransX += 15;
break;
case Display.KEY_8:
nTransY -= 15;
break;
case Display.KEY_2:
nTransY += 15;
break;
//light
case Display.KEY_0:
bAmbientLight = !bAmbientLight;
break;
case Display.KEY_7:
nLight -= 512;
nLight %= 4096;
nLight = (nLight<0?-nLight:nLight);
break;
case Display.KEY_9:
nLight += 512;
nLight %= 4096;
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Set the command list for methods.
* @return true - success. false - fail.
*/
private boolean setupCmdListFunc() {
try {
int[] cmdListFunc = {
Graphics3D.COMMAND_LIST_VERSION_1,
Graphics3D.COMMAND_CLIP_RECT, 0, 0, getWidth(),
getHeight(), Graphics3D.COMMAND_SCREEN_CENTER,
(getWidth() >> 1), (getHeight() >> 1),
Graphics3D.COMMAND_PERSPECTIVE1, 200, 10000,
4096 * 50 / 360, Graphics3D.COMMAND_END };
cmdListFunc_ = cmdListFunc;
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* Set the command list for primitives.
* @return true - Setting succeeded. false - Setting failed.
*/
private boolean setupCmdListPrim() {
try {
//draw four trangles.
int[] cmdListPrim = {
Graphics3D.COMMAND_LIST_VERSION_1,
// TRIANGLES
(Graphics3D.COMMAND_RENDER_TRIANGLES
| Graphics3D.COLOR_PER_FACE | (4 << 16)),//trangle NUM << 16
//each trangle's three vertexes.vertex:(x,y,z)
0, 120, 0, -120, -120, 120, 120, -120, 120,
0, 120, 0, 120, -120, 120, 120, -120, -120,
0, 120, 0, 120, -120, -120, -120, -120, -120,
0, 120, 0, -120, -120, -120, -120, -120, 120,
//each trangle's color
0xff << 16, //red
0xff << 8, //green
0xff << 4, //blue
0xff << 8 | 0xff << 16, //yellow
// flush
Graphics3D.COMMAND_FLUSH, Graphics3D.COMMAND_END };
cmdListPrim_ = cmdListPrim;
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}