Пример #1
0
        public static ObjFile Load(string filename)
        {
            ObjFile file = new ObjFile();

            LoadModels(filename, file);
            GenNormals(file);
            OrganizeModels(file);

            return file;
        }
Пример #2
0
        private static void OrganizeModels(ObjFile file)
        {
            List<ObjModel> models = new List<ObjModel>();
            foreach (var model in file.models)
            {
                var newModel = OrganizeModels(model);
                models.Add(newModel);
            }

            file.models.Clear();
            file.models.AddRange(models);
        }
Пример #3
0
 private static void GenNormals(ObjFile file)
 {
     foreach (var model in file.models)
     {
         GenNormals(model);
     }
 }
Пример #4
0
        private static void LoadModels(string filename, ObjFile file)
        {
            using (var sr = new StreamReader(filename))
            {
                var model = new ObjModel();

                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    string[] parts = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
                    if (parts[0] == ("v"))
                    {
                        if (model.innerFaceList.Count > 0)
                        {
                            file.models.Add(model);
                            model = new ObjModel();
                        }

                        vec3 position = new vec3(float.Parse(parts[1]), float.Parse(parts[2]), float.Parse(parts[3]));
                        model.positionList.Add(position);
                    }
                    else if (parts[0] == ("vt"))
                    {
                        vec2 uv = new vec2(float.Parse(parts[1]), float.Parse(parts[2]));
                        model.uvList.Add(uv);
                    }
                    else if (parts[0] == ("vn"))
                    {
                        vec3 normal = new vec3(float.Parse(parts[1]), float.Parse(parts[2]), float.Parse(parts[3]));
                        model.normalList.Add(normal);
                    }
                    else if (parts[0] == ("f"))
                    {
                        Triangle triangle = ParseFace(parts);
                        model.innerFaceList.Add(triangle);
                    }
                }

                file.models.Add(model);
            }
        }