Simple question about transparent textures and what can be seen though them.
Members - Reputation: 116
Posted 28 April 2012 - 03:04 PM
Basically, when I use a transparent texture in my direct 3d program, the only other objects I can see when I look though the transparently textured object, are objects that were rendered BEFORE the transparently textured object was. Seeing as changing the entire rendering order every time the transparently textured object moves seems ridiculous, I assume there is some well known solution for this.
Three screen shots for extra examples. I have a 7x7x7 hollow cube made up of smaller, equal sized cubes. Which ever cube I look at has it's texture changed to a transparent texture (working on picking atm). First screenshot shows me inside the hollow cube of cubes looking out at the skybox. The second screenshot shows me outside the hollow cube of cubes looking in. As you can see, the cubes rendered before the cube I'm looking though are visible, the rest aren't. The third screen shot shows a big planet in the background that is not visible though a cube.
Thanks ahead of time for your help.
Crossbones+ - Reputation: 2815
Posted 28 April 2012 - 04:57 PM
1. At load time (or in your asset processing pipeline) flag any textures that have alpha in them as transparent.
2. Create a complete list of objects to render.
3. Sort that list. You want to render.
- Opaque objects first, in order from closest to furthest away. That makes the z-buffer work more efficiently.
- Transparent objects second, furthest away ones first.
4. Render the objects.
Note that for textures where the alpha channel is either 100% opaque or 100% transparent you may be able to get away with just turning on the alpha test and treating them as opaque. It doesn't always look that good on the smaller mip map levels though when there's lots of detail in the alpha channel.
Edited by Adam_42, 28 April 2012 - 04:58 PM.
Members - Reputation: 1647
Posted 29 April 2012 - 10:56 AM
Seeing as changing the entire rendering order every time the transparently textured object moves seems ridiculous, I assume there is some well known solution for this.
As Adam_42 already said, you don't have to change the entire rendering order.
But you really have to sort transparent objects back-to-front. Because of how alpha blending works, other objects will be visible through (semi-)transparent objects ONLY if rendered first. And I should also mention that this doesn't apply to whole objects, but to individual pixels.
When the device is rendering a transparent pixel to the backbuffer, it combines it with the pixel that already is in backbuffer on this position.
Imagine object A which is opaque and is positioned at the back and object B which is half-transparent and in the front of object A.
Let's say you render object B first, its pixels are blended with the actual content of the back buffer, which is black (suppose you cleared backbuffer to black and there are no other objects but A and B). So you'll get a darkened object B (blended 50:50 with black color). Then you render object A, but because of depth test (z buffer), its pixels won't be rendered at all.
Now let's say you render object A first, it will be drawn normally. And when you render object B later, it will pass depth test (it is in the front) and it will blend 50:50 with the existing pixels in the backbuffer, which is an image of object A. You get the expected result, you'll see A through B.