📄 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 + -