Beispiel #1
0
 private void LoadFromExistingModel(OBJModel model)
 {
     Vertices      = model.Vertices;
     Normals       = model.Normals;
     TexCoords     = model.TexCoords;
     Faces         = model.Faces;
     FaceGroups    = model.FaceGroups;
     currFaceGroup = model.currFaceGroup;
     HasTexCoords  = model.HasTexCoords;
     HasNormals    = model.HasNormals;
     pathFileName  = model.pathFileName;
     relativePath  = model.relativePath;
     TexEnabled    = model.TexEnabled;
 }
Beispiel #2
0
 private void LoadFromExistingModel(OBJModel model)
 {
     Vertices = model.Vertices;
     Normals = model.Normals;
     TexCoords = model.TexCoords;
     Faces = model.Faces;
     FaceGroups = model.FaceGroups;
     currFaceGroup = model.currFaceGroup;
     HasTexCoords = model.HasTexCoords;
     HasNormals = model.HasNormals;
     pathFileName = model.pathFileName;
     relativePath = model.relativePath;
     TexEnabled = model.TexEnabled;
 }
Beispiel #3
0
        public bool Load(string filename)
        {
            if (models.ContainsKey(filename))
            {
                OBJModel model = models[filename];
                LoadFromExistingModel(model);
                return true;
            }

            string[] s;
            string sLine;
            //			char[] cLine = new char[256];
            char[] separators = new char[] { ' ', '/' };
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);

            string absolutePath = fs.Name;
            //char[] sep = new char[] { '\\', '/' };
            char[] sep = new char[1];
            sep[0] = Game.GetPathSeparator()[0];
            string[] splitAbsolutePath = absolutePath.Split(sep);

            for (int i = 0; i < splitAbsolutePath.Length - 1; i++)
                relativePath += splitAbsolutePath[i] + sep[0];

            int ic = sr.Read();
            uint lineNumber = 1;

            List<Material> MaterialList = null;

            while (ic != -1)
            {
                char c = (char)ic;

                if (c == 'g')
                {
                    sLine = sr.ReadLine().Remove(0, 1);

                    if (sLine.CompareTo("default") == 0)
                    {
                        //Console.WriteLine("WARNING: Default group name ignored.");
                    }
                    else
                    {
                        FaceGroup fg = new FaceGroup(sLine);
                        FaceGroups.Add(fg);
                        currFaceGroup = fg;
                    }
                }
                else if (c == 'v')
                {
                    float[] fTemp;
                    int iNext = sr.Read();

                    if (iNext == ' ' || iNext == '\t')
                    {
                        fTemp = new float[3];
                        sLine = sr.ReadLine();
                        s = sLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                        float.TryParse(s[0], out fTemp[0]);
                        float.TryParse(s[1], out fTemp[1]);
                        float.TryParse(s[2], out fTemp[2]);

                        Vertices.Add(fTemp);
                    }
                    else if (iNext == 't')
                    {
                        fTemp = new float[2];
                        sLine = sr.ReadLine();
                        s = sLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                        float.TryParse(s[0], out fTemp[0]);
                        float.TryParse(s[1], out fTemp[1]);

                        TexCoords.Add(fTemp);

                        HasTexCoords = true;
                    }
                    else if (iNext == 'n')
                    {
                        fTemp = new float[3];
                        sLine = sr.ReadLine();
                        s = sLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                        float.TryParse(s[0], out fTemp[0]);
                        float.TryParse(s[1], out fTemp[1]);
                        float.TryParse(s[2], out fTemp[2]);

                        Normals.Add(fTemp);

                        HasNormals = true;
                    }
                    else
                    {
                        sLine = sr.ReadLine();
                    }
                }
                else if (c == 'f')
                {
                    uint[][] iTemp = new uint[3][];
                    int faceSize;
                    bool isQuad = false;

                    sLine = sr.ReadLine();

                    if (HasTexCoords && HasNormals)
                    {
                        //f v/t/n v/t/n v/t/n (v/t/n)
                        s = sLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                        faceSize = s.Length / 3;
                        isQuad = (faceSize == 4);

                        iTemp[0] = isQuad ? new uint[4] : new uint[3];
                        uint.TryParse(s[0], out iTemp[0][0]);
                        uint.TryParse(s[3], out iTemp[0][1]);
                        uint.TryParse(s[6], out iTemp[0][2]);

                        if (isQuad)
                            uint.TryParse(s[9], out iTemp[0][3]);

                        iTemp[1] = isQuad ? new uint[4] : new uint[3];
                        uint.TryParse(s[1], out iTemp[1][0]);
                        uint.TryParse(s[4], out iTemp[1][1]);
                        uint.TryParse(s[7], out iTemp[1][2]);

                        if (isQuad)
                            uint.TryParse(s[10], out iTemp[1][3]);

                        iTemp[2] = isQuad ? new uint[4] : new uint[3];
                        uint.TryParse(s[2], out iTemp[2][0]);
                        uint.TryParse(s[5], out iTemp[2][1]);
                        uint.TryParse(s[8], out iTemp[2][2]);

                        if (isQuad)
                            uint.TryParse(s[11], out iTemp[2][3]);

                        Assertion(iTemp[0][0] - 1, (uint)Vertices.Count, lineNumber);
                        Assertion(iTemp[0][1] - 1, (uint)Vertices.Count, lineNumber);
                        Assertion(iTemp[0][2] - 1, (uint)Vertices.Count, lineNumber);

                        if (isQuad)
                            Assertion(iTemp[0][3] - 1, (uint)Vertices.Count, lineNumber);

                        Assertion(iTemp[1][0] - 1, (uint)TexCoords.Count, lineNumber);
                        Assertion(iTemp[1][1] - 1, (uint)TexCoords.Count, lineNumber);
                        Assertion(iTemp[1][2] - 1, (uint)TexCoords.Count, lineNumber);

                        if (isQuad)
                            Assertion(iTemp[1][3] - 1, (uint)TexCoords.Count, lineNumber);

                        Assertion(iTemp[2][0] - 1, (uint)Normals.Count, lineNumber);
                        Assertion(iTemp[2][1] - 1, (uint)Normals.Count, lineNumber);
                        Assertion(iTemp[2][2] - 1, (uint)Normals.Count, lineNumber);

                        if (isQuad)
                            Assertion(iTemp[2][3] - 1, (uint)Normals.Count, lineNumber);

                        currFaceGroup.AddFace(new Face(iTemp));
                    }
                    else if (HasTexCoords && !HasNormals)
                    {
                        //f v/t v/t v/t (v/t)
                        s = sLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                        faceSize = s.Length / 3;
                        isQuad = (faceSize == 4);

                        iTemp[0] = isQuad ? new uint[4] : new uint[3];
                        uint.TryParse(s[0], out iTemp[0][0]);
                        uint.TryParse(s[2], out iTemp[0][1]);
                        uint.TryParse(s[4], out iTemp[0][2]);

                        if (isQuad)
                            uint.TryParse(s[6], out iTemp[0][3]);

                        iTemp[1] = isQuad ? new uint[4] : new uint[3];
                        uint.TryParse(s[1], out iTemp[1][0]);
                        uint.TryParse(s[3], out iTemp[1][1]);
                        uint.TryParse(s[5], out iTemp[1][2]);

                        if (isQuad)
                            uint.TryParse(s[7], out iTemp[1][3]);

                        Assertion(iTemp[0][0] - 1, (uint)Vertices.Count, lineNumber);
                        Assertion(iTemp[0][1] - 1, (uint)Vertices.Count, lineNumber);
                        Assertion(iTemp[0][2] - 1, (uint)Vertices.Count, lineNumber);

                        if (isQuad)
                            Assertion(iTemp[0][3] - 1, (uint)Vertices.Count, lineNumber);

                        Assertion(iTemp[1][0] - 1, (uint)TexCoords.Count, lineNumber);
                        Assertion(iTemp[1][1] - 1, (uint)TexCoords.Count, lineNumber);
                        Assertion(iTemp[1][2] - 1, (uint)TexCoords.Count, lineNumber);

                        if (isQuad)
                            Assertion(iTemp[1][3] - 1, (uint)Vertices.Count, lineNumber);

                        currFaceGroup.AddFace(new Face(iTemp));
                    }
                    else if (!HasTexCoords && HasNormals)
                    {
                        //f v//n v//n v//n (v//n)
                        s = sLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                        faceSize = s.Length / 3;
                        isQuad = (faceSize == 4);

                        iTemp[0] = isQuad ? new uint[4] : new uint[3];

                        uint.TryParse(s[0], out iTemp[0][0]);
                        uint.TryParse(s[2], out iTemp[0][1]);
                        uint.TryParse(s[4], out iTemp[0][2]);

                        if (isQuad)
                            uint.TryParse(s[6], out iTemp[0][3]);

                        iTemp[2] = isQuad ? new uint[4] : new uint[3];

                        uint.TryParse(s[1], out iTemp[2][0]);
                        uint.TryParse(s[3], out iTemp[2][1]);
                        uint.TryParse(s[5], out iTemp[2][2]);

                        if (isQuad)
                            uint.TryParse(s[7], out iTemp[2][3]);

                        Assertion(iTemp[0][0] - 1, (uint)Vertices.Count, lineNumber);
                        Assertion(iTemp[0][1] - 1, (uint)Vertices.Count, lineNumber);
                        Assertion(iTemp[0][2] - 1, (uint)Vertices.Count, lineNumber);

                        if (isQuad)
                            Assertion(iTemp[0][3] - 1, (uint)Vertices.Count, lineNumber);

                        Assertion(iTemp[2][0] - 1, (uint)Normals.Count, lineNumber);
                        Assertion(iTemp[2][1] - 1, (uint)Normals.Count, lineNumber);
                        Assertion(iTemp[2][2] - 1, (uint)Normals.Count, lineNumber);

                        if (isQuad)
                            Assertion(iTemp[2][3] - 1, (uint)Vertices.Count, lineNumber);

                        currFaceGroup.AddFace(new Face(iTemp));
                    }
                    else
                    {
                        s = sLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                        faceSize = s.Length / 3;
                        isQuad = (faceSize == 4);

                        iTemp[0] = isQuad ? new uint[4] : new uint[3];
                        uint.TryParse(s[0], out iTemp[0][0]);
                        uint.TryParse(s[1], out iTemp[0][1]);
                        uint.TryParse(s[2], out iTemp[0][2]);

                        if (isQuad)
                            uint.TryParse(s[3], out iTemp[0][3]);

                        Assertion(iTemp[0][0] - 1, (uint)Vertices.Count, lineNumber);
                        Assertion(iTemp[0][1] - 1, (uint)Vertices.Count, lineNumber);
                        Assertion(iTemp[0][2] - 1, (uint)Vertices.Count, lineNumber);

                        if (isQuad)
                            Assertion(iTemp[0][3] - 1, (uint)Vertices.Count, lineNumber);

                        currFaceGroup.AddFace(new Face(iTemp));
                    }
                }
                else if (c == 'm') //Material Library File
                {
                    sLine = sr.ReadLine();
                    s = sLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                    MaterialList = Material.ParseMaterials(relativePath + s[1]);
                }
                else if (c == 'u')
                {
                    sr.Read(); //'s'
                    sr.Read(); //'e'
                    sr.Read(); //'m'
                    sr.Read(); //'t'
                    sr.Read(); //'l'
                    sr.Read(); //' '

                    sLine = sr.ReadLine();

                    s = sLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                    if (MaterialList != null)
                    {
                        for (int i = 0; i < MaterialList.Count; i++)
                        {
                            if (MaterialList[i].matName.CompareTo(s[0]) == 0)
                            {
                                currFaceGroup.SetMaterial(MaterialList[i]);
                            }
                        }
                    }
                }
                else if (c != '\n')
                {
                    sLine = sr.ReadLine();
                }

                lineNumber++;
                ic = sr.Read();
            }

            sr.Close();
            fs.Close();

            models.Add(filename, this);

            return true;
        }
