⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 micro3dcanvas.java

📁 3D手机游戏开发实例源代码
💻 JAVA
字号:
import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;import com.mascotcapsule.micro3d.v3.*; class Micro3DCanvas extends GameCanvas implements Runnable{        private Graphics3D g3d;    private int COMMAND =   Graphics3D.PRIMITVE_POINT_SPRITES |                            Graphics3D.PDATA_POINT_SPRITE_PARAMS_PER_CMD;    private int BG_COMMAND =   Graphics3D.PRIMITVE_POINT_SPRITES |                            Graphics3D.PDATA_POINT_SPRITE_PARAMS_PER_CMD;        private int []POINT = {0, 0, 0};    private int []textureCoords = {40, 50, 0, 0, 0, 80, 100, Graphics3D.POINT_SPRITE_PERSPECTIVE};    private int []textureCoords2 = {128, 160, 0, 0, 0, 128, 160, Graphics3D.POINT_SPRITE_PERSPECTIVE};    //w0, h0, a0, x00, y00, x01, y01, f0,        private int [] NORMAL = new int[] {0, 0, 4096};    private int []colors = {0x00FFFFFF};        private Effect3D effect;    private AffineTrans trans;    private FigureLayout layout;    private Effect3D effect2;    private AffineTrans trans2;    private FigureLayout layout2;        private Texture ftTexture;    private Texture bgTexture;        private int index = 0;        public Micro3DCanvas(){        super(false);      //  setFullScreenMode(true);        g3d = new Graphics3D();        try{            ftTexture = new Texture("/ft.bmp", true);            bgTexture = new Texture("/bg.bmp", true);        }catch(Exception e){            System.err.println("Failed to load Texture!");        }        effect = new Effect3D();        effect.setShadingType(Effect3D.NORMAL_SHADING);        effect.setTransparency(true);        trans = new AffineTrans();        trans.setIdentity();        trans.lookAt(new Vector3D(0, 0, 128), new Vector3D(0, 0, -4096), new Vector3D(0, 4096, 0));                layout = new FigureLayout();        layout.setPerspective(1, 4096, 512);        layout.setCenter(getWidth()/2, getHeight()/2);        layout.setAffineTrans(trans);// -----------------------------------------        effect2 = new Effect3D();        effect2.setShadingType(Effect3D.NORMAL_SHADING);        trans2 = new AffineTrans();        trans2.setIdentity();        trans2.lookAt(new Vector3D(0, 0, 150), new Vector3D(0, 0, -4096), new Vector3D(0, 4096, 0));                layout2 = new FigureLayout();        layout2.setPerspective(1, 4096, 512);        layout2.setCenter(getWidth()/2, getHeight()/2);        layout2.setAffineTrans(trans2);        Thread t = new Thread(this);        t.start();    }    public void keyPressed(int keycode) {	if(getGameAction(keycode)==Canvas.FIRE) {index = index == 4 ? 0 : index + 1;if (index == 0) {COMMAND ^= Graphics3D.PATTR_BLEND_ADD;// 去掉前一种的混合效果COMMAND |= Graphics3D.PATTR_BLEND_NORMAL;// 正常模式} else if (index == 1) {COMMAND ^= Graphics3D.PATTR_BLEND_NORMAL;COMMAND |= Graphics3D.PATTR_COLORKEY;// 使用COLORKEY} else if (index == 2){COMMAND ^= Graphics3D.PATTR_COLORKEY;COMMAND |= Graphics3D.PATTR_BLEND_HALF;// 一半的混合模式} else if (index == 3){COMMAND ^= Graphics3D.PATTR_BLEND_HALF;COMMAND |= Graphics3D.PATTR_BLEND_SUB;// 相减的混合模式} else if (index == 4){COMMAND ^= Graphics3D.PATTR_BLEND_SUB;COMMAND |= Graphics3D.PATTR_BLEND_ADD;// 相加的混合模式}}}        private void draw3D(Graphics g){        g.setColor(0xFF00FF);        g.fillRect(0,0, getWidth(), getHeight());            g3d.bind(g);        try{            g3d.renderPrimitives(bgTexture, 0, 0, layout2, effect2, BG_COMMAND, 1, POINT, NORMAL, textureCoords2, colors);            g3d.renderPrimitives(ftTexture, 0, 0, layout, effect, COMMAND, 1, POINT, NORMAL, textureCoords, colors);            g3d.flush();        }catch(Exception e){            System.err.println(e);        }finally{            g3d.release(g);        }    }    public void run(){        Graphics g = getGraphics();        while(true){                        draw3D(g);            flushGraphics();            try{                Thread.sleep(20);            }catch(Exception e){}        }    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -