Пример #1
0
 public Mesh(string name, int verticesCount, int facesCount)
 {
     Vertices = new Vertex[verticesCount];
     Faces = new Face[facesCount];
     Name = name;
 }
Пример #2
0
        public Mesh(Vector3f[] vertices, Vector3f[] normals, int[] triangleIndices)
        {
            Vertices = new Vertex[vertices.Length];
            for (int i = 0; i < vertices.Length; i++)
            {
                normals[i].Normalize();
                Vertices[i] = new Vertex()
                {
                    Coordinates = vertices[i],
                    Normal      = normals[i],
                };
            }

            Faces = new Face[triangleIndices.Length / 3];
            int faceIndex = 0;
            int triIndex  = 0;

            Vector3f buff1 = new Vector3f();
            Vector3f buff2 = new Vector3f();

            while (faceIndex < Faces.Length)
            {
                Vector3f n = new Vector3f();

                // Get a hint for the right directions
                Vector3f.AvgAndNorm(
                    ref normals[triangleIndices[triIndex + 0]],
                    ref normals[triangleIndices[triIndex + 1]],
                    ref normals[triangleIndices[triIndex + 2]],
                    ref n);

                // Then compute based on crossing the edges

                Vector3f.SubAndNorm(
                    ref vertices[triangleIndices[triIndex + 0]],
                    ref vertices[triangleIndices[triIndex + 1]],
                    ref buff1);
                Vector3f.SubAndNorm(
                    ref vertices[triangleIndices[triIndex + 0]],
                    ref vertices[triangleIndices[triIndex + 2]],
                    ref buff2);

                Vector3f newN = new Vector3f();
                Vector3f.Cross(ref buff1, ref buff2, ref newN);
                newN.Normalize();

                if (Vector3f.Dot(ref n, ref newN) < 0.0f)
                {
                    newN.X *= -1;
                    newN.Y *= -1;
                    newN.Z *= -1;
                }

                Faces[faceIndex++] = new Face()
                {
                    A      = triangleIndices[triIndex++],
                    B      = triangleIndices[triIndex++],
                    C      = triangleIndices[triIndex++],
                    Normal = newN,
                };
            }

            //var x = Faces
            //    .Select((p, i) => new[] { new { i, Ind = p.A }, new { i, Ind = p.B }, new { i, Ind = p.C } })
            //    .SelectMany(p => p)
            //    .GroupBy(p => p.Ind)
            //    .Select(p => new { VertInd = p.Key, TriInds = p.Select(q => q.i).ToArray() })
            //    .ToDictionary(p => p.VertInd, p => p.TriInds);
            //foreach (int vertInd in x.Keys)
            //{
            //    Vector3f.AvgAndNorm(x[vertInd].Select(p => Faces[p]).Select(p => p.Normal).ToArray(), ref Vertices[vertInd].Normal);
            //}
        }
Пример #3
0
 public Mesh(string name, int verticesCount, int faceCount)
 {
     Vertices = new Vector3[verticesCount];
     Faces    = new Face[faceCount];
     Name     = name;
 }
Пример #4
0
 public Mesh(int verticesCount, int facesCount)
 {
     Vertices = new Vertex[verticesCount];
     Faces    = new Face[facesCount];
 }