-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriangle.h
212 lines (166 loc) · 6.22 KB
/
triangle.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#ifndef TRIANGLE_H
#define TRIANGLE_H
#include "RTIOW/vec3.h"
#include "primitive.h"
#include "hittable.h"
#include "texture.h"
#include <cmath>
inline void triangle_rotate_in_x(point3 &p1, point3 &p2, point3 &p3,
double rotate_angle_x) {
double v1_x, v2_x, v3_x;
double v1_y, v2_y, v3_y;
double v1_z, v2_z, v3_z;
v1_x = p1.x();
v1_y = p1.y() * cos(rotate_angle_x) + p1.z() * sin(rotate_angle_x);
v1_z = p1.y() * (-sin(rotate_angle_x)) + p1.z() * cos(rotate_angle_x);
v2_x = p2.x();
v2_y = p2.y() * cos(rotate_angle_x) + p2.z() * sin(rotate_angle_x);
v2_z = p2.y() * (-sin(rotate_angle_x)) + p2.z() * cos(rotate_angle_x);
v3_x = p3.x();
v3_y = p3.y() * cos(rotate_angle_x) + p3.z() * sin(rotate_angle_x);
v3_z = p3.y() * (-sin(rotate_angle_x)) + p3.z() * cos(rotate_angle_x);
p1 = vec3(v1_x, v1_y, v1_z);
p2 = vec3(v2_x, v2_y, v2_z);
p3 = vec3(v3_x, v3_y, v3_z);
}
inline void triangle_rotate_in_y(point3 &p1, point3 &p2, point3 &p3,
double rotate_angle_y) {
double v1_x, v2_x, v3_x;
double v1_y, v2_y, v3_y;
double v1_z, v2_z, v3_z;
v1_x = p1.x() * cos(rotate_angle_y) + p1.z() * -sin(rotate_angle_y);
v1_y = p1.y();
v1_z = p1.x() * (sin(rotate_angle_y)) + p1.z() * cos(rotate_angle_y);
v2_x = p2.x() * cos(rotate_angle_y) + p2.z() * -sin(rotate_angle_y);
v2_y = p2.y();
v2_z = p2.x() * (sin(rotate_angle_y)) + p2.z() * cos(rotate_angle_y);
v3_x = p3.x() * cos(rotate_angle_y) + p3.z() * -sin(rotate_angle_y);
v3_y = p3.y();
v3_z = p3.x() * (sin(rotate_angle_y)) + p3.z() * cos(rotate_angle_y);
p1 = vec3(v1_x, v1_y, v1_z);
p2 = vec3(v2_x, v2_y, v2_z);
p3 = vec3(v3_x, v3_y, v3_z);
}
inline void triangle_rotate_in_z(point3 &p1, point3 &p2, point3 &p3,
double rotate_angle_z) {
double v1_x, v2_x, v3_x;
double v1_y, v2_y, v3_y;
double v1_z, v2_z, v3_z;
v1_x = p1.x() * cos(rotate_angle_z) + p1.y() * sin(rotate_angle_z);
v1_y = p1.x() * (-sin(rotate_angle_z)) + p1.y() * cos(rotate_angle_z);
v1_z = p1.z();
v2_x = p2.x() * cos(rotate_angle_z) + p2.y() * sin(rotate_angle_z);
v2_y = p2.x() * (-sin(rotate_angle_z)) + p2.y() * cos(rotate_angle_z);
v2_z = p2.z();
v3_x = p3.x() * cos(rotate_angle_z) + p3.y() * sin(rotate_angle_z);
v3_y = p3.x() * (-sin(rotate_angle_z)) + p3.y() * cos(rotate_angle_z);
v3_z = p3.z();
p1 = vec3(v1_x, v1_y, v1_z);
p2 = vec3(v2_x, v2_y, v2_z);
p3 = vec3(v3_x, v3_y, v3_z);
}
class triangle : public primitive {
public:
triangle() {}
triangle(point3 p0_, point3 p1_, point3 p2_, color m) {
p0 = p0_;
p1 = p1_;
p2 = p2_;
albedo = m;
has_texture = false;
center = point3((p0.x() + p1.x() + p2.x()) / 3, (p0.y() + p1.y() + p2.y()) / 3, (p0.z() + p1.z() + p2.z()) / 3);
}
triangle(point3 p0_, point3 p1_, point3 p2_, texture t) {
p0 = p0_;
p1 = p1_;
p2 = p2_;
tex = t;
has_texture = true;
center = point3((p0.x() + p1.x() + p2.x()) / 3, (p0.y() + p1.y() + p2.y()) / 3, (p0.z() + p1.z() + p2.z()) / 3);
}
void transform(vec3 translate_by, vec3 scale_by, vec3 rotate_by) {
double rotate_x = rotate_by.x() * 0.01745;
double rotate_y = rotate_by.y() * 0.01745;
double rotate_z = rotate_by.z() * 0.01745;
triangle_rotate_in_z(p0, p1, p2, rotate_z);
triangle_rotate_in_x(p0, p1, p2, rotate_x);
triangle_rotate_in_y(p0, p1, p2, rotate_y);
p0 = vec3(p0.x() * scale_by.x(), p0.y() * scale_by.y(),
p0.z() * scale_by.z());
p1 = vec3(p1.x() * scale_by.x(), p1.y() * scale_by.y(),
p1.z() * scale_by.z());
p2 = vec3(p2.x() * scale_by.x(), p2.y() * scale_by.y(),
p2.z() * scale_by.z());
p0 = p0 + translate_by;
p1 = p1 + translate_by;
p2 = p2 + translate_by;
center = point3((p0.x() + p1.x() + p2.x()) / 3, (p0.y() + p1.y() + p2.y()) / 3, (p0.z() + p1.z() + p2.z()) / 3);
}
virtual bool hit(const ray &r, double t_min, double t_max, hit_record &rec) const override;
virtual aabb construct_aabb() override {
vec3 max_of_two = get_maximum_vector_for_aabb(p0, p1);
vec3 aabb_max = get_maximum_vector_for_aabb(p2, max_of_two);
vec3 min_of_two = get_minimum_vector_for_aabb(p0, p1);
vec3 aabb_min = get_minimum_vector_for_aabb(p2, min_of_two);
return aabb(aabb_min, aabb_max);
}
virtual vec3 get_center() override {
return center;
}
public:
point3 p0;
point3 p1;
point3 p2;
point3 center;
color albedo;
bool has_texture;
texture tex;
};
// Special thanks to author Inigo Quilez:
// https://www.iquilezles.org/www/articles/intersectors/intersectors.htm
bool triangle::hit(const ray &r, double t_min, double t_max, hit_record &rec) const {
const vec3 p0_to_p1 = p1 - p0;
const vec3 p0_to_p2 = p2 - p0;
vec3 p0_to_ray_origin = r.origin() - p0;
vec3 normal = cross(p0_to_p1, p0_to_p2);
vec3 q = cross(p0_to_ray_origin, r.direction());
double determinant = 1 / dot(r.direction(), normal);
if (std::isinf(determinant)) {
return false;
}
double u_ = determinant * dot(-q, p0_to_p2);
if (u_ < 0.0 || u_ > 1.0)
return false;
double v_ = determinant * dot(q, p0_to_p1);
if (v_ < 0.0 || (u_ + v_) > 1.0)
return false;
double t = determinant * dot(-normal, p0_to_ray_origin);
if (t < t_min || t_max < t) {
return false;
}
rec.t = t;
// if (isnan(rec.t)) {
// std::cout << std::endl << "Warning: this triangle was hit but the hit constant t is nan :(\n";
// std::cout << "Ray direction: " << r.direction() << std::endl;
// std::cout << "Determinant: " << determinant << std::endl;
// std::cout << "dot product with -normal and point0 to ray origin: " << dot(-normal, p0_to_ray_origin) << std::endl;
// std::cout << "t: " << rec.t << std::endl;
// std::cout << "t_min: " << t_min << std::endl;
// std::cout << "t_max: " << t_max << std::endl;
// }
rec.p = r.at(rec.t);
rec.u = u_;
rec.v = v_;
// if (isnan(rec.p.x()) || isnan(rec.p.y()) || isnan(rec.p.z())) {
// std::cout << std::endl << "Warning: this triangle was hit but the hit position is nan :(\n";
// }
rec.set_face_normal(r, unit_vector(normal));
if (has_texture) {
rec.albedo = tex.get_texel(rec.u, rec.v);
} else {
rec.albedo = albedo;
}
rec.name = "triangle";
return true;
}
#endif