Search in sources :

Example 1 with Parser

use of org.sunflow.system.Parser in project joons-renderer by joonhyublee.

the class ShaveRibParser method parse.

@Override
public boolean parse(String filename, SunflowAPIInterface api) {
    try {
        Parser p = new Parser(filename);
        p.checkNextToken("version");
        p.checkNextToken("3.04");
        p.checkNextToken("TransformBegin");
        if (p.peekNextToken("Procedural")) {
            // read procedural shave rib
            boolean done = false;
            while (!done) {
                p.checkNextToken("DelayedReadArchive");
                p.checkNextToken("[");
                String f = p.getNextToken();
                UI.printInfo(Module.USER, "RIB - Reading voxel: \"%s\" ...", f);
                api.include(f);
                p.checkNextToken("]");
                while (true) {
                    String t = p.getNextToken();
                    if (t == null || t.equals("TransformEnd")) {
                        done = true;
                        break;
                    } else if (t.equals("Procedural")) {
                        break;
                    }
                }
            }
            return true;
        }
        boolean 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")) {
            // name
            p.getNextToken();
            // interpolation & type
            p.getNextToken();
        }
        int index = 0;
        boolean 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(Module.USER, "RIB - Found variable number of hair segments");
                    return false;
                }
            }
            int nhairs = nverts.length;
            UI.printInfo(Module.USER, "RIB - Parsed %d 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(Module.USER, "RIB - Invalid number of points - expecting %d - found %d", nhairs * nverts[0], points.length / 3);
                return false;
            }
            api.parameter("points", "point", "vertex", points);
            UI.printInfo(Module.USER, "RIB - Parsed %d hair vertices", points.length / 3);
            p.checkNextToken("width");
            float[] w = parseFloatArray(p);
            if (w.length != nhairs * nverts[0]) {
                UI.printError(Module.USER, "RIB - Invalid number of hair widths - expecting %d - found %d", nhairs * nverts[0], w.length);
                return false;
            }
            api.parameter("widths", "float", "vertex", w);
            UI.printInfo(Module.USER, "RIB - Parsed %d hair widths", w.length);
            String name = String.format("%s[%d]", filename, index);
            UI.printInfo(Module.USER, "RIB - Creating hair object \"%s\"", name);
            api.geometry(name, "hair");
            api.instance(name + ".instance", name);
            UI.printInfo(Module.USER, "RIB - Searching for next curve group ...");
            while (true) {
                String t = p.getNextToken();
                if (t == null || t.equals("TransformEnd")) {
                    done = true;
                    break;
                } else if (t.equals("Curves")) {
                    break;
                }
            }
            index++;
        } while (!done);
        UI.printInfo(Module.USER, "RIB - Finished reading rib file");
    } catch (FileNotFoundException exp) {
        UI.printError(Module.USER, "RIB - File not found: %s", filename);
        Logger.getLogger(ShaveRibParser.class.getName()).log(Level.SEVERE, null, exp);
        return false;
    } catch (ParserException exp) {
        UI.printError(Module.USER, "RIB - Parser exception: %s", exp);
        Logger.getLogger(ShaveRibParser.class.getName()).log(Level.SEVERE, null, exp);
        return false;
    } catch (IOException exp) {
        UI.printError(Module.USER, "RIB - I/O exception: %s", exp);
        Logger.getLogger(ShaveRibParser.class.getName()).log(Level.SEVERE, null, exp);
        return false;
    }
    return true;
}
Also used : ParserException(org.sunflow.system.Parser.ParserException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) SceneParser(org.sunflow.core.SceneParser) Parser(org.sunflow.system.Parser)

Example 2 with Parser

use of org.sunflow.system.Parser in project joons-renderer by joonhyublee.

the class SCParser method parse.

@Override
public boolean parse(String filename, SunflowAPIInterface api) {
    String localDir = new File(filename).getAbsoluteFile().getParentFile().getAbsolutePath();
    numLightSamples = 1;
    Timer timer = new Timer();
    timer.start();
    UI.printInfo(Module.API, "Parsing \"%s\" ...", filename);
    try {
        p = new Parser(filename);
        while (true) {
            String token = p.getNextToken();
            if (token == null) {
                break;
            }
            if (token.equals("image")) {
                UI.printInfo(Module.API, "Reading image settings ...");
                parseImageBlock(api);
            } else if (token.equals(BACKGROUND)) {
                UI.printInfo(Module.API, "Reading background ...");
                parseBackgroundBlock(api);
            } else if (token.equals("accel")) {
                UI.printInfo(Module.API, "Reading accelerator type ...");
                p.getNextToken();
                UI.printWarning(Module.API, "Setting accelerator type is not recommended - ignoring");
            } else if (token.equals(FILTER)) {
                UI.printInfo(Module.API, "Reading image filter type ...");
                parseFilter(api);
            } else if (token.equals("bucket")) {
                UI.printInfo(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.equals("photons")) {
                UI.printInfo(Module.API, "Reading photon settings ...");
                parsePhotonBlock(api);
            } else if (token.equals("gi")) {
                UI.printInfo(Module.API, "Reading global illumination settings ...");
                parseGIBlock(api);
            } else if (token.equals("lightserver")) {
                UI.printInfo(Module.API, "Reading light server settings ...");
                parseLightserverBlock(api);
            } else if (token.equals("trace-depths")) {
                UI.printInfo(Module.API, "Reading trace depths ...");
                parseTraceBlock(api);
            } else if (token.equals("camera")) {
                parseCamera(api);
            } else if (token.equals(SHADER)) {
                if (!parseShader(api)) {
                    return false;
                }
            } else if (token.equals(MODIFIER)) {
                if (!parseModifier(api)) {
                    return false;
                }
            } else if (token.equals("override")) {
                api.parameter("override.shader", p.getNextToken());
                api.parameter("override.photons", p.getNextBoolean());
                api.options(SunflowAPI.DEFAULT_OPTIONS);
            } else if (token.equals("object")) {
                parseObjectBlock(api);
            } else if (token.equals("instance")) {
                parseInstanceBlock(api);
            } else if (token.equals("light")) {
                parseLightBlock(api);
            } else if (token.equals("texturepath")) {
                String path = p.getNextToken();
                if (!new File(path).isAbsolute()) {
                    path = localDir + File.separator + path;
                }
                api.searchpath(TEXTURE, path);
            } else if (token.equals("includepath")) {
                String path = p.getNextToken();
                if (!new File(path).isAbsolute()) {
                    path = localDir + File.separator + path;
                }
                api.searchpath("include", path);
            } else if (token.equals("include")) {
                String file = p.getNextToken();
                UI.printInfo(Module.API, "Including: \"%s\" ...", file);
                api.include(file);
            } else {
                UI.printWarning(Module.API, "Unrecognized token %s", token);
            }
        }
        p.close();
    } catch (ParserException e) {
        UI.printError(Module.API, "%s", e.getMessage());
        Logger.getLogger(SCParser.class.getName()).log(Level.SEVERE, null, e);
        return false;
    } catch (FileNotFoundException e) {
        UI.printError(Module.API, "%s", e.getMessage());
        return false;
    } catch (IOException e) {
        UI.printError(Module.API, "%s", e.getMessage());
        return false;
    } catch (ColorSpecificationException e) {
        UI.printError(Module.API, "%s", e.getMessage());
        return false;
    }
    timer.end();
    UI.printInfo(Module.API, "Done parsing.");
    UI.printInfo(Module.API, "Parsing time: %s", timer.toString());
    return true;
}
Also used : ParserException(org.sunflow.system.Parser.ParserException) Timer(org.sunflow.system.Timer) ColorSpecificationException(org.sunflow.image.ColorFactory.ColorSpecificationException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File) SceneParser(org.sunflow.core.SceneParser) Parser(org.sunflow.system.Parser)

Example 3 with Parser

use of org.sunflow.system.Parser in project joons-renderer by joonhyublee.

the class RA2Parser method parse.

@Override
public boolean parse(String filename, SunflowAPIInterface api) {
    try {
        UI.printInfo(Module.USER, "RA2 - Reading geometry: \"%s\" ...", filename);
        File file = new File(filename);
        FileInputStream stream = new FileInputStream(filename);
        MappedByteBuffer map = stream.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
        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("points", "point", "vertex", data);
        int[] triangles = new int[3 * (data.length / 9)];
        for (int i = 0; i < triangles.length; i++) {
            triangles[i] = i;
        }
        // create geo
        api.parameter("triangles", triangles);
        api.geometry(filename, "triangle_mesh");
        // create shader
        api.shader(filename + ".shader", "simple");
        // create instance
        api.parameter("shaders", filename + ".shader");
        api.instance(filename + ".instance", filename);
    } catch (FileNotFoundException e) {
        Logger.getLogger(RA2Parser.class.getName()).log(Level.SEVERE, null, e);
        return false;
    } catch (IOException e) {
        Logger.getLogger(RA2Parser.class.getName()).log(Level.SEVERE, null, e);
        return false;
    }
    try {
        filename = filename.replace(".ra2", ".txt");
        UI.printInfo(Module.USER, "RA2 - Reading camera  : \"%s\" ...", filename);
        Parser p = new Parser(filename);
        Point3 eye = new Point3();
        eye.x = p.getNextFloat();
        eye.y = p.getNextFloat();
        eye.z = p.getNextFloat();
        Point3 to = new Point3();
        to.x = p.getNextFloat();
        to.y = p.getNextFloat();
        to.z = p.getNextFloat();
        Vector3 up = new Vector3();
        switch(p.getNextInt()) {
            case 0:
                up.set(1, 0, 0);
                break;
            case 1:
                up.set(0, 1, 0);
                break;
            case 2:
                up.set(0, 0, 1);
                break;
            default:
                UI.printWarning(Module.USER, "RA2 - Invalid up vector specification - using Z axis");
                up.set(0, 0, 1);
                break;
        }
        api.parameter("eye", eye);
        api.parameter("target", to);
        api.parameter("up", up);
        String cameraName = filename + ".camera";
        api.parameter(FOV, 80f);
        api.camera(cameraName, "pinhole");
        api.parameter("camera", cameraName);
        api.parameter("resolutionX", 1024);
        api.parameter("resolutionY", 1024);
        api.options(SunflowAPI.DEFAULT_OPTIONS);
        p.close();
    } catch (FileNotFoundException e) {
        UI.printWarning(Module.USER, "RA2 - Camera file not found");
    } catch (IOException e) {
        Logger.getLogger(RA2Parser.class.getName()).log(Level.SEVERE, null, e);
        return false;
    }
    return true;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) FloatBuffer(java.nio.FloatBuffer) Vector3(org.sunflow.math.Vector3) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SceneParser(org.sunflow.core.SceneParser) Parser(org.sunflow.system.Parser) Point3(org.sunflow.math.Point3) MappedByteBuffer(java.nio.MappedByteBuffer) File(java.io.File)

Example 4 with Parser

use of org.sunflow.system.Parser in project joons-renderer by joonhyublee.

the class Gumbo method main.

// generate raw patch data from source rib file
public static void main(String[] args) {
    try {
        Parser p;
        p = new Parser("gumbo.rib");
        int begins = 1;
        System.out.println("{");
        Matrix4 m = Matrix4.IDENTITY;
        p.checkNextToken("AttributeBegin");
        while (begins != 0) {
            if (p.peekNextToken("Patch")) {
                p.checkNextToken("bicubic");
                p.checkNextToken("P");
                float[] patch = parseFloatArray(p);
                if (patch.length == 48) {
                    // transform patch
                    for (int i = 0; i < 16; i++) {
                        float x = patch[3 * i + 0];
                        float y = patch[3 * i + 1];
                        float z = patch[3 * i + 2];
                        patch[3 * i + 0] = m.transformPX(x, y, z);
                        patch[3 * i + 1] = m.transformPY(x, y, z);
                        patch[3 * i + 2] = m.transformPZ(x, y, z);
                    }
                    System.out.println("{");
                    for (float v : patch) {
                        System.out.printf("  %g,\n", v);
                    }
                    System.out.println("},");
                }
            } else if (p.peekNextToken("Translate")) {
                Matrix4 t = Matrix4.translation(p.getNextFloat(), p.getNextFloat(), p.getNextFloat());
                m = m.multiply(t);
            } else if (p.peekNextToken("Rotate")) {
                float angle = (float) Math.toRadians(p.getNextFloat());
                Matrix4 t = Matrix4.rotate(p.getNextFloat(), p.getNextFloat(), p.getNextFloat(), angle);
                m = m.multiply(t);
            } else if (p.peekNextToken("Scale")) {
                Matrix4 t = Matrix4.scale(p.getNextFloat(), p.getNextFloat(), p.getNextFloat());
                m = m.multiply(t);
            } else if (p.peekNextToken("TransformEnd")) {
                m = Matrix4.IDENTITY;
            } else if (p.peekNextToken("AttributeBegin")) {
                begins++;
            } else if (p.peekNextToken("AttributeEnd")) {
                begins--;
            } else {
                p.getNextToken();
            }
        }
        System.out.println("};");
    } catch (FileNotFoundException e) {
        Logger.getLogger(Gumbo.class.getName()).log(Level.SEVERE, null, e);
    } catch (IOException e) {
        Logger.getLogger(Gumbo.class.getName()).log(Level.SEVERE, null, e);
    } catch (ParserException e) {
        Logger.getLogger(Gumbo.class.getName()).log(Level.SEVERE, null, e);
    }
}
Also used : ParserException(org.sunflow.system.Parser.ParserException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Matrix4(org.sunflow.math.Matrix4) Parser(org.sunflow.system.Parser)

Example 5 with Parser

use of org.sunflow.system.Parser in project joons-renderer by joonhyublee.

the class TriParser method parse.

@Override
public boolean parse(String filename, SunflowAPIInterface api) {
    try {
        UI.printInfo(Module.USER, "TRI - Reading geometry: \"%s\" ...", filename);
        Parser p = new Parser(filename);
        float[] verts = new float[3 * p.getNextInt()];
        for (int v = 0; v < verts.length; v += 3) {
            verts[v + 0] = p.getNextFloat();
            verts[v + 1] = p.getNextFloat();
            verts[v + 2] = p.getNextFloat();
            p.getNextToken();
            p.getNextToken();
        }
        int[] triangles = new int[p.getNextInt() * 3];
        for (int t = 0; t < triangles.length; t += 3) {
            triangles[t + 0] = p.getNextInt();
            triangles[t + 1] = p.getNextInt();
            triangles[t + 2] = p.getNextInt();
        }
        // create geometry
        api.parameter("triangles", triangles);
        api.parameter("points", "point", "vertex", verts);
        api.geometry(filename, "triangle_mesh");
        // create shader
        api.shader(filename + ".shader", "simple");
        api.parameter("shaders", filename + ".shader");
        // create instance
        api.instance(filename + ".instance", filename);
        p.close();
        // output to ra3 format
        RandomAccessFile stream = new RandomAccessFile(filename.replace(".tri", ".ra3"), "rw");
        MappedByteBuffer map = stream.getChannel().map(MapMode.READ_WRITE, 0, 8 + 4 * (verts.length + triangles.length));
        map.order(ByteOrder.LITTLE_ENDIAN);
        IntBuffer ints = map.asIntBuffer();
        FloatBuffer floats = map.asFloatBuffer();
        ints.put(0, verts.length / 3);
        ints.put(1, triangles.length / 3);
        for (int i = 0; i < verts.length; i++) {
            floats.put(2 + i, verts[i]);
        }
        for (int i = 0; i < triangles.length; i++) {
            ints.put(2 + verts.length + i, triangles[i]);
        }
        stream.close();
    } catch (FileNotFoundException e) {
        Logger.getLogger(TriParser.class.getName()).log(Level.SEVERE, null, e);
        return false;
    } catch (IOException e) {
        Logger.getLogger(TriParser.class.getName()).log(Level.SEVERE, null, e);
        return false;
    }
    return true;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) MappedByteBuffer(java.nio.MappedByteBuffer) IntBuffer(java.nio.IntBuffer) FileNotFoundException(java.io.FileNotFoundException) FloatBuffer(java.nio.FloatBuffer) IOException(java.io.IOException) SceneParser(org.sunflow.core.SceneParser) Parser(org.sunflow.system.Parser)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 Parser (org.sunflow.system.Parser)5 SceneParser (org.sunflow.core.SceneParser)4 ParserException (org.sunflow.system.Parser.ParserException)3 File (java.io.File)2 FloatBuffer (java.nio.FloatBuffer)2 MappedByteBuffer (java.nio.MappedByteBuffer)2 FileInputStream (java.io.FileInputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 IntBuffer (java.nio.IntBuffer)1 ColorSpecificationException (org.sunflow.image.ColorFactory.ColorSpecificationException)1 Matrix4 (org.sunflow.math.Matrix4)1 Point3 (org.sunflow.math.Point3)1 Vector3 (org.sunflow.math.Vector3)1 Timer (org.sunflow.system.Timer)1