/// <summary> /// Calculate the polygon normals. By convention, LW's polygon normals /// are found as the cross product of the first and last edges. It's /// undefined for one- and two-point polygons. /// </summary> /// <param name="lwPolygonList"></param> public void GetPolyNormals(ref lwPolygonList polygon) { float[] p1 = new float[3]; float[] p2 = new float[3]; float[] pn = new float[3]; float[] v1 = new float[3]; float[] v2 = new float[3]; for (int i = 0; i < polygon.count; i++) { if (polygon.pol[i].nverts < 3) { continue; } for (int j = 0; j < 3; j++) { p1[j] = pt[polygon.pol[i].v[0].index].pos[j]; p2[j] = pt[polygon.pol[i].v[1].index].pos[j]; pn[j] = pt[polygon.pol[i].v[polygon.pol[i].nverts - 1].index].pos[j]; } for (int j = 0; j < 3; j++) { v1[j] = p2[j] - p1[j]; v2[j] = pn[j] - p1[j]; } LwVecMath.cross(v1, v2, polygon.pol[i].norm); LwVecMath.normalize(polygon.pol[i].norm); } }
/// <summary> /// Calculate the vertex normals. For each polygon vertex, sum the /// normals of the polygons that share the point. If the normals of the /// current and adjacent polygons form an angle greater than the max /// smoothing angle for the current polygon's surface, the normal of the /// adjacent polygon is excluded from the sum. It's also excluded if the /// polygons aren't in the same smoothing group. /// /// Assumes that lwGetPointPolygons(), lwGetPolyNormals() and /// lwResolvePolySurfaces() have already been called. /// </summary> /// <param name="lwPolygonList"></param> public void GetVertNormals(ref lwPolygonList polygon) { int h, p; float a; for (int j = 0; j < polygon.count; j++) { for (int n = 0; n < polygon.pol[j].nverts; n++) { for (int k = 0; k < 3; k++) { polygon.pol[j].v[n].norm[k] = polygon.pol[j].norm[k]; } if (polygon.pol[j].surf.smooth <= 0) { continue; } p = polygon.pol[j].v[n].index; for (int g = 0; g < pt[p].npols; g++) { h = pt[p].pol[g]; if (h == j) { continue; } if (polygon.pol[j].smoothgrp != polygon.pol[h].smoothgrp) { continue; } a = LwVecMath.vecangle(polygon.pol[j].norm, polygon.pol[h].norm); if (a > polygon.pol[j].surf.smooth) { continue; } for (int k = 0; k < 3; k++) { polygon.pol[j].v[n].norm[k] += polygon.pol[h].norm[k]; } } LwVecMath.normalize(polygon.pol[j].v[n].norm); } } }