Beispiel #1
0
        public static void parseVertex(FVLMesh mesh, String[] args, List <Vector3> vertexList)
        {
            String  sender = "ObjFileParser.parseVertex: ";
            Vector3 vertex = new Vector3();

            switch (args.Length)
            {
            case 2:
                log(sender, "Vertex definition must be (x,y,z [,w]). Found only x.");
                vertex.X = float.Parse(args[1], System.Globalization.NumberStyles.Number);
                vertex.Y = 0.0f;
                vertex.Z = 0.0f;
                //vertex.W = 1.0f;
                break;

            case 3:
                log(sender, "Vertex definition must be (x,y,z [,w]). Found only x, y.");
                vertex.X = float.Parse(args[1], CultureInfo.InvariantCulture);
                vertex.Y = float.Parse(args[2], CultureInfo.InvariantCulture);
                vertex.Z = 0.0f;
                //vertex.W = 1.0f;
                break;

            case 4:
                vertex.X = float.Parse(args[1], CultureInfo.InvariantCulture);
                vertex.Y = float.Parse(args[2], CultureInfo.InvariantCulture);
                vertex.Z = float.Parse(args[3], CultureInfo.InvariantCulture);
                //vertex.W = 1.0f;
                break;

            case 5:
                log(sender, "Found (x, y, z, w). Discarding w component.");
                vertex.X = float.Parse(args[1], CultureInfo.InvariantCulture);
                vertex.Y = float.Parse(args[2], CultureInfo.InvariantCulture);
                vertex.Z = float.Parse(args[3], CultureInfo.InvariantCulture);
                //vertex.W = float.Parse(args[4], System.Globalization.NumberStyles.Number);
                break;

            default:
                break;
            }
            mesh.AddVertex(vertex);
            vertexList.Add(vertex);
            //vertexCount++;
        }
Beispiel #2
0
        public static List <FVLMesh> parseFile(String objFileName, String mtlFileName)
        {
            List <FVLMesh> listaMeshes = new List <FVLMesh>();
            FVLMesh        mesh        = new FVLMesh();
            //Lista de todos los f, v, vn y vt del obj
            List <Vector3> vertexList       = new List <Vector3>();
            List <Vector2> texCordList      = new List <Vector2>();
            List <Vector3> vertexNormalList = new List <Vector3>();

            //Parseo el archivo de materiales
            List <MatConTextura> listaMateriales = MtlFileParser.parseFile(mtlFileName);

            String line;

            String[] lineSplit;

            StreamReader file = new StreamReader(objFileName);

            line = file.ReadLine();
            while (line != null)
            {
                line = line.Trim();                                   //Saco espacios en blanco.
                if ((line.Length != 0) && (!line[0].Equals(COMMENT))) //Si no es comentario
                {
                    lineSplit = line.Split(SEPARATORS, StringSplitOptions.RemoveEmptyEntries);
                    if (lineSplit[0].Equals(VERTEX))
                    {
                        parseVertex(mesh, lineSplit, vertexList);
                    }
                    else if (lineSplit[0].Equals(NORMAL))
                    {
                        parseNormal(mesh, lineSplit, vertexNormalList);
                    }

                    else if (lineSplit[0].Equals(FACE))
                    {
                        parseFace(mesh, line);//HERE!!
                    }

                    else if (lineSplit[0].Equals(TEXCORD))
                    {
                        parseTexCord(mesh, lineSplit, texCordList);
                    }

                    else if (lineSplit[0].Equals(USEMTL))
                    {
                        parseMtl(mesh, lineSplit, listaMateriales);
                    }
                    else if (lineSplit[0].Equals(O))
                    {
                        mesh = new FVLMesh();
                        mesh.NombreObjeto = lineSplit[1];
                        //Agrego todos los v, vn y vt a la lista del subobjeto (para que no se desacomoden los indices en el build)
                        foreach (Vector3 vertex in vertexList)
                        {
                            mesh.AddVertex(vertex);
                        }
                        foreach (Vector3 vertexNormal in vertexNormalList)
                        {
                            mesh.AddVertexNormal(vertexNormal);
                        }
                        foreach (Vector2 texCoord in texCordList)
                        {
                            mesh.AddTexCord(texCoord);
                        }

                        listaMeshes.Add(mesh);
                    }
                }
                line = file.ReadLine();
            }
            file.Close();
            return(listaMeshes);
        }