-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriangle.h
More file actions
80 lines (64 loc) · 1.86 KB
/
triangle.h
File metadata and controls
80 lines (64 loc) · 1.86 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
#ifndef TRIANGLE_H
#define TRIANGLE_H
#include "rtweekend.h"
#include "hittable.h"
class triangle : public hittable {
public:
triangle(const point3& Q, const vec3& u, const vec3& v, shared_ptr<material> mat)
: Q(Q), u(u), v(v), mat(mat)
{
auto n = cross(u, v);
if (n.length_squared() < 1e-8) {
normal = vec3(1, 0, 0);
w = vec3(0, 0, 0);
D = dot(normal, Q);
}
else {
normal = unit_vector(n);
D = dot(normal, Q);
w = n / dot(n, n);
}
set_bounding_box();
}
virtual void set_bounding_box() {
auto bbox_edge = aabb(Q, Q + u);
auto bbox_point = aabb(Q + v, Q + v);
bbox = aabb(bbox_edge, bbox_point);
}
aabb bounding_box() const override { return bbox; }
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {
auto denom = dot(normal, r.direction());
if (std::fabs(denom) < 1e-8)
return false;
auto t = (D - dot(normal, r.origin())) / denom;
if (!ray_t.contains(t))
return false;
auto intersection = r.at(t);
vec3 planar_hitpt_vector = intersection - Q;
auto alpha = dot(w, cross(planar_hitpt_vector, v));
auto beta = dot(w, cross(u, planar_hitpt_vector));
if (!is_interior(alpha, beta, rec))
return false;
rec.t = t;
rec.p = intersection;
rec.mat = mat;
rec.set_face_normal(r, normal);
return true;
}
virtual bool is_interior(double a, double b, hit_record& rec) const {
if ((a < 0) || (b < 0) || (a + b > 1))
return false;
rec.u = a;
rec.v = b;
return true;
}
private:
point3 Q;
vec3 u, v;
shared_ptr<material> mat;
aabb bbox;
vec3 normal;
double D;
vec3 w;
};
#endif