Skip to main content
GameDev.net gamedev.net
🔒 Locked

Transparency and DepthBuffer

Started by iSina Jul 22, 2008 at 2:55 PM 1 replies 750+ views
Original Post
iSina
iSina
I would like to give transparency to some objects. I do that using glEnable (GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); Say A is behind B and B is transparent but if A is rendered before B then this blending method does not work due to depth buffer. But when I enable depth buffer, the rendered model gets weird. So how do I achieve correct transparency with our without disabling depth buffer? Thanks
cmptrgy412
cmptrgy412
The answer to this problem is straightforward, but the solution is not. You have to draw the objects or polygons from furthest from the camera to closest.

There are a few ways of doing this.

A:
If only a few objects or polygons in your scene are transparent, put a m_bTransparency flag or something on their object and when you come across them during rendering, put them in a list and render them after everything that doesn't have the flag set is rendered. (And put the in their order in distance from camera)

B:
If you're unsure of what will be transparent in your world it gets harder. You can partition your world out using BSP (Binary Space Partitioning) or a similar method and draw the spaces that are furthest away from the camera to closest. That should solve your problem.

http://en.wikipedia.org/wiki/Binary_space_partitioning
Lord_Evil
Lord_Evil
Quote:
Original post by iSina
Say A is behind B and B is transparent but if A is rendered before B then this blending method does not work due to depth buffer.

So A has a greater distance to the camera than B? It should work then, except you don't have GL_LESS or GL_LEQUAL depth compare mode (GL_LESS being default).

What are the results you get? Any screenshots?


Like cmptrgy412 said, the way to get "correct" blending is to draw the transparent objects after the opaque ones and in back-to-front order.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.