繁体   English   中英

libGdx:着色网格

[英]libGdx: Coloring a mesh

您好,我想知道是否有一种方法可以从没有VertexAtribute颜色的网格中为三角形着色。 但是将其保存在单独的数组中。

编辑:

现在我想要的是顶点只有位置,没有颜色。

每个三角形的颜色应由单独的数组设置,以保持颜色。

我知道如何向着色器发送制服,但是render方法确实将整个网格渲染为一个而不是每个三角形。

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

如果您给出代码的更完整示例,我将尝试向您展示如何使用着色器中的制服进行处理。

约翰

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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