簡體   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