Alpha blending - How to sort?

Started by
5 comments, last by Wicked Ewok 18 years, 12 months ago
I have my 3D world exported from the modelling program as a .X file. This world mesh contains both opaque and [semi]transparent parts. As you know, to get alphablending working properly, you must sort objects so that those that are further away from the camera get rendered before those closer to it. My question is: how do you achieve this sorting when you have the transparent polygons mixed with the opaque ones? And, what's more important, how do you do this fast enough? (I'm descarding sorting by polygon as it would be too slow. Or am I wrong?) Would the only way to achieve this efficiently be to export transparent objects separately? What's the usual way to get this working in most 3D engines?
Advertisement
Quote:Original post by Night Elf
My question is: how do you achieve this sorting when you have the transparent polygons mixed with the opaque ones? And, what's more important, how do you do this fast enough? (I'm descarding sorting by polygon as it would be too slow. Or am I wrong?)

you almost certainly want to split these apart.

Most modern engines tend to "bucket sort" based on material/effect properties - e.g. all geometry that has a certain shader effect, or all geometry that is partially transparent etc... This allows for much more efficient state management and batching - two things that make GPU's [smile]...

One way that I saw described previously that looked quite good - not perfect, but definitely acceptable:

1. Render all OPAQUE geometry first, as per usual
2. Disable Z-Writing, but keep Z-Testing
3. Sort all transparent objects based on their center-points/bounding areas
4. Draw, from back to front the transparent objects

Two key points:
for #3, sort using the bounding regions, to get a rough order, only sort at a higher precision if the spheres intersect.

for #4, when drawing transparent geometry, draw the back-faces first (swap D3DRS_CULLMODE) and then draw the front faces. 2 distinct passes.

I forget where the article was as I'd include a link. Google might well be able to find it.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Thanks for your reply, but here's my problem:

I am sorting the scene by material but there are many parts that share the same semitransparent material. For example, say there are two wire grates, both share the same material, so they are in the same chunk for the renderer. There is no way of identifying each separate grate... So how would I sort in this case (as I don't have such thing as an 'object centre point')?
Quote:Original post by Night Elf
Thanks for your reply, but here's my problem:

I am sorting the scene by material but there are many parts that share the same semitransparent material. For example, say there are two wire grates, both share the same material, so they are in the same chunk for the renderer. There is no way of identifying each separate grate... So how would I sort in this case (as I don't have such thing as an 'object centre point')?

Hmm, okay... Do you have access to any geometric information? Even as part of a parent node in the scene graph?

If you're to do any sort of back-to-front sorting then you're going to need some sort of spacial information in order to get accurate results. I suppose it might be possible to use some form of Z-Testing and multiple passes to 'automagically' render them correctly, but that sounds painful [smile]

Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

You might find this helpful

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Quote:Original post by superpig
You might find this helpful


Very interesting. That would be the answer to my problem if it wasn't for two details:

1- All code is OpenGL and uses some specific extensions to get things done
2- More important, I'm still using the fixed function pipeline...

Any ideas how to implement something like that using FF? I guess it's pretty much impossible...
100% correct alpha sorting is not always possible(usually impossible actually), since you would need to split up every translucent polychunk that crosses another translucent one. By sorting using materials, you're experiencing this problem on a larger scale, since each polychunk you render is of a certain material. If you want optimal performance at a cost of some artifacts being produced, what you will need to do is first sort by material, then sort by depth. Your rendering would look like this:

settexture( A );
drawprimitive( obj at depth 1000 )
...
drawprimitive( obj at depth 1 )
settexture( B );
...

A more accurate method, but will cost you some on texture switching is:
settexture( A )
drawprimitive( at depth 1000 )
...
settexture( B )
drawprimitive( at depth 500 )
settexture( A )
... at depth 1

which is basically sorting only by depth. But before you do all this, you'll definately need to split polygon chunks in terms of them having or not having transparency values.

-Marv
-=~''^''~=-.,_Wicked_,.-=~''^''~=-

This topic is closed to new replies.

Advertisement