Skip to content

Commit

Permalink
Parse list of objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitor Greati committed Apr 14, 2019
1 parent 9d68034 commit db28acc
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 19 deletions.
4 changes: 2 additions & 2 deletions examples/red_spheres_ortho.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ raytracer:
target: [0, 0, -10.0]
up: [0, 1, 1]
vpdims: [-3.0, 3.0, -2.25, 2.25]
width: 80
height: 60
width: 800
height: 600
scene:
objects:
- type: sphere
Expand Down
4 changes: 2 additions & 2 deletions examples/red_spheres_persp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ raytracer:
fdist: 1.0
v_angle: 65.0
aspect_ratio: 1.33
width: 80
height: 60
width: 800
height: 600
scene:
objects:
- type: sphere
Expand Down
9 changes: 9 additions & 0 deletions sources/include/omg/parsing/yaml/YAMLParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,18 @@ class YAMLParser : public Parser {

private:

/**
* Parse some node of a specific type.
* */
template<typename NodeType>
std::shared_ptr<NodeType> parse(const YAML::Node& node);

/**
* Parse a list of nodes.
* */
template<typename NodeType>
std::vector<std::shared_ptr<NodeType>> parse_list(const YAML::Node& node);

/**
* Check if a node exists in the current one. If not, throws
* an error.
Expand Down
10 changes: 8 additions & 2 deletions sources/include/omg/scene/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "SceneNode.h"
#include "omg/cameras/Camera.h"
#include "omg/backgrounds/Background.h"
#include "omg/objects/Object.h"

namespace omg {
/**
Expand All @@ -17,6 +18,7 @@ class Scene : public SceneNode {

std::shared_ptr<Background> _background; /** Background reference */
std::shared_ptr<Camera> _camera; /** Camera reference */
std::vector<std::shared_ptr<Object>> _objects; /** Scene objects */

public:

Expand All @@ -29,9 +31,11 @@ class Scene : public SceneNode {
* Complete scene constructor.
* */
Scene(std::shared_ptr<Background> background,
std::shared_ptr<Camera> camera)
std::shared_ptr<Camera> camera,
const decltype(_objects)& objects)
: _background {background},
_camera {camera}
_camera {camera},
_objects {objects}
{ /* empty */ }

/**
Expand Down Expand Up @@ -90,6 +94,8 @@ class Scene : public SceneNode {
* */
inline void set_camera(std::shared_ptr<Camera> camera) { this->_camera = camera; }

inline const std::vector<std::shared_ptr<Object>>& get_objects() const { return _objects; }

void accept(Visitor& visitor) override {
visitor.visit(std::shared_ptr<Scene>(this));
};
Expand Down
33 changes: 32 additions & 1 deletion sources/src/parsing/yaml/YAMLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "omg/backgrounds/SolidBackground.h"
#include "omg/cameras/OrthoCamera.h"
#include "omg/cameras/PerspectiveCamera.h"
#include "omg/objects/Sphere.h"
#include "omg/objects/Object.h"
#include <iostream>

using namespace omg;
Expand All @@ -24,6 +26,26 @@ struct YAML::convert<RGBColor> {
}
};

template<>
std::shared_ptr<Sphere> YAMLParser::parse(const YAML::Node& node) {
auto radius = hard_require(node, "radius").as<float>();
auto center = hard_require(node, "center").as<RGBColor>();
return std::make_shared<Sphere>(radius, center);
}

template<>
std::vector<std::shared_ptr<Object>> YAMLParser::parse_list(const YAML::Node& node) {
std::vector<std::shared_ptr<Object>> objs;

for (auto & n : node) {
auto type = hard_require(n, "type").as<std::string>();
if (type == "sphere")
objs.push_back(parse<Sphere>(n));
}

return objs;
}

template<>
std::shared_ptr<Background> YAMLParser::parse(const YAML::Node& node) {
try {
Expand Down Expand Up @@ -118,9 +140,18 @@ std::shared_ptr<Camera> YAMLParser::parse(const YAML::Node& node) {

template<>
std::shared_ptr<Scene> YAMLParser::parse(const YAML::Node& node) {
// context
auto camera = this->parse<Camera>(node);
auto background = this->parse<Background>(node);
auto scene = std::make_shared<Scene>(background, camera);
std::vector<std::shared_ptr<Object>> objects;
// scene
if (node["scene"]) {
auto scene_node = node["scene"];
if (scene_node["objects"]) {
objects = this->parse_list<Object>(scene_node["objects"]);
}
}
auto scene = std::make_shared<Scene>(background, camera, objects);
return scene;
}

Expand Down
22 changes: 10 additions & 12 deletions sources/src/raytracer/RaytracerVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,23 @@ void RaytracerVisitor::visit(const std::shared_ptr<Scene>& scene) {
auto background = scene->get_background();
this->_buffer = std::move(std::make_unique<Buffer<3>>(width, height));

Sphere sphere {0.8, Point3{0.0, 0.0, -3.0}};

SurfaceInteraction hit_record;
auto objects = scene->get_objects();

for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
auto [px, py] = std::pair{x / static_cast<float>(width), y / static_cast<float>(height)};
Ray ray = camera->generate_ray(px, py);
//----- temporary built in shader
bool hit = sphere.intersect(ray, hit_record);
float pt = 1.0/hit_record._t;
auto color = hit
? RGBColor {
(pt * 255),
(pt * 255),
(pt * 255)
}
//-------------------------------
RGBColor color;
for (auto & obj : objects) {
SurfaceInteraction hit_record;
bool hit = obj->intersect(ray, hit_record);
color = hit
? RGBColor {255, 0, 0}
//-------------------------------
: background->find(px, py);
if (hit) break;
}
auto [r, g, b] = std::tuple {color(0), color(1), color(2)};
this->_buffer->set({x, y}, {
static_cast<unsigned char>(r),
Expand Down

0 comments on commit db28acc

Please sign in to comment.