There are files on the Internet which describe 3D mesh
es that you'd like to use, but the vertices
of the face
s are by random chance sometimes arranged clockwise
and sometimes counterclockwise
. Some triangulation routines and some normal generator
s will freak out at such a mesh, producing visible faces sometimes on the outside of your object and sometimes on the inside - you will appear to have 'holes' in your render
ed object. I describe a simple algorithm
ic approach to take care of this problem.
Real life example
An Internet site called "Science U
" informs, among many other things, about Platonic
and Archimedean solids
. They show you these thingies in VRML
and as Java applets
. They also kindly give you the coordinate
s in .OFF
format. You can find this stuff by searching Google
for "Archimedean". I was hoping to use one of these meshes in an Anfy3d
applet. I hand-transposed all the coordinates and face vertex lists into arrays in my program, rendered the solid and ended up with a lot of unsightly holes.
My approach only works when the meshed solid is what I call "very convex
". This means that all faces must be fully or at least mostly visible from some point in the middle that you identify as the "center" without being obscured by other faces of the solid. Similarly, all the faces (in 2D, now) need to be convex as well. Essentially, no gaping chasms or crevasses in the outside topology
. Of course, Platonic and Archimedean solids fulfill this requirement to a tee.
For each face:
- Find the center of the face. Taking the average of the coordinates of all the vertices does the trick nicely. I'm going to call this midpoint
and all of the original vertex points P1, P2, ... P(however-many).
- Construct a transform that will rotate P0 around the center of the solid such that P0' lies at x=0 and y=0 (or x=0 and z=0, whichever coordinate system you prefer). The idea is to take the face from wherever it may be and to map it flat on to a "wall" in front of your "eyes", assuming that your "eyes" are at (0,0,0) or somewhere similarly convenient. The "wall" should end up mostly perpendicular to the vector from your "eyes" to the midpoint of the face as mapped onto this imaginary "wall".
- "measure" (i.e. mathematically determine) the relative angles of all vectors drawn from P0' (on the wall, of course) to each of the vertices
(P1', P2', ... )
of the face. Sort the vertices such that the angles form an ascending sequence. This will put the vertices into counterclockwise order as seen from inside the object and clockwise from the outside.
I'm a jack-of-all-trades programmer
, not a mathematician
- If none of the above, starting from "abstract", made any sense to you, then you probably have no use for this information. I'm sorry I wasted your time. I wrote this in the hope that some day some poor sod will have the same problems as I did, and will find this useful.
- There's probably a much more succinct way of saying what I just explained. To mathematicians, anyway. But then I suspect a mathematician would already know about this anyway.
- This method worked fine for me and I expect it to work fine for you if your application is similar to mine and you fill in the places where I covered ignorance with hand-waving. But if it doesn't work, or it seems to work and you construct an important building and it collapses, killing thousands, please bear in mind that this method comes with no guarantees whatsoever.