Esempio n. 1
0
        public static List <Material> ParseMaterials(string filename)
        {
            List <Material> MaterialList = new List <Material>();

            String fileName = filename;

            FileStream   fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);

            string sLine;

            char[]   seperators = new char[] { ' ', '/' };
            string[] s;
            float[]  fTemp;

            string absolutePath = fs.Name;

            char[] sep = new char[1];
            sep[0] = Game.GetPathSeparator()[0];
            string[] splitAbsolutePath = absolutePath.Split(sep);

            string relativepath = "";

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

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

            Material currMaterial = null;

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

                if (c == 'n')                 //New Material
                {
                    sLine = sr.ReadLine();
                    s     = sLine.Split(seperators, StringSplitOptions.RemoveEmptyEntries);

                    currMaterial = new Material();

                    //matName = s[1];
                    currMaterial.setName(s[1]);
                    MaterialList.Add(currMaterial);
                }
                else if (c == 'N')
                {
                    int iNext = sr.Read();

                    if (iNext == 's')
                    {
                        sLine = sr.ReadLine();
                        s     = sLine.Split(seperators, StringSplitOptions.RemoveEmptyEntries);
                        fTemp = new float[s.Length];

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

                        currMaterial.setShine((fTemp[0] / 100.0f) * 128.0f);
                    }
                    else
                    {
                        sLine = sr.ReadLine();
                    }
                }
                else if (c == 'i')
                {
                    sLine = sr.ReadLine();

                    Console.WriteLine("WARNING: Material File " + filename + " Illumination Parameter Not Supported on Line " + lineNumber);
                }
                else if (c == 'T')
                {
                    sLine = sr.ReadLine();

                    Console.WriteLine("WARNING: Material File " + filename + " Transmission Filter Parameter Not Supported on Line " + lineNumber);
                }
                else if (c == 'K')
                {
                    int iNext = sr.Read();

                    if (iNext == 'd')
                    {
                        sLine = sr.ReadLine();
                        s     = sLine.Split(seperators, StringSplitOptions.RemoveEmptyEntries);
                        fTemp = new float[s.Length];

                        float.TryParse(s[0], out fTemp[0]);
                        float.TryParse(s[1], out fTemp[1]);
                        float.TryParse(s[2], out fTemp[2]);
                        if (s.Length == 4)
                        {
                            float.TryParse(s[3], out fTemp[3]);
                        }

                        //setDiffuse(fTemp[0], fTemp[1], fTemp[2], fTemp.Length == 4 ? fTemp[3] : 1.0f);
                        currMaterial.setDiffuse(fTemp[0], fTemp[1], fTemp[2], fTemp.Length == 4 ? fTemp[3] : 1.0f);
                    }
                    else if (iNext == 'a')
                    {
                        sLine = sr.ReadLine();
                        s     = sLine.Split(seperators, StringSplitOptions.RemoveEmptyEntries);
                        fTemp = new float[s.Length];

                        float.TryParse(s[0], out fTemp[0]);
                        float.TryParse(s[1], out fTemp[1]);
                        float.TryParse(s[2], out fTemp[2]);
                        if (s.Length == 4)
                        {
                            float.TryParse(s[3], out fTemp[3]);
                        }

                        //setAmbient(fTemp[0], fTemp[1], fTemp[2], fTemp.Length == 4 ? fTemp[3] : 1.0f);
                        currMaterial.setAmbient(fTemp[0], fTemp[1], fTemp[2], fTemp.Length == 4 ? fTemp[3] : 1.0f);
                    }
                    else if (iNext == 's')
                    {
                        sLine = sr.ReadLine();
                        s     = sLine.Split(seperators, StringSplitOptions.RemoveEmptyEntries);
                        fTemp = new float[s.Length];

                        float.TryParse(s[0], out fTemp[0]);
                        float.TryParse(s[1], out fTemp[1]);
                        float.TryParse(s[2], out fTemp[2]);
                        if (s.Length == 4)
                        {
                            float.TryParse(s[3], out fTemp[3]);
                        }

                        //setSpecular(fTemp[0], fTemp[1], fTemp[2], fTemp.Length == 4 ? fTemp[3] : 1.0f);
                        currMaterial.setSpecular(fTemp[0], fTemp[1], fTemp[2], fTemp.Length == 4 ? fTemp[3] : 1.0f);
                    }
                }
                else if (c == 'm')                 //Texture file
                {
                    sr.Read();                     //'a'
                    sr.Read();                     //'p'
                    sr.Read();                     //'_'
                    sr.Read();                     //'K'

                    int iNext = sr.Read();

                    if (iNext == 'd' || iNext == 'a' || iNext == 's')
                    {
                        Console.WriteLine("WARNING: Material File " + filename + " Texture and Material Parameter Not Supported on Line " + lineNumber);
                        sr.Read();

                        string texFileName = sr.ReadLine();

                        //texture = new Texture(relativePath + texFileName);
                        currMaterial.setTexture(Texture.AcquireTexture(relativepath + texFileName));
                    }
                }
                else if (c != '\n')
                {
                    sLine = sr.ReadLine();
                }

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

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

            return(MaterialList);
        }
