micro3dcanvas.java
来自「3D手机游戏开发实例源代码 3D迷宫游戏的演示程序」· Java 代码 · 共 111 行
JAVA
111 行
import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;import com.mascotcapsule.micro3d.v3.*;public class Micro3DCanvas extends GameCanvas implements Runnable{ private Graphics3D g3d = new Graphics3D(); private FigureLayout layout; private Effect3D effect; private AffineTrans trans; private Texture texture = null; private AffineTrans rotationX; private AffineTrans rotationY; private AffineTrans rotationZ; private AffineTrans camera; private Command ExitCommand; private static final int COMMAND = Graphics3D.PRIMITVE_QUADS | Graphics3D.PDATA_COLOR_PER_FACE; private int[] POINTS = {10, 10, 10, -10, 10, 10, -10, -10, 10, 10,-10, 10, // front -10, 10,-10, 10, 10,-10, 10, -10,-10, -10,-10,-10, // back -10, 10, 10, -10, 10,-10, -10, -10,-10, -10,-10, 10, // left 10, 10,-10, 10, 10, 10, 10, -10, 10, 10,-10,-10, // right 10, 10,-10, -10, 10,-10, -10, 10,10, 10, 10, 10, // top 10,-10, 10, -10,-10, 10, -10,-10, -10, 10,-10,-10}; // bottom private int[] NORMALS = {0, 0, 4096}; private int []TEXTURES = {0,0}; private int []COLORS = {0xFFFF00, 0xFF0000, 0x00FFFF, 0x00FF00, 0x0000FF, 0xFF00FF}; public Micro3DCanvas(){ super(false); layout = new FigureLayout(); effect = new Effect3D(); trans = new AffineTrans(); camera = new AffineTrans(); rotationX = new AffineTrans(); rotationY = new AffineTrans(); rotationZ = new AffineTrans(); camera.setIdentity(); camera.lookAt( new Vector3D(0, 0, 64), new Vector3D(0, 0, -4096), new Vector3D(0, 4096, 0) ); effect.setShadingType(Effect3D.NORMAL_SHADING); trans.setIdentity(); trans.mul(camera); layout.setAffineTrans( trans ); layout.setPerspective(1, 4096, 512); layout.setCenter(getWidth()/2, getHeight()/2); Thread t = new Thread(this); t.start(); } public void draw3D(Graphics g){ try{ g3d.bind(g); g3d.renderPrimitives(null, 0, 0, layout, effect, COMMAND, 6, POINTS, NORMALS, TEXTURES, COLORS); g3d.flush(); }finally{ g3d.release( g ); } } public void run(){ Graphics g = getGraphics(); while(true){ g.setColor(0x000000); g.fillRect(0, 0, getWidth(), getHeight()); draw3D(g); flushGraphics(); // specify the rotation angle. rotationX.rotationX(56); rotationY.rotationY(32); rotationZ.rotationZ(48); trans.mul(rotationX); trans.mul(rotationY); trans.mul(rotationZ); try{ Thread.sleep(10); }catch(Exception e){ } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?