View Full Version : Colour Threshold Alpha
09-29-2011, 09:36 AM
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?
09-30-2011, 03:59 AM
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
10-03-2011, 03:58 AM
Thanks for the pointers. I have tried implementing the (my first) shader as such:
uniform float threshold;
uniform sampler2D texture;
gl_FragColor = texture2D(texture, gl_TexCoord.xy);
float intensity = gl_FragColor.r + gl_FragColor.g + gl_FragColor.b;
if(intensity < 0.2)
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)
GLint texNumLoc = glGetUniformLocation(fragmentProgram, "texture");
if(texNumLoc != - 1)
10-03-2011, 05:04 AM
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
10-04-2011, 01:36 AM
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.
10-04-2011, 04:26 AM
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.
10-04-2011, 04:57 AM
I see. All makes sense now. Thanks.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.