2014-07-17 20:14:52 -07:00
|
|
|
/* object_parser.cc
|
|
|
|
* vim: set tw=80:
|
|
|
|
* Eryn Wells <eryn@erynwells.me>
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Implementation of ObjectParser.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2014-07-19 14:45:37 -07:00
|
|
|
#include "material.h"
|
2014-07-17 20:14:52 -07:00
|
|
|
#include "object_sphere.h"
|
2014-07-19 14:45:37 -07:00
|
|
|
#include "yaml/objectParser.hh"
|
2014-07-17 20:14:52 -07:00
|
|
|
#include "yaml/vector_parser.hh"
|
|
|
|
|
|
|
|
|
|
|
|
namespace yaml {
|
|
|
|
|
|
|
|
ObjectParser::ObjectParser(Scene& scene,
|
|
|
|
ParserStack& parsers)
|
2014-07-19 14:45:37 -07:00
|
|
|
: ScalarMappingParser(scene, parsers),
|
2014-07-19 15:41:45 -07:00
|
|
|
mObject(new Sphere()),
|
2014-07-19 14:45:37 -07:00
|
|
|
mSection(NoSection)
|
2014-07-19 15:41:45 -07:00
|
|
|
{
|
|
|
|
GetScene().add_shape(mObject);
|
|
|
|
}
|
2014-07-17 20:14:52 -07:00
|
|
|
|
|
|
|
|
2014-07-19 14:45:37 -07:00
|
|
|
ObjectParser::~ObjectParser()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ObjectParser::HandleKeyEvent(const std::string& key)
|
|
|
|
{
|
|
|
|
static const std::map<std::string, Section> sSections = {
|
|
|
|
{"origin", OriginSection},
|
|
|
|
{"color", ColorSection}
|
|
|
|
};
|
|
|
|
|
|
|
|
printf("%s: got key = %s", __PRETTY_FUNCTION__, key.c_str());
|
|
|
|
|
|
|
|
if (sSections.count(key) > 0) {
|
|
|
|
mSection = sSections.at(key);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mSection = NoSection;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-17 20:14:52 -07:00
|
|
|
void
|
2014-07-19 14:45:37 -07:00
|
|
|
ObjectParser::HandleValueEvent(yaml_event_t& event)
|
2014-07-17 20:14:52 -07:00
|
|
|
{
|
|
|
|
switch (mSection) {
|
2014-07-19 14:45:37 -07:00
|
|
|
case ColorSection:
|
|
|
|
HandleColorEvent(event);
|
2014-07-17 20:14:52 -07:00
|
|
|
break;
|
|
|
|
case OriginSection:
|
2014-07-19 14:45:37 -07:00
|
|
|
HandleOriginEvent(event);
|
2014-07-17 20:14:52 -07:00
|
|
|
break;
|
|
|
|
case RadiusSection:
|
2014-07-19 14:45:37 -07:00
|
|
|
HandleRadiusEvent(event);
|
2014-07-17 20:14:52 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2014-07-19 14:45:37 -07:00
|
|
|
ObjectParser::HandleColorEvent(yaml_event_t& event)
|
2014-07-17 20:14:52 -07:00
|
|
|
{
|
2014-07-19 14:45:37 -07:00
|
|
|
if (event.type != YAML_SEQUENCE_START_EVENT) {
|
|
|
|
assert(event.type != YAML_SEQUENCE_START_EVENT);
|
2014-07-17 20:14:52 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-19 14:45:37 -07:00
|
|
|
auto onDone = [this](std::vector<double> color) {
|
|
|
|
if (color.size() < 3) {
|
|
|
|
assert(color.size() < 3);
|
|
|
|
}
|
|
|
|
if (!mObject->get_material()) {
|
|
|
|
mObject->set_material(new Material());
|
|
|
|
}
|
|
|
|
mObject->get_material()->set_diffuse_color(Color(color[0], color[1], color[2]));
|
2014-07-17 20:14:52 -07:00
|
|
|
|
2014-07-19 14:45:37 -07:00
|
|
|
mSection = NoSection;
|
|
|
|
SetShouldExpectKey(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
GetParsers().push(new VectorParser<double>(GetScene(), GetParsers(), onDone));
|
2014-07-17 20:14:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ObjectParser::HandleOriginEvent(yaml_event_t& event)
|
|
|
|
{
|
|
|
|
if (event.type != YAML_SEQUENCE_START_EVENT) {
|
|
|
|
/* TODO: Clean this up. */
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto onDone = [this](std::vector<double> origin) {
|
|
|
|
if (origin.size() < 3) {
|
|
|
|
assert(origin.size() < 3);
|
|
|
|
}
|
|
|
|
mObject->set_origin(Vector3(origin[0], origin[1], origin[2]));
|
2014-07-19 14:45:37 -07:00
|
|
|
|
2014-07-17 20:14:52 -07:00
|
|
|
mSection = NoSection;
|
2014-07-19 14:45:37 -07:00
|
|
|
SetShouldExpectKey(true);
|
2014-07-17 20:14:52 -07:00
|
|
|
};
|
2014-07-19 14:45:37 -07:00
|
|
|
|
2014-07-17 20:14:52 -07:00
|
|
|
GetParsers().push(new VectorParser<double>(GetScene(), GetParsers(), onDone));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ObjectParser::HandleRadiusEvent(yaml_event_t& event)
|
|
|
|
{
|
|
|
|
if (event.type != YAML_SCALAR_EVENT) {
|
|
|
|
/* TODO: Clean this up. */
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
double radius;
|
2014-07-19 11:34:19 -07:00
|
|
|
std::string scalar((char *)event.data.scalar.value,
|
|
|
|
event.data.scalar.length);
|
|
|
|
if (!ParseScalar<double>(scalar, radius)) {
|
2014-07-17 20:14:52 -07:00
|
|
|
/* TODO: Clean this up. */
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
mObject->set_radius(radius);
|
|
|
|
mSection = NoSection;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace yaml */
|