-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphere.h
74 lines (60 loc) · 2.07 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef SPHERE_H
#define SPHERE_H
#include "hittable.h"
#include "vec3.h"
class sphere : public hittable {
public:
sphere() = default;
sphere(point3 _center, double _radius, shared_ptr<material> _material)
: center{ _center }, radius{ _radius }, mat{_material} {
update_bounds();
};
void update_bounds () {
auto rvec = vec3f(radius, radius, radius);
bbox = aabb(vec3f{center} - rvec, vec3f{center} + rvec);
}
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
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);
// Find the nearest root that lies in the acceptable range
auto root = ( - half_b - sqrt(discriminant)) / a;
if (!ray_t.surrounds(root)) {
root = (-half_b + sqrt(discriminant)) / a;
if (!ray_t.surrounds(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);
get_sphere_uv(outward_normal, rec.u, rec.v);
rec.mat = mat;
return true;
}
aabb bounding_box() const override { return bbox; }
point3f centroid() const override { return vec3f{center}; }
private:
point3 center;
double radius;
shared_ptr<material> mat;
aabb bbox;
static void get_sphere_uv(const point3& p, double& u, double& v) {
// p: a given point on the sphere of radius one, centered at the origin.
// u: returned value [0,1] of angle around the Y axis from X=-1.
// v: returned value [0,1] of angle from Y=-1 to Y=+1.
// <1 0 0> yields <0.50 0.50> <-1 0 0> yields <0.00 0.50>
// <0 1 0> yields <0.50 1.00> < 0 -1 0> yields <0.50 0.00>
// <0 0 1> yields <0.25 0.50> < 0 0 -1> yields <0.75 0.50>
auto theta = acos(-p.y());
auto phi = atan2(-p.z(), p.x()) + pi;
u = phi / (2*pi);
v = theta / pi;
}
};
#endif