views:

147

answers:

3

How can I z-order the faces of a 3d object just using the 4 vertices of each of its faces? I've tried using a z-buffer where I store the average z value of each face; this works great most of the times, but fails when an object have large and small faces.

I'm building a small 3d-engine in flash, just for the fun of learning, so the only data i have is those 4 vertices and the normal of the face.

Thanks!

A: 

Z-buffering works best when all the faces are roughly the same size. If you have any that are very much larger than the rest then they're going to "overwrite" the smaller ones giving you artefacts.

The only solution is to break the larger faces down into smaller ones that are nearer the average size.

ChrisF
+1  A: 

You're butting up against the Visibility Problem, and there's no simple answer. This page describes the issue in a bit more detail, and may give you some thoughts. The way "real" 3D engines solve this is, they draw the scene pixel by pixel, and they keep running track of the Z coord of the topmost object drawn to any given pixel so far. This approach is not available to us in Flash, so the best we can generally do is approximate or generalize.

One common approach (described in the earlier link) is to group the faces into convex polygons, because it's easy to depth-sort the faces of a convex polygon, and relatively easy to depth-sort the polygons themselves. If this is feasible, it's the way I'd go.

fenomas
Thanks for the senocular link. I'll stay with the engine as is, currently it has backface removal so I just need to pay attention to concave surfaces when modeling. Thanks!
Paul
A: 

Another approach which is pretty simple to implement is BSP trees. The one problem with it is that in 3D it has some pretty bad worst case performance characteristics. You'll usually only hit that with some pretty odd-ball cases, so it might be worth trying it and seeing how it works in your case.

The basic idea is that you build a binary tree where each internal node has a plane equation and each leaf has a polygon. At each internal node, all of the polygons in the left child lie on one side of the plane and all of the polygons in the right child lie on the other side. You have to split any polygons which cross the plane. That's where the bad cases come in. If you have polygons which are arranged so that each polygon's plane splits all of the other polygons, then it gets ugly.

MPG