Esempio n. 1
0
        public static void Parse(string fileName)
        {
            WavefrontObj obj = new WavefrontObj();

            using (StreamReader fs = new StreamReader(fileName))
            {
                int lineNumber = 0;
                while (!fs.EndOfStream)
                {
                    lineNumber++;
                    string line = fs.ReadLine();
                    if ((line.Length <= 0) || (line[0] == '#'))
                    {
                        continue;
                    }
                    string[] parts = line.Split(new[] { ' ' });
                    switch (parts[0])
                    {
                        case "v":
                            if (parts.Length != 4)
                            {
                                throw new ApplicationException(string.Format("Bad vertex vector size at line: {0}", lineNumber));
                            }
                            obj.vertices.Add(ParseVector3(parts[1], parts[2], parts[3]));
                            break;
                        case "vn":
                            if (parts.Length != 4)
                            {
                                throw new ApplicationException(string.Format("Bad normal vector size at line: {0}", lineNumber));
                            }
                            Vector3 normal = ParseVector3(parts[1], parts[2], parts[3]);
                            normal.Normalize();
                            obj.normals.Add(normal);
                            break;
                        case "vt":
                            if (parts.Length != 3)
                            {
                                throw new ApplicationException(string.Format("Bad texture coordinates vector size at line: {0}", lineNumber));
                            }
                            obj.texCoords.Add(ParseVector2(parts[1], parts[2]));
                            break;
                        case "f":
                            if (parts.Length != 4)
                            {
                                throw new ApplicationException(string.Format("Face being not a triangle at line: {0}", lineNumber));
                            }
                            int[] vertexIndices = new int[3];
                            for (int i = 0; i < 3; i++)
                            {
                                obj.faces.Add(uint.Parse(parts[i + 1]) - 1);
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
Esempio n. 2
0
        public static void Parse(string fileName)
        {
            WavefrontObj obj = new WavefrontObj();

            using (StreamReader fs = new StreamReader(fileName))
            {
                int lineNumber = 0;
                while (!fs.EndOfStream)
                {
                    lineNumber++;
                    string line = fs.ReadLine();
                    if ((line.Length <= 0) || (line[0] == '#'))
                    {
                        continue;
                    }
                    string[] parts = line.Split(new[] { ' ' });
                    switch (parts[0])
                    {
                    case "v":
                        if (parts.Length != 4)
                        {
                            throw new ApplicationException(string.Format("Bad vertex vector size at line: {0}", lineNumber));
                        }
                        obj.vertices.Add(ParseVector3(parts[1], parts[2], parts[3]));
                        break;

                    case "vn":
                        if (parts.Length != 4)
                        {
                            throw new ApplicationException(string.Format("Bad normal vector size at line: {0}", lineNumber));
                        }
                        Vector3 normal = ParseVector3(parts[1], parts[2], parts[3]);
                        normal.Normalize();
                        obj.normals.Add(normal);
                        break;

                    case "vt":
                        if (parts.Length != 3)
                        {
                            throw new ApplicationException(string.Format("Bad texture coordinates vector size at line: {0}", lineNumber));
                        }
                        obj.texCoords.Add(ParseVector2(parts[1], parts[2]));
                        break;

                    case "f":
                        if (parts.Length != 4)
                        {
                            throw new ApplicationException(string.Format("Face being not a triangle at line: {0}", lineNumber));
                        }
                        int[] vertexIndices = new int[3];
                        for (int i = 0; i < 3; i++)
                        {
                            obj.faces.Add(uint.Parse(parts[i + 1]) - 1);
                        }
                        break;

                    default:
                        break;
                    }
                }
            }
        }