OpenGL transparency not working properly

白昼怎懂夜的黑 提交于 2019-12-02 06:20:23

问题


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

  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.



来源:https://stackoverflow.com/questions/30151134/opengl-transparency-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!