-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphere.h
43 lines (39 loc) · 1.11 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
#ifndef SPHEREH
#define SPHEREH
#include "hitable.h"
class sphere: public hitable {
public:
sphere() {}
sphere(vec3 cen, double r, material *mat) : center(cen), radius(r), matPtr(mat) {};
virtual bool hit(const ray& r, double tMin, double tMax, hit_record& rec) const;
vec3 center;
double radius;
material *matPtr;
};
bool sphere::hit(const ray& r, double tMin, double tMax, hit_record& rec) const {
vec3 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(b*b - a*c))/a;
if(temp < tMax && temp > tMin) {
rec.t = temp;
rec.p = r.pointAtParameter(rec.t);
rec.normal = (rec.p - center) / radius;
rec.matPtr = matPtr;
return true;
}
temp = (-b + sqrt(b*b - a*c))/a;
if(temp < tMax && temp > tMin) {
rec.t = temp;
rec.p = r.pointAtParameter(rec.t);
rec.normal = (rec.p - center) / radius;
rec.matPtr = matPtr;
return true;
}
}
return false;
}
#endif