如何在纹理上绘画[Libgdx]

时间:2015-02-07 06:01:53

标签: java android opengl-es libgdx

我有原始图片: http://i.stack.imgur.com/7qYW2.png

如何绘制此图像,而不是获取以下内容: http://i.stack.imgur.com/w3ij3.png

PS:此代码无效:

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

    batcher.begin();
    batcher.setColor(Color.BLUE);
    batcher.draw(texture, x, y, width, height);
    batcher.end();
}

2 个答案:

答案 0 :(得分:0)

您可以使用具有以下逻辑的片段着色器:

if(alpha of the pixel is 1.0) then pixel=cyan
else pixel=white

这当然要求企鹅周围的空间是透明的(有一个= 0)

varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;

void main() {
    vec4 color=v_color * texture2D(u_texture, v_texCoords);
    if(color.a==1.0)gl_FragColor=vec4(0.5,0.5,1,1);
    else gl_FragColor=vec(0.0,0.0,0.0,0.0);
}

答案 1 :(得分:0)

完整有效代码:

        String vertexShader = "attribute vec4 a_position;\n" +
                "attribute vec4 a_color;\n" +
                "attribute vec2 a_texCoord0;\n" +
                "\n" +
                "uniform mat4 u_projTrans;\n" +
                "\n" +
                "varying vec4 v_color;\n" +
                "varying vec2 v_texCoords;\n" +
                "\n" +
                "void main() {\n" +
                "    v_color = a_color;\n" +
                "    v_texCoords = a_texCoord0;\n" +
                "    gl_Position = u_projTrans * a_position;\n" +
                "}";

        String fragmentShader = "#ifdef GL_ES\n" +
                "precision mediump float;\n" +
                "#endif\n" +
                "\n" +
                "varying vec4 v_color;\n" +
                "varying vec2 v_texCoords;\n" +
                "uniform sampler2D u_texture;\n" +
                "uniform mat4 u_projTrans;\n" +
                "\n" +
                "void main() {\n" +
                "       vec4 color = v_color * texture2D( u_texture, v_texCoords );\n" +
                "       if( color.a == 0.0 ) gl_FragColor = vec4( color.r, color.g, color.b, 0.0 );\n" +
                "       else gl_FragColor = vec4( 1.0, 1.0, 1.0, 1.0 );" +
                "}";

        ShaderProgram shaderProgram = new ShaderProgram(vertexShader,fragmentShader);

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

            batch.begin();
            batch.setShader(shaderProgram);
            batch.draw(texture, x, y, width, height);
            batch.end();

        }