-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphere.cpp
35 lines (33 loc) · 1.07 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
#include "sphere.h"
Sphere::Sphere(Vector3 cen, float r)
:center(cen),
radius(r)
{}
bool Sphere::hit(const Ray& r, float t_min, float t_max, hit_record& rec) const {
Vector3 oc = r.origin() - center;
float a = dot(r.direction(), r.direction());
float b = dot(oc, r.direction());
float c = dot(oc, oc) - radius*radius;
float discriminant = b*b - a*c;
if (discriminant > 0) {
float temp = (-b - sqrt(discriminant))/a;
if (temp < t_max && temp > t_min) {
rec.t = temp;
Vector3 p = r.point_at_parameter(rec.t);
rec.p = p;
Vector3 calulation = ((rec.p) - center) / radius;
rec.normal = calulation;
return true;
}
temp = (-b + sqrt(discriminant)) / a;
if (temp < t_max && temp > t_min) {
rec.t = temp;
Vector3 ptest = r.point_at_parameter(rec.t);
rec.p = ptest;
Vector3 calc = ((rec.p) - center) / radius;
rec.normal = calc;
return true;
}
}
return false;
}