1
votes

Let's say I have this texture on a polygon:

img

How would I render that polygon and only allow the blue color to shine through and de-saturate the red color? I assume it will require the use of a specifically designed OpenGL shader.

Are there any good examples of this for specific values i.e. if blue > 200 and red < 20 and green < 20 then render that color else, desaturate the color. Would I be manipulating the color buffer?

1
What do you mean by "desaturated"? Do you mean "black" or "transparent" or what?Nicol Bolas
@NicolBolas Maybe I am using the wrong term. When you take an image in Photoshop and desaturate it. Black/white and grey.Satchmo Brown

1 Answers

1
votes

Yes you could design a shader and use the fragment shader to do (in pseudo-shader):

frag()
{
    float4 color = tex2D(myTexture);
    return float4(0,0,color.b,1);
}

This will only return the blue color spectrum of your texture. Notice that color values in the fragment shader are always between 0 and 1.

ANSWER TO QUESTION 1

It returns the parts of your texture that contain any blue. So in case of your red-blue texture. It will only return the blue parts and the red parts will be black instead. If you would have some kind of mixed texture, you would get a blue tinted texture.

I realise that you actually don't want to see the red parts at all. You can do it in different ways. In HLSL, there is a clip function. But I don't know the GLSL synonym. Another one would be to set the transparency of your texture like so:

frag()
{
    float4 color = tex2D(myTexture);
    return float4(color.rgb, color.b);
}

This will use the blue part of your texture to determine the transparency. Since red has no blue, red will be completely invisible. And blue has 1, so it will be completely visible.

ANSWER TO QUESTION 2

If you would have a texture of rock/stone and you would only like to see blue pixels in a range, you should make an if statement. If you want to see the "blue-ness" of the texture, this shader will do the trick.

If it really is for debugging purposes, it would be better to use the first shader, because it will keep the alpha value to 1. So instead of transparency, you will get black.

To use only show a range of "blue-ness", you could combine the if-statement with the color creation described above:

frag()
{
    float4 color = tex2D(myTexture);
    float4 result;

    if(color.b > 0.2f && color.b < 0.7f)
    {
        result = float4(0,0,color.b,1); // only return the blue part
        // or
        result = float4(color.rgb,1); // return the original color if blue is ok
    }
    else
    {
        result = float4(0,0,0,0); // fully transparent, won't show a thing
    }

    return result;
}

As you can see, it is fairly easy to make these checks. GLSL is the same as C, so you can use your basic programming skills for such statements.