Esempio n. 1
0
        public void Transform(Matrix TransformMatrix, double GridLambda = 3)
        {
            for (int i = 0; i < Vertices.Length; i++)
            {
                Vertices[i]      = Vector3.Transform(Vertices[i], TransformMatrix);
                VertexNormals[i] = Vector3.TransformNormal(VertexNormals[i], TransformMatrix);
            }

            Grid = new SpatialGrid(this, GridLambda);
            CalculateFaceNormals();
            CalculateTriangleProbabilities();
        }
Esempio n. 2
0
        public TriangleMesh(Material Material, Matrix TransformMatrix, string Path, double GridLambda = 3, bool SmoothShading = true, bool BackFaceCulling = true)
        {
            this.Material        = Material;
            this.SmoothShading   = SmoothShading;
            this.BackFaceCulling = BackFaceCulling;

            StreamReader Reader = new StreamReader(Path);
            string       Line;

            //Read header
            while ((Line = Reader.ReadLine()) != null)
            {
                string[] Tokens = Line.Split(' ');
                if (Tokens[0] == "element")
                {
                    if (Tokens[1] == "vertex")
                    {
                        Vertices = new Vector3[int.Parse(Tokens[2])];
                    }
                    if (Tokens[1] == "face")
                    {
                        NumFaces      = int.Parse(Tokens[2]);
                        VertexIndices = new int[NumFaces * 3];
                    }
                }
                if (Tokens[0] == "end_header")
                {
                    break;
                }
            }

            //Import vertex normals, uvs, if they are specified
            this.VertexNormals = new Vector3[Vertices.Length];
            this.VertexUVs     = new Vector2[Vertices.Length];
            for (int i = 0; i < Vertices.Length; i++)
            {
                Line = Reader.ReadLine();
                string[] Tokens = Line.Split(' ');
                Vertices[i] = new Vector3(double.Parse(Tokens[0], CultureInfo.InvariantCulture), double.Parse(Tokens[1], CultureInfo.InvariantCulture), double.Parse(Tokens[2], CultureInfo.InvariantCulture));
                if (Tokens.Length >= 6)
                {
                    VertexNormals[i] = new Vector3(double.Parse(Tokens[3], CultureInfo.InvariantCulture), double.Parse(Tokens[4], CultureInfo.InvariantCulture), double.Parse(Tokens[5], CultureInfo.InvariantCulture));
                }
                if (Tokens.Length >= 8)
                {
                    VertexUVs[i] = new Vector2(double.Parse(Tokens[6], CultureInfo.InvariantCulture), double.Parse(Tokens[7], CultureInfo.InvariantCulture));
                }
            }

            //Import faces
            for (int i = 0; i < NumFaces; i++)
            {
                Line = Reader.ReadLine();
                string[] Tokens = Line.Split(' ');
                VertexIndices[i * 3]     = int.Parse(Tokens[1]);
                VertexIndices[i * 3 + 1] = int.Parse(Tokens[2]);
                VertexIndices[i * 3 + 2] = int.Parse(Tokens[3]);
            }

            //Move mesh to wanted location, calculated spatial grid
            for (int i = 0; i < Vertices.Length; i++)
            {
                Vertices[i]      = Vector3.Transform(Vertices[i], TransformMatrix);
                VertexNormals[i] = Vector3.TransformNormal(VertexNormals[i], TransformMatrix);
            }
            Grid = new SpatialGrid(this, GridLambda);

            CalculateFaceNormals();
            CalculateTriangleProbabilities();
        }