I am writing a straightforward image viewer for displaying images on a couple of triangles in OpenGL ES 2.0 on Android. There is a particular colour in these images that must be replaced with black in some way. The problem is that if GL_LINEAR is used for my min and maxifying filters, the outline of the colour that needs to be replaced remains visible. If I use GL_NEAREST for my filters, the shader is able to replace the colour without too much difficulty, however it doesn’t render the images as smoothly and of course looks pixelated when zooming in and out.
I wonder if there is a better way to perform this type of task. Some ideas I’ve had are:
- Build another shader that runs only once on the texture pixels and replaces the colours before the texture is actually used during my fragment shader?
- Rewrite my shader so that it works even when GL_LINEAR is used.
Can anyone give me any clues as to how this is usually done? The reason I’d rather perform this operation in the shader is so that I can ensure that the mask pixel colour always stays black.
I am quite new to OpenGL ES 2.0 but I am trying to learn how things work. Please keep that in mind, and help me to learn. Thanks in advance.
In that case (mask always same place), I think you’ve got more options.
I would create a single channel image with mask areas as 1 and unmasked areas with 0.
Then in the shader, you sample the original texture and the mask texture, and you can replace the masked color with your own if the mask value is greater than a certain threshold.
This somewhat alleviates the problem with using GL_LINEAR, because you can adjust the value at which you reject the pixel.
For example, if you sample right between a masked and unmasked pixel, then your mask value will be 0.5, and the color will be (0.5*color_key + 0.5*unmasked_color). If you find this unacceptable color bleeding, you can set the alpha reject at 0.1, 0.001, etc, to mask any pixel that has even a slight trace of mask in it. This will slightly enlarge your masked area, but will allow you to not see a trace of the color key anywhere in the result.