Beispiel #4
0
        public bool Load(string filename)
        {
            if (models.ContainsKey(filename))
            {
                OBJModel model = models[filename];
                LoadFromExistingModel(model);
                return(true);
            }

            string[] s;
            string   sLine;

//			char[] cLine = new char[256];
            char[]       separators = new char[] { ' ', '/' };
            FileStream   fs         = new FileStream(filename, FileMode.Open, FileAccess.Read);
            StreamReader sr         = new StreamReader(fs);

            string absolutePath = fs.Name;

            //char[] sep = new char[] { '\\', '/' };
            char[] sep = new char[1];
            sep[0] = Game.GetPathSeparator()[0];
            string[] splitAbsolutePath = absolutePath.Split(sep);

            for (int i = 0; i < splitAbsolutePath.Length - 1; i++)
            {
                relativePath += splitAbsolutePath[i] + sep[0];
            }

            int  ic         = sr.Read();
            uint lineNumber = 1;

            List <Material> MaterialList = null;

            while (ic != -1)
            {
                char c = (char)ic;

                if (c == 'g')
                {
                    sLine = sr.ReadLine().Remove(0, 1);

                    if (sLine.CompareTo("default") == 0)
                    {
                        //Console.WriteLine("WARNING: Default group name ignored.");
                    }
                    else
                    {
                        FaceGroup fg = new FaceGroup(sLine);
                        FaceGroups.Add(fg);
                        currFaceGroup = fg;
                    }
                }
                else if (c == 'v')
                {
                    float[] fTemp;
                    int     iNext = sr.Read();

                    if (iNext == ' ' || iNext == '\t')
                    {
                        fTemp = new float[3];
                        sLine = sr.ReadLine();
                        s     = sLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                        float.TryParse(s[0], out fTemp[0]);
                        float.TryParse(s[1], out fTemp[1]);
                        float.TryParse(s[2], out fTemp[2]);

                        Vertices.Add(fTemp);
                    }
                    else if (iNext == 't')
                    {
                        fTemp = new float[2];
                        sLine = sr.ReadLine();
                        s     = sLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                        float.TryParse(s[0], out fTemp[0]);
                        float.TryParse(s[1], out fTemp[1]);

                        TexCoords.Add(fTemp);

                        HasTexCoords = true;
                    }
                    else if (iNext == 'n')
                    {
                        fTemp = new float[3];
                        sLine = sr.ReadLine();
                        s     = sLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                        float.TryParse(s[0], out fTemp[0]);
                        float.TryParse(s[1], out fTemp[1]);
                        float.TryParse(s[2], out fTemp[2]);

                        Normals.Add(fTemp);

                        HasNormals = true;
                    }
                    else
                    {
                        sLine = sr.ReadLine();
                    }
                }
                else if (c == 'f')
                {
                    uint[][] iTemp = new uint[3][];
                    int      faceSize;
                    bool     isQuad = false;

                    sLine = sr.ReadLine();

                    if (HasTexCoords && HasNormals)
                    {
                        //f v/t/n v/t/n v/t/n (v/t/n)
                        s = sLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                        faceSize = s.Length / 3;
                        isQuad   = (faceSize == 4);

                        iTemp[0] = isQuad ? new uint[4] : new uint[3];
                        uint.TryParse(s[0], out iTemp[0][0]);
                        uint.TryParse(s[3], out iTemp[0][1]);
                        uint.TryParse(s[6], out iTemp[0][2]);

                        if (isQuad)
                        {
                            uint.TryParse(s[9], out iTemp[0][3]);
                        }

                        iTemp[1] = isQuad ? new uint[4] : new uint[3];
                        uint.TryParse(s[1], out iTemp[1][0]);
                        uint.TryParse(s[4], out iTemp[1][1]);
                        uint.TryParse(s[7], out iTemp[1][2]);

                        if (isQuad)
                        {
                            uint.TryParse(s[10], out iTemp[1][3]);
                        }

                        iTemp[2] = isQuad ? new uint[4] : new uint[3];
                        uint.TryParse(s[2], out iTemp[2][0]);
                        uint.TryParse(s[5], out iTemp[2][1]);
                        uint.TryParse(s[8], out iTemp[2][2]);

                        if (isQuad)
                        {
                            uint.TryParse(s[11], out iTemp[2][3]);
                        }

                        Assertion(iTemp[0][0] - 1, (uint)Vertices.Count, lineNumber);
                        Assertion(iTemp[0][1] - 1, (uint)Vertices.Count, lineNumber);
                        Assertion(iTemp[0][2] - 1, (uint)Vertices.Count, lineNumber);

                        if (isQuad)
                        {
                            Assertion(iTemp[0][3] - 1, (uint)Vertices.Count, lineNumber);
                        }

                        Assertion(iTemp[1][0] - 1, (uint)TexCoords.Count, lineNumber);
                        Assertion(iTemp[1][1] - 1, (uint)TexCoords.Count, lineNumber);
                        Assertion(iTemp[1][2] - 1, (uint)TexCoords.Count, lineNumber);

                        if (isQuad)
                        {
                            Assertion(iTemp[1][3] - 1, (uint)TexCoords.Count, lineNumber);
                        }

                        Assertion(iTemp[2][0] - 1, (uint)Normals.Count, lineNumber);
                        Assertion(iTemp[2][1] - 1, (uint)Normals.Count, lineNumber);
                        Assertion(iTemp[2][2] - 1, (uint)Normals.Count, lineNumber);

                        if (isQuad)
                        {
                            Assertion(iTemp[2][3] - 1, (uint)Normals.Count, lineNumber);
                        }

                        currFaceGroup.AddFace(new Face(iTemp));
                    }
                    else if (HasTexCoords && !HasNormals)
                    {
                        //f v/t v/t v/t (v/t)
                        s = sLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                        faceSize = s.Length / 3;
                        isQuad   = (faceSize == 4);

                        iTemp[0] = isQuad ? new uint[4] : new uint[3];
                        uint.TryParse(s[0], out iTemp[0][0]);
                        uint.TryParse(s[2], out iTemp[0][1]);
                        uint.TryParse(s[4], out iTemp[0][2]);

                        if (isQuad)
                        {
                            uint.TryParse(s[6], out iTemp[0][3]);
                        }

                        iTemp[1] = isQuad ? new uint[4] : new uint[3];
                        uint.TryParse(s[1], out iTemp[1][0]);
                        uint.TryParse(s[3], out iTemp[1][1]);
                        uint.TryParse(s[5], out iTemp[1][2]);

                        if (isQuad)
                        {
                            uint.TryParse(s[7], out iTemp[1][3]);
                        }

                        Assertion(iTemp[0][0] - 1, (uint)Vertices.Count, lineNumber);
                        Assertion(iTemp[0][1] - 1, (uint)Vertices.Count, lineNumber);
                        Assertion(iTemp[0][2] - 1, (uint)Vertices.Count, lineNumber);

                        if (isQuad)
                        {
                            Assertion(iTemp[0][3] - 1, (uint)Vertices.Count, lineNumber);
                        }

                        Assertion(iTemp[1][0] - 1, (uint)TexCoords.Count, lineNumber);
                        Assertion(iTemp[1][1] - 1, (uint)TexCoords.Count, lineNumber);
                        Assertion(iTemp[1][2] - 1, (uint)TexCoords.Count, lineNumber);

                        if (isQuad)
                        {
                            Assertion(iTemp[1][3] - 1, (uint)Vertices.Count, lineNumber);
                        }

                        currFaceGroup.AddFace(new Face(iTemp));
                    }
                    else if (!HasTexCoords && HasNormals)
                    {
                        //f v//n v//n v//n (v//n)
                        s = sLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                        faceSize = s.Length / 3;
                        isQuad   = (faceSize == 4);

                        iTemp[0] = isQuad ? new uint[4] : new uint[3];

                        uint.TryParse(s[0], out iTemp[0][0]);
                        uint.TryParse(s[2], out iTemp[0][1]);
                        uint.TryParse(s[4], out iTemp[0][2]);

                        if (isQuad)
                        {
                            uint.TryParse(s[6], out iTemp[0][3]);
                        }

                        iTemp[2] = isQuad ? new uint[4] : new uint[3];

                        uint.TryParse(s[1], out iTemp[2][0]);
                        uint.TryParse(s[3], out iTemp[2][1]);
                        uint.TryParse(s[5], out iTemp[2][2]);

                        if (isQuad)
                        {
                            uint.TryParse(s[7], out iTemp[2][3]);
                        }

                        Assertion(iTemp[0][0] - 1, (uint)Vertices.Count, lineNumber);
                        Assertion(iTemp[0][1] - 1, (uint)Vertices.Count, lineNumber);
                        Assertion(iTemp[0][2] - 1, (uint)Vertices.Count, lineNumber);

                        if (isQuad)
                        {
                            Assertion(iTemp[0][3] - 1, (uint)Vertices.Count, lineNumber);
                        }

                        Assertion(iTemp[2][0] - 1, (uint)Normals.Count, lineNumber);
                        Assertion(iTemp[2][1] - 1, (uint)Normals.Count, lineNumber);
                        Assertion(iTemp[2][2] - 1, (uint)Normals.Count, lineNumber);

                        if (isQuad)
                        {
                            Assertion(iTemp[2][3] - 1, (uint)Vertices.Count, lineNumber);
                        }

                        currFaceGroup.AddFace(new Face(iTemp));
                    }
                    else
                    {
                        s = sLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                        faceSize = s.Length / 3;
                        isQuad   = (faceSize == 4);

                        iTemp[0] = isQuad ? new uint[4] : new uint[3];
                        uint.TryParse(s[0], out iTemp[0][0]);
                        uint.TryParse(s[1], out iTemp[0][1]);
                        uint.TryParse(s[2], out iTemp[0][2]);

                        if (isQuad)
                        {
                            uint.TryParse(s[3], out iTemp[0][3]);
                        }

                        Assertion(iTemp[0][0] - 1, (uint)Vertices.Count, lineNumber);
                        Assertion(iTemp[0][1] - 1, (uint)Vertices.Count, lineNumber);
                        Assertion(iTemp[0][2] - 1, (uint)Vertices.Count, lineNumber);

                        if (isQuad)
                        {
                            Assertion(iTemp[0][3] - 1, (uint)Vertices.Count, lineNumber);
                        }

                        currFaceGroup.AddFace(new Face(iTemp));
                    }
                }
                else if (c == 'm')                 //Material Library File
                {
                    sLine        = sr.ReadLine();
                    s            = sLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                    MaterialList = Material.ParseMaterials(relativePath + s[1]);
                }
                else if (c == 'u')
                {
                    sr.Read();                     //'s'
                    sr.Read();                     //'e'
                    sr.Read();                     //'m'
                    sr.Read();                     //'t'
                    sr.Read();                     //'l'
                    sr.Read();                     //' '

                    sLine = sr.ReadLine();

                    s = sLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                    if (MaterialList != null)
                    {
                        for (int i = 0; i < MaterialList.Count; i++)
                        {
                            if (MaterialList[i].matName.CompareTo(s[0]) == 0)
                            {
                                currFaceGroup.SetMaterial(MaterialList[i]);
                            }
                        }
                    }
                }
                else if (c != '\n')
                {
                    sLine = sr.ReadLine();
                }

                lineNumber++;
                ic = sr.Read();
            }

            sr.Close();
            fs.Close();

            models.Add(filename, this);

            return(true);
        }