Ejemplo n.º 1
0
            /// <summary>
            /// Calculates the normals.
            /// </summary>
            /// <param name="vertices">vertex array of the shape</param>
            /// <param name="shapeIndices">The shape mapping indices.</param>
            /// <param name="shapeIndicesIndex">Index of the shape indices.</param>
            public static void CalculateNormals(
                VertexType[] vertices
                , ILArray <int> shapeIndices
                , Dictionary <int, List <int> > shapeIndicesIndex)
            {
#if MEASURE_NORMAL_CALCULATION
                DateTime start = DateTime.Now;
#endif
                System.Diagnostics.Debug.Assert(vertices != null && vertices.Length > 0 && vertices[0].StoresNormals);
                // first calculate the normal for all vertices
                ILPoint3Df[] snormals = new ILPoint3Df[shapeIndices.Dimensions[1]];
                for (int shapeCol = 0; shapeCol < snormals.Length; shapeCol++)
                {
                    // crossproduct of vertex: (#1 - #0) x (#2 - #0)
                    int        vi0   = shapeIndices.GetValue(0, shapeCol);
                    int        vi1   = shapeIndices.GetValue(1, shapeCol);
                    int        vi2   = shapeIndices.GetValue(2, shapeCol);
                    ILPoint3Df cross = ILPoint3Df.crossN(
                        vertices[vi1].Position - vertices[vi0].Position,
                        vertices[vi1].Position - vertices[vi2].Position);
                    snormals[shapeCol] = cross;
                }
#if MEASURE_NORMAL_CALCULATION
                TimeSpan crossDone    = DateTime.Now - start;
                DateTime startSorting = DateTime.Now;
                System.Diagnostics.Debug.WriteLine("Normals Calculation: cross products in " + crossDone.TotalMilliseconds.ToString() + "ms");
#endif
                // find all facettes using this vertex
                for (int i = 0; i < vertices.Length; i++)
                {
                    if (!shapeIndicesIndex.ContainsKey(i))
                    {
                        continue;
                    }
                    ILPoint3Df normal = new ILPoint3Df();
                    foreach (int shapeIdx in shapeIndicesIndex[i])
                    {
                        normal += snormals[shapeIdx];
                    }
                    // or should we let OpenGL normalize the normals...?
                    vertices[i].Normal = ILPoint3Df.normalize(normal);

                    //System.Diagnostics.Debug.Assert(
                    //    Math.Abs(Math.Sqrt(
                    //    vertices[0].Normal.X * vertices[0].Normal.X +
                    //    vertices[0].Normal.Y * vertices[0].Normal.Y +
                    //    vertices[0].Normal.Z * vertices[0].Normal.Z) - 1.0) < (double)MachineParameterFloat.eps);
                }
#if MEASURE_NORMAL_CALCULATION
                TimeSpan sortDone = DateTime.Now - startSorting;
                System.Diagnostics.Debug.WriteLine("Normals Calculation: sorting done in " + sortDone.TotalMilliseconds.ToString() + "ms");
#endif
            }