aboutsummaryrefslogtreecommitdiff
path: root/src/Engine/Object.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/Engine/Object.h')
-rw-r--r--src/Engine/Object.h43
1 files changed, 38 insertions, 5 deletions
diff --git a/src/Engine/Object.h b/src/Engine/Object.h
index 0ae3430..ea672db 100644
--- a/src/Engine/Object.h
+++ b/src/Engine/Object.h
@@ -10,7 +10,9 @@ namespace Donut
glm::vec3 m_Direction;
glm::vec3 m_Origin;
- Ray(glm::vec3 o, glm::vec3 d);
+ Ray(glm::vec3 o, glm::vec3 d)
+ : m_Origin(o),
+ m_Direction(glm::normalize(d)) { }
};
class Material
@@ -20,7 +22,10 @@ namespace Donut
float m_Specular;
float m_Emission;
- Material(glm::vec3 c, float s, float e);
+ Material(glm::vec3 c, float s, float e)
+ : m_Color(c),
+ m_Specular(s),
+ m_Emission(e) { }
};
class Object
@@ -30,9 +35,37 @@ namespace Donut
float m_Radius;
Material m_Material;
- Object(glm::vec3 c, float r, Material m);
+ Object(glm::vec3 c, float r, Material m)
+ : m_Centre(c),
+ m_Radius(r),
+ m_Material(m) { }
- bool Intersect(Ray &ray, float &t);
- glm::vec3 GetNormal(glm::vec3 &point) const;
+ bool 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 GetNormal(glm::vec3 &point) const
+ {
+ return glm::normalize(point - m_Centre);
+ }
};
};