-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphere.hpp
45 lines (37 loc) · 1.05 KB
/
sphere.hpp
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
39
40
41
42
43
44
45
#pragma once
#include "hittable.hpp"
class sphere : public hittable {
public:
sphere(const point3 ¢er, double radius, std::shared_ptr<material> mat)
: center_(center), radius_(std::fmax(0, radius)), mat_(mat) {
// FIXME: add material pointer
}
bool hit(const ray &r, interval rayT, hitRecord &rec) const override {
vec3 oc = center_ - r.origin();
auto a = r.direction().lengthSquared();
auto h = dot(r.direction(), oc);
auto c = oc.lengthSquared() - radius_ * radius_;
auto discriminant = h * h - a * c;
if (discriminant < 0) {
return false;
}
auto sqrtd = std::sqrt(discriminant);
auto root = (h - sqrtd) / a;
if (!rayT.surrounds(root)) {
root = (h + sqrtd) / a;
if (!rayT.surrounds(root)) {
return false;
}
}
rec.t = root;
rec.p = r.at(rec.t);
vec3 outwardNormal = (rec.p - center_) / radius_;
rec.setFaceNormal(r, outwardNormal);
rec.mat = mat_;
return true;
}
private:
point3 center_;
double radius_;
std::shared_ptr<material> mat_;
};