-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle.cpp
More file actions
37 lines (31 loc) · 931 Bytes
/
Triangle.cpp
File metadata and controls
37 lines (31 loc) · 931 Bytes
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
#include "Triangle.h"
Triangle::Triangle(vectors::vec3 a, vectors::vec3 b, vectors::vec3 c) :
WorldObject((a+b+c).scale(1/3)),
vertices({a,b,c}),
normal((a-b).cross(b-c).normalize()){}
double Triangle::rayTouching(Ray r){
vectors::vec3 e1 = vertices[1] - vertices[0];
vectors::vec3 e2 = vertices[2] - vertices[0];
// Calculate planes normal vector
vectors::vec3 pvec = r.direction.cross(e2);
double det = e1.dot(pvec);
// Ray is parallel to plane
if (det < 1e-8 && det > -1e-8) {
return 0;
}
double inv_det = 1 / det;
vectors::vec3 tvec = r.pos - vertices[0];
double u = tvec.dot(pvec) * inv_det;
if (u < 0 || u > 1) {
return 0;
}
vectors::vec3 qvec = tvec.cross(e1);
double v = r.direction.dot(qvec) * inv_det;
if (v < 0 || u + v > 1) {
return 0;
}
return e2.dot(qvec) * inv_det;
}
vectors::vec3 Triangle::getNormal() const{
return normal;
}