-
Notifications
You must be signed in to change notification settings - Fork 1
/
sphere.cpp
executable file
·41 lines (34 loc) · 1.12 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
#include <cmath>
#include "sphere.hpp"
Sphere::Sphere()
{}
Sphere::Sphere(Vector3 center, float radius, std::shared_ptr<Behavioral> m): center(center), radius(radius),
material(m)
{}
bool Sphere::hit(const Ray &ray, float t_min, float t_max, HitRecord &rec) const
{
Vector3 oc = ray.getOrigin() - center;
float a = Vector3::dot(ray.getDirection(), ray.getDirection());
float b = Vector3::dot(oc, ray.getDirection());
float c = Vector3::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;
rec.p = ray.point(rec.t);
rec.normal = (rec.p - center) / radius;
rec.material = material;
return true;
}
temp = (-b + sqrt(discriminant)) / a;
if (temp < t_max && temp > t_min) {
rec.t = temp;
rec.p = ray.point(rec.t);
rec.normal = (rec.p - center) / radius;
rec.material = material;
return true;
}
}
return false;
}