创建正方体

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值