Beispiel #1
0
        private float[] parseFloatArray(SunflowSharp.Systems.Parser p)
        {
            List <float> array = new List <float>();
            bool         done  = false;

            do
            {
                string s = p.getNextToken();
                if (s.StartsWith("["))
                {
                    s = s.Substring(1);
                }
                if (s.EndsWith("]"))
                {
                    s    = s.Substring(0, s.Length - 1);
                    done = true;
                }
                array.Add(float.Parse(s, System.Globalization.CultureInfo.InvariantCulture));
            } while (!done);
            return(array.ToArray());
        }
Beispiel #2
0
        private int[] parseIntArray(SunflowSharp.Systems.Parser p)
        {
            List <int> array = new List <int>();
            bool       done  = false;

            do
            {
                string s = p.getNextToken();
                if (s.StartsWith("["))
                {
                    s = s.Substring(1);
                }
                if (s.EndsWith("]"))
                {
                    s    = s.Substring(0, s.Length - 1);
                    done = true;
                }
                array.Add(int.Parse(s));
            } while (!done);
            return(array.ToArray());
        }
Beispiel #3
0
        public override bool parse(Stream stream, SunflowAPI api)
        {
            try
            {
                SunflowSharp.Systems.Parser p = new SunflowSharp.Systems.Parser(stream);
                p.checkNextToken("version");
                p.checkNextToken("3.04");
                p.checkNextToken("TransformBegin");

                if (p.peekNextToken("Procedural"))
                {
                    // read procedural shave rib
                    bool done1 = false;
                    while (!done1)
                    {
                        p.checkNextToken("DelayedReadArchive");
                        p.checkNextToken("[");
                        string f = p.getNextToken();
                        UI.printInfo(UI.Module.USER, "RIB - Reading voxel: \"{0}\" ...", f);
                        api.include(f);
                        p.checkNextToken("]");
                        while (true)
                        {
                            string t = p.getNextToken();
                            if (t == null || t == "TransformEnd")
                            {
                                done1 = true;
                                break;
                            }
                            else if (t == "Procedural")
                                break;
                        }
                    }
                    return true;
                }

                bool cubic = false;
                if (p.peekNextToken("Basis"))
                {
                    cubic = true;
                    // u basis
                    p.checkNextToken("catmull-rom");
                    p.checkNextToken("1");
                    // v basis
                    p.checkNextToken("catmull-rom");
                    p.checkNextToken("1");
                }
                while (p.peekNextToken("Declare"))
                {
                    p.getNextToken(); // name
                    p.getNextToken(); // interpolation & type
                }
                int index = 0;
                bool done = false;
                p.checkNextToken("Curves");
                do
                {
                    if (cubic)
                        p.checkNextToken("cubic");
                    else
                        p.checkNextToken("linear");
                    int[] nverts = parseIntArray(p);
                    for (int i = 1; i < nverts.Length; i++)
                    {
                        if (nverts[0] != nverts[i])
                        {
                            UI.printError(UI.Module.USER, "RIB - Found variable number of hair segments");
                            return false;
                        }
                    }
                    int nhairs = nverts.Length;

                    UI.printInfo(UI.Module.USER, "RIB - Parsed {0} hair curves", nhairs);

                    api.parameter("segments", nverts[0] - 1);

                    p.checkNextToken("nonperiodic");
                    p.checkNextToken("P");
                    float[] points = parseFloatArray(p);
                    if (points.Length != 3 * nhairs * nverts[0])
                    {
                        UI.printError(UI.Module.USER, "RIB - Invalid number of points - expecting {0} - found {0}", nhairs * nverts[0], points.Length / 3);
                        return false;
                    }
                    api.parameter("points", "point", "vertex", points);

                    UI.printInfo(UI.Module.USER, "RIB - Parsed {0} hair vertices", points.Length / 3);

                    p.checkNextToken("width");
                    float[] w = parseFloatArray(p);
                    if (w.Length != nhairs * nverts[0])
                    {
                        UI.printError(UI.Module.USER, "RIB - Invalid number of hair widths - expecting {0} - found {0}", nhairs * nverts[0], w.Length);
                        return false;
                    }
                    api.parameter("widths", "float", "vertex", w);

                    UI.printInfo(UI.Module.USER, "RIB - Parsed {0} hair widths", w.Length);

                    string name = string.Format("{0}[{1}]", "[Stream]", index);
                    UI.printInfo(UI.Module.USER, "RIB - Creating hair object \"{0}\"", name);
                    api.geometry(name, "hair");
                    api.instance(name + ".instance", name);

                    UI.printInfo(UI.Module.USER, "RIB - Searching for next curve group ...");
                    while (true)
                    {
                        string t = p.getNextToken();
                        if (t == null || t == "TransformEnd")
                        {
                            done = true;
                            break;
                        }
                        else if (t == "Curves")
                            break;
                    }
                    index++;
                } while (!done);
                UI.printInfo(UI.Module.USER, "RIB - Finished reading rib file");
            }
            catch (Exception e)
            {
                UI.printError(UI.Module.USER, "RIB - File not found: {0}", "[Stream]");
                return false;
            }
            return true;
        }
Beispiel #4
0
        public override bool parse(Stream stream, SunflowAPI api)
        {
            try
            {
                SunflowSharp.Systems.Parser p = new SunflowSharp.Systems.Parser(stream);
                p.checkNextToken("version");
                p.checkNextToken("3.04");
                p.checkNextToken("TransformBegin");

                if (p.peekNextToken("Procedural"))
                {
                    // read procedural shave rib
                    bool done1 = false;
                    while (!done1)
                    {
                        p.checkNextToken("DelayedReadArchive");
                        p.checkNextToken("[");
                        string f = p.getNextToken();
                        UI.printInfo(UI.Module.USER, "RIB - Reading voxel: \"{0}\" ...", f);
                        api.include(f);
                        p.checkNextToken("]");
                        while (true)
                        {
                            string t = p.getNextToken();
                            if (t == null || t == "TransformEnd")
                            {
                                done1 = true;
                                break;
                            }
                            else if (t == "Procedural")
                            {
                                break;
                            }
                        }
                    }
                    return(true);
                }

                bool cubic = false;
                if (p.peekNextToken("Basis"))
                {
                    cubic = true;
                    // u basis
                    p.checkNextToken("catmull-rom");
                    p.checkNextToken("1");
                    // v basis
                    p.checkNextToken("catmull-rom");
                    p.checkNextToken("1");
                }
                while (p.peekNextToken("Declare"))
                {
                    p.getNextToken(); // name
                    p.getNextToken(); // interpolation & type
                }
                int  index = 0;
                bool done  = false;
                p.checkNextToken("Curves");
                do
                {
                    if (cubic)
                    {
                        p.checkNextToken("cubic");
                    }
                    else
                    {
                        p.checkNextToken("linear");
                    }
                    int[] nverts = parseIntArray(p);
                    for (int i = 1; i < nverts.Length; i++)
                    {
                        if (nverts[0] != nverts[i])
                        {
                            UI.printError(UI.Module.USER, "RIB - Found variable number of hair segments");
                            return(false);
                        }
                    }
                    int nhairs = nverts.Length;

                    UI.printInfo(UI.Module.USER, "RIB - Parsed {0} hair curves", nhairs);

                    api.parameter("segments", nverts[0] - 1);

                    p.checkNextToken("nonperiodic");
                    p.checkNextToken("P");
                    float[] points = parseFloatArray(p);
                    if (points.Length != 3 * nhairs * nverts[0])
                    {
                        UI.printError(UI.Module.USER, "RIB - Invalid number of points - expecting {0} - found {0}", nhairs * nverts[0], points.Length / 3);
                        return(false);
                    }
                    api.parameter("points", "point", "vertex", points);

                    UI.printInfo(UI.Module.USER, "RIB - Parsed {0} hair vertices", points.Length / 3);

                    p.checkNextToken("width");
                    float[] w = parseFloatArray(p);
                    if (w.Length != nhairs * nverts[0])
                    {
                        UI.printError(UI.Module.USER, "RIB - Invalid number of hair widths - expecting {0} - found {0}", nhairs * nverts[0], w.Length);
                        return(false);
                    }
                    api.parameter("widths", "float", "vertex", w);

                    UI.printInfo(UI.Module.USER, "RIB - Parsed {0} hair widths", w.Length);

                    string name = string.Format("{0}[{1}]", "[Stream]", index);
                    UI.printInfo(UI.Module.USER, "RIB - Creating hair object \"{0}\"", name);
                    api.geometry(name, "hair");
                    api.instance(name + ".instance", name);

                    UI.printInfo(UI.Module.USER, "RIB - Searching for next curve group ...");
                    while (true)
                    {
                        string t = p.getNextToken();
                        if (t == null || t == "TransformEnd")
                        {
                            done = true;
                            break;
                        }
                        else if (t == "Curves")
                        {
                            break;
                        }
                    }
                    index++;
                } while (!done);
                UI.printInfo(UI.Module.USER, "RIB - Finished reading rib file");
            }
            catch (Exception e)
            {
                UI.printError(UI.Module.USER, "RIB - File not found: {0}", "[Stream]");
                return(false);
            }
            return(true);
        }
Beispiel #5
0
 public override bool parse(Stream stream, SunflowAPI api)
 {
     //string localDir = Path.GetFullPath(filename);
     numLightSamples = 1;
     Timer timer = new Timer();
     timer.start();
     UI.printInfo(UI.Module.API, "Parsing stream ...");
     try
     {
         p = new Systems.Parser(stream);
         while (true)
         {
             string token = p.getNextToken();
             if (token == null)
                 break;
             if (token == "image")
             {
                 UI.printInfo(UI.Module.API, "Reading image settings ...");
                 parseImageBlock(api);
             }
             else if (token == "background")
             {
                 UI.printInfo(UI.Module.API, "Reading background ...");
                 parseBackgroundBlock(api);
             }
             else if (token == "accel")
             {
                 UI.printInfo(UI.Module.API, "Reading accelerator type ...");
                 p.getNextToken();
                 UI.printWarning(UI.Module.API, "Setting accelerator type is not recommended - ignoring");
             }
             else if (token == "filter")
             {
                 UI.printInfo(UI.Module.API, "Reading image filter type ...");
                 parseFilter(api);
             }
             else if (token == "bucket")
             {
                 UI.printInfo(UI.Module.API, "Reading bucket settings ...");
                 api.parameter("bucket.size", p.getNextInt());
                 api.parameter("bucket.order", p.getNextToken());
                 api.options(SunflowAPI.DEFAULT_OPTIONS);
             }
             else if (token == "photons")
             {
                 UI.printInfo(UI.Module.API, "Reading photon settings ...");
                 parsePhotonBlock(api);
             }
             else if (token == "gi")
             {
                 UI.printInfo(UI.Module.API, "Reading global illumination settings ...");
                 parseGIBlock(api);
             }
             else if (token == "lightserver")
             {
                 UI.printInfo(UI.Module.API, "Reading light server settings ...");
                 parseLightserverBlock(api);
             }
             else if (token == "trace-depths")
             {
                 UI.printInfo(UI.Module.API, "Reading trace depths ...");
                 parseTraceBlock(api);
             }
             else if (token == "camera")
             {
                 parseCamera(api);
             }
             else if (token == "shader")
             {
                 if (!parseShader(api))
                     return false;
             }
             else if (token == "modifier")
             {
                 if (!parseModifier(api))
                     return false;
             }
             else if (token == "override")
             {
                 api.shaderOverride(p.getNextToken(), p.getNextbool());
             }
             else if (token == "object")
             {
                 parseObjectBlock(api);
             }
             else if (token == "instance")
             {
                 parseInstanceBlock(api);
             }
             else if (token == "light")
             {
                 parseLightBlock(api);
             }
             else if (token == "texturepath")
             {
                 string path = p.getNextToken();
                 //if (!new File(path).isAbsolute())
                 //    path = localDir + File.separator + path;
                 api.addTextureSearchPath(Path.GetFullPath(path));
             }
             else if (token == "includepath")
             {
                 string path = p.getNextToken();
                 //if (!new File(path).isAbsolute())
                 //    path = localDir + File.separator + path;
                 api.addIncludeSearchPath(Path.GetFullPath(path));
             }
             else if (token == "include")
             {
                 string file = p.getNextToken();
                 UI.printInfo(UI.Module.API, "Including: \"{0}\" ...", file);
                 api.parse(file);
             }
             else
                 UI.printWarning(UI.Module.API, "Unrecognized token {0}", token);
         }
         p.close();
     }
     catch (Exception e)
     {
         UI.printError(UI.Module.API, "{0}", e);
         return false;
     }
     timer.end();
     UI.printInfo(UI.Module.API, "Done parsing.");
     UI.printInfo(UI.Module.API, "Parsing time: {0}", timer.ToString());
     return true;
 }