Colour Threshold Alpha

Hi All,
I have a 2D texture and wish set the alpha based on the colour intensity (R+G+B), with the intention of making colours below a certain threshold totally transparent. Is there an straightforward method to achieve this? Or a better method of making colours below a certain threshold transparent?
TIA

I you want to perform customized operations like that during runtime, you use shaders. Sample the texture and perform whatever operation you want and then output.

If you don’t need it at runtime, then do it on the CPU.

If you just need to have threshold for alpha value, then there is the GL_ALPHA_TEST which is available since GL 1.0

Thanks for the pointers. I have tried implementing the (my first) shader as such:


uniform float threshold;
uniform sampler2D texture;
void main()
{
	gl_FragColor = texture2D(texture, gl_TexCoord[0].xy);
	float intensity = gl_FragColor.r + gl_FragColor.g + gl_FragColor.b;
	if(intensity < 0.2)
		discard;
}

This works, but if I change the explicit 0.2 value to my uniform float threshold value, it does not work.

The uniform values are set up like this:


GLint thresholdLoc = glGetUniformLocation(fragmentProgram, "threshold");
if(thresholdLoc != - 1)
{
	glUniform1f(thresholdLoc, 0.2);
}

GLint texNumLoc = glGetUniformLocation(fragmentProgram, "texture");
if(texNumLoc != - 1)
{
	glUniform1i(texNumLoc, m_texNum);
}

Don’t read from gl_FragColor. It is write only variable.

When you call glUniform1i, the right shader must be bound. Maybe you have another one bound?

There is an app for that
glUniform_doesn.27t_work

Yes, you are right. Didn’t set the threshold (float) uniform value after glUseProgram. The sampler2D uniform seems to work if it is set prior to glUseProgram though.

Many thanks.

The sampler2D uniform seems to work if it is set prior to glUseProgram though.

It doesn’t. It is left to its default value of 0.

I see. All makes sense now. Thanks.