-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphere.cpp
44 lines (33 loc) · 1.03 KB
/
sphere.cpp
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
//
// Created by PETROS on 15/09/2024.
//
#include "sphere.h"
sphere::sphere(const point3 ¢er, const double radius, const std::shared_ptr<material> &mat) :
center(center),
radius(std::fmax(0, radius)),
mat(mat) {
}
bool sphere::hit(const ray &r, interval ray_t, hit_record &rec) const {
const vec3 oc = center - r.origin();
const auto a = r.direction().length_squared();
const auto h = dot(r.direction(), oc);
const auto c = oc.length_squared() - radius * radius;
const auto discriminant = h * h - a * c;
if (discriminant < 0) {
return false;
}
auto sqrt_discriminant = sqrt(discriminant);
auto root = (h - sqrt_discriminant) / a;
if (!ray_t.surrounds(root)) {
root = (h + sqrt_discriminant) / a;
if (!ray_t.surrounds(root)) {
return false;
}
}
rec.t = root;
rec.p = r.at(rec.t);
vec3 outward_normal = (rec.p - center) / radius;
rec.set_face_normal(r, outward_normal);
rec.mat = mat;
return true;
}