diff --git a/src/Geometry.cc b/src/Geometry.cc index 5eaab26..f78b14c 100644 --- a/src/Geometry.cc +++ b/src/Geometry.cc @@ -1,5 +1,7 @@ #include "Geometry.h" +#include + void Geometry::setTransform(const Matrix4f &transform) { this->transform = transform; } @@ -13,4 +15,18 @@ bool Sphere::intersect(const Ray &r) const { return b * b - 4 * a * c >= 0; } -bool Rectangle::intersect(const Ray &r) const { return false; } +bool Rectangle::intersect(const Ray &r) const { + Vector3f normal = (p2 - p1).cross(p3 - p1).normalized(); + float d = normal.dot(p1); + + float denom = normal.dot(r.getDirection()); + if (abs(denom) < 1e-6f) + return false; + + float t = (normal.dot(r.getOrigin() - p1) - d) / denom; + if (t <= 0) + return false; + + Vector3f p = r.getOrigin() + t * r.getDirection(); + return true; +}