简体   繁体   中英

Cube mesh with libgdx on Android

I've done a mesh with libgdx and I'm trying to fill the mesh with some color.

create() {
         if (bigMesh == null) {
            bigMesh = new Mesh(true, 8, 8, 
                    new VertexAttribute(Usage.Position, 3, "a_position"),
                    new VertexAttribute(Usage.ColorPacked, 4, "a_color"));

            bigMesh.setVertices(new float[] {
                    0, -0.5f, -4, Color.toFloatBits(255, 0, 0, 255),
                    1, -0.5f, -4, Color.toFloatBits(255, 0, 0, 255),
                    1, 0.5f, -4, Color.toFloatBits(255, 0, 0, 255),
                    0, 0.5f, -4, Color.toFloatBits(255, 0, 0, 255),

                    1, 0.5f, -3, Color.toFloatBits(0, 255, 0, 255),
                    1, -0.5f, -3, Color.toFloatBits(0, 255, 0, 255),
                    0, -0.5f, -3, Color.toFloatBits(0, 255, 0, 255),
                    0, 0.5f,-3, Color.toFloatBits(0, 255, 0, 255)
                   });   
            bigMesh.setIndices(new short[] { 0, 1, 2, 3,4,5,6,7});
        }
}

render(){
        Gdx.gl.glClearColor(0,0,0,1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        bigMesh.render(GL10.GL_TRIANGLE_STRIP);
}

Which render paremeter shall I use? I'm using PerspectiveCamera.

For drawing images in the cube you have to add following two lines of code in your render method before drawing mesh on the screen.

Gdx.graphics.getGL10().glEnable(GL10.GL_TEXTURE_2D);
texture.bind();

See http://code.google.com/p/libgdx-users/wiki/MeshColor . You can either go with a default color for a whole model, or a per-vertex color.

You've got per-vertex color information in your example, so to change the color you need to change the vertices, and re-invoke setVertices on the mesh.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM