-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtriangle.cpp
54 lines (44 loc) · 1.74 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
#include <triangle.h>
// an implementation of the Moller Trumbore algorithm from
// https://www.scratchapixel.com/lessons/3d-basic-rendering/ray-tracing-rendering-a-triangle/moller-trumbore-ray-triangle-intersection
bool Triangle::hit(const Ray& r, float tMin, float tMax, hitRecord& rec) const {
using namespace glm;
vec3 v0v1 = v1 - v0;
vec3 v0v2 = v2 - v0;
vec3 pvec = cross(r.direction(), v0v2);
float det = dot(v0v1, pvec);
// // if the determinant is negative the triangle is backfacing
// // if the determinant is close to 0, the ray misses the triangle
if (det < tMin) return false;
// // ray and triangle are parallel if det is close to 0
if (fabs(det) < tMin) return false;
float invDet = 1 / det;
vec3 tvec = r.origin() - v0;
float u = dot(tvec, pvec) * invDet;
if (u < 0.0f || u > 1.0f) return false;
vec3 qvec = cross(tvec, v0v1);
float v = dot(r.direction(), qvec) * invDet;
if (v < 0.0f || u + v > 1.0f) return false;
float t = dot(v0v2, qvec) * invDet;
rec.t = t;
rec.p = vec3(u,v,t);
rec.materialPtr = material;
// Get the normal at the hit point by interpolating from the vertex normals.
rec.normal = (1.0f - u - v) * n0 + u * n1 + v * n2;
return true;
}
// TODO verify
bool Triangle::boundingBox(Aabb& outputBox) const {
using namespace mathStuff;
vec3 offset(0.001f, 0.001f, 0.001f);
point min(
ffmin(v0.x, v1.x, v2.x),
ffmin(v0.y, v1.y, v2.y),
ffmin(v0.z, v1.z, v2.z));
point max(
ffmax(v0.x, v1.x, v2.x),
ffmax(v0.y, v1.y, v2.y),
ffmax(v0.z, v1.z, v2.z));
outputBox = Aabb(min - offset, max + offset);
return true;
}