Esempio n. 2
0
        public Material(string filename)
        {
            useDefault();

            pathFileName = filename;

            FileStream   fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);

            string sLine;

            char[]   seperators = new char[] { ' ', '/' };
            string[] s;
            float[]  fTemp;

            string absolutePath = fs.Name;

            char[]   sep = new char[] { '\\' };
            string[] splitAbsolutePath = absolutePath.Split(sep);

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


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

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

                if (c == 'n')                //New Material
                {
                    sLine = sr.ReadLine();
                    s     = sLine.Split(seperators, StringSplitOptions.RemoveEmptyEntries);

                    matName = s[1];
                }
                else if (c == 'N')
                {
                    int iNext = sr.Read();

                    if (iNext == 's')
                    {
                        sLine = sr.ReadLine();
                        s     = sLine.Split(seperators, StringSplitOptions.RemoveEmptyEntries);
                        fTemp = new float[s.Length];

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

                        setShine(fTemp[0]);
                    }
                    else
                    {
                        sLine = sr.ReadLine();
                    }
                }
                else if (c == 'i')
                {
                    sLine = sr.ReadLine();

                    Console.WriteLine("WARNING: Material File " + filename + " Illumination Parameter Not Supported on Line " + lineNumber);
                }
                else if (c == 'T')
                {
                    sLine = sr.ReadLine();

                    Console.WriteLine("WARNING: Material File " + filename + " Transmission Filter Parameter Not Supported on Line " + lineNumber);
                }
                else if (c == 'K')
                {
                    int iNext = sr.Read();

                    if (iNext == 'd')
                    {
                        sLine = sr.ReadLine();
                        s     = sLine.Split(seperators, StringSplitOptions.RemoveEmptyEntries);
                        fTemp = new float[s.Length];

                        float.TryParse(s[0], out fTemp[0]);
                        float.TryParse(s[1], out fTemp[1]);
                        float.TryParse(s[2], out fTemp[2]);
                        if (s.Length == 4)
                        {
                            float.TryParse(s[3], out fTemp[3]);
                        }

                        setDiffuse(fTemp[0], fTemp[1], fTemp[2], fTemp.Length == 4 ? fTemp[3] : 1.0f);
                    }
                    else if (iNext == 'a')
                    {
                        sLine = sr.ReadLine();
                        s     = sLine.Split(seperators, StringSplitOptions.RemoveEmptyEntries);
                        fTemp = new float[s.Length];

                        float.TryParse(s[0], out fTemp[0]);
                        float.TryParse(s[1], out fTemp[1]);
                        float.TryParse(s[2], out fTemp[2]);
                        if (s.Length == 4)
                        {
                            float.TryParse(s[3], out fTemp[3]);
                        }

                        setAmbient(fTemp[0], fTemp[1], fTemp[2], fTemp.Length == 4 ? fTemp[3] : 1.0f);
                    }
                    else if (iNext == 's')
                    {
                        sLine = sr.ReadLine();
                        s     = sLine.Split(seperators, StringSplitOptions.RemoveEmptyEntries);
                        fTemp = new float[s.Length];

                        float.TryParse(s[0], out fTemp[0]);
                        float.TryParse(s[1], out fTemp[1]);
                        float.TryParse(s[2], out fTemp[2]);
                        if (s.Length == 4)
                        {
                            float.TryParse(s[3], out fTemp[3]);
                        }

                        setSpecular(fTemp[0], fTemp[1], fTemp[2], fTemp.Length == 4 ? fTemp[3] : 1.0f);
                    }
                }
                else if (c == 'm')                 //Texture file
                {
                    sr.Read();                     //'a'
                    sr.Read();                     //'p'
                    sr.Read();                     //'_'
                    sr.Read();                     //'K'

                    int iNext = sr.Read();

                    if (iNext == 'd' || iNext == 'a' || iNext == 's')
                    {
                        Console.WriteLine("WARNING: Material File " + filename + " Texture and Material Parameter Not Supported on Line " + lineNumber);
                        sr.Read();

                        string texFileName = sr.ReadLine();

                        texture = Texture.AcquireTexture(relativePath + texFileName);
                    }
                }
                else if (c != '\n')
                {
                    sLine = sr.ReadLine();
                }

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


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