-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsphere.cpp
More file actions
29 lines (25 loc) · 829 Bytes
/
Copy pathsphere.cpp
File metadata and controls
29 lines (25 loc) · 829 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
#include "sphere.h"
bool sphere::hit(const Ray& r, double t_min, double t_max, hit_record& rec) const{
Vec3 oc = r.origin() - center;
auto a = r.direction().length_squared();
auto half_b = dot(oc, r.direction());
auto c = oc.length_squared() - 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.t = root;
rec.p = r.at(rec.t);
Vec3 outward_normal = (rec.p - center) / radius;
rec.set_face_normal(r, outward_normal);
rec.mat_ptr = mat_ptr;
return true;
}