hachem.wtf
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..2e76b16
--- /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::get_normal(glm::vec3 &point) const
+ {
+ return glm::normalize(point - m_centre);
+ }
+}