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

📄 m3gcanvas.java

📁 3D J2ME 代码集合
💻 JAVA
字号:
import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;import javax.microedition.m3g.*;class M3GCanvas extends GameCanvas implements Runnable{      private Graphics3D g3d; // Graphics object used to render the world.    private World world; // This world contains the camera and the pyramidMesh.    private Camera camera; // the camera in the scene    private Mesh pyramidMesh; // the pyramid in the scene        public M3GCanvas(){        super(false);                setFullScreenMode(true);                g3d = Graphics3D.getInstance();        world = new World();        camera = new Camera();        world.addChild(camera); // add the camera to the world.        float w = getWidth();        float h = getHeight();        // Constructs a perspective projection matrix and sets that as the current projection matrix.        camera.setPerspective(60.0f, w / h, 0.1f, 50f);                pyramidMesh = createpyramid(); // create our pyramid.        pyramidMesh.setTranslation(0.0f, 0.0f, -3.2f); // move the pyramid 3 units into the screen.        world.addChild(pyramidMesh); // add the pyramid to the world      //pyramidMesh.translate(0.0f, 0.0f, -3.2f);        world.setActiveCamera(camera);                Thread t = new Thread(this);        t.start();    }    public void draw3D(Graphics g){        try{            g3d.bindTarget(g); // Binds the given Graphics or mutable Image2D as the rendering target of this Graphics3D            g3d.render(world); // Render the world        }finally{            g3d.releaseTarget();        }    }    public void run() {        Graphics g = getGraphics();        while(true){            // rotate the pyramid 1 degree around the Y-axis.            pyramidMesh.postRotate(3.0f, 0.0f, 1.0f, 0.0f);            draw3D(g);            flushGraphics();        }    }    // this method creates a colored pyramid.    private Mesh createpyramid(){                // The vertices used by the pyramid. x, y, z        short []POINTS = new short[] {-1, -1, 1, // point 1                                      1, -1, 1, // point 2                                      1, -1, -1, // point 3                                     -1, -1, -1, // point 4                                     0, 1, 0}; // point 5, top                                                                                                                                // The points sequence.        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, //R                                    0, 127, 0,  //G                                    0, 0, 127, //B                                   127, 0, 127, //B                                   0, 127, 127};//B                                                                // The length of each sequence in the indices array.        int []LENGTH = new int[] {3, 3, 3, 3, 3, 3}; // the pyramid is built by six triangles                VertexArray POSITION_ARRAY, COLOR_ARRAY;        IndexBuffer INDEX_BUFFER;                // Create a VertexArray to be used by the VertexBuffer        POSITION_ARRAY = new VertexArray(POINTS.length / 3, 3, 2);        POSITION_ARRAY.set(0, POINTS.length / 3, POINTS);        COLOR_ARRAY = new VertexArray(COLORS.length / 3, 3, 1);        COLOR_ARRAY.set(0, COLORS.length / 3, COLORS);        INDEX_BUFFER = new TriangleStripArray(INDICES, LENGTH);                // VertexBuffer holds references to VertexArrays that contain the positions, colors, normals,         // and texture coordinates for a set of vertices        VertexBuffer vertexBuffer = new VertexBuffer();        vertexBuffer.setPositions(POSITION_ARRAY, 1.0f, null);        vertexBuffer.setColors(COLOR_ARRAY);        System.out.println(vertexBuffer.getVertexCount());                /* System.out.println(vertexBuffer.getDefaultColor());         vertexBuffer.setDefaultColor(0xFFFFFF);        System.out.println(vertexBuffer.getDefaultColor());        vertexBuffer.setDefaultColor(0x00FF0000);*/        //vertexBuffer.setDefaultColor(0xFF0000);                        // Create the 3D object defined as a polygonal surface        Mesh mesh = new Mesh(vertexBuffer, INDEX_BUFFER, null);                Appearance appearance = new Appearance(); // A set of component objects that define the rendering attributes of a Mesh        PolygonMode polygonMode = new PolygonMode(); // An Appearance component encapsulating polygon-level attributes        polygonMode.setPerspectiveCorrectionEnable(true);        polygonMode.setCulling(PolygonMode.CULL_NONE); // By using CULL_NONE all faces of the pyramid will be shown.        polygonMode.setShading(PolygonMode.SHADE_SMOOTH); // use a smooth shading of the colors on the pyramid.       // polygonMode.setPerspectiveCorrectionEnable(true);        polygonMode.setTwoSidedLightingEnable(false);        polygonMode.setLocalCameraLightingEnable(true);        appearance.setPolygonMode(polygonMode);                /*Fog fog = new Fog();        fog.setMode(Fog.EXPONENTIAL);        fog.setDensity(0.2f);        System.out.println(fog.getColor());        fog.setColor(0xB7B7B7);        appearance.setFog(fog);*/         Appearance appearance1 = new Appearance();         PolygonMode polygonMode1 = new PolygonMode(); // An Appearance component encapsulating polygon-level attributes        polygonMode1.setPerspectiveCorrectionEnable(true);        polygonMode1.setCulling(PolygonMode.CULL_NONE); // By using CULL_NONE all faces of the pyramid will be shown.        polygonMode1.setShading(PolygonMode.SHADE_FLAT); // use a smooth shading of the colors on the pyramid.       // polygonMode.setPerspectiveCorrectionEnable(true);        polygonMode1.setTwoSidedLightingEnable(false);        polygonMode1.setLocalCameraLightingEnable(true);         appearance1.setPolygonMode(polygonMode1);      /*  Fog fog = new Fog();        fog.setMode(Fog.LINEAR);        //fog.setDensity(0.2f);        fog.setLinear(0.8f,2f);        //fog.setColor(0xFFFF00);        fog.setColor(0xB7B7B7);        appearance.setFog(fog);*/        mesh.setAppearance(0, appearance1);//hou jia xian xian        mesh.setAppearance(0, appearance);                appearance1.setLayer(2);        appearance.setLayer(1);        System.out.println(mesh.getSubmeshCount());         System.out.println(appearance1.getLayer());        System.out.println(appearance.getLayer());        // Set the appearance to the 3D object        return mesh;    }    }

⌨️ 快捷键说明

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