Sunday, 15 September 2013

OpenGL Transparency?

OpenGL Transparency?

I'm following some OpenGL tutorials on how to get the transparency effect
using a RGBA texture format, and so I have an image data set up with a
specific color being set to alpha 0, everything else remains the same.
//OpenGL initialization
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
Then I'm creating a VBO, a Texture object, and calling them during the
draw function. In my fragment shader, I have this code (for tests):
#version 330 core
in vec2 UV;
out vec4 color;
uniform sampler2D texture;
void main()
{
vec4 cColor = texture2D(texture, UV).rgba;
//Using just this gets me the original texture, but without the alpha
(black background), so I did this to test:
if(cColor.w == 0) //if this pixel is set to be transparent...
{
color = vec4(1, 0, 0, 1); //show a red pixel
}
else
{
color = texture2D(texture, UV).rgba;
}
}
I'm seeing the red pixels where it is supposed to be transparent, but if I
change the w component of it to 0, it goes back to the black background of
the texture. If I use the glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA), I
get the red pixel even with the w set to 0.
I have another image drawn in the background to test it, I tried changing
the order, but it didn't help, changing the order just hid my small
texture.
I've checked a lot of tutorials and I think the code's mostly correct, but
the transparency just isn't showing.
What am I doing wrong?

No comments:

Post a Comment