From 447134108dc66e387e19df9b607b38cf201afa4b Mon Sep 17 00:00:00 2001 From: Akiko Date: Wed, 23 Oct 2013 11:01:25 +0200 Subject: [PATCH] - added Ray class for doing visibility checks (and for raytracing) - updated Object class (the base of every 3D object) for holding more common data --- engine/Object.hxx | 96 +++++++++++++++++++++-------- engine/Ray.hxx | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 246 insertions(+), 26 deletions(-) create mode 100644 engine/Ray.hxx diff --git a/engine/Object.hxx b/engine/Object.hxx index 0cec8c3..b931e24 100644 --- a/engine/Object.hxx +++ b/engine/Object.hxx @@ -1,43 +1,44 @@ #pragma once +#include #include #include "common/BaseObject.hxx" #include "Vector.hxx" +#include "Ray.hxx" enum class ObjectType : uint8_t { None, Camera, + Plane, + Sphere, Mesh }; -template -class Object : public BaseObject { +template +class Object : public BaseObject> { + static_assert (std::is_integral::value || std::is_floating_point::value, + "ERROR: template parameter is not an integral or floating point type"); public: - // --- public data structures --- - - std::string name; - bool visible; - // --- constructors and deconstructors --- Object() - : BaseObject(), name("unknown"), type(ObjectType::Null), visible(true), - _origin(static_cast(0), static_cast(0), static_cast(0)) + : BaseObject>(), _name("unknown"), _visible(true), _origin(0, 0, 0), _up(0, 1, 0) { } - Object(const std::string& n, const bool v, const Vector3& o) - : BaseObject(), name(n), visible(v), _origin(o) + Object(const std::string& name, const bool vis, const Vector3& org, const Vector3& up) + : BaseObject>(), _name(name), _visible(vis), _origin(org), _up(up) { } Object(const Object& rhs) - : BaseObject(rhs), name(rhs.name), visible(rhs.visible), _origin(rhs._origin) + : BaseObject>(rhs), _name(rhs.name), _visible(rhs.visible), _origin(rhs._origin), _up(rhs._up) { } Object(Object&& rhs) - : BaseObject(rhs), name(std::move(rhs.name)), visible(std::move(rhs.visible)), _origin(std::move(rhs._origin)) + : BaseObject>(rhs), _name(std::move(rhs._name)), _visible(std::move(rhs._visible)), + _origin(std::move(rhs._origin)), _up(std::move(rhs._up)) { } @@ -51,9 +52,11 @@ public: { if (this != &rhs) { - name = rhs.name; - visible = rhs.visible; - BaseObject::operator=(rhs); + BaseObject>::operator=(rhs); + _name = rhs._name; + _visible = rhs._visible; + _origin = rhs._origin; + _up = rhs._up; } return *this; @@ -63,9 +66,11 @@ public: { if (this != &rhs) { - name = std::move(rhs.name); - visible = std::move(rhs.visible); - BaseObject::operator=(rhs); + BaseObject>::operator=(rhs); + _name = std::move(rhs._name); + _visible = std::move(rhs._visible); + _origin = std::move(rhs._origin); + _up = std::move(rhs._up); } return *this; @@ -73,22 +78,61 @@ public: // --- public methods --- - virtual void setOrigin(const Vector3& o) + virtual void setName(const std::string& name) + { + _name = name; + } + + virtual std::string name() const + { + return _name; + } + + virtual void setVisible(const bool visible) + { + _visible = visible; + } + + virtual bool visible() const + { + return _visible; + } + + virtual void setOrigin(const Vector3& o) { _origin = o; } - virtual Vector3 origin() const + virtual Vector3 origin() const { return _origin; } - // --- interfaces --- + virtual void setUp(const Vector3& up) + { + _up = up; + } + + virtual Vector3 up() const + { + return _up; + } + + virtual ObjectType type() const + { + return ObjectType::None; + } - virtual ObjectType type() const = 0; - virtual void setUp() = 0; - virtual Vector3 up() const = 0; + virtual Ray intersect(const std::vector *>& /*objects*/, const Ray& /*ray*/) const + { + return Ray(); + } protected: - Vector3 _origin; + // --- protected data structures --- + + std::string _name; + bool _visible; + Vector3 _origin; + Vector3 _up; }; diff --git a/engine/Ray.hxx b/engine/Ray.hxx new file mode 100644 index 0000000..dd30bad --- /dev/null +++ b/engine/Ray.hxx @@ -0,0 +1,176 @@ +#pragma once + +#include +#include "Vector.hxx" + +template +class Ray { + static_assert (std::is_integral::value || std::is_floating_point::value, + "ERROR: template parameter is not an integral or floating point type"); +public: + // --- constructors and deconstructors --- + + Ray() + : _origin(0, 0, 0), _direction(0, 0, 0), _rgb(0, 0, 0), _min(5), _max(1000), _depth(10), _valid(false) + { + } + + Ray(const Vector3& org, const Vector3& dir, const Vector3& rgb, const MT& min, const MT& max, + const size_t& depth, const bool valid) + : _origin(org), _direction(dir), _rgb(rgb), _min(min), _max(max), _depth(depth), _valid(valid) + { + } + + Ray(const Ray& rhs) + : _origin(rhs._origin), _direction(rhs._direction), _rgb(rhs._rgb), _min(rhs._min), _max(rhs._max), + _depth(rhs._depth), _valid(rhs._valid) + { + } + + Ray(Ray&& rhs) + : _origin(std::move(rhs._origin)), _direction(std::move(rhs._direction)), _rgb(std::move(rhs._rgb)), + _min(std::move(rhs._min)), _max(std::move(rhs._max)), _depth(std::move(rhs._depth)), _valid(std::move(rhs._valid)) + { + } + + virtual ~Ray() + { + } + + // --- default operators --- + + Ray& operator=(const Ray& rhs) + { + if (this != &rhs) + { + _origin = rhs._origin; + _direction = rhs._direction; + _rgb = rhs._rgb; + _min = rhs._min; + _max = rhs._max; + _depth = rhs._depth; + _valid = rhs._valid; + } + + return *this; + } + + Ray& operator=(Ray& rhs) + { + if (this != &rhs) + { + _origin = std::move(rhs._origin); + _direction = std::move(rhs._direction); + _rgb = std::move(rhs._rgb); + _min = std::move(rhs._min); + _max = std::move(rhs._max); + _depth = std::move(rhs._depth); + _valid = std::move(rhs._valid); + } + + return *this; + } + + bool operator==(const Ray& rhs) const + { + return _origin == rhs._origin && _direction == rhs._direction && _rgb == rhs._rgb && _min == rhs._min && + _max == rhs._max && _depth == rhs._depth && _valid == rhs._valid; + } + + bool operator!=(const Ray& rhs) const + { + return !(*this == rhs); + } + + // --- public methods --- + + void setOrigin(const Vector3& org) + { + _origin = org; + } + + Vector3 origin() const + { + return _origin; + } + + void setDirection(const Vector3& dir) + { + _direction = dir.normalized(); + } + + Vector3 direction() const + { + return _direction; + } + + void setRgb(const Vector3& rgb) + { + _rgb = rgb; + } + + Vector3 rgb() const + { + return _rgb; + } + + void setMin(const MT& min) + { + _min = min; + } + + MT min() const + { + return _min; + } + + void setMax(const MT& max) + { + _max = max; + } + + MT max() const + { + return _max; + } + + void setDepth(const size_t& depth) + { + _depth = depth; + } + + size_t depth() const + { + return _depth; + } + + void setValid(const bool valid) + { + _valid = valid; + } + + bool valid() const + { + return _valid; + } + + std::string string() const + { + std::stringstream result; + + result << "(" << _origin.string() << "), (" << _direction.string() << ")"; + + return result.str(); + } + +protected: + // --- protected data structures --- + + Vector3 _origin; + Vector3 _direction; + Vector4 _rgb; // color ARGB (0, 255 for now) + MT _min; // near plane value + MT _max; // far plane value + size_t _depth; // recursion depth + bool _valid; +}; -- 2.15.1