use of org.sunflow.system.Parser.ParserException 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;
}
use of org.sunflow.system.Parser.ParserException 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;
}
use of org.sunflow.system.Parser.ParserException in project joons-renderer by joonhyublee.
the class SCAsciiParser method parseMatrix.
@Override
protected Matrix4 parseMatrix() throws IOException {
if (p.peekNextToken("row")) {
return new Matrix4(parseFloatArray(16), true);
} else if (p.peekNextToken("col")) {
return new Matrix4(parseFloatArray(16), false);
} else {
Matrix4 m = Matrix4.IDENTITY;
try {
p.checkNextToken("{");
} catch (ParserException e) {
throw new IOException(e.getMessage());
}
while (!p.peekNextToken("}")) {
Matrix4 t = null;
if (p.peekNextToken("translate")) {
float x = p.getNextFloat();
float y = p.getNextFloat();
float z = p.getNextFloat();
t = Matrix4.translation(x, y, z);
} else if (p.peekNextToken("scaleu")) {
float s = p.getNextFloat();
t = Matrix4.scale(s);
} else if (p.peekNextToken("scale")) {
float x = p.getNextFloat();
float y = p.getNextFloat();
float z = p.getNextFloat();
t = Matrix4.scale(x, y, z);
} else if (p.peekNextToken("rotatex")) {
float angle = p.getNextFloat();
t = Matrix4.rotateX((float) Math.toRadians(angle));
} else if (p.peekNextToken("rotatey")) {
float angle = p.getNextFloat();
t = Matrix4.rotateY((float) Math.toRadians(angle));
} else if (p.peekNextToken("rotatez")) {
float angle = p.getNextFloat();
t = Matrix4.rotateZ((float) Math.toRadians(angle));
} else if (p.peekNextToken("rotate")) {
float x = p.getNextFloat();
float y = p.getNextFloat();
float z = p.getNextFloat();
float angle = p.getNextFloat();
t = Matrix4.rotate(x, y, z, (float) Math.toRadians(angle));
} else {
UI.printWarning(Module.API, "Unrecognized transformation type: %s", p.getNextToken());
}
if (t != null) {
m = t.multiply(m);
}
}
return m;
}
}
use of org.sunflow.system.Parser.ParserException 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);
}
}
Aggregations