calculate nearest hit

This commit is contained in:
Shuo Feng 2024-02-28 00:58:29 -05:00
parent a8a00d386e
commit 184812c533
Signed by: sfeng
GPG key ID: 1E83AE6CD1C037B1
6 changed files with 92 additions and 17 deletions

View file

@ -1,6 +1,7 @@
#include "Geometry.h"
#include <Eigen/Dense>
#include <cmath>
Vector3f Geometry::diffuse() const { return cd; }
Vector3f Geometry::specular() const { return cs; }
@ -10,13 +11,24 @@ void Geometry::setTransform(const Matrix4f &transform) {
this->transform = transform;
}
bool Sphere::intersect(const Ray &r) const {
Optional<float> Sphere::intersect(const Ray &r) const {
Vector3f originCenter = r.getOrigin() - center;
float a = r.getDirection().dot(r.getDirection());
float b = 2.0f * originCenter.dot(r.getDirection());
float c = originCenter.dot(originCenter) - radius * radius;
return b * b - 4 * a * c >= 0;
float delta = b * b - 4 * a * c;
if (delta >= 0) {
float t1 = (-b + sqrt(delta)) / 2.0f / a;
float t2 = (-b - sqrt(delta)) / 2.0f / a;
float mint = std::min(t1, t2);
float maxt = std::max(t1, t2);
return maxt <= 0
? Optional<float>::nullopt
: (mint < 0 ? Optional<float>(maxt) : Optional<float>(mint));
}
return Optional<float>::nullopt;
}
bool isInRectangle(const Vector3f &p, const Vector3f &a, const Vector3f &b,
@ -30,18 +42,19 @@ bool isInRectangle(const Vector3f &p, const Vector3f &a, const Vector3f &b,
(s1 <= 0 && s2 <= 0 && s3 <= 0 && s4 <= 0);
}
bool Rectangle::intersect(const Ray &r) const {
Optional<float> Rectangle::intersect(const Ray &r) const {
Vector3f normal = (p2 - p1).cross(p3 - p1).normalized();
float denom = normal.dot(r.getDirection());
if (abs(denom) < 1e-6f)
return false;
return Optional<float>::nullopt;
float t = -normal.dot(r.getOrigin() - p1) / denom;
if (t <= 0)
return false;
return Optional<float>::nullopt;
Vector3f p = r.getOrigin() + t * r.getDirection();
return isInRectangle(p, p1, p2, p3, p4, normal);
return isInRectangle(p, p1, p2, p3, p4, normal) ? Optional<float>(t)
: Optional<float>::nullopt;
}