I want to make transparency on my 32bit bitmap texture using GLSL. My fragment shader looks like this:
#version 120
uniform sampler2D myTexture;
varying vec2 TexCoord;
void main(void)
{
if(texture2D(myTexture, TexCoord).a != 1.0f)
{
discard;
}
gl_FragColor = texture2D(myTexture, TexCoord);
}
But that makes transparent only the pixels where alpha is equal 0 and I want to maintain the fade from the texture. For example in this picture the first image is the texture, the second is the texture mask and the third is the desired result:
The texture and the texture mask are in one 32bit bitmap.
Does anyone know, how to achieve this using GLSL?
OpenGL “transparency” would instead blend the RGB of the red object with the RGB of the green glass, giving a shade of yellow. Instead of using glColor3f( ) to specify the red, green, and blue of an object, use glColor4f( ) to specify red, green, blue, and alpha. Alpha is the transparency factor.
Yes, gl_FragColor is deprecated. You should use the following syntax: layout(location = 0) out vec4 diffuseColor; It is included in the GLSL 4.60 spec under the section 7.1.
The texture2D function returns a texel, i.e. the (color) value of the texture for the given coordinates. The function has one input parameter of the type sampler2D and one input parameter of the type vec2 : sampler, the uniform the texture is bound to, and coord, the 2-dimensional coordinates of the texel to look up.
A sampler2D is used to do lookup in a standard texture image; a samplerCube is used to do lookup in a cubemap texture (Subsection 5.3. 4). The value of a sampler variable is a reference to a texture unit. The value tells which texture unit is invoked when the sampler variable is used to do texture lookup.
You need to enable GL_BLEND
, and the alpha output of your fragment shader will be used to blend the fragment output with the framebuffer.
glEnable(GL_BLEND);
glBlendFunc(..., ...);
glDraw????(...);
// You don't have to do it here, but GL_BLEND will remain enabled
// until you disable it again.
glDisable(GL_BLEND);
If you use premultiplied alpha,
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
If you use non-premultiplied alpha,
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
The fragment shader can't do the blending because it doesn't have access to the framebuffer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With