-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphere.h
45 lines (39 loc) · 1.33 KB
/
sphere.h
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
#ifndef SPHERE_H
#define SPHERE_H
#include "hittable.h"
class Sphere: public Hittable {
public:
__device__ Sphere() {}
__device__ Sphere(Vector cent, double r, Material* m): center(cent), radius(r), mat_ptr(m) {};
__device__ virtual bool hit(const Ray& r, double t_min, double t_max, HitRecord& rec) const;
Vector center;
double radius;
Material* mat_ptr;
};
__device__ bool Sphere::hit(const Ray& r, double t_min, double t_max, HitRecord& rec) const {
Vector oc = r.origin() - center;
double a = dot(r.direction(), r.direction());
double b = dot(oc, r.direction());
double c = dot(oc, oc) - radius * radius;
double discriminant = b*b - a*c;
if (discriminant > 0) {
double temp = (-b - sqrt(discriminant)) / a;
if (temp < t_max && temp > t_min) {
rec.t = temp;
rec.p = r.at(rec.t);
rec.normal = (rec.p - center) / radius;
rec.mat_ptr = mat_ptr;
return true;
}
temp = (-b + sqrt(discriminant)) / a;
if (temp < t_max && temp > t_min) {
rec.t = temp;
rec.p = r.at(rec.t);
rec.normal = (rec.p - center) / radius;
rec.mat_ptr = mat_ptr;
return true;
}
}
return false;
}
#endif // SPHERE_H