简体   繁体   中英

libGdx: Coloring a mesh

Hello I would like to know if there is a way to color a triangle from a mesh without VertexAtribute color. But save it in a seperate array.

EDIT:

Now what I whant is that vertices only has POSITION and no COLOR.

The color of each triangle should be set by a seperate array that holds the color.

I know how to send a uniform to the shader but the render method does render the whole mesh at one and not each triangle.

public class TestBench implements ApplicationListener {

    public static final String VERT_SHADER =
            "attribute vec2 a_position;\n" +
                    "attribute vec4 a_color;\n" +
                    "uniform mat4 u_projTrans;\n" +
                    "varying vec4 vColor;\n" +
                    "void main() {\n" +
                    "   vColor = a_color;\n" +
                    "   gl_Position =  u_projTrans * vec4(a_position.xy, 0.0, 1.0);\n" +
                    "}";

    public static final String FRAG_SHADER =
            "#ifdef GL_ES\n" +
                    "precision mediump float;\n" +
                    "#endif\n" +
                    "uniform vec4 aTest;\n" +
                    "varying vec4 vColor;\n" +
                    "void main() {\n" +
                    "   gl_FragColor = vColor;\n" +
                    "}";

    public void create() {
        mesh = new Mesh(true, MAX_VERTS, MAX_INDICES,
                new VertexAttribute(VertexAttributes.Usage.Position, POSITION_COMPONENTS, "a_position"),
                new VertexAttribute(VertexAttributes.Usage.ColorUnpacked, COLOR_COMPONENTS, "a_color"));
    }

    public void render() {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        Gdx.gl.glClearColor(0, 0, 0, 1);

        flush();
    }

    void flush() {
        mesh.setVertices(vertices);
        mesh.setIndices(indices);
        Gdx.gl.glDepthMask(false);
        Gdx.gl.glEnable(GL20.GL_BLEND);
        Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        shader.begin();
        shader.setUniformMatrix("u_projTrans", camera.combined);
        mesh.render(shader, GL20.GL_TRIANGLES, 0, vertices.lenght);
        shader.end();
        Gdx.gl.glDepthMask(true);
    }
}

If you give a more complete example of your code I'll try to show you how to do it using a uniform in the shaders.

John

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