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

📄 m3gtexturing.java

📁 3D J2ME 代码集合
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;
import javax.microedition.lcdui.game.*;


public class M3GTexturing extends MIDlet {
    
    private Display d;
    private M3GTexCanvas texCanvas;
    
    public M3GTexturing(){
        texCanvas = new M3GTexCanvas(this);
        d = Display.getDisplay(this);
        d.setCurrent(texCanvas);
    }
    
    public void startApp() {
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }

}

class M3GTexCanvas extends GameCanvas implements Runnable{

    private MIDlet midlet;
  
    private Graphics3D g3d; // Graphics object used to render the world.
    private World world; // This world contains the two meshes to be rendered..
    private Camera camera; // the camera in the scene
    private Mesh pyramidMesh; // the pyramid in the scene
    
    private Texture2D brickTexture = null; // reference to the first texture on the pyramid.
    private Texture2D multiTexture = null; // reference to the second texture on the pyramid.
    private Texture2D infoTexture = null; // reference to the text texture.
    
    private Appearance meshAppearance; //  
    
   
    private Image SwitchImage = null;
    
    private int INDEX = 0;
    
    public M3GTexCanvas(MIDlet m){
        super(false);
        midlet = m;
        load2dImages();
        
        setFullScreenMode(true);
        brickTexture  = createBrickTexture("/texture.png"); // create the 1st pyramid texture.
        multiTexture  = createTexture("/texture2.png"); // create the 2nd pyramid texture.
        
        
        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.0f); // move the pyramid 3 units into the screen.
        meshAppearance = pyramidMesh.getAppearance(0);
        world.addChild(pyramidMesh); // add the pyramid to the world

        world.setActiveCamera(camera);

        Thread t = new Thread(this);
        t.start();
    }

    private void load2dImages(){
        try{
           
            SwitchImage = Image.createImage("/switch.png");
        }catch(Exception e){
            System.out.println("Failed to load 2D images");
        }
    }

    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();
        }
    }

    private void draw2D(Graphics g){
        
        g.drawImage(SwitchImage, getWidth(), getHeight(), Graphics.BOTTOM | Graphics.RIGHT);
    }

    protected void keyPressed(int key){
        switch(key){
            case -6:
                midlet.notifyDestroyed();
                break;
            case -7:
                INDEX = INDEX==2?0:INDEX+1;
                break;
        }
    }

    public void run() {
        Graphics g = getGraphics();

        while(true){

            // rotate the pyramid 3 degrees around the Y-axis.
           pyramidMesh.postRotate(3.0f, 0.0f, 1.0f, 0.0f);
            
            switch(INDEX){
                case 0:
                    meshAppearance.setTexture(0,  null); // remove the second texture.
                    meshAppearance.setTexture(1,  null); // remove the second texture.
                    break;
                case 1:
                    meshAppearance.setTexture(0,  brickTexture); // remove the second texture.
                    break;
                case 2:
                    meshAppearance.setTexture(1,  multiTexture); // add the second texture
                    break;
              
            }
  
            draw3D(g);
            draw2D(g);
            flushGraphics();
        }
    }

    /*
     * Create Clamped texture with modulate blending, this is used for the second pyramid texture.
     */
    private Texture2D createTexture(String path){
        Texture2D texture = null;
        try{
            Image texImg = Image.createImage(path); // Load the image
            texture = new Texture2D(new Image2D(Image2D.RGB, texImg)); // create the texture from the image
            texture.setWrapping(Texture2D.WRAP_CLAMP, Texture2D.WRAP_CLAMP); // don't repeat the texture.
            texture.setBlending(Texture2D.FUNC_MODULATE);
            texture.setFiltering(Texture2D.FILTER_LINEAR, Texture2D.FILTER_LINEAR);
        }catch(Exception e){
            System.out.println("Failed to create texture");
        }
        return texture;
    }
    
    private Texture2D createBrickTexture(String path){
        Texture2D texture = null;
        try{
            Image texImg = Image.createImage(path); // load the image
            texture = new Texture2D(new Image2D(Image2D.RGB, texImg)); // create texture from loaded image.
            texture.setWrapping(Texture2D.WRAP_REPEAT, Texture2D.WRAP_REPEAT); // repeat texture on surface
            texture.setBlending(Texture2D.FUNC_DECAL); // Blend mode to use.
            texture.setFiltering(Texture2D.FILTER_BASE_LEVEL, Texture2D.FILTER_NEAREST); // Use nearest for performance, linear for quality
 
        }catch(Exception e){
            System.out.println("Failed to create texture");
        }        
        
        return texture;
    }

   
    /*
     * Create a 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 texture coordinates is scaled down to 0-1 values in the setTextCoords method
        short []TEXTURES = new short[] {0, 255,        255, 255,     127, 0,
                                                              0, 255,        255, 255,     127, 0,
                                                              0, 255,        255, 255,     127, 0,
                                                              0, 255,        255, 255,     127, 0,
                                                              0, 0,            255, 0,         255, 255,
                                                              0, 0,            255, 255,     0, 255};
                                                            
        // 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, TEXTURE_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);
        TEXTURE_ARRAY = new VertexArray(TEXTURES.length / 2, 2, 2);
        TEXTURE_ARRAY.set(0, TEXTURES.length / 2, TEXTURES);
        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.setTexCoords(0, TEXTURE_ARRAY, (1.0f/255.0f), null);
        vertexBuffer.setTexCoords(1, TEXTURE_ARRAY, (1.0f/255.0f), null);
        // 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); // Use CULL_BACK for performace.
        appearance.setPolygonMode(polygonMode);
        
        mesh.setAppearance(0, appearance); // Set the appearance to the 3D object
        return mesh;
    }    

        
    
}

⌨️ 快捷键说明

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