Пример #1
0
        private void parseObjectBlock(SunflowAPI api)
        {
            p.checkNextToken("{");
            bool noInstance = false;
            Matrix4[] transform = null;
              float transformTime0 = 0, transformTime1 = 0;
            string name = null;
            string[] shaders = null;
            string[] modifiers = null;
            if (p.peekNextToken("noinstance"))
            {
                // this indicates that the geometry is to be created, but not
                // instanced into the scene
                noInstance = true;
            }
            else
            {
                // these are the parameters to be passed to the instance
                if (p.peekNextToken("shaders"))
                {
                    int n = p.getNextInt();
                    shaders = new string[n];
                    for (int i = 0; i < n; i++)
                        shaders[i] = p.getNextToken();
                }
                else
                {
                    p.checkNextToken("shader");
                    shaders = new string[] { p.getNextToken() };
                }
                if (p.peekNextToken("modifiers"))
                {
                    int n = p.getNextInt();
                    modifiers = new string[n];
                    for (int i = 0; i < n; i++)
                        modifiers[i] = p.getNextToken();
                }
                else if (p.peekNextToken("modifier"))
                    modifiers = new string[] { p.getNextToken() };
            if (p.peekNextToken("transform")) {
              if (p.peekNextToken("steps")) {
            transform = new Matrix4[p.getNextInt()];
            p.checkNextToken("times");
            transformTime0 = p.getNextFloat();
            transformTime1 = p.getNextFloat();
            for (int i = 0; i < transform.Length; i++)
              transform[i] = parseMatrix();
              } else
            transform = new Matrix4[] { parseMatrix() };
            }
            }
            if (p.peekNextToken("accel"))
                api.parameter("accel", p.getNextToken());
            p.checkNextToken("type");
            string type = p.getNextToken();
            if (p.peekNextToken("name"))
                name = p.getNextToken();
            else
            name = generateUniqueName(type);
            if (type == "mesh")
            {
                UI.printWarning(UI.Module.API, "Deprecated object type: mesh");
                UI.printInfo(UI.Module.API, "Reading mesh: {0} ...", name);
                int numVertices = p.getNextInt();
                int numTriangles = p.getNextInt();
                float[] points = new float[numVertices * 3];
                float[] normals = new float[numVertices * 3];
                float[] uvs = new float[numVertices * 2];
                for (int i = 0; i < numVertices; i++)
                {
                    p.checkNextToken("v");
                    points[3 * i + 0] = p.getNextFloat();
                    points[3 * i + 1] = p.getNextFloat();
                    points[3 * i + 2] = p.getNextFloat();
                    normals[3 * i + 0] = p.getNextFloat();
                    normals[3 * i + 1] = p.getNextFloat();
                    normals[3 * i + 2] = p.getNextFloat();
                    uvs[2 * i + 0] = p.getNextFloat();
                    uvs[2 * i + 1] = p.getNextFloat();
                }
                int[] triangles = new int[numTriangles * 3];
                for (int i = 0; i < numTriangles; i++)
                {
                    p.checkNextToken("t");
                    triangles[i * 3 + 0] = p.getNextInt();
                    triangles[i * 3 + 1] = p.getNextInt();
                    triangles[i * 3 + 2] = p.getNextInt();
                }
                // create geometry
                api.parameter("triangles", triangles);
                api.parameter("points", "point", "vertex", points);
                api.parameter("normals", "vector", "vertex", normals);
                api.parameter("uvs", "texcoord", "vertex", uvs);
                api.geometry(name, "triangle_mesh");
            }
            else if (type == "flat-mesh")
            {
                UI.printWarning(UI.Module.API, "Deprecated object type: flat-mesh");
                UI.printInfo(UI.Module.API, "Reading flat mesh: {0} ...", name);
                int numVertices = p.getNextInt();
                int numTriangles = p.getNextInt();
                float[] points = new float[numVertices * 3];
                float[] uvs = new float[numVertices * 2];
                for (int i = 0; i < numVertices; i++)
                {
                    p.checkNextToken("v");
                    points[3 * i + 0] = p.getNextFloat();
                    points[3 * i + 1] = p.getNextFloat();
                    points[3 * i + 2] = p.getNextFloat();
                    p.getNextFloat();
                    p.getNextFloat();
                    p.getNextFloat();
                    uvs[2 * i + 0] = p.getNextFloat();
                    uvs[2 * i + 1] = p.getNextFloat();
                }
                int[] triangles = new int[numTriangles * 3];
                for (int i = 0; i < numTriangles; i++)
                {
                    p.checkNextToken("t");
                    triangles[i * 3 + 0] = p.getNextInt();
                    triangles[i * 3 + 1] = p.getNextInt();
                    triangles[i * 3 + 2] = p.getNextInt();
                }
                // create geometry
                api.parameter("triangles", triangles);
                api.parameter("points", "point", "vertex", points);
                api.parameter("uvs", "texcoord", "vertex", uvs);
            api.geometry(name, "triangle_mesh");
            }
            else if (type == "sphere")
            {
                UI.printInfo(UI.Module.API, "Reading sphere ...");
            api.geometry(name, "sphere");
                if (transform == null && !noInstance)
                {
                    // legacy method of specifying transformation for spheres
                    p.checkNextToken("c");
                    float x = p.getNextFloat();
                    float y = p.getNextFloat();
                    float z = p.getNextFloat();
                    p.checkNextToken("r");
                    float radius = p.getNextFloat();
                    api.parameter("transform", Matrix4.translation(x, y, z).multiply(Matrix4.scale(radius)));
                    api.parameter("shaders", shaders);
                    if (modifiers != null)
                        api.parameter("modifiers", modifiers);
                    api.instance(name + ".instance", name);
              // disable future auto-instancing - instance has already been created
                    noInstance = true;
                }
            }
              else if (type.Equals("cylinder"))
              {
            UI.printInfo(UI.Module.API, "Reading cylinder ...");
            api.geometry(name, "cylinder");
              }
            else if (type == "banchoff")
            {
                UI.printInfo(UI.Module.API, "Reading banchoff ...");
                api.geometry(name, "banchoff");
            }
            else if (type == "torus")
            {
                UI.printInfo(UI.Module.API, "Reading torus ...");
                p.checkNextToken("r");
                api.parameter("radiusInner", p.getNextFloat());
                api.parameter("radiusOuter", p.getNextFloat());
                api.geometry(name, "torus");
            }
              else if (type.Equals("sphereflake")) {
            UI.printInfo(UI.Module.API, "Reading sphereflake ...");
            if (p.peekNextToken("level"))
              api.parameter("level", p.getNextInt());
            if (p.peekNextToken("axis"))
              api.parameter("axis", parseVector());
            if (p.peekNextToken("radius"))
              api.parameter("radius", p.getNextFloat());
            api.geometry(name, "sphereflake");
              }
            else if (type == "plane")
            {
                UI.printInfo(UI.Module.API, "Reading plane ...");
                p.checkNextToken("p");
                api.parameter("center", parsePoint());
                if (p.peekNextToken("n"))
                {
                    api.parameter("normal", parseVector());
                }
                else
                {
                    p.checkNextToken("p");
                    api.parameter("point1", parsePoint());
                    p.checkNextToken("p");
                    api.parameter("point2", parsePoint());
                }
                api.geometry(name, "plane");
            }
            else if (type == "generic-mesh")
            {
                UI.printInfo(UI.Module.API, "Reading generic mesh: {0} ... ", name);
                // parse vertices
                p.checkNextToken("points");
                int np = p.getNextInt();
                api.parameter("points", "point", "vertex", parseFloatArray(np * 3));
                // parse triangle indices
                p.checkNextToken("triangles");
                int nt = p.getNextInt();
                api.parameter("triangles", parseIntArray(nt * 3));
                // parse normals
                p.checkNextToken("normals");
                if (p.peekNextToken("vertex"))
                    api.parameter("normals", "vector", "vertex", parseFloatArray(np * 3));
                else if (p.peekNextToken("facevarying"))
                    api.parameter("normals", "vector", "facevarying", parseFloatArray(nt * 9));
                else
                    p.checkNextToken("none");
                // parse texture coordinates
                p.checkNextToken("uvs");
                if (p.peekNextToken("vertex"))
                    api.parameter("uvs", "texcoord", "vertex", parseFloatArray(np * 2));
                else if (p.peekNextToken("facevarying"))
                    api.parameter("uvs", "texcoord", "facevarying", parseFloatArray(nt * 6));
                else
                    p.checkNextToken("none");
                if (p.peekNextToken("face_shaders"))
                    api.parameter("faceshaders", parseIntArray(nt));
                api.geometry(name, "triangle_mesh");
            }
            else if (type == "hair")
            {
                UI.printInfo(UI.Module.API, "Reading hair curves: {0} ... ", name);
                p.checkNextToken("segments");
                api.parameter("segments", p.getNextInt());
                p.checkNextToken("width");
                api.parameter("widths", p.getNextFloat());
                p.checkNextToken("points");
                api.parameter("points", "point", "vertex", parseFloatArray(p.getNextInt()));
                api.geometry(name, "hair");
            }
            else if (type == "csharp-tesselatable")
            {
                UI.printInfo(UI.Module.API, "Reading procedural primitive: {0} ... ", name);
            string code = p.getNextCodeBlock();
                try
                {
              String typename = p.peekNextToken("typename") ? p.getNextToken() : PluginRegistry.tesselatablePlugins.generateUniqueName(name);
              if (!PluginRegistry.tesselatablePlugins.registerPlugin(typename, code))
            return;
              api.geometry(name, typename);

                }
                catch (Exception e)
                {
                    UI.printDetailed(UI.Module.API, "Compiling: {0}", code);
                    UI.printError(UI.Module.API, "{0}", e);
                    noInstance = true;
                }
            }
            else if (type == "teapot")
            {
                UI.printInfo(UI.Module.API, "Reading teapot: {0} ... ", name);
                if (p.peekNextToken("subdivs"))
                {
              api.parameter("subdivs", p.getNextInt());
                }
                if (p.peekNextToken("smooth"))
                {
                    api.parameter("smooth", p.getNextbool());
                }

            api.geometry(name, "teapot");
            }
            else if (type == "gumbo")
            {
                UI.printInfo(UI.Module.API, "Reading gumbo:{0} ... ", name);
                if (p.peekNextToken("subdivs"))
                {
                    api.parameter("subdivs", p.getNextInt());
                }
                if (p.peekNextToken("smooth"))
                {
                    api.parameter("smooth", p.getNextbool());
                }
                api.geometry(name, "gumbo");
            }
            else if (type == "julia")
            {
                UI.printInfo(UI.Module.API, "Reading julia fractal: {0} ... ", name);
                if (p.peekNextToken("q"))
                {
                    api.parameter("cw", p.getNextFloat());
                    api.parameter("cx", p.getNextFloat());
                    api.parameter("cy", p.getNextFloat());
                    api.parameter("cz", p.getNextFloat());
                }
                if (p.peekNextToken("iterations"))
                    api.parameter("iterations", p.getNextInt());
                if (p.peekNextToken("epsilon"))
                    api.parameter("epsilon", p.getNextFloat());
                api.geometry(name, "julia");
            }
            else if (type == "particles" || type == "dlasurface")
            {
                if (type == "dlasurface")
                    UI.printWarning(UI.Module.API, "Deprecated object type: \"dlasurface\" - please use \"particles\" instead");

            float[] data;

            if (p.peekNextToken("filename")) {

                  string filename = p.getNextToken();
                  bool littleEndian = false;
                  if (p.peekNextToken("little_endian"))
                      littleEndian = true;
                  UI.printInfo(UI.Module.USER, "Loading particle file: {0}", filename);
                  //File file = new File(filename);
                  //FileInputStream stream = new FileInputStream(filename);
                  //MappedByteBuffer map = stream.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.Length());
                  //if (littleEndian)
                  //    map.order(ByteOrder.LITTLE_ENDIAN);
                  //FloatBuffer buffer = map.asFloatBuffer();
                  BinaryReader reader = new BinaryReader(File.OpenRead(filename));
                  data = new float[reader.BaseStream.Length / 4];
              if (!littleEndian) {
            for (int i = 0; i < data.Length; i++) {
              byte[] newBytes = reader.ReadBytes(4);
              Array.Reverse(newBytes);
              data[i] = BitConverter.ToSingle(newBytes, 0);//buffer.get(i);
              //            UI.printInfo(UI.Module.USER, " particle {0}: {1}", i, data[i]);
            }
              } else {
                    for (int i = 0; i < data.Length; i++) {
                        data[i] = BitConverter.ToSingle(reader.ReadBytes(4), 0);//buffer.get(i);
              //              UI.printInfo(UI.Module.USER, " particle {0}: {1}", i, data[i]);
            }

              }

              reader.Close();

            } else {
              p.checkNextToken("points");
              int n = p.getNextInt();
              data = parseFloatArray(n * 3); // read 3n points
            }
                api.parameter("particles", "point", "vertex", data);
                if (p.peekNextToken("num"))
                    api.parameter("num", p.getNextInt());
                else
                    api.parameter("num", data.Length / 3);
                p.checkNextToken("radius");
                api.parameter("radius", p.getNextFloat());
                api.geometry(name, "particles");
            }
            else if (type == "file-mesh")
            {
                UI.printInfo(UI.Module.API, "Reading file mesh: {0} ... ", name);
                p.checkNextToken("filename");
                api.parameter("filename", p.getNextToken());
                if (p.peekNextToken("smooth_normals"))
                    api.parameter("smooth_normals", p.getNextbool());
                api.geometry(name, "file_mesh");
            }
            else if (type == "bezier-mesh")
            {
                UI.printInfo(UI.Module.API, "Reading bezier mesh: {0} ... ", name);
                p.checkNextToken("n");
                int nu, nv;
                api.parameter("nu", nu = p.getNextInt());
                api.parameter("nv", nv = p.getNextInt());
                if (p.peekNextToken("wrap"))
                {
                    api.parameter("uwrap", p.getNextbool());
                    api.parameter("vwrap", p.getNextbool());
                }
                p.checkNextToken("points");
                float[] points = new float[3 * nu * nv];
                for (int i = 0; i < points.Length; i++)
                    points[i] = p.getNextFloat();
                api.parameter("points", "point", "vertex", points);
                if (p.peekNextToken("subdivs"))
                    api.parameter("subdivs", p.getNextInt());
                if (p.peekNextToken("smooth"))
                    api.parameter("smooth", p.getNextbool());
                api.geometry(name, "bezier_mesh");
            }
            else
            {
                UI.printWarning(UI.Module.API, "Unrecognized object type: {0}", p.getNextToken());
                noInstance = true;
            }
            if (!noInstance)
            {
                // create instance
                api.parameter("shaders", shaders);
                if (modifiers != null)
                    api.parameter("modifiers", modifiers);
            if (transform != null && transform.Length > 0) {
              if (transform.Length == 1)
             api.parameter("transform", transform[0]);
              else {
            api.parameter("transform.steps", transform.Length);
            api.parameter("transform.times", "float", "none", new float[] {
              transformTime0, transformTime1 });
            for (int i = 0; i < transform.Length; i++)
              api.parameter(string.Format("transform[{0}]", i), transform[i]);
              }
            }
                api.instance(name + ".instance", name);
            }
            p.checkNextToken("}");
        }
Пример #2
0
 private void parseBackgroundBlock(SunflowAPI api)
 {
     p.checkNextToken("{");
     p.checkNextToken("color");
     api.parameter("color", null, parseColor().getRGB());
     api.shader("background.shader", "constant");
     api.geometry("background", "background");
     api.parameter("shaders", "background.shader");
     api.instance("background.instance", "background");
     p.checkNextToken("}");
 }