Path: chuka.playstation.co.uk!news From: James Russell Newsgroups: scea.yaroze.programming.3d_graphics Subject: Re: Where to place my normals? Date: Wed, 07 Jan 1998 10:59:07 +1300 Organization: Peace Computers NZ Ltd Lines: 112 Message-ID: <34B2A92B.446B@peace.co.nz> References: <34B18E53.B74AAAC5@ix.netcom.com> NNTP-Posting-Host: intro.peace.co.nz Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 3.0Gold (X11; I; IRIX 6.2 IP22) Manny Najera wrote: > > I haven't seen much information posted about how to use normals in a > polygon. The only coordinates I've seen given to a normal were just > variations of the same three numbers: (0, 4096, 0). For a regular > square polygon, where the center is at (0, 0, 0), and the sides are ten > units long, what would be the coordinates of the normal? The normal is a vector that is perpendicular to the plane the polygon lies in. Every 3D triangle lies in some infinite plane. To find the normal of a 3D triangle with vertices at points A, B, and C, you take the cross product of the vectors (A-B) and (C-B). The cross product is a simple (but not so simple that I can remember it, I think it's in the Yaroze manuals somewhere) formula that you apply to two vectors (in our case, (A-B) and (C-B) and you get another vector back (D) that is perpendicular to the two. The cross product formula is not commutative (associative?). That is, S (cp) T does not equal T (cp) S (S & T are vectors). S (cp) T does, however, equal -( T (cp) S ) Thus, the direction of D depends on which order you cross product the two vectors. Normals are used for lots of things. In different ways, the normal is used for lighting effects, determining whether a polygon is visible or not, or an alternate description of the plane the polygon lies in. For instance, to speed up drawing times, the computer does not want to draw polygons that are not visible. If you were drawing a single die (a cube), then you only want to draw the faces facing you, since they will cover the faces behind. Every face in the cube has a normal, and the normals are defined in such a way that they point 'out', away from the center of the cube. Then when you come to draw the scene, the computer figures out which faces have normals pointing towards the screen, and draws them, and ignores any polygons with normals facing away from the screen. The most important thing about the normal is its direction. It doesn't matter how long it is, as long as it's pointing the right way. The reason why some apps require it to be unit length (that is, a length of 1 unit), is because when you're doing certain operations with normals, unit length normals mean that sometimes you don't have to do square roots and divides (which as we all know, takes ages). The reason why the length seems so be 4096 is that the Yaroze uses integers to store floating point numbers, and 4096 is defined as 1.000 2.000 is 8192 (4096 + 4096), 3 is 12288 (4096 * 3), and 1.5 is 6144 (4096 + 4096/2). If you write those numbers down in binary, you'll understand a lot better. The C macro 'ONE' is defined to be 4096, and you should use ONE to make your code clearer. If you have done the cross product thing, and come up with a vector that is not unit length, then you've got to 'unitise' it (make it unit length). The way to do this with vector A that has components (x,y,z) is the formula: Len = square_root(x*x + y*y + z*z) unit_x = x / Len unit_y = y / Len unit_z = z / Len and the new unitised normal is (unit_x, unit_y, unit_z). A common question is "How do I know what direction I will get when I take the cross product? Will it be pointing on the same 'side' as the face I've defined (like outward from the cube in my example), or the other way?" The answer is: It doesn't matter, as long as you're consistent. That is, if you work out the normals in the same way, and test them in the same way, you'll be OK. Personally, if I have polygons defined with vertices A,B,C,D... then I always use the formula (B-A) crossed with (C-B) to work out the normal. Things get more complicated with polygons that have more than 3 vertices. This is because it is possible to make a 3D polygon with >= 4 sides where not all the vertices lie in the same plane. I.e. take an A4 piece of paper and place it flat on the desk - all corners (vertices) are in the same plane. Now pick up one corner; There are now *4* distinct planes. There's the original one, and one for every corner and its two adjacent corners. Which plane is the 'right' one? It's impossible to say. This is why it is important to keep all vertices in a single plane when you've got >= 4 vertices. But most 3D modelling packages do this for you, or will split the polygon if you move a vertex out of the plane. The other problem with polygons >= 4 vertices is that it is possible for them to be concave (that is, if you let a rubber band snap around the outside of your polygon, the rubber band wouldn't touch all the sides). This means that you *might* get the normal going in the opposite direction from what you'd expect. These problems and other optimisation issues is the main reason why 3D graphics software and hardware prefer to deal with triangles only. J -- ==PEACE COMPUTERS ==James.Russell@peace.co.nz - 64(9)3730400 -Fax 64(9)3730401 Beauty fades. Ugly is forever.