Exemple #1
0
 public VertexInfoSimple(VertexInfo v)
 {
     this.m_color      = new Vec4Simple(v.m_color);
     this.m_normal     = new Vec3Simple(v.m_normal);
     this.m_position   = new Vec3Simple(v.m_position);
     this.m_textureUV  = new Vec2Simple(v.m_textureUV);
     this.m_textureUV2 = new Vec2Simple(v.m_textureUV2);
 }
Exemple #2
0
 public VertexInfo(VertexInfo copy)
 {
     m_position   = new Vec3(copy.m_position);
     m_textureUV  = new Vec2(copy.m_textureUV);
     m_textureUV2 = new Vec2(copy.m_textureUV2);
     m_normal     = new Vec3(copy.m_normal);
     m_color      = new Vec4(copy.m_color);
 }
        public static Object Import(string path)
        {
            List <Vec3> vertices           = new List <Vec3>();
            List <Vec3> normals            = new List <Vec3>();
            List <Vec2> textureCoordinates = new List <Vec2>();

            Dictionary <string, Material> materials = new Dictionary <string, Material>();
            string fileName    = path.Substring(1 + path.LastIndexOfAny(new char[] { '/', '\\' }));
            string pathName    = path.Substring(0, path.Length - fileName.Length);
            Object ret         = new Object(fileName);
            Mesh   currentMesh = null;

            foreach (string line in File.ReadAllLines(path))
            {
                string l = line.Trim();
                if (l.StartsWith("#") || l == "")
                {
                    //--- do nothing = comment
                }
                else
                {
                    if (l.StartsWith("v "))
                    {
                        string[] elements = l.Split(space, System.StringSplitOptions.RemoveEmptyEntries);
                        if (elements.Length != 4)
                        {
                            throw new Exception("Something wrong with obj file!");
                        }
                        vertices.Add(new Vec3((float)double.Parse(elements[1]), (float)double.Parse(elements[2]), (float)double.Parse(elements[3])));
                    }
                    else if (l.StartsWith("vn "))
                    {
                        string[] elements = l.Split(space, System.StringSplitOptions.RemoveEmptyEntries);
                        if (elements.Length != 4)
                        {
                            throw new Exception("Something wrong with obj file!");
                        }
                        normals.Add(new Vec3((float)double.Parse(elements[1]), (float)double.Parse(elements[2]), (float)double.Parse(elements[3])));
                    }
                    else if (l.StartsWith("vt "))
                    {
                        string[] elements = l.Split(space, System.StringSplitOptions.RemoveEmptyEntries);
                        if (elements.Length < 3)
                        {
                            throw new Exception("Something wrong with obj file!");
                        }
                        textureCoordinates.Add(new Vec2((float)double.Parse(elements[1]), (float)double.Parse(elements[2])));
                    }
                    else if (l.StartsWith("g "))
                    {
                        string groupName = l.Substring(2).Trim();
                        currentMesh        = new Mesh(8);
                        currentMesh.m_name = groupName;
                        ret.AddNode(currentMesh);
                    }
                    else if (l.StartsWith("mtllib "))
                    {
                        string materialFile = pathName + l.Substring(7).Trim();
                        LoadMaterialLib(materialFile, materials);
                    }
                    else if (l.StartsWith("usemtl "))
                    {
                        currentMesh.Material = materials[l.Substring(7).Trim()];
                    }
                    else if (l.StartsWith("s "))
                    {
                        //--- ToDo: do something with smooth groups
                    }
                    else if (l.StartsWith("f "))
                    {
                        l = l.Substring(2).Trim();
                        string[] elements = l.Split(space, System.StringSplitOptions.RemoveEmptyEntries);
                        if (elements.Length > 4)
                        {
                            throw new System.NotImplementedException();
                        }
                        else if (elements.Length == 3)
                        {
                            bool       setNormal;
                            VertexInfo v1 = Vert(elements[0], vertices, normals, textureCoordinates, out setNormal);
                            VertexInfo v2 = Vert(elements[1], vertices, normals, textureCoordinates, out setNormal);
                            VertexInfo v3 = Vert(elements[2], vertices, normals, textureCoordinates, out setNormal);
                            if (!setNormal)
                            {
                                Vec3 normal = Vec3.CalculateNormal(v1.m_position, v2.m_position, v3.m_position);
                                v1.m_normal = normal;
                                v2.m_normal = normal;
                                v3.m_normal = normal;
                            }
                            currentMesh.AddTriangle(v1, v2, v3);
                        }
                        else
                        {
                            throw new System.NotImplementedException();
                        }
                    }
                    else
                    {
                        throw new System.NotImplementedException();
                    }
                }
            }

            return(ret);
        }