aboutsummaryrefslogtreecommitdiff
path: root/src/Engine/Object.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Engine/Object.cpp')
-rw-r--r--src/Engine/Object.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/Engine/Object.cpp b/src/Engine/Object.cpp
new file mode 100644
index 0000000..8e042fd
--- /dev/null
+++ b/src/Engine/Object.cpp
@@ -0,0 +1,46 @@
+#include "Object.h"
+
+namespace Donut
+{
+ Ray::Ray(glm::vec3 o, glm::vec3 d)
+ : m_Origin(o),
+ m_Direction(glm::normalize(d)) { }
+
+ Material::Material(glm::vec3 c, float s, float e)
+ : m_Color(c),
+ m_Specular(s),
+ m_Emission(e) { }
+
+ Object::Object(glm::vec3 c, float r, Material m)
+ : m_Centre(c),
+ m_Radius(r),
+ m_Material(m) { }
+
+ bool Object::Intersect(Ray &ray, float &t)
+ {
+ glm::vec3 oc = ray.m_Origin - m_Centre;
+ float a = glm::dot(ray.m_Direction, ray.m_Direction);
+ float b = 2.0f * glm::dot(oc, ray.m_Direction);
+ float c = glm::dot(oc, oc) - m_Radius * m_Radius;
+ double discriminant = b*b - 4*a*c;
+
+ if(discriminant < 0)
+ return false;
+
+ float intercept = (-b - sqrt(discriminant)) / (2.0f*a);
+ if(intercept < 0)
+ {
+ intercept = (-b + sqrt(discriminant)) / (2.0f*a);
+ if(intercept<0)
+ return false;
+ }
+
+ t = intercept;
+ return true;
+ }
+
+ glm::vec3 Object::GetNormal(glm::vec3 &point) const
+ {
+ return glm::normalize(point - m_Centre);
+ }
+}