1
votes

I am working on opengl es 2.0 for android application. I have a model for which i apply texture. I do this by using the below shaders. Its a simple shader.

In the below shader when the texture file is transparent at some places I get a black color. Instead I want to find the places where it is transparent and give a particular color like RGB(0.6,0.7,0.3). Please let me know how to modify the below shader to get this in my model.

protected static final String mVShader = 

    "uniform mat4 uMVPMatrix;\n" +
    "attribute vec4 aPosition;\n" +
    "attribute vec2 aTextureCoord;\n" +
    "varying vec2 vTextureCoord;\n" +

    "void main() {\n" +
    "   gl_Position = uMVPMatrix * aPosition;\n" +
    "   vTextureCoord = aTextureCoord;\n" +
    "}\n";

protected static final String mFShader = 

    "precision mediump float;\n" +

    "varying vec2 vTextureCoord;\n" +
    "uniform sampler2D uTexture0;\n" +

    "void main() {\n" +
    "   gl_FragColor = texture2D(uTexture0, vTextureCoord);\n" +
    "}\n";
1
How do you define places "when the texture file is transparent"? Do you mean having an alpha of zero? Can't you just put that particular color into the texture at those points?Nicol Bolas
Yes i mean the alpha is zero. But i want to put different colors in that place. So I want to do it via code. Please let me know your comments.Vinod

1 Answers

0
votes

Now the easiest thing would just be to check the texture color's alpha and output a different color depending on this:

void main()
{
    vec4 color = texture2D(uTexture0, vTextureCoord);
    gl_FragColor = (color.a > 0.1) ? color : vec4(0.6, 0.7, 0.3, 1.0);
}

Of course you can adapt this threshold, but using just 0.0 might not work due to precision issues. Dynamic branching in the fragment shader might not be the best idea performance-wise, but if you say you cannot just change the texture because the color may change, this will be the easiest way.

If the color change is persistent over a large number of frames, you maybe also could create a temporary texture, which contains the changed color in the transparent regions. This can be achieved easily on the GPU using the above shader and just rendering into this temporary texture (using FBOs) instead of the display framebuffer.