예제 #1
0
        public static Texture AcquireTexture(string filename)
        {
            if (textures.ContainsKey(filename))
                return textures[filename];

            Texture texture = new Texture(filename);
            textures.Add(filename, texture);

            return texture;
        }
예제 #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();
        }
예제 #3
0
 public void setTexture(Texture tex)
 {
     texture = tex;
 }