-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsphere.cpp
More file actions
38 lines (30 loc) · 915 Bytes
/
sphere.cpp
File metadata and controls
38 lines (30 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "sphere.h"
#include "utility.h"
bool Sphere::hit(const Ray& r, double t_min, double t_max, HitRecord& rec) const
{
QVector3D oc = r.origin() - _center;
auto a = r.direction().lengthSquared();
auto half_b = Utility::dot(oc, r.direction());
auto c = oc.lengthSquared() - _radius*_radius;
auto discriminant = half_b*half_b - a*c;
if (discriminant < 0)
{
return false;
}
auto sqrtd = sqrt(discriminant);
auto root = (-half_b - sqrtd) / a;
if (root < t_min || t_max < root)
{
root = (-half_b + sqrtd) / a;
if (root < t_min || t_max < root)
{
return false;
}
}
rec.setInRange(root);
rec.setPointsInRange(r.at(rec.inRange()));
QVector3D outwardNormal = (rec.pointsInRange() - _center) / _radius;
rec.setFaceNormal(r, outwardNormal);
rec.setMaterial(_material);
return true;
}