⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scparser.java

📁 Sunflow是一个照片级的渲染系统
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        p.checkNextToken("}");
        return true;
    }

    private void parseObjectBlock(SunflowAPI api) throws ParserException, IOException {
        p.checkNextToken("{");
        boolean noInstance = false;
        Matrix4 transform = null;
        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"))
                transform = 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 = api.getUniqueName(type);
        if (type.equals("mesh")) {
            UI.printWarning(Module.API, "Deprecated object type: mesh");
            UI.printInfo(Module.API, "Reading mesh: %s ...", 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, new TriangleMesh());
        } else if (type.equals("flat-mesh")) {
            UI.printWarning(Module.API, "Deprecated object type: flat-mesh");
            UI.printInfo(Module.API, "Reading flat mesh: %s ...", 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, new TriangleMesh());
        } else if (type.equals("sphere")) {
            UI.printInfo(Module.API, "Reading sphere ...");
            api.geometry(name, new 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);
                noInstance = true; // disable future auto-instancing because
                // instance has already been created
            }
        } else if (type.equals("banchoff")) {
            UI.printInfo(Module.API, "Reading banchoff ...");
            api.geometry(name, new BanchoffSurface());
        } else if (type.equals("torus")) {
            UI.printInfo(Module.API, "Reading torus ...");
            p.checkNextToken("r");
            api.parameter("radiusInner", p.getNextFloat());
            api.parameter("radiusOuter", p.getNextFloat());
            api.geometry(name, new Torus());
        } else if (type.equals("plane")) {
            UI.printInfo(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, new Plane());
        } else if (type.equals("cornellbox")) {
            UI.printInfo(Module.API, "Reading cornell box ...");
            if (transform != null)
                UI.printWarning(Module.API, "Instancing is not supported on cornell box -- ignoring transform");
            p.checkNextToken("corner0");
            api.parameter("corner0", parsePoint());
            p.checkNextToken("corner1");
            api.parameter("corner1", parsePoint());
            p.checkNextToken("left");
            api.parameter("leftColor", parseColor());
            p.checkNextToken("right");
            api.parameter("rightColor", parseColor());
            p.checkNextToken("top");
            api.parameter("topColor", parseColor());
            p.checkNextToken("bottom");
            api.parameter("bottomColor", parseColor());
            p.checkNextToken("back");
            api.parameter("backColor", parseColor());
            p.checkNextToken("emit");
            api.parameter("radiance", parseColor());
            if (p.peekNextToken("samples"))
                api.parameter("samples", p.getNextInt());
            new CornellBox().init(name, api);
            noInstance = true; // instancing is handled natively by the init
            // method
        } else if (type.equals("generic-mesh")) {
            UI.printInfo(Module.API, "Reading generic mesh: %s ... ", 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, new TriangleMesh());
        } else if (type.equals("hair")) {
            UI.printInfo(Module.API, "Reading hair curves: %s ... ", 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, new Hair());
        } else if (type.equals("janino-tesselatable")) {
            UI.printInfo(Module.API, "Reading procedural primitive: %s ... ", name);
            String code = p.getNextCodeBlock();
            try {
                Tesselatable tess = (Tesselatable) ClassBodyEvaluator.createFastClassBodyEvaluator(new Scanner(null, new StringReader(code)), Tesselatable.class, ClassLoader.getSystemClassLoader());
                api.geometry(name, tess);
            } catch (CompileException e) {
                UI.printDetailed(Module.API, "Compiling: %s", code);
                UI.printError(Module.API, "%s", e.getMessage());
                e.printStackTrace();
                noInstance = true;
            } catch (ParseException e) {
                UI.printDetailed(Module.API, "Compiling: %s", code);
                UI.printError(Module.API, "%s", e.getMessage());
                e.printStackTrace();
                noInstance = true;
            } catch (ScanException e) {
                UI.printDetailed(Module.API, "Compiling: %s", code);
                UI.printError(Module.API, "%s", e.getMessage());
                e.printStackTrace();
                noInstance = true;
            } catch (IOException e) {
                UI.printDetailed(Module.API, "Compiling: %s", code);
                UI.printError(Module.API, "%s", e.getMessage());
                e.printStackTrace();
                noInstance = true;
            }
        } else if (type.equals("teapot")) {
            UI.printInfo(Module.API, "Reading teapot: %s ... ", name);
            boolean hasTesselationArguments = false;
            if (p.peekNextToken("subdivs")) {
                api.parameter("subdivs", p.getNextInt());
                hasTesselationArguments = true;
            }
            if (p.peekNextToken("smooth")) {
                api.parameter("smooth", p.getNextBoolean());
                hasTesselationArguments = true;
            }
            if (hasTesselationArguments)
                api.geometry(name, (Tesselatable) new Teapot());
            else
                api.geometry(name, (PrimitiveList) new Teapot());
        } else if (type.equals("gumbo")) {
            UI.printInfo(Module.API, "Reading gumbo: %s ... ", name);
            boolean hasTesselationArguments = false;
            if (p.peekNextToken("subdivs")) {
                api.parameter("subdivs", p.getNextInt());
                hasTesselationArguments = true;
            }
            if (p.peekNextToken("smooth")) {
                api.parameter("smooth", p.getNextBoolean());
                hasTesselationArguments = true;
            }
            if (hasTesselationArguments)
                api.geometry(name, (Tesselatable) new Gumbo());
            else
                api.geometry(name, (PrimitiveList) new Gumbo());
        } else if (type.equals("julia")) {
            UI.printInfo(Module.API, "Reading julia fractal: %s ... ", 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, new JuliaFractal());
        } else if (type.equals("particles") || type.equals("dlasurface")) {
            if (type.equals("dlasurface"))
                UI.printWarning(Module.API, "Deprecated object type: \"dlasurface\" - please use \"particles\" instead");
            p.checkNextToken("filename");
            String filename = p.getNextToken();
            boolean littleEndian = false;
            if (p.peekNextToken("little_endian"))
                littleEndian = true;
            UI.printInfo(Module.USER, "Loading particle file: %s", 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();
            float[] data = new float[buffer.capacity()];
            for (int i = 0; i < data.length; i++)
                data[i] = buffer.get(i);
            stream.close();
            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, new ParticleSurface());
        } else if (type.equals("file-mesh")) {
            UI.printInfo(Module.API, "Reading file mesh: %s ... ", name);
            p.checkNextToken("filename");
            api.parameter("filename", p.getNextToken());
            if (p.peekNextToken("smooth_normals"))
                api.parameter("smooth_normals", p.getNextBoolean());
            api.geometry(name, new FileMesh());
        } else if (type.equals("bezier-mesh")) {
            UI.printInfo(Module.API, "Reading bezier mesh: %s ... ", 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.getNextBoolean());
                api.parameter("vwrap", p.getNextBoolean());

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -