Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL transparency not working properly

I have a problem with transparency in my project. As you can see, the transparency is working fine for other objects - you can see the rhino behind leaves, BUT the problem is with the tree itself. When there are two branches and one is closer than the other, the further one is not visible. Same with the trunk. The transparency is just not working for the object itself.

This is the code I use when creating texture:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

Image of my scene

like image 300
Karlos Otruba Avatar asked Oct 19 '25 15:10

Karlos Otruba


1 Answers

Although the color of the fragments you generate is transparent, they are still written to the depth buffer so that some of the triangles of your tree obscure the others, despite not affecting the color buffer.

To properly render transparent object you must

  1. Draw all the opaque objects first
  2. Disable depth buffer writes
  3. Sort the transparent triangles
  4. Render the transparent triangles from back to front.

There is a trick to avoid the above complexity though. If you are OK with only two alpha values (zero and one), then you can enable alpha test (glAlphaFunc) to discard the transparent fragments completely.

like image 196
Yakov Galka Avatar answered Oct 21 '25 05:10

Yakov Galka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!