Archived

This topic is now archived and is closed to further replies.

Wavewash

Alpha Blend Poly Sorting

Recommended Posts

Sorting Polygons back to front and rendering over already rendered stuff is such a headache. The problem I have is that the tree model that I have renders leaves that are in the background infront of the tree trunk. Now since I am working on my level editor this seems alright as of now because I''ll beable to output the geometry for the game in anyway I feel fit. Also since my game is a 2d platformer in a 3d format I removed the leaves behind the trunk so now it looks like: This is alright (or atleast I think so) but what methods are people using in games? Playing GTA Vice city. I see the problem I have also in the leaves of plants, but when you get close to the foilage it''s like it sorts the leaves. Like geometry close to your charachter gets sorted. My game being 2d platformer like I could easily apply this but is there any other methods? ~Wave

Share this post


Link to post
Share on other sites
A few thoughts:


1) If the things you need to sort only rotate around the Y axis with respect to the camera (e.g. trees in a driving game), pre-sorted index buffers is one way of getting fairly nice results.

For each tree model, precompute say 16 to 32 versions of its index buffer, each representing the Z *SORTED* polygons for that tree from one of 16-32 different directions.

At runtime all you do is look at the direction the camera vector is facing and find the nearest sort order to that when choosing which index buffer to use for the current frame.


2) Most foliage looks fairly opaque (or at least it looks acceptable as opaque) so as mentioned you can use alpha testing. Because A-test exits the pipeline before the Z buffer write, you can use Z buffering as usual. The tradeoff is you get very sharp edges where pixels are either accepted or rejected.


3) If you''re doing any manual Z sort, it always helps to pre-process your data to help as much as possible. For example: chopping your geometry into totally convex chunks, pre-sorting the polygon indices from a set direction, constructing an octree of all the chunks and polygons - preferably pre-sorting the data into octree node address order (i.e. tree traversal order). All your pre-processing and additional data structures should help a lot when it comes to sorting, e.g. sort by octree node first.


4) If it''s just the tree trunk that''s the problem, since its opaque you should separate it from the leaves - it should be a different mesh/call to Draw*Primitive*(). In that case, the trunk should be drawn with ZWRITE=TRUE, ZTEST=TRUE so that it writes to the Z buffer. All the alpha''d stuff like the leaves shouldn''t write to the Z buffer (i.e. ZWRITE=FALSE), but **should** be rendered *AFTER* all the opaque stuff and **should** still use Z test (ZTEST=TRUE) - that way, the leaves that are meant to be behind the trunk will appear behind the trunk.


5) Your camera is unlikely to make sudden direction changes unless you do a "cut" to another scene. In most games it''ll be tracking an object or something else interesting. Which indicates a high level of frame to frame coherence - i.e. the Z sort order for your alpha polys doesn''t change much from frame to frame. This has implications for your sort algorithm - keep the sorted version of the polys around in memory and only resort those that NEED to be re-sorted.


6) For Z sorting in general, I''ve already mentioned chopping things into convex pieces. Sort very loosely by convex chunk FIRST - you probably only need to worry about polygons if there''s an intersection between two convex chunks.


7) Also for many convex objects there''s the culling trick too. Draw the object TWICE. First with the cull mode REVERSED (i.e. CW) to draw the back of the object, then a second time with the cull mode normal (i.e. CCW)


8) Depending on your target hardware and other overheads there are other techniques such as "depth peeling", some variations on nailboards etc. Alpha Z sorting is however one of the nasties still left in realtime 3D graphics.

--
Simon O''Connor
ex -Creative Asylum
Programmer &
Microsoft MVP

Share this post


Link to post
Share on other sites