// Parse a single particle def void ParticlesManager::parseParticleDef(parser::DefTokeniser& tok) { // Standard DEF, starts with "particle <name> {" tok.assertNextToken("particle"); std::string name = tok.nextToken(); tok.assertNextToken("{"); ParticleDef pdef(name); // Any global keywords will come first, after which we get a series of // brace-delimited stages. std::string token = tok.nextToken(); while (token != "}") { if (token == "depthHack") { tok.skipTokens(1); // we don't care about depthHack } else if (token == "{") { // Parse stage ParticleStage stage(parseParticleStage(tok)); // Append to the ParticleDef pdef.appendStage(stage); } // Get next token token = tok.nextToken(); } // Add the ParticleDef to the map _particleDefs.insert(ParticleDefMap::value_type(name, pdef)); }
// Parse a single particle def void ParticlesManager::parseParticleDef(parser::DefTokeniser& tok, const std::string& filename) { // Standard DEF, starts with "particle <name> {" std::string declName = tok.nextToken(); // Check for a valid particle declaration, some .prt files contain materials if (declName != "particle") { // No particle, skip name plus whole block tok.skipTokens(1); tok.assertNextToken("{"); for (std::size_t level = 1; level > 0;) { std::string token = tok.nextToken(); if (token == "}") { level--; } else if (token == "{") { level++; } } return; } // Valid particle declaration, go ahead parsing the name std::string name = tok.nextToken(); tok.assertNextToken("{"); ParticleDefPtr pdef = findOrInsertParticleDef(name); pdef->setFilename(filename); // Let the particle construct itself from the token stream pdef->parseFromTokens(tok); }
/* // Example Primitive { patchDef2 { "textures/darkmod/stone/brick/rough_big_blocks03" ( 5 3 0 0 0 ) ( ( ( 64 -88 108 0 0 ) ( 64 -88 184 0 -1.484375 ) ( 64 -88 184 0 -1.484375 ) ) ( ( 64 -88 184 1.484375 0 ) ( 64 -88 184 1.484375 -1.484375 ) ( 64 -88 184 1.484375 -1.484375 ) ) ( ( 112 -88 184 2.421875 0 ) ( 112 -88 184 2.421875 -1.484375 ) ( 112 -88 184 2.421875 -1.484375 ) ) ( ( 160 -88 184 3.359375 0 ) ( 160 -88 184 3.359375 -1.484375 ) ( 160 -88 184 3.359375 -1.484375 ) ) ( ( 160 -88 108 4.84375 0 ) ( 160 -88 184 4.84375 -1.484375 ) ( 160 -88 184 4.84375 -1.484375 ) ) ) } } */ scene::INodePtr PatchDef2Parser::parse(parser::DefTokeniser& tok) const { scene::INodePtr node = GlobalPatchCreator(DEF2).createPatch(); IPatchNodePtr patchNode = boost::dynamic_pointer_cast<IPatchNode>(node); assert(patchNode != NULL); IPatch& patch = patchNode->getPatch(); tok.assertNextToken("{"); // Parse shader patch.setShader(tok.nextToken()); // Parse parameters tok.assertNextToken("("); // parse matrix dimensions std::size_t cols = string::convert<std::size_t>(tok.nextToken()); std::size_t rows = string::convert<std::size_t>(tok.nextToken()); patch.setDims(cols, rows); // ignore contents/flags values tok.skipTokens(3); tok.assertNextToken(")"); // Parse Patch Matrix parseMatrix(tok, patch); // Parse Footer tok.assertNextToken("}"); tok.assertNextToken("}"); patch.controlPointsChanged(); return node; }
void ParticleParameter::parseFromTokens(parser::DefTokeniser& tok) { std::string val = tok.nextToken(); try { setFrom(boost::lexical_cast<float>(val)); } catch (boost::bad_lexical_cast&) { rError() << "[particles] Bad lower value, token is '" << val << "'" << std::endl; } if (tok.peek() == "to") { // Upper value is there, parse it tok.skipTokens(1); // skip the "to" val = tok.nextToken(); try { // cut off the quotes before converting to double setTo(boost::lexical_cast<float>(val)); } catch (boost::bad_lexical_cast&) { rError() << "[particles] Bad upper value, token is '" << val << "'" << std::endl; } } else { setTo(getFrom()); } }
scene::INodePtr BrushDef3Parser::parse(parser::DefTokeniser& tok) const { // Create a new brush scene::INodePtr node = GlobalBrushCreator().createBrush(); // Cast the node, this must succeed IBrushNodePtr brushNode = boost::dynamic_pointer_cast<IBrushNode>(node); assert(brushNode != NULL); IBrush& brush = brushNode->getIBrush(); tok.assertNextToken("{"); // Parse face tokens until a closing brace is encountered while (1) { std::string token = tok.nextToken(); // Token should be either a "(" (start of face) or "}" (end of brush) if (token == "}") { break; // end of brush } else if (token == "(") // FACE { // Construct a plane and parse its values Plane3 plane; plane.normal().x() = string::to_float(tok.nextToken()); plane.normal().y() = string::to_float(tok.nextToken()); plane.normal().z() = string::to_float(tok.nextToken()); plane.dist() = -string::to_float(tok.nextToken()); // negate d tok.assertNextToken(")"); // Parse TexDef Matrix4 texdef; tok.assertNextToken("("); tok.assertNextToken("("); texdef.xx() = string::to_float(tok.nextToken()); texdef.yx() = string::to_float(tok.nextToken()); texdef.tx() = string::to_float(tok.nextToken()); tok.assertNextToken(")"); tok.assertNextToken("("); texdef.xy() = string::to_float(tok.nextToken()); texdef.yy() = string::to_float(tok.nextToken()); texdef.ty() = string::to_float(tok.nextToken()); tok.assertNextToken(")"); tok.assertNextToken(")"); // Parse Shader std::string shader = tok.nextToken(); // Parse Flags (usually each brush has all faces detail or all faces structural) IBrush::DetailFlag flag = static_cast<IBrush::DetailFlag>( string::convert<std::size_t>(tok.nextToken(), IBrush::Structural)); brush.setDetailFlag(flag); // Ignore the other two flags tok.skipTokens(2); // Finally, add the new face to the brush /*IFace& face = */brush.addFace(plane, texdef, shader); } else { std::string text = (boost::format(_("BrushDef3Parser: invalid token '%s'")) % token).str(); throw parser::ParseException(text); } } // Final outer "}" tok.assertNextToken("}"); return node; }
/* // Example Primitive { brushDef { ( -1216 -464 232 ) ( -1088 -464 232 ) ( -1088 -80 120 ) ( ( 0.031250 0 14 ) ( -0.000009 0.031250 4.471550 ) ) common/caulk 134217728 4 0 ( -1088 -464 248 ) ( -1216 -464 248 ) ( -1216 -80 136 ) ( ( 0 -0.031373 -0.147059 ) ( 0.007812 0 0.049020 ) ) common/caulk 134217728 0 0 ( -1088 -560 120 ) ( -1088 -560 136 ) ( -1088 -80 136 ) ( ( 0.031250 0 16.500000 ) ( 0 0.031250 0.250000 ) ) common/caulk 134217728 4 0 ( -1088 -80 136 ) ( -1216 -80 136 ) ( -1216 -80 8 ) ( ( 0.031250 0 2 ) ( 0 0.031250 0.250000 ) ) common/caulk 134217728 4 0 ( -1216 -400 136 ) ( -1216 -400 120 ) ( -1216 -80 120 ) ( ( 0.031250 0 -16.500000 ) ( 0 0.031250 0.250000 ) ) common/caulk 134217728 4 0 ( -1088 -464 232 ) ( -1216 -464 232 ) ( -1216 -464 248 ) ( ( 0.031250 0 -2 ) ( 0 0.031250 0.250000 ) ) common/caulk 134217728 4 0 } } */ scene::INodePtr BrushDefParser::parse(parser::DefTokeniser& tok) const { // Create a new brush scene::INodePtr node = GlobalBrushCreator().createBrush(); // Cast the node, this must succeed IBrushNodePtr brushNode = boost::dynamic_pointer_cast<IBrushNode>(node); assert(brushNode != NULL); IBrush& brush = brushNode->getIBrush(); tok.assertNextToken("{"); // Parse face tokens until a closing brace is encountered while (1) { std::string token = tok.nextToken(); // Token should be either a "(" (start of face) or "}" (end of brush) if (token == "}") { break; // end of brush } else if (token == "(") // FACE { // Parse three 3D points to construct a plane Vector3 p1(string::to_float(tok.nextToken()), string::to_float(tok.nextToken()), string::to_float(tok.nextToken())); tok.assertNextToken(")"); tok.assertNextToken("("); Vector3 p2(string::to_float(tok.nextToken()), string::to_float(tok.nextToken()), string::to_float(tok.nextToken())); tok.assertNextToken(")"); tok.assertNextToken("("); Vector3 p3(string::to_float(tok.nextToken()), string::to_float(tok.nextToken()), string::to_float(tok.nextToken())); tok.assertNextToken(")"); // Construct the plane from the three points Plane3 plane(p1, p2, p3); // Parse TexDef Matrix4 texdef; tok.assertNextToken("("); tok.assertNextToken("("); texdef.xx() = string::to_float(tok.nextToken()); texdef.yx() = string::to_float(tok.nextToken()); texdef.tx() = string::to_float(tok.nextToken()); tok.assertNextToken(")"); tok.assertNextToken("("); texdef.xy() = string::to_float(tok.nextToken()); texdef.yy() = string::to_float(tok.nextToken()); texdef.ty() = string::to_float(tok.nextToken()); tok.assertNextToken(")"); tok.assertNextToken(")"); // Parse Shader, brushDef has an implicit "textures/" not written to the map std::string shader = "textures/" + tok.nextToken(); // Parse Contents Flags (and ignore them) tok.skipTokens(3); // Finally, add the new face to the brush /*IFace& face = */brush.addFace(plane, texdef, shader); } else { std::string text = (boost::format(_("BrushDefParser: invalid token '%s'")) % token).str(); throw parser::ParseException(text); } } // Final outer "}" tok.assertNextToken("}"); return node; }