Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL GLSL texture transparency

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:

Example

The texture and the texture mask are in one 32bit bitmap.

Does anyone know, how to achieve this using GLSL?

like image 649
ProXicT Avatar asked Jan 22 '15 00:01

ProXicT


People also ask

How do I enable transparency in OpenGL?

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.

Is gl_FragColor deprecated?

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.

What is texture2D GLSL?

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.

What is sampler2D?

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.


1 Answers

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.

like image 197
Dietrich Epp Avatar answered Sep 18 '22 17:09

Dietrich Epp