Path: chuka.playstation.co.uk!scea!peter_alau@playstation.sony.com From: "Jar Williams" Newsgroups: scea.yaroze.programming.3d_graphics Subject: Re: Calculating RSD Normals Date: Fri, 8 May 1998 10:44:21 -0400 Organization: SCEA News Server Lines: 81 Message-ID: <6iv58b$6r13@scea> References: <3551A622.6DF487E9@clara.net> NNTP-Posting-Host: usr-tort-m25-r25.clover.net X-Newsreader: Microsoft Outlook Express 4.72.2106.4 X-MimeOle: Produced By Microsoft MimeOLE V4.72.2106.4 I am just starting with Yaroze, but I have tried to make a similar converter, so I'll give it my best shot. As far as I can tell (please, anyone feel free to correct any mistakes!) the flat shading model calculates a single normal for each polygon. Try looking at PLY file that was generated by dxf2rsd. You will notice that there is one entry in the Normal section for every entry in the Polygon section (as long as the conversion was not made with the Reduce Normals flag set). In the Polygon entries, you see that there are values corresponding to each vertex that makes up the polygon (these are actually indexes to the Vertex section). These values are immediately followed by indexes to the Normal section. For flat shading, you will notice that all of these indexes into the Normal section are the same. An example of a triangle entry would be: 0 1 2 3 0 1 1 1 0 The reason the indexes are the same is because only one normal is used to render a surface with flat shading. You can picture this single normal as protruding from the center of the polygon, or you can picture the polygon having a normal at each vertex, but each normal has the same value. When you use Gouraud Shading each vertex of the polygon can have a unique normal, so an entry for the same polygon might look like this: 0 1 2 3 0 1 2 3 0 The number of normals, however, now matches the number of vertices (as long as you don't reduce normals) because each vertex has its own normal. --By reducing normals I mean that if two normals had identical values you could only enter one in the Normals section and polygons requiring this normal could reference this entry. Just a way to save space! Now, to calculate a normal for flat shading you already have 3 vertices in the polygon (all which lie in the same plane). Say that these points are P1, P2, and P3 and all are obviously of the form: (x, y, z). To begin you need to take what is called a vector cross product. The equation is: P1P2 X P1P3 where P1P2 is defined to be: P2 - P1. If P2 = (x2, y2, z2) and P1 = (x1, y1, z1), then P2 - P1 is: (x2 - x1, y2 - y1, z2 - z1) We do the same for P1P3 resulting in (x3 - x1, y3 - y1, z3 - z1). Now that we have P1P2 and P1P3 we can take their cross product (the big 'X'). The cross product is defined as: u X v = (a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1) For our example, this gives: P1P2 X P1P3 = ((y2 - y1)(z3 - z1) - (z2 - z1)(y3 - y1), (z2 - z1)(x3 - x1) - (x2 - x1)(z3 - z1), (x2 - x1)(y3 - y1) - (y2 - y1)(x3 - x1)) This should give you the correct proportions, now you have to normalize it. Your answer is in the form (x, y, z) so what you want to do is this: L = sqrt(x^2 + y^2 + z^2) --this is the length of the vector The normal is: (-x/L, -y/L, -z/L) --I am not sure yet why you negate each value, but it seems right. This is what you have to do for flat shading. As mentioned above, Gouraud shading uses different normals for each vertex in a polygon. A vertex normal is calculated as an average of the normals of all polygons that share this vertex. So, to find a vertex normal, first find the normal of every polygon that uses this vertex. You do this the same way we did above (except don't normalize yet). Then add all of these normals together like this: (x1 + x2 + x3..., y1 + y2 + y3..., z1 + z2 + z3...) Now you can normalize the result as shown above. Like I said, I haven't tested this thoroughly so I'm not positive, but hopefully it will help some.