예제 #1
0
 public TriangleMesh(TriangleMesh mesh) : base(Vector3.Zero, Quaternion.Identity, 1.0f, new Vector4(0, 0, 1.0f, 1.0f))
 {
     m_vertices  = new List <Vertex>(mesh.GetVertices());
     m_indices   = new List <int>(mesh.GetIndices());
     m_triangles = new List <Triangle>(new Triangle[m_indices.Count / 3]);
 }
예제 #2
0
        public void LoadFile(string filename)
        {
            //Create an input filestream
            var    stream      = new StreamReader(filename);
            var    vertices    = new List <Vector3>();
            var    indices     = new List <int>();
            var    faceNormals = new List <Vector3>();
            string line;

            while (stream.Peek() >= 0)
            {
                line = stream.ReadLine();
                if (line == null)
                {
                    continue;
                }
                switch (line[0])
                {
                case 'f':
                    ParseFace(indices, line);
                    break;

                case 'v':
                    ParseVertex(vertices, line);
                    break;

                default:
                    break;
                }
            }
            var normals = new List <Vector3>(new Vector3[vertices.Count]);

            for (var i = 0; i < indices.Count; i += 3)
            {
                var p1 = vertices[indices[i]];
                var p2 = vertices[indices[i + 1]];
                var p3 = vertices[indices[i + 2]];
                var u  = p2 - p1;
                var v  = p3 - p1;
                var n  = Vector3.Cross(u, v);
                faceNormals.Add(new Vector3(n));
            }
            for (var i = 0; i < indices.Count; i++)
            {
                var f = i / 3;
                var v = indices[i];
                normals[v] += faceNormals[f];
            }
            for (var i = 0; i < normals.Count; i++)
            {
                normals[i] = normals[i].Normalized();
            }
            List <Vertex> verts = new List <Vertex>(new Vertex[vertices.Count]);

            for (var i = 0; i < verts.Count; i++)
            {
                verts[i] = new Vertex(new Vector3(vertices[i]), new Vector3(normals[i]));
            }
            //CalcVertexNormals(indices, faceNormals, normals);
            m_resourceMap[filename] = new TriangleMesh(verts, indices);
        }