Skip to content

Commit 5f14ee8

Browse files
author
Peter Shirley
committed
First commit of code from Ray Tracing the Rest of Your Life
1 parent 6dc5aa5 commit 5f14ee8

31 files changed

+9152
-0
lines changed

aabb.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#ifndef AABBH
2+
#define AABBH
3+
#include "ray.h"
4+
#include "hitable.h"
5+
6+
inline float ffmin(float a, float b) { return a < b ? a : b; }
7+
inline float ffmax(float a, float b) { return a > b ? a : b; }
8+
9+
class aabb {
10+
public:
11+
aabb() {}
12+
aabb(const vec3& a, const vec3& b) { _min = a; _max = b;}
13+
14+
vec3 min() const {return _min; }
15+
vec3 max() const {return _max; }
16+
17+
bool hit(const ray& r, float tmin, float tmax) const {
18+
for (int a = 0; a < 3; a++) {
19+
float t0 = ffmin((_min[a] - r.origin()[a]) / r.direction()[a],
20+
(_max[a] - r.origin()[a]) / r.direction()[a]);
21+
float t1 = ffmax((_min[a] - r.origin()[a]) / r.direction()[a],
22+
(_max[a] - r.origin()[a]) / r.direction()[a]);
23+
tmin = ffmax(t0, tmin);
24+
tmax = ffmin(t1, tmax);
25+
if (tmax <= tmin)
26+
return false;
27+
}
28+
return true;
29+
}
30+
31+
float area() const {
32+
float a = _max.x() - _min.x();
33+
float b = _max.y() - _min.y();
34+
float c = _max.z() - _min.z();
35+
return 2*(a*b + b*c + c*a);
36+
}
37+
38+
int longest_axis() const {
39+
float a = _max.x() - _min.x();
40+
float b = _max.y() - _min.y();
41+
float c = _max.z() - _min.z();
42+
if (a > b && a > c)
43+
return 0;
44+
else if (b > c)
45+
return 1;
46+
else
47+
return 2;
48+
}
49+
50+
vec3 _min;
51+
vec3 _max;
52+
};
53+
54+
aabb surrounding_box(aabb box0, aabb box1) {
55+
vec3 small( fmin(box0.min().x(), box1.min().x()),
56+
fmin(box0.min().y(), box1.min().y()),
57+
fmin(box0.min().z(), box1.min().z()));
58+
vec3 big ( fmax(box0.max().x(), box1.max().x()),
59+
fmax(box0.max().y(), box1.max().y()),
60+
fmax(box0.max().z(), box1.max().z()));
61+
return aabb(small,big);
62+
}
63+
64+
65+
#endif
66+
67+
68+
69+
70+

aarect.h

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#ifndef AARECTH
2+
#define AARECTH
3+
4+
#include "hitable.h"
5+
6+
class xy_rect: public hitable {
7+
public:
8+
xy_rect() {}
9+
xy_rect(float _x0, float _x1, float _y0, float _y1, float _k, material *mat) : x0(_x0), x1(_x1), y0(_y0), y1(_y1), k(_k), mp(mat) {};
10+
virtual bool hit(const ray& r, float t0, float t1, hit_record& rec) const;
11+
virtual bool bounding_box(float t0, float t1, aabb& box) const {
12+
box = aabb(vec3(x0,y0, k-0.0001), vec3(x1, y1, k+0.0001));
13+
return true; }
14+
material *mp;
15+
float x0, x1, y0, y1, k;
16+
};
17+
18+
class xz_rect: public hitable {
19+
public:
20+
xz_rect() {}
21+
xz_rect(float _x0, float _x1, float _z0, float _z1, float _k, material *mat) : x0(_x0), x1(_x1), z0(_z0), z1(_z1), k(_k), mp(mat) {};
22+
virtual bool hit(const ray& r, float t0, float t1, hit_record& rec) const;
23+
virtual bool bounding_box(float t0, float t1, aabb& box) const {
24+
box = aabb(vec3(x0,k-0.0001,z0), vec3(x1, k+0.0001, z1));
25+
return true;
26+
}
27+
virtual float pdf_value(const vec3& o, const vec3& v) const {
28+
hit_record rec;
29+
if (this->hit(ray(o, v), 0.001, FLT_MAX, rec)) {
30+
float area = (x1-x0)*(z1-z0);
31+
float distance_squared = rec.t * rec.t * v.squared_length();
32+
float cosine = fabs(dot(v, rec.normal) / v.length());
33+
return distance_squared / (cosine * area);
34+
}
35+
else
36+
return 0;
37+
}
38+
virtual vec3 random(const vec3& o) const {
39+
vec3 random_point = vec3(x0 + drand48()*(x1-x0), k, z0 + drand48()*(z1-z0));
40+
return random_point - o;
41+
}
42+
material *mp;
43+
float x0, x1, z0, z1, k;
44+
};
45+
46+
class yz_rect: public hitable {
47+
public:
48+
yz_rect() {}
49+
yz_rect(float _y0, float _y1, float _z0, float _z1, float _k, material *mat) : y0(_y0), y1(_y1), z0(_z0), z1(_z1), k(_k), mp(mat) {};
50+
virtual bool hit(const ray& r, float t0, float t1, hit_record& rec) const;
51+
virtual bool bounding_box(float t0, float t1, aabb& box) const {
52+
box = aabb(vec3(k-0.0001, y0, z0), vec3(k+0.0001, y1, z1));
53+
return true; }
54+
material *mp;
55+
float y0, y1, z0, z1, k;
56+
};
57+
58+
59+
60+
61+
bool xy_rect::hit(const ray& r, float t0, float t1, hit_record& rec) const {
62+
float t = (k-r.origin().z()) / r.direction().z();
63+
if (t < t0 || t > t1)
64+
return false;
65+
float x = r.origin().x() + t*r.direction().x();
66+
float y = r.origin().y() + t*r.direction().y();
67+
if (x < x0 || x > x1 || y < y0 || y > y1)
68+
return false;
69+
rec.u = (x-x0)/(x1-x0);
70+
rec.v = (y-y0)/(y1-y0);
71+
rec.t = t;
72+
rec.mat_ptr = mp;
73+
rec.p = r.point_at_parameter(t);
74+
rec.normal = vec3(0, 0, 1);
75+
return true;
76+
}
77+
78+
79+
bool xz_rect::hit(const ray& r, float t0, float t1, hit_record& rec) const {
80+
float t = (k-r.origin().y()) / r.direction().y();
81+
if (t < t0 || t > t1)
82+
return false;
83+
float x = r.origin().x() + t*r.direction().x();
84+
float z = r.origin().z() + t*r.direction().z();
85+
if (x < x0 || x > x1 || z < z0 || z > z1)
86+
return false;
87+
rec.u = (x-x0)/(x1-x0);
88+
rec.v = (z-z0)/(z1-z0);
89+
rec.t = t;
90+
rec.mat_ptr = mp;
91+
rec.p = r.point_at_parameter(t);
92+
rec.normal = vec3(0, 1, 0);
93+
return true;
94+
}
95+
96+
bool yz_rect::hit(const ray& r, float t0, float t1, hit_record& rec) const {
97+
float t = (k-r.origin().x()) / r.direction().x();
98+
if (t < t0 || t > t1)
99+
return false;
100+
float y = r.origin().y() + t*r.direction().y();
101+
float z = r.origin().z() + t*r.direction().z();
102+
if (y < y0 || y > y1 || z < z0 || z > z1)
103+
return false;
104+
rec.u = (y-y0)/(y1-y0);
105+
rec.v = (z-z0)/(z1-z0);
106+
rec.t = t;
107+
rec.mat_ptr = mp;
108+
rec.p = r.point_at_parameter(t);
109+
rec.normal = vec3(1, 0, 0);
110+
return true;
111+
}
112+
113+
#endif

box.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef BOXH
2+
#define BOXH
3+
4+
#include "aarect.h"
5+
#include "hitable_list.h"
6+
7+
class box: public hitable {
8+
public:
9+
box() {}
10+
box(const vec3& p0, const vec3& p1, material *ptr);
11+
virtual bool hit(const ray& r, float t0, float t1, hit_record& rec) const;
12+
virtual bool bounding_box(float t0, float t1, aabb& box) const {
13+
box = aabb(pmin, pmax);
14+
return true; }
15+
vec3 pmin, pmax;
16+
hitable *list_ptr;
17+
};
18+
19+
box::box(const vec3& p0, const vec3& p1, material *ptr) {
20+
pmin = p0;
21+
pmax = p1;
22+
hitable **list = new hitable*[6];
23+
list[0] = new xy_rect(p0.x(), p1.x(), p0.y(), p1.y(), p1.z(), ptr);
24+
list[1] = new flip_normals(new xy_rect(p0.x(), p1.x(), p0.y(), p1.y(), p0.z(), ptr));
25+
list[2] = new xz_rect(p0.x(), p1.x(), p0.z(), p1.z(), p1.y(), ptr);
26+
list[3] = new flip_normals(new xz_rect(p0.x(), p1.x(), p0.z(), p1.z(), p0.y(), ptr));
27+
list[4] = new yz_rect(p0.y(), p1.y(), p0.z(), p1.z(), p1.x(), ptr);
28+
list[5] = new flip_normals(new yz_rect(p0.y(), p1.y(), p0.z(), p1.z(), p0.x(), ptr));
29+
list_ptr = new hitable_list(list,6);
30+
}
31+
32+
bool box::hit(const ray& r, float t0, float t1, hit_record& rec) const {
33+
return list_ptr->hit(r, t0, t1, rec);
34+
}
35+
36+
#endif

bucamera.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef CAMERAH
2+
#define CAMERAH
3+
4+
#include "ray.h"
5+
6+
class camera {
7+
public:
8+
camera() {
9+
lower_left_corner = vec3(-2.0, -1.0, -1.0);
10+
horizontal = vec3(4.0, 0.0, 0.0);
11+
vertical = vec3(0.0, 2.0, 0.0);
12+
/*
13+
lower_left_corner = vec3(-.1, -0.05, -1.0);
14+
horizontal = vec3(0.2, 0.0, 0.0);
15+
vertical = vec3(0.0, 0.1, 0.0);
16+
*/
17+
origin = vec3(0.0, 0.0, 0.0);
18+
}
19+
20+
camera(vec3 lookfrom, vec3 lookat, vec3 view_up, float aspect, float vfov, float aperture, float distance_to_focus) {
21+
origin = lookfrom;
22+
w = unit_vector(lookfrom - lookat;
23+
u = unit_vector(cross(vup, w));
24+
v = cross(w, u);
25+
ZZ
26+
}
27+
ray get_ray(float s, float t) { return ray(origin, lower_left_corner + s*horizontal + t*vertical - origin); }
28+
29+
vec3 origin;
30+
vec3 lower_left_corner;
31+
vec3 horizontal;
32+
vec3 vertical;
33+
vec3 u, v, w;
34+
float radius;
35+
};
36+
37+
#endif
38+
39+
40+
41+

0 commit comments

Comments
 (0)