-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsphere.h
More file actions
99 lines (79 loc) · 2.32 KB
/
Copy pathsphere.h
File metadata and controls
99 lines (79 loc) · 2.32 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#ifndef SPHERE_H
#define SPHERE_H
#include "primitive.h"
#include "hittable.h"
#include "texture.h"
#include <math.h>
// Special thanks to Ray Tracing in One Weekend:
// https://raytracing.github.io/books/RayTracingInOneWeekend.html
class sphere : public primitive {
public:
sphere() {}
sphere(point3 cen, double r, color m) {
center = cen;
radius = r;
albedo = m;
has_texture = false;
sphere_aabb = construct_aabb();
// std::cout << sphere_aabb.min_ << " " << sphere_aabb.max_ << std::endl;
}
sphere(point3 cen, double r, texture &t) {
center = cen;
radius = r;
tex = t;
has_texture = true;
sphere_aabb = construct_aabb();
// std::cout << sphere_aabb.min_ << " " << sphere_aabb.max_ << std::endl;
}
virtual bool hit(const ray &r, double t_min, double t_max, hit_record &rec) const override;
void transform(vec3 translate_by, double scale_by, vec3 rotate_by) {
center = center + translate_by;
radius = radius * scale_by;
sphere_aabb = construct_aabb();
}
virtual aabb construct_aabb() override {
return aabb(center - vec3(radius, radius, radius), center + vec3(radius, radius, radius));
}
virtual vec3 get_center() override {
return center;
}
public:
point3 center;
double radius;
color albedo;
bool has_texture;
texture tex;
aabb sphere_aabb;
};
bool sphere::hit(const ray &r, double t_min, double t_max, hit_record &rec) const {
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 - sqrtd) / a;
if (root < t_min || t_max < root) {
root = (-half_b + sqrtd) / a;
if (root < t_min || t_max < 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);
rec.u = atan2(rec.normal.x(), rec.normal.z()) / (2 * pi) + 0.5;
rec.v = 0.5 - asin(rec.normal.y()) / pi;
if (has_texture) {
rec.albedo = tex.get_texel(rec.u, rec.v);
}
else {
rec.albedo = albedo;
}
rec.name = "sphere";
return true;
}
#endif