zturtleman / mm3d

Maverick Model 3D is a 3D model editor and animator for games.
https://clover.moe/mm3d
GNU General Public License v2.0
114 stars 22 forks source link

Face normal calculation is unorthodox??? #115

Open m-7761 opened 4 years ago

m-7761 commented 4 years ago

In various places the following code is used to produce surfaces normals. Never mind it shouldn't be so duplicated.

        float A = y1 *(z2-z3)+y2 *(z3-z1)+y3 *(z1-z2);
        float B = z1 *(x2-x3)+z2 *(x3-x1)+z3 *(x1-x2);
        float C = x1 *(y2-y3)+x2 *(y3-y1)+x3 *(y1-y2);

Standard cross-product way is 6 multiplications and 9 additions. Looking around I think this is probably like Method 2 here (https://www.euclideanspace.com/maths/algebra/vectors/applications/normals/index.htm) that seems to use 3 projected areas.

If that's right, my guess is there's no advantage of that for triangles. It seems like something you'd do for a complex polygon.

EDITED: This (https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal) link has pseudo code for Newell's Method that I have heard of and used before. Doing a quick search turns up similar looking C like code. This method is kind of fuzzy I think and it's more clear that it's not intended for triangles.

EDITED: glmath.h has calculate_normal that uses the same way. At least that solves the code duplication problem.