-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintersection.cpp
More file actions
56 lines (41 loc) · 1.22 KB
/
intersection.cpp
File metadata and controls
56 lines (41 loc) · 1.22 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
#include "model.h"
#define EPSILON 0.0001
// ray-triangle intersection predicate (Möller–Trumbore algorithm)
int ray_triangle(const vertex v0, // triangle vertices
const vertex v1,
const vertex v2,
const vertex o, // ray origin
const vector d, // ray direction
float* out)
{
vector e0, e1, p, q, t;
float det, inv_det, u, v, tf;
// vectors of edges sharing V0
e0 = vector(v0, v1);
e1 = vector(v0, v2);
// determinant calculation - also used for u parameter
p = d % e1;
// if det ~= zero, ray lies in plane of triangle
det = e0 * p;
// not culling
if(det > -EPSILON && det < EPSILON) return 0;
inv_det = 1.f / det;
// distance from V0 to ray origin
t = vector(v0, o);
// u parameter calculation and bound test
u = (t * p) * inv_det;
// intersection lies outside of the triangle
if(u < 0.f || u > 1.f) return 0;
// v parameter test preparation
q = t % e0;
// v parameter calculation and bound test
v = (d * q) * inv_det;
// intersection outside of the triangle
if(v < 0.f || u + v > 1.f) return 0;
tf = (e1 * q) * inv_det;
if(tf > EPSILON) {
*out = tf;
return 1;
}
return 0;
}