Update ObjectParser for boxes
ObjectParser's constructor takes the tag value, which determines the type of object constructed. ObjectParser adds "near" and "far" sections for boxes.
This commit is contained in:
parent
b878db592c
commit
deff3079b8
3 changed files with 110 additions and 14 deletions
|
@ -12,6 +12,7 @@
|
||||||
|
|
||||||
#include "material.h"
|
#include "material.h"
|
||||||
#include "object.h"
|
#include "object.h"
|
||||||
|
#include "objectBox.hh"
|
||||||
#include "object_sphere.h"
|
#include "object_sphere.h"
|
||||||
#include "yaml/objectParser.hh"
|
#include "yaml/objectParser.hh"
|
||||||
#include "yaml/vectorParser.hh"
|
#include "yaml/vectorParser.hh"
|
||||||
|
@ -23,34 +24,68 @@ using namespace charles;
|
||||||
namespace yaml {
|
namespace yaml {
|
||||||
|
|
||||||
ObjectParser::ObjectParser(Scene& scene,
|
ObjectParser::ObjectParser(Scene& scene,
|
||||||
ParserStack& parsers)
|
ParserStack& parsers,
|
||||||
|
const std::string& tag)
|
||||||
: ScalarMappingParser(scene, parsers),
|
: ScalarMappingParser(scene, parsers),
|
||||||
mObject(new Sphere()),
|
mObject(nullptr),
|
||||||
mSection(NoSection)
|
mSection(NoSection)
|
||||||
{
|
{
|
||||||
|
if (tag == "!Object.Box") {
|
||||||
|
mType = Type::Box;
|
||||||
|
mObject.reset(new Box());
|
||||||
|
} else if (tag == "!Object.Sphere") {
|
||||||
|
mType = Type::Sphere;
|
||||||
|
mObject.reset(new Sphere());
|
||||||
|
} else {
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
GetScene().add_shape(mObject);
|
GetScene().add_shape(mObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ObjectParser::~ObjectParser()
|
ObjectParser::~ObjectParser()
|
||||||
{ }
|
{
|
||||||
|
mObject.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
ObjectParser::HandleKeyEvent(const std::string& key)
|
ObjectParser::HandleKeyEvent(const std::string& key)
|
||||||
{
|
{
|
||||||
static const std::map<std::string, Section> sSections = {
|
static const std::map<std::string, Section> sCommonSections = {
|
||||||
{"color", ColorSection},
|
{"color", ColorSection}
|
||||||
|
};
|
||||||
|
|
||||||
|
static const std::map<std::string, Section> sSphereSections = {
|
||||||
{"origin", OriginSection},
|
{"origin", OriginSection},
|
||||||
{"radius", RadiusSection}
|
{"radius", RadiusSection}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (sSections.count(key) > 0) {
|
static const std::map<std::string, Section> sBoxSections = {
|
||||||
mSection = sSections.at(key);
|
{"near", NearSection},
|
||||||
|
{"far", FarSection}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (sCommonSections.count(key) > 0) {
|
||||||
|
mSection = sCommonSections.at(key);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
|
if (mType == Type::Box) {
|
||||||
|
if (sBoxSections.count(key) > 0) {
|
||||||
|
mSection = sBoxSections.at(key);
|
||||||
|
} else {
|
||||||
mSection = NoSection;
|
mSection = NoSection;
|
||||||
}
|
}
|
||||||
|
} else if (mType == Type::Sphere) {
|
||||||
|
if (sSphereSections.count(key) > 0) {
|
||||||
|
mSection = sSphereSections.at(key);
|
||||||
|
} else {
|
||||||
|
mSection = NoSection;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -67,6 +102,12 @@ ObjectParser::HandleValueEvent(yaml_event_t& event)
|
||||||
case RadiusSection:
|
case RadiusSection:
|
||||||
HandleRadiusEvent(event);
|
HandleRadiusEvent(event);
|
||||||
break;
|
break;
|
||||||
|
case NearSection:
|
||||||
|
HandleNearEvent(event);
|
||||||
|
break;
|
||||||
|
case FarSection:
|
||||||
|
HandleFarEvent(event);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
assert(false);
|
assert(false);
|
||||||
break;
|
break;
|
||||||
|
@ -127,9 +168,47 @@ ObjectParser::HandleRadiusEvent(yaml_event_t& event)
|
||||||
/* TODO: Clean this up. */
|
/* TODO: Clean this up. */
|
||||||
assert(false);
|
assert(false);
|
||||||
}
|
}
|
||||||
mObject->set_radius(radius);
|
std::dynamic_pointer_cast<Sphere>(mObject)->set_radius(radius);
|
||||||
mSection = NoSection;
|
mSection = NoSection;
|
||||||
SetShouldExpectKey(true);
|
SetShouldExpectKey(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
ObjectParser::HandleNearEvent(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 near) {
|
||||||
|
std::dynamic_pointer_cast<Box>(mObject)->SetNear(near);
|
||||||
|
mSection = NoSection;
|
||||||
|
SetShouldExpectKey(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
GetParsers().push(new Vector3Parser(GetScene(), GetParsers(), onDone));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
ObjectParser::HandleFarEvent(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 far) {
|
||||||
|
std::dynamic_pointer_cast<Box>(mObject)->SetFar(far);
|
||||||
|
mSection = NoSection;
|
||||||
|
SetShouldExpectKey(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
GetParsers().push(new Vector3Parser(GetScene(), GetParsers(), onDone));
|
||||||
|
}
|
||||||
|
|
||||||
} /* namespace yaml */
|
} /* namespace yaml */
|
||||||
|
|
|
@ -19,7 +19,7 @@ namespace yaml {
|
||||||
struct ObjectParser
|
struct ObjectParser
|
||||||
: public ScalarMappingParser
|
: public ScalarMappingParser
|
||||||
{
|
{
|
||||||
ObjectParser(Scene& scene, ParserStack& parsers);
|
ObjectParser(Scene& scene, ParserStack& parsers, const std::string& tag);
|
||||||
~ObjectParser();
|
~ObjectParser();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -27,18 +27,34 @@ protected:
|
||||||
void HandleValueEvent(yaml_event_t& event);
|
void HandleValueEvent(yaml_event_t& event);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
enum class Type {
|
||||||
|
Box,
|
||||||
|
Sphere
|
||||||
|
};
|
||||||
|
|
||||||
enum Section {
|
enum Section {
|
||||||
NoSection,
|
NoSection,
|
||||||
ColorSection,
|
ColorSection,
|
||||||
|
|
||||||
|
/* Sphere sections */
|
||||||
OriginSection,
|
OriginSection,
|
||||||
RadiusSection
|
RadiusSection,
|
||||||
|
|
||||||
|
/* Box sections */
|
||||||
|
NearSection,
|
||||||
|
FarSection
|
||||||
};
|
};
|
||||||
|
|
||||||
void HandleColorEvent(yaml_event_t& event);
|
void HandleColorEvent(yaml_event_t& event);
|
||||||
|
|
||||||
void HandleOriginEvent(yaml_event_t& event);
|
void HandleOriginEvent(yaml_event_t& event);
|
||||||
void HandleRadiusEvent(yaml_event_t& event);
|
void HandleRadiusEvent(yaml_event_t& event);
|
||||||
|
|
||||||
charles::Sphere::Ptr mObject;
|
void HandleNearEvent(yaml_event_t& event);
|
||||||
|
void HandleFarEvent(yaml_event_t& event);
|
||||||
|
|
||||||
|
charles::Object::Ptr mObject;
|
||||||
|
Type mType;
|
||||||
Section mSection;
|
Section mSection;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -132,7 +132,8 @@ SceneParser::HandleObjectsEvent(yaml_event_t& event)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
GetParsers().push(new ObjectParser(GetScene(), GetParsers()));
|
GetParsers().push(new ObjectParser(GetScene(), GetParsers(),
|
||||||
|
(char*)event.data.mapping_start.tag));
|
||||||
}
|
}
|
||||||
|
|
||||||
} /* namespace yaml */
|
} /* namespace yaml */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue