📄 m3gcanvas.java
字号:
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;
public class M3GCanvas extends GameCanvas implements Runnable {
private Graphics3D g3d;
private Graphics g2d;
private Transform tempTransform;
//private World world;
private VertexBuffer vertexBuffer;
private IndexBuffer indexBuffer;
private Appearance appearance;
private Mesh mesh;
private Background bg;
private Camera camera;
private Light light;
private Texture2D texImg;
public M3GCanvas() {
super(false);
setFullScreenMode(false);
// TODO Auto-generated constructor stub
try{
createScene();
}catch(Exception e){
System.out.println("failed to create scene");
}
Thread t = new Thread(this);
t.start();
}
public void run() {
// TODO Auto-generated method stub
g2d = getGraphics();
while(true){
try{
g3d.bindTarget(g2d, true, 0);
g3d.clear(bg);
tempTransform.postRotate(3.0f, 0.0f, 1.0f, 0.0f);
g3d.render(mesh, tempTransform);
}finally{
g3d.releaseTarget();
}
flushGraphics();
}
}
private void makeGeometry(){
short []POINTS = new short[]{
-1,-1, 1, 1,-1, 1, 0, 1, 0,
1,-1, 1, 1,-1,-1, 0, 1, 0,
1,-1,-1, -1,-1,-1, 0, 1, 0,
-1,-1,-1, -1,-1, 1, 0, 1, 0,
-1,-1, 1, 1,-1, 1, 1,-1,-1,
-1,-1, 1, 1,-1,-1, -1,-1,-1,
};
short []TEXTURES = new short[]{
0,255, 255,255, 127, 0,
0,255, 255,255, 127, 0,
0,255, 255,255, 127, 0,
0,255, 255,255, 127, 0,
0, 0, 255, 0, 255,255,
0, 0, 255,255, 0,255,
};
byte []COLORS = new byte[]{
0,(byte)255,0,0,0,(byte)255,(byte)255,0,0,
0,0,(byte)255,(byte)255,0,(byte)255,(byte)255,0,0,
(byte)255,0,(byte)255,0,(byte)255,(byte)255,(byte)255,0,0,
0,(byte)255,(byte)255,0,(byte)255,0,(byte)255,0,0,
0,(byte)255,0,0,0,(byte)255,(byte)255,0,(byte)255,
0,(byte)255,0,(byte)255,0,(byte)255,0,(byte)255,(byte)255,
};
byte []NORMALS = {
0,(byte)447,(byte)894,0,(byte)447,(byte)894,0,(byte)447,(byte)894,
(byte)894,(byte)447,0,(byte)894,(byte)447,0,(byte)894,(byte)447,0,
0,(byte)447,(byte)-894,0,(byte)447,(byte)-894,0,(byte)447,(byte)-894,
(byte)-894,(byte)447,0,(byte)-894,(byte)447,0,(byte)-894,(byte)447,0,
0,(byte)-127,0,0,(byte)-127,0,0,(byte)-127,0,
0,(byte)-127,0,0,(byte)-127,0,0,(byte)-127,0,
};
int []INDICES = new int[]{
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17
};
int []LENGTH = new int[]{
3,3,3,3,3,3
};
VertexArray POSITION_ARRAY = new VertexArray(POINTS.length/3,3,2);
POSITION_ARRAY.set(0, POINTS.length/3, POINTS);
VertexArray COLOR_ARRAY = new VertexArray(COLORS.length/3,3,1);
COLOR_ARRAY.set(0, COLORS.length/3, COLORS);
VertexArray NORMAL_ARRAY = new VertexArray(NORMALS.length/3,3,1);
NORMAL_ARRAY.set(0, NORMALS.length/3, NORMALS);
VertexArray TEXCOORD_ARRAY = new VertexArray(TEXTURES.length/2,2,2);
TEXCOORD_ARRAY.set(0, TEXTURES.length/2, TEXTURES);
indexBuffer = new TriangleStripArray(INDICES,LENGTH);
vertexBuffer = new VertexBuffer();
vertexBuffer.setPositions(POSITION_ARRAY, 1.0f, null);
vertexBuffer.setColors(COLOR_ARRAY);
vertexBuffer.setNormals(NORMAL_ARRAY);
vertexBuffer.setTexCoords(0, TEXCOORD_ARRAY, 1/255.0f, null);
mesh = new Mesh(vertexBuffer,indexBuffer,null);
}
private Texture2D createBrickTexture(String path){
Texture2D texture = null;
try{
Image texImg = Image.createImage(path);
texture = new Texture2D(new Image2D(Image2D.RGB, texImg));
texture.setWrapping(Texture2D.WRAP_REPEAT, Texture2D.WRAP_REPEAT);
texture.setBlending(Texture2D.FUNC_DECAL);
texture.setFiltering(Texture2D.FILTER_BASE_LEVEL, Texture2D.FILTER_NEAREST);
}catch(Exception e){
}
return texture;
}
private void makeAppearance()throws Exception{
appearance = new Appearance();
CompositingMode compositionmode = new CompositingMode();
compositionmode.setBlending(CompositingMode.ALPHA);
compositionmode.setAlphaWriteEnable(true);
compositionmode.setColorWriteEnable(true);
compositionmode.setDepthWriteEnable(true);
compositionmode.setDepthTestEnable(true);
appearance.setCompositingMode(compositionmode);
texImg = createBrickTexture("/texture1.png");
// Texture2D first_texture = new Texture2D(new Image2D(Image2D.RGB,texImg));
// first_texture.setWrapping(Texture2D.WRAP_REPEAT,Texture2D.WRAP_REPEAT);
// first_texture.setBlending(Texture2D.FUNC_MODULATE);
// first_texture.setFiltering(Texture2D.FILTER_BASE_LEVEL, Texture2D.FILTER_NEAREST);
// appearance.setTexture(0, first_texture);
// texImg = createBrickTexture("/texture2.png");
// Texture2D second_texture = new Texture2D(new Image2D(Image2D.RGBA,texImg));
// second_texture.setWrapping(Texture2D.WRAP_CLAMP,Texture2D.WRAP_CLAMP);
// second_texture.setBlending(Texture2D.FUNC_DECAL);
// second_texture.setFiltering(Texture2D.FILTER_LINEAR, Texture2D.FILTER_LINEAR);
// appearance.setTexture(1, second_texture);
appearance.setTexture(0, texImg);
Material material = new Material();
material.setColor(Material.AMBIENT, 0x00000000);
material.setColor(Material.EMISSIVE, 0x00000000);
material.setColor(Material.DIFFUSE, 0xffffffff);
material.setColor(Material.SPECULAR, 0xff000000);
material.setShininess(1.0f);
appearance.setMaterial(material);
PolygonMode polygonMode = new PolygonMode();
polygonMode.setPerspectiveCorrectionEnable(true);
polygonMode.setWinding(PolygonMode.WINDING_CCW);
polygonMode.setCulling(PolygonMode.CULL_BACK);
polygonMode.setShading(PolygonMode.SHADE_SMOOTH);
appearance.setPolygonMode(polygonMode);
Fog fog = new Fog();
fog.setMode(Fog.EXPONENTIAL);
fog.setDensity(0.005f);
fog.setColor(0xffff00);
appearance.setFog(fog);
mesh.setAppearance(0, appearance);
}
private void createScene()throws Exception{
g3d = Graphics3D.getInstance();
camera = new Camera();
float aspect = (float)getWidth()/(float)getHeight();
camera.setPerspective(45.0f, aspect, 1.0f, 1000.0f);
tempTransform = new Transform();
tempTransform.postTranslate(0, 0, 9.0f);
g3d.setCamera(camera, tempTransform);
light = new Light();
light.setMode(Light.OMNI);
light.setColor(0xff0000);
tempTransform.setIdentity();
tempTransform.postTranslate(0, 0, 20.0f);
g3d.addLight(light, tempTransform);
tempTransform.setIdentity();
makeGeometry();
makeAppearance();
bg = new Background();
bg.setColor(0x000000);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -