- added Ray class for doing visibility checks (and for raytracing)
authorAkiko <akiko@linux-addicted.net>
Wed, 23 Oct 2013 09:01:25 +0000 (11:01 +0200)
committerAkiko <akiko@linux-addicted.net>
Wed, 23 Oct 2013 09:01:25 +0000 (11:01 +0200)
- updated Object class (the base of every 3D object) for holding more common data

engine/Object.hxx
engine/Ray.hxx [new file with mode: 0644]

index 0cec8c3..b931e24 100644 (file)
@@ -1,43 +1,44 @@
 #pragma once
 
+#include <vector>
 #include <string>
 #include "common/BaseObject.hxx"
 #include "Vector.hxx"
+#include "Ray.hxx"
 
 enum class ObjectType : uint8_t {
     None,
     Camera,
+    Plane,
+    Sphere,
     Mesh
 };
 
-template <typename T>
-class Object : public BaseObject<Object> {
+template <typename MT>
+class Object : public BaseObject<Object<MT>> {
+    static_assert (std::is_integral<MT>::value || std::is_floating_point<MT>::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<const T>(0), static_cast<const T>(0), static_cast<const T>(0))
+    : BaseObject<Object<MT>>(), _name("unknown"), _visible(true), _origin(0, 0, 0), _up(0, 1, 0)
     {
     }
 
-    Object(const std::string& n, const bool v, const Vector3<T>& o)
-    : BaseObject(), name(n), visible(v), _origin(o)
+    Object(const std::string& name, const bool vis, const Vector3<MT>& org, const Vector3<MT>& up)
+    : BaseObject<Object<MT>>(), _name(name), _visible(vis), _origin(org), _up(up)
     {
     }
 
     Object(const Object& rhs)
-    : BaseObject(rhs), name(rhs.name), visible(rhs.visible), _origin(rhs._origin)
+    : BaseObject<Object<MT>>(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<Object<MT>>(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<Object<MT>>::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<Object<MT>>::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<T>& 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<MT>& o)
     {
         _origin = o;
     }
 
-    virtual Vector3<T> origin() const
+    virtual Vector3<MT> origin() const
     {
         return _origin;
     }
 
-    // --- interfaces ---
+    virtual void setUp(const Vector3<MT>& up)
+    {
+        _up = up;
+    }
+
+    virtual Vector3<MT> up() const
+    {
+        return _up;
+    }
+
+    virtual ObjectType type() const
+    {
+        return ObjectType::None;
+    }
 
-    virtual ObjectType type() const = 0;
-    virtual void setUp() = 0;
-    virtual Vector3<T> up() const = 0;
+    virtual Ray<MT> intersect(const std::vector<Object<MT> *>& /*objects*/, const Ray<MT>& /*ray*/) const
+    {
+        return Ray<MT>();
+    }
 
 protected:
-    Vector3<T> _origin;
+    // --- protected data structures ---
+
+    std::string _name;
+    bool _visible;
+    Vector3<MT> _origin;
+    Vector3<MT> _up;
 };
diff --git a/engine/Ray.hxx b/engine/Ray.hxx
new file mode 100644 (file)
index 0000000..dd30bad
--- /dev/null
@@ -0,0 +1,176 @@
+#pragma once
+
+#include <sstream>
+#include "Vector.hxx"
+
+template <typename MT>
+class Ray {
+    static_assert (std::is_integral<MT>::value || std::is_floating_point<MT>::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<MT>& org, const Vector3<MT>& dir, const Vector3<MT>& 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<MT>& org)
+    {
+        _origin = org;
+    }
+
+    Vector3<MT> origin() const
+    {
+        return _origin;
+    }
+
+    void setDirection(const Vector3<MT>& dir)
+    {
+        _direction = dir.normalized();
+    }
+
+    Vector3<MT> direction() const
+    {
+        return _direction;
+    }
+
+    void setRgb(const Vector3<MT>& rgb)
+    {
+        _rgb = rgb;
+    }
+
+    Vector3<MT> 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<MT> _origin;
+    Vector3<MT> _direction;
+    Vector4<uint8_t> _rgb;  // color ARGB (0, 255 for now)
+    MT _min;                // near plane value
+    MT _max;                // far plane value
+    size_t _depth;          // recursion depth
+    bool _valid;
+};