3

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

1 Answer 1

4

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.

3
  • So I was looking into the trick you suggested and deleted blending and added simple code into fragmnt shader if (color.a < 0.5) discard; else gl_FragColor = color; And this helped. The only problem I have now is that with my lighting setup when the object is fully black due to lighting, the fragments get discarded. May 10, 2015 at 13:15
  • @KarlosOtruba: this is incorrect. If the object is opaque black then it should have color = vec4(0,0,0,1), If you observe something else, then there must be a bug in your lighting code. May 10, 2015 at 13:27
  • Thank you for the explanation! I figured out my issue with the code, just by reading your comment.
    – SRG
    Oct 15, 2017 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.