Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL alpha blending

I've found 5349574673 pages on alpha blending and I still can't get the desired result. I'm trying to make gif/png files display properly (WITH transparency/translucency) using opengl.

Here's my initialization bit:

glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); //I've seen this on most tutorials
glDisable(GL_DEPTH_TEST); //it's a 2D game
//...loading matrices and so on

I am SURE the images have transparency, and are loaded properly.
glBlendFunc(GL_ONE, GL_ONE); works but I can't tell if it's doing anything on the alpha channel since it would render as black => blending works
glColor4f(1f,1f,1f,0.3f); works fine, I can draw transparent stuff

P.S. I'm using this example http://lwjgl.org/wiki/index.php?title=Space_Invaders_Example_Game for learning

EDIT I used other textures made encoded with GIMP and other texture loaders and it now works fine.

like image 286
adrianton3 Avatar asked Oct 25 '11 05:10

adrianton3


People also ask

How do you blend colors with Alpha?

This computation is done separately for the red, blue, and green color components. This is called alpha blending. The value of the resulting color when color Value1 with an alpha value of Alpha is drawn over a background of color Value0 is given by: Value = Value0(1.0 - Alpha) + Value1(Alpha)

How do I enable blending in OpenGL?

To render images with different levels of transparency we have to enable blending . Like most of OpenGL's functionality we can enable blending by enabling GL_BLEND : glEnable (GL_BLEND); Now that we've enabled blending we need to tell OpenGL how it should actually blend.

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 alpha blending in computer graphics?

In computer graphics, alpha compositing or alpha blending is the process of combining one image with a background to create the appearance of partial or full transparency.


1 Answers

Your blending setup is correct. However for blending to work the texture must contain an alpha channel, properly valued of course. And then the alpha channel data must be preserved by the image loading process. May we see the image loading and texture generation code, please?

like image 133
datenwolf Avatar answered Sep 20 '22 01:09

datenwolf