創建正方體

   MyCanvas.java

 

 private Mesh createCube() throws Exception
    {
        short[] wallTextures = {
            0,1, 1,1, 0,0, 1,0,//(x,y)
           0,1, 1,1, 0,0, 1,0,
            0,1, 1,1, 0,0, 1,0,
            0,1, 1,1, 0,0, 1,0,
            0,1, 1,1, 0,0, 1,0,
            0,1, 1,1, 0,0, 1,0,
        };
        Appearance wallApp = new Appearance();
        Image wallImg = Image.createImage("/wall.png");
        Texture2D wallTexture = new Texture2D(new Image2D(Image2D.RGB, wallImg));
        wallTexture.setWrapping(Texture2D.WRAP_REPEAT, Texture2D.WRAP_REPEAT);
        wallTexture.setBlending(Texture2D.FUNC_MODULATE);
        wallTexture.setFiltering(Texture2D.FILTER_BASE_LEVEL, Texture2D.FILTER_NEAREST);
        wallApp.setTexture(0, wallTexture);

        PolygonMode wallMode = new PolygonMode();
        wallMode.setPerspectiveCorrectionEnable(true);
        wallApp.setPolygonMode(wallMode);
        Mesh mesh =  Plane.createCube(wallApp, wallTextures, 1);
        mesh.scale(wallWidth, wallWidth, wallHeight);
        return mesh;
    }

 

Plane.java

public static Mesh createCube(Appearance app, short[] textures, float repeatCount)
    {
        short[] points = {
        0,0,0, 1,0,0, 0,1,0, 1,1,0, //front
        1,0,-1, 0,0,-1, 1,1,-1, 0,1,-1,//back
         0,0,-1, 0,0,0, 0,1,-1, 0,1,0,//left
        1,0,0, 1,0,-1, 1,1,0, 1,1,-1,//right
        0,1,0, 1,1,0, 0,1,-1, 1,1,-1,//top
        0,0,-1, 1,0,-1, 0,0,0, 1,0,0,//bottom
        };

        int[] indices = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};
        int[] length = {4, 4, 4, 4, 4, 4};
         VertexArray position_array = new VertexArray(points.length/3, 3, 2);
        position_array.set(0, points.length/3, points);
        IndexBuffer indexBuffer = new TriangleStripArray(indices, length);
        VertexBuffer vertexBuffer = new VertexBuffer();
        vertexBuffer.setPositions(position_array, 1.0f, null);

        VertexArray textureArray = new VertexArray(textures.length/2, 2, 2);
        textureArray.set(0, textures.length/2, textures);
        vertexBuffer.setTexCoords(0, textureArray, repeatCount, null);

        Mesh mesh = new Mesh(vertexBuffer, indexBuffer, null);
        mesh.setAppearance(0, app);
        return mesh;
    }

發佈了20 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章