问题
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);

回答1:
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
- Draw all the opaque objects first
- Disable depth buffer writes
- Sort the transparent triangles
- 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.
来源:https://stackoverflow.com/questions/30151134/opengl-transparency-not-working-properly