diff --git a/src/yaml/objectParser.cc b/src/yaml/objectParser.cc index 6638924..660a540 100644 --- a/src/yaml/objectParser.cc +++ b/src/yaml/objectParser.cc @@ -13,6 +13,7 @@ #include "material.h" #include "object.h" #include "objectBox.hh" +#include "objectPlane.hh" #include "object_sphere.h" #include "yaml/objectParser.hh" #include "yaml/vectorParser.hh" @@ -33,6 +34,9 @@ ObjectParser::ObjectParser(Scene& scene, if (tag == "!Object.Box") { mType = Type::Box; mObject.reset(new Box()); + } else if (tag == "!Object.Plane") { + mType = Type::Plane; + mObject.reset(new Plane()); } else if (tag == "!Object.Sphere") { mType = Type::Sphere; mObject.reset(new Sphere()); @@ -66,6 +70,11 @@ ObjectParser::HandleKeyEvent(const std::string& key) {"far", FarSection} }; + static const std::map sPlaneSections = { + {"normal", NormalSection}, + {"distance", DistanceSection} + }; + if (sCommonSections.count(key) > 0) { mSection = sCommonSections.at(key); return; @@ -77,6 +86,12 @@ ObjectParser::HandleKeyEvent(const std::string& key) } else { mSection = NoSection; } + } else if (mType == Type::Plane) { + if (sPlaneSections.count(key) > 0) { + mSection = sPlaneSections.at(key); + } else { + mSection = NoSection; + } } else if (mType == Type::Sphere) { if (sSphereSections.count(key) > 0) { mSection = sSphereSections.at(key); @@ -96,18 +111,31 @@ ObjectParser::HandleValueEvent(yaml_event_t& event) case ColorSection: HandleColorEvent(event); break; + + /* Spheres */ case OriginSection: HandleOriginEvent(event); break; case RadiusSection: HandleRadiusEvent(event); break; + + /* Boxes */ case NearSection: HandleNearEvent(event); break; case FarSection: HandleFarEvent(event); break; + + /* Planes */ + case NormalSection: + HandleNormalEvent(event); + break; + case DistanceSection: + HandleDistanceEvent(event); + break; + default: assert(false); break; @@ -211,4 +239,44 @@ ObjectParser::HandleFarEvent(yaml_event_t& event) GetParsers().push(new Vector3Parser(GetScene(), GetParsers(), onDone)); } + +void +ObjectParser::HandleNormalEvent(yaml_event_t& event) +{ + if (event.type != YAML_SEQUENCE_START_EVENT) { + /* TODO: Clean this up. */ + assert(event.type != YAML_SEQUENCE_START_EVENT); + return; + } + + auto onDone = [this](Vector3 normal) { + std::dynamic_pointer_cast(mObject)->SetNormal(normal); + mSection = NoSection; + SetShouldExpectKey(true); + }; + + GetParsers().push(new Vector3Parser(GetScene(), GetParsers(), onDone)); +} + + +void +ObjectParser::HandleDistanceEvent(yaml_event_t& event) +{ + if (event.type != YAML_SCALAR_EVENT) { + /* TODO: Clean this up. */ + assert(false); + } + + Double distance; + std::string scalar((char *)event.data.scalar.value, + event.data.scalar.length); + if (!ParseScalar(scalar, distance)) { + /* TODO: Clean this up. */ + assert(false); + } + std::dynamic_pointer_cast(mObject)->SetDistance(distance); + mSection = NoSection; + SetShouldExpectKey(true); +} + } /* namespace yaml */ diff --git a/src/yaml/objectParser.hh b/src/yaml/objectParser.hh index 4cf727e..ea5cd1f 100644 --- a/src/yaml/objectParser.hh +++ b/src/yaml/objectParser.hh @@ -29,7 +29,8 @@ protected: private: enum class Type { Box, - Sphere + Plane, + Sphere, }; enum Section { @@ -42,7 +43,11 @@ private: /* Box sections */ NearSection, - FarSection + FarSection, + + /* Plane sections */ + NormalSection, + DistanceSection, }; void HandleColorEvent(yaml_event_t& event); @@ -53,6 +58,9 @@ private: void HandleNearEvent(yaml_event_t& event); void HandleFarEvent(yaml_event_t& event); + void HandleNormalEvent(yaml_event_t& event); + void HandleDistanceEvent(yaml_event_t& event); + charles::Object::Ptr mObject; Type mType; Section mSection;