Sort triangles by depth?

Started by
8 comments, last by laeuchli 22 years, 5 months ago
Hey, I understand that to do correct blending, I need to draw my alpha blended triangles from front to back. However, I''m not really sure how to sort them. I came up with a idea, but it seems like it would be really slow to implement. Is there a easy way to do this that I''m missing, or do I just have to bite the bullet and watch it chug? Jesse www.laeuchli.com/jesse/
Advertisement
Well, it all depends on how you are drawing them, and what you are using them for.
Simplest Way:
********** Start **********
Draw solid polygons.
Disable z-buffer writes (leave z-buffer reads/test).
Draw Alpha-Triangles.
********** End **********


Billy

BillyB@mrsnj.com
Hmm, that does sound simple. But how to I disable Depth Writes in opengl?
Look into: GL_DEPTH_WRITEMASK

Billy

BillyB@mrsnj.com

ps. This wouldn''t happen to be for your terrain engine would it. I used to talk to you a long time back about it (probably about a year ago).
Never mind.. to disable opengl zbuffer writes...

glDepthMask(GL_FALSE);

then use GL_TRUE to enable them.

Billy
Never mind.. to disable opengl zbuffer writes...

glDepthMask(GL_FALSE);

then use GL_TRUE to enable them.

Billy
yes, it''s for my terrian engine. I remember you now! How is your programming going?
Pretty good.. been getting much better at it :D Email me sometime.

Billy

BillyB@mrsnj.com
actually, that still won''t render your alpha polys correctly, only with respect to the solid objects. you still have to do the depth test with all your alpha polys, but if you think your engine can get away with rendering them in any order, it still might look okay. the way i''ve done mine is make a list of all the alpha polys, take the average value of the three verts that make up each poly, take the camera plane, and measure the distance from the camera plane to each poly''s average point. at this point, i can sort the distances, hence sorting the polys, then i render them back to front.

a2k
------------------General Equation, this is Private Function reporting for duty, sir!a2k
What the first AP says won''t solve all alpha problems - the alpha polygons DO need to be drawn last, but they still need to be Z sorted in some circumstances (consider a sphere of alpha polygons - just drawing them all last with Z writes turned off won''t work for views of half of the sphere).

1. Draw all opaque polygons first.


2. If the alpha parts of your models have any concave parts, split them up into multiple convex parts (this should be done by an external tool or at load time.


3. Make a bounding sphere for the convex alpha part of every object (this can be done at load time or even in an external tool).


4. Z sort the centres of all of the bounding spheres - this can be done in world space (you can use a single dot product to project the centre point of the sphere onto a normalised view direction vector - that will give you the distance along the view/camera vector).


5. Traverse all the bounding spheres in reverse Z sort order - i.e. back to near (far away to close).
Check if the current bounding sphere intersects any of the other bounding spheres:

a. If the current sphere doesn''t intersect any others, you don''t need to Z sort the polygons. You can instead render in two passes:

- Turn Z writes on
- Reverse culling mode (i.e. draw backs/indside of polygons)
- Draw the polygons of the convex part bounded by the sphere
- Set culling mode to forward (i.e. normal culling)
- Draw the polygons of the convex part bounded by the sphere

b. Otherwise, the polygons from both spheres in the *area of intersection* will need manually Z sorting. You can either just manually sort both spheres worth of polygons OR determine only those which are in the intersection area, manually sort those, and sort all which aren''t in the intersection using a) above.

--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

This topic is closed to new replies.

Advertisement