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

📄 spotlightdemocanvas.java

📁 3D J2ME 代码集合
💻 JAVA
字号:
import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;import javax.microedition.m3g.*;class SpotLightDemoCanvas 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    private Mesh icosahedronMesh; // the icosahedron in the scene    private Light light;    private int meshIndex = 0;        private javax.microedition.midlet.MIDlet midlet;    private final int SCREEN_WIDTH;    private final int SCREEN_HEIGHT;        public SpotLightDemoCanvas(javax.microedition.midlet.MIDlet m){        super(false);                midlet = m;             setFullScreenMode(true);        SCREEN_WIDTH = getWidth();        SCREEN_HEIGHT = getHeight();                g3d = Graphics3D.getInstance();        world = new World();        camera = new Camera();        world.addChild(camera); // add the camera to the world.        light = new Light();                    // Create a new light        light.translate(0.0f, 0.0f, -1.0f); // The light position        light.setMode(Light.DIRECTIONAL);   // Light Mode        light.setColor(0xFFFFFF);           // The color of the light 'WHITE'        light.setSpotExponent(10.0f); // splot light concentration 0 to 128        light.setSpotAngle(30.0f);        world.addChild(light);                // Add the light to the world to be rendered.        // Constructs a perspective projection matrix and sets that as the current projection matrix.        camera.setPerspective(60.0f, (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT, 0.1f, 50f);                pyramidMesh = createpyramid(); // create our pyramid.        pyramidMesh.setTranslation(0.0f, 0.3f, -3.0f); // move the pyramid 3 units into the screen.                world.addChild(pyramidMesh);        world.setActiveCamera(camera);                Thread t = new Thread(this);        t.start();    }        public void keyPressed(int keyCode){        switch(keyCode){        }    }        /*     * Change the Light Mode to be used.     */            public void draw3D(Graphics g){        g.setColor(0x000000);        g.fillRect(0, 0, getWidth(), getHeight());        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();        }    }    private void draw2D(Graphics g){        g.setColor(0xFFFFFF);        /*switch(lightIndex){            case 0:                g.drawString("Ambient", 0, 0, 0);                break;            case 1:                g.drawString("Directional", 0, 0, 0);                break;            case 2:                g.drawString("Omni", 0, 0, 0);                break;            case 3:                g.drawString("Spot", 0, 0, 0);                break;        }*/               // g.drawImage(SwapImage, 0, SCREEN_HEIGHT, Graphics.LEFT | Graphics.BOTTOM);       // g.drawImage(NextImage, SCREEN_WIDTH, SCREEN_HEIGHT, Graphics.RIGHT | Graphics.BOTTOM);    }        public void run() {        Graphics g = getGraphics();        while(true){                  /*  switch(meshIndex){            case 0:                icosahedronMesh.postRotate(1.0f, 0.0f, 1.0f, 0.0f);                break;            case 1:                // rotate the pyramid 1 degree around the Y-axis.                pyramidMesh.postRotate(1.0f, 0.0f, 1.0f, 0.0f);                break;        }*/        pyramidMesh.postRotate(1.0f, 0.0f, 1.0f, 0.0f);       light.postRotate(1.0f, 1.0f, 0.0f, 0.0f); //enable this to make the light rotate around it's x-axis                    draw3D(g);            draw2D(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,    1, -1, 1,   0, 1, 0,     // front                                                           1, -1, 1,    1, -1, -1,  0, 1, 0,    // right                                                           1, -1, -1,  -1, -1, -1,  0, 1, 0,    // back                                                          -1, -1, -1,  -1, -1, 1,   0, 1, 0,    // left                                                          -1, -1, 1,    1, -1, 1,   1, -1, -1,  // bottom right                                                          -1, -1, 1,    1, -1, -1, -1, -1, -1}; // bottom left                                                                 // The wall angle of the pyramid is 70?                short []NORMALS = new short[] { 0, 56,113,        0, 56, 113,     0, 56, 113,                                                              113, 56,0,        113, 56,0,     113, 56,0,                                                                                                                          0, 56,-113,        0, 56, -113,     0, 56, -113,                                                                                                                          -113, 56,0,        -113, 56,0,     -113, 56,0,                                                                                                                          0, -127,0,        0, -127,0,     0, -127,0,                                                                                                                          0, -127,0,        0, -127,0,     0, -127,0};                                                                      // The points sequence.        int []INDICES = new int[] {0, 1, 2, // front                                   3, 4, 5,  // right                                   6, 7, 8,  // back                                   9, 10, 11, // left                                   12, 13, 14, // bottomright                                   15, 16, 17}; // bottomleft                                                                // 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, NORMAL_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);        NORMAL_ARRAY = new VertexArray(NORMALS.length / 3, 3, 2);        NORMAL_ARRAY.set(0, NORMALS.length / 3, NORMALS);        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.setNormals(NORMAL_ARRAY);                // 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_BACK); // By using CULL_NONE all faces of the pyramid will be shown.        appearance.setPolygonMode(polygonMode);                Material material = new Material();        material.setVertexColorTrackingEnable(true);        appearance.setMaterial(material);                mesh.setAppearance(0, appearance); // Set the appearance to the 3D object        return mesh;    }}

⌨️ 快捷键说明

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