// generate raw patch data from source rib file public static void main(string[] args) { try { Systems.Parser p = new Systems.Parser("gumbo.rib"); int begins = 1; Console.WriteLine("{"); 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); } Console.WriteLine("{"); foreach (float v in patch) Console.WriteLine(" {0},\n", v);//was %g Console.WriteLine("},"); } } else if (p.peekNextToken("Translate")) { Maths.Matrix4 t = Maths.Matrix4.translation(p.getNextFloat(), p.getNextFloat(), p.getNextFloat()); m = m.multiply(t); } else if (p.peekNextToken("Rotate")) { float angle = (float)Maths.MathUtils.toRadians(p.getNextFloat()); Maths.Matrix4 t = Maths.Matrix4.rotate(p.getNextFloat(), p.getNextFloat(), p.getNextFloat(), angle); m = m.multiply(t); } else if (p.peekNextToken("Scale")) { Maths.Matrix4 t = Maths.Matrix4.scale(p.getNextFloat(), p.getNextFloat(), p.getNextFloat()); m = m.multiply(t); } else if (p.peekNextToken("TransformEnd")) { m = Maths.Matrix4.IDENTITY; } else if (p.peekNextToken("AttributeBegin")) { begins++; } else if (p.peekNextToken("AttributeEnd")) { begins--; } else p.getNextToken(); } Console.WriteLine("};"); } catch (Exception e) { Console.WriteLine(e); } }
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.parameter("override.shader", p.getNextToken()); api.parameter("override.photons", p.getNextbool()); api.options(SunflowAPI.DEFAULT_OPTIONS); } 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.searchpath("texture", Path.GetFullPath(path)); } else if (token == "includepath") { string path = p.getNextToken(); //if (!new File(path).isAbsolute()) // path = localDir + File.separator + path; api.searchpath("include", Path.GetFullPath(path)); } else if (token == "include") { string file = p.getNextToken(); UI.printInfo(UI.Module.API, "Including: \"{0}\" ...", file); api.include(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; }