m3gcanvas.java

来自「3D手机游戏开发实例源代码」· Java 代码 · 共 175 行

JAVA
175
字号
import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;import javax.microedition.m3g.*;public class M3GCanvas extends GameCanvas implements Runnable {    private Graphics3D g3d ;    private World world;    private Mesh cube;    private Background bg;    private Camera camera;    private Light light;    private Node ground;    private KeyframeSequence seq;    private int iStartTime;    private long sSeed = 5555555;    private Graphics g;        public M3GCanvas(){        super(true);        world = new World();        cube = createCube();        bg = new Background();        bg.setColor(0xff200080);        world.setBackground(bg);        camera = new Camera();        camera.setPerspective(90.0f, 1.0f, 1.0f, 100.0f);        camera.setTranslation(0.0f, 0.0f, 30.0f);        world.addChild(camera);        world.setActiveCamera(camera);        light = new Light();        light.setTranslation(0.0f, 0.0f, 30.0f);        world.addChild(light);        ground = (Node)cube.duplicate();        ground.setTranslation(0.0f, -15.0f, 0.0f);        ground.setScale(2.0f, 0.1f, 2.0f);        world.addChild(ground);            int num = 5;            int duration = 3000;            int comps = 4;            cube.setScale(0.5f, 0.5f, 0.5f);            world.addChild(cube);   //      KeyframeSequence seq = new KeyframeSequence(num, comps, KeyframeSequence.LINEAR);              KeyframeSequence seq = new KeyframeSequence(num, comps, KeyframeSequence.SLERP);//          KeyframeSequence seq = new KeyframeSequence(num, comps, KeyframeSequence.SPLINE);    //        KeyframeSequence seq = new KeyframeSequence(num, comps, KeyframeSequence.SQUAD);   //       KeyframeSequence seq = new KeyframeSequence(num, comps, KeyframeSequence.STEP);//          seq.setRepeatMode(KeyframeSequence.CONSTANT);            seq.setRepeatMode(KeyframeSequence.LOOP);            seq.setDuration(duration);            for (int j = 0; j < num; j++)            {                float[] value = new float[comps];                for (int k = 0; k < comps; k++)                {                    value[k] = ((float)random() - 0.5f) * 30.0f;                }                seq.setKeyframe(j, (j * 2 + 1) * duration / (num * 2), value);            }            AnimationController ctrl = new AnimationController();      //    AnimationTrack track = new AnimationTrack(seq, AnimationTrack.TRANSLATION);            AnimationTrack track = new AnimationTrack(seq, AnimationTrack.ORIENTATION);            track.setController(ctrl);            cube.addAnimationTrack(track);            iStartTime = (int)System.currentTimeMillis();        Thread t = new Thread(this);        t.start();    }              /**     * 由thread执行的方法     */    public void run() {        Graphics g = getGraphics();        while(true) {            tick();            draw(g);            try {                Thread.sleep(200);             }            catch (InterruptedException e) {                            }        }    }    /**     * 每经过一定时间的处理     */    private void tick() {        world.animate((int) System.currentTimeMillis() - iStartTime);        world.align(null);    }    /**     * 描绘处理     */    protected void draw( Graphics g ) {        g.setColor( 255, 255, 255 );        g.fillRect( 0, 0, getWidth(), getHeight() );         g3d = Graphics3D.getInstance();        g3d.bindTarget(g);        try{            g3d.render(world);               }catch(Exception e){            System.err.println(e);        }finally{            g3d.releaseTarget();        }        flushGraphics();    }          public static Mesh createCube()    {        byte[] vert = { 10, 10, 10, -10, 10, 10, 10, -10, 10, -10, -10, 10,                -10, 10, -10, 10, 10, -10, -10, -10, -10, 10, -10, -10, -10,                10, 10, -10, 10, -10, -10, -10, 10, -10, -10, -10, 10, 10, -10,                10, 10, 10, 10, -10, -10, 10, -10, 10, 10, 10, -10, -10, 10,                -10, 10, 10, 10, -10, 10, 10, 10, -10, 10, -10, -10, 10, 10,                -10, -10, -10, -10, -10, };        VertexArray vertArray = new VertexArray(vert.length / 3, 3, 1);        vertArray.set(0, vert.length / 3, vert);        byte[] norm = { 0, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0, -127,                0, 0, -127, 0, 0, -127, 0, 0, -127, -127, 0, 0, -127, 0, 0,                -127, 0, 0, -127, 0, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0, 127,                0, 0, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0, -127, 0,                0, -127, 0, 0, -127, 0, 0, -127, 0, };        VertexArray normArray = new VertexArray(norm.length / 3, 3, 1);        normArray.set(0, norm.length / 3, norm);        int[] stripLen = { 4, 4, 4, 4, 4, 4 };                VertexBuffer vertexBuf = new VertexBuffer();        vertexBuf.setPositions(vertArray, 1.0f, null);        vertexBuf.setNormals(normArray);                IndexBuffer indexBuf = new TriangleStripArray(0, stripLen);                Appearance app = new Appearance();        app.setMaterial(new Material());                return new Mesh(vertexBuf, indexBuf, app);    }    private double random()    {        sSeed = (16807 * sSeed + 3) % 0xFFFFFFF;        return (double) sSeed / (double) 0xFFFFFFF;    }}

⌨️ 快捷键说明

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