-
Notifications
You must be signed in to change notification settings - Fork 0
/
Triangle.cpp
100 lines (78 loc) · 2.32 KB
/
Triangle.cpp
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
#include "Triangle.h"
Triangle::Triangle(const Vector3 &p0, const Vector3 &p1, const Vector3 &p2, Material *material)
: p0(p0), p1(p1), p2(p2), material(material)
{
bbox.min.x = fmin(fmin(p0.x, p1.x), p2.x);
bbox.min.y = fmin(fmin(p0.y, p1.y), p2.y);
bbox.min.z = fmin(fmin(p0.z, p1.z), p2.z);
bbox.max.x = fmax(fmax(p0.x, p1.x), p2.x);
bbox.max.y = fmax(fmax(p0.y, p1.y), p2.y);
bbox.max.z = fmax(fmax(p0.z, p1.z), p2.z);
}
bool Triangle::Hit(const Ray &r, float tmin, float tmax, float time, HitRecord &record) const
{
float t;
float A = p0.x - p1.x;
float B = p0.y - p1.y;
float C = p0.z - p1.z;
float D = p0.x - p2.x;
float E = p0.y - p2.y;
float F = p0.z - p2.z;
float G = r.d.x;
float H = r.d.y;
float I = r.d.z;
float J = p0.x - r.o.x;
float K = p0.y - r.o.y;
float L = p0.z - r.o.z;
float EIHF = E*I - H*F;
float GFDI = G*F - D*I;
float DHEG = D*H - E*G;
float denom = (A*EIHF + B*GFDI + C*DHEG);
float beta = (J*EIHF + K*GFDI + L*DHEG) / denom;
if (beta <= 0.0f || beta >= 1.0f) return false;
float AKJB = A*K - J*B;
float JCAL = J*C - A*L;
float BLKC = B*L - K*C;
float gamma = (I*AKJB + H*JCAL + G*BLKC) / denom;
if (gamma <= 0.0 || beta + gamma >= 1.0f) return false;
t= -(F*AKJB + E*JCAL + D*BLKC) / denom;
if (t>= tmin && t<= tmax)
{
record.t = t;
record.pos = r.o + t*r.d;
//record.normal = UnitVector(Cross((p1 - p0), (p2 - p0)));
record.uvw.InitFromW(Cross(p1 - p0, p2 - p0));
record.material = material;
return true;
}
return false;
}
bool Triangle::ShadowHit(const Ray &r, float tmin, float tmax, float time) const
{
float tval;
float A = p0.x - p1.x;
float B = p0.y - p1.y;
float C = p0.z - p1.z;
float D = p0.x - p2.x;
float E = p0.y - p2.y;
float F = p0.z - p2.z;
float G = r.d.x;
float H = r.d.y;
float I = r.d.z;
float J = p0.x - r.o.x;
float K = p0.y - r.o.y;
float L = p0.z - r.o.z;
float EIHF = E*I - H*F;
float GFDI = G*F - D*I;
float DHEG = D*H - E*G;
float denom = (A*EIHF + B*GFDI + C*DHEG);
float beta = (J*EIHF + K*GFDI + L*DHEG) / denom;
if (beta <= 0.0f || beta >= 1.0f) return false;
float AKJB = A*K - J*B;
float JCAL = J*C - A*L;
float BLKC = B*L - K*C;
float gamma = (I*AKJB + H*JCAL + G*BLKC) / denom;
if (gamma <= 0.0f || beta + gamma >= 1.0f) return false;
tval = -(F*AKJB + E*JCAL + D*BLKC) / denom;
return (tval >= tmin&&tval <= tmax);
}