Transparency, Blending and Opaque objects

Started by MattCa, October 12, 2011, 22:44:17

Previous topic - Next topic

MattCa

Hey guys!
I've gotten pretty far in the development of my game, and have begun to implement basic transparent objects. At the moment all I'm doing is disabling the depth buffer, enabling blending, drawing the object, disabling blending and then enabling depth testing. Pretty much what you're supposed to do, and it works perfectly! For example, if you look at the transparent object (with the opaque object behind it), it works perfectly!

Until..

Heres where it fails, if you look towards where the transparent object from behind the opaque object (which should be blocking it!), you can still see the transparent object through the opaque one...

Why is this? Is there a way to fix it?? I kinda understand how/what it's doing, but finding a solution is beyond me..

Thanks guys,
Matt.

MattCa

Aha! Ignore that, I've fixed it. I didn't realise that you can keep the depth buffer enabled when rendering transparent object(s), it seems to be working now though.

There is now another problem, I have two transparent objects and render them back to front (with the depth buffer enabled), however, when I look through the furthest object, the one behind it (the 'closest') is not visible. How do I fix this?

Heres an example of what I mean:

(Looking through 'closest' object)


(Looking through the 'furthest' object)

spasi

You can enable alpha testing. It will work fine, as long as your textures only have fully transparent or fully opaque texels, like the example you posted. Otherwise you'd need to depth-sort your triangles in back-to-front order, on every frame.

MattCa

Thanks for the reply!
Yeah, the first image above is actually incorrect (that was when I still had depth testing disabled), I've managed to fix that now by enabling the depth buffer and alpha blending (thanks for that btw, honestly didn't know about such a feature). The program now works fine, and works as it should. But if I were to have partially transparent objects, what would I exactly have to do to still make it work?

Even without alpha blending, looking through the gear that was further away from the player in the above image worked and I could see the other transparent and opaque objects behind it. But only when I look through the closer one (the second image) does the transparency fail.

Do you know why this is happening? Also, by back to front do you mean drawing the object that is the furthest from 0 (greatest negative) to the greatest positive? This is where I can't understand whether the documents mean relative to the player or the world co-ordinates.

I guess my question is, what is the typical process you would use to render any number of translucent objects in a way that let's you see the ones in front and behind no matter the position of the player (e.g. If I had three objects, what states would I set and in what way would I draw in order to be able to see both object 1 and 3 through parts of the middle object, no 2)?

Thanks,
Matt.

spasi

Quote from: MattCa on October 13, 2011, 14:30:55Even without alpha blending, looking through the gear that was further away from the player in the above image worked and I could see the other transparent and opaque objects behind it. But only when I look through the closer one (the second image) does the transparency fail.

Do you know why this is happening?

It's happening because without alpha test you're still writing to the depth buffer, even for fully transparent fragments. It's not blending that fails, it's the early depth test that culls the small gear. As far as depth testing is concerned, there are opaque fragments there with lower z-depth than the fragments you're trying to draw. With alpha test enabled, the fully transparent fragments are discarded and there are no depth writes taking place.

Quote from: MattCa on October 13, 2011, 14:30:55Also, by back to front do you mean drawing the object that is the furthest from 0 (greatest negative) to the greatest positive? This is where I can't understand whether the documents mean relative to the player or the world co-ordinates.

Yes, further to closest, relative to the player/camera position.

Quote from: MattCa on October 13, 2011, 14:30:55I guess my question is, what is the typical process you would use to render any number of translucent objects in a way that let's you see the ones in front and behind no matter the position of the player (e.g. If I had three objects, what states would I set and in what way would I draw in order to be able to see both object 1 and 3 through parts of the middle object, no 2)?

The typical rendering order goes like this:

1) Render opaque objects, sorted front-to-back for performance reasons (more likely to take advantage of early depth test).
2) Render transparent objects (alpha 0 or 1), sorted front-to-back, with alpha test enabled.
2) Render translucent objects (alpha 0..1), sorted back-to-front, for correct blending. Depending on the object's nature, you may need to depth sort individual triangles as well (e.g. particle systems, objects with layers of translucent surfaces)

spasi

Btw, order independent transparency is an active research subject. You can google for it, but it's pretty advanced stuff and requires shaders and high-end hardware to do correctly/efficiently.

MattCa

Quote from: spasi on October 13, 2011, 15:02:57
The typical rendering order goes like this:

1) Render opaque objects, sorted front-to-back for performance reasons (more likely to take advantage of early depth test).
2) Render transparent objects (alpha 0 or 1), sorted front-to-back, with alpha test enabled.
2) Render translucent objects (alpha 0..1), sorted back-to-front, for correct blending. Depending on the object's nature, you may need to depth sort individual triangles as well (e.g. particle systems, objects with layers of translucent surfaces)

Aaaah, so any objects which have fully transparent or fully opaque texels are drawn front to back with alpha test (i.e. the gears above), and only objects which are partially transparent are drawn back to front?

I see now, and just for clarification on the back to front drawing, does it not matter which way the camera is facing? So it's solely based on camera position, rendering all objects < camera.z first and all objects > camera.z last?

Thanks for the help,
Matt.

MattCa

Quote from: spasi on October 13, 2011, 15:06:24
Btw, order independent transparency is an active research subject. You can google for it, but it's pretty advanced stuff and requires shaders and high-end hardware to do correctly/efficiently.

High-end hardware? That's me out then, haha!  ;D

spasi

Quote from: MattCa on October 13, 2011, 15:08:17I see now, and just for clarification on the back to front drawing, does it not matter which way the camera is facing? So it's solely based on camera position, rendering all objects < camera.z first and all objects > camera.z last?

Objects behind the camera are not visible by definition so you don't need to sort/render them at all. You only need to care about objects that are contained within or intersect the camera frustum.