CSharpGL.TeapotModel.GenNormals C# (CSharp) Method

GenNormals() private static method

private static GenNormals ( ) : void
return void
        private static void GenNormals()
        {
            var faceNormals = new vec3[faceData.Length / 3];

            for (int i = 0; i < faceData.Length / 3; i++)
            {
                ushort vertexId1 = faceData[i * 3 + 0];
                ushort vertexId2 = faceData[i * 3 + 1];
                ushort vertexId3 = faceData[i * 3 + 2];
                vec3 vertex0 = new vec3(
                    positionData[(vertexId1 - 1) * 3 + 0],
                    positionData[(vertexId1 - 1) * 3 + 1],
                    positionData[(vertexId1 - 1) * 3 + 2]);
                vec3 vertex1 = new vec3(
                    positionData[(vertexId2 - 1) * 3 + 0],
                    positionData[(vertexId2 - 1) * 3 + 1],
                    positionData[(vertexId2 - 1) * 3 + 2]);
                vec3 vertex2 = new vec3(
                    positionData[(vertexId3 - 1) * 3 + 0],
                    positionData[(vertexId3 - 1) * 3 + 1],
                    positionData[(vertexId3 - 1) * 3 + 2]);
                vec3 v1 = vertex0 - vertex2;
                vec3 v2 = vertex2 - vertex1;
                faceNormals[i] = v2.cross(v1).normalize();
            }

            var normals = new float[positionData.Length];
            for (int i = 0; i < positionData.Length / 3; i++)
            {
                vec3 sum = new vec3();
                int shared = 0;
                for (int j = 0; j < faceData.Length / 3; j++)
                {
                    ushort vertexId1 = faceData[j * 3 + 0];
                    ushort vertexId2 = faceData[j * 3 + 1];
                    ushort vertexId3 = faceData[j * 3 + 2];
                    if (vertexId1 - 1 == i || vertexId2 - 1 == i || vertexId3 - 1 == i)
                    {
                        sum = sum + faceNormals[j];
                        shared++;
                    }
                }

                if (shared > 0)
                {
                    sum = (sum / shared).normalize();
                }

                normals[i * 3 + 0] = sum.x;
                normals[i * 3 + 1] = sum.y;
                normals[i * 3 + 2] = sum.z;
            }

            TeapotModel.normals = normals;
        }