?? fogdemo.java
字號:
/*
* FogDemo.java
*
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.m3g.*;
public class FogDemo extends MIDlet implements CommandListener{
private Command cmdExit;
private Display d;
private M3GCanvas m3gCanvas;
public FogDemo(){
d = Display.getDisplay(this);
cmdExit = new Command("退出", Command.EXIT, 0);
m3gCanvas = new M3GCanvas();
m3gCanvas.addCommand(cmdExit);
m3gCanvas.setCommandListener(this);
}
public void startApp() {
d.setCurrent(m3gCanvas);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d){
notifyDestroyed();
}
}
class M3GCanvas extends GameCanvas implements Runnable{
private Graphics3D g3d;
private World world;
private Camera camera;
private Mesh pyramidMesh;
private Fog fog;
public M3GCanvas(){
super(false);
g3d = Graphics3D.getInstance();
world = new World();
//創建并添加攝像機
camera = new Camera();
float w = getWidth();
float h = getHeight();
camera.setPerspective(60.0f, w/h, 0.1f, 50f);
world.addChild(camera);
world.setActiveCamera(camera);
//創建并添加金字塔對象
pyramidMesh = createpyramid();
pyramidMesh.setTranslation(0.0f, 0.0f, -3.5f);
world.addChild(pyramidMesh);
Thread t = new Thread(this);
t.start();
}
public void run() {
Graphics g = getGraphics();
while(true){
//調整fog濃度和顏色
int keyState = getKeyStates();
if ((keyState & LEFT_PRESSED) != 0) {
fog.setDensity(fog.getDensity()+0.01f);
System.out.println("當前濃度值為:" + fog.getDensity());
} else if ((keyState & RIGHT_PRESSED) != 0) {
float den = fog.getDensity()- 0.01f;
if (den <0) {
den = 0;
}
fog.setDensity(den);
System.out.println("當前濃度值為:" + fog.getDensity());
} else if ((keyState & UP_PRESSED) != 0) {
fog.setColor(fog.getColor()+100);
System.out.println("當前顏色值為:" + fog.getColor());
} else if ((keyState & DOWN_PRESSED) != 0) {
fog.setColor(fog.getColor()-100);
System.out.println("當前顏色值為:" + fog.getColor());
}
//繞y軸旋轉3度
pyramidMesh.postRotate(3.0f, 0.0f, 1.0f, 0.0f);
try{
g3d.bindTarget(g);
g3d.render(world);
}finally{
g3d.releaseTarget();
}
flushGraphics();
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
}
// this method creates a colored pyramid.
private Mesh createpyramid(){
//金字塔的頂點坐標
short []POINTS = new short[] {-1, -1, 1, //點0
1, -1, 1, //點1
1, -1, -1, //點2
-1, -1, -1, //點3
0, 1, 0 //點4(頂點)
};
//外部索引
int []INDICES = new int[] {
0, 1, 4, 1, 2, 4, 2, 3, 4, 3, 0, 4, 2, 1, 0, 2, 0, 3
};
//定義頂點的顏色
byte []COLORS = new byte[] {127, 0, 0, //紅色
0, 127, 0, //綠色
0, 0, 127, //藍色
127, 0, 127,
0, 127, 127
};
//金字塔由六個三角形組成(地面為兩個三角形)
//指定索引數組中每個順序的長度,下面兩個語句都可以
int[] stripLen = new int[] {3, 3, 3, 3, 4};
//int[] stripLen = new int[] {3, 3, 3, 3, 3, 3};
//創建VertexArray
//在創建VertexBuffer時要使用VertexArray
VertexArray vaVertex = new VertexArray(POINTS.length / 3, 3, 2);
vaVertex.set(0, POINTS.length / 3, POINTS);
VertexArray vaColors = new VertexArray(COLORS.length / 3, 3, 1);
vaColors.set(0, COLORS.length / 3, COLORS);
//以外部索引創建IndexBuffer
IndexBuffer ibPyramid = new TriangleStripArray(INDICES, stripLen);
//創建VertexBuffer對象
VertexBuffer vertexBuffer = new VertexBuffer();
vertexBuffer.setPositions(vaVertex, 1.0f, null); //設置頂點坐標
vertexBuffer.setColors(vaColors); //設置顏色
//創建一個Mesh對象
Mesh mesh = new Mesh(vertexBuffer, ibPyramid, null);
//創建fog對象
fog = new Fog();
fog.setColor(0x00103F0B);
//fog.setLinear(0.2f, 1.0f);
fog.setMode(Fog.EXPONENTIAL);
fog.setDensity(0.0f);
//創建外表Appearance對象
Appearance appearance = new Appearance();
appearance.setFog(fog);
mesh.setAppearance(0, appearance);
return mesh;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -