-
Notifications
You must be signed in to change notification settings - Fork 1
/
complexObject.cpp~
68 lines (56 loc) · 2.36 KB
/
complexObject.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
#include "complexObject.h"
ComplexObject::ComplexObject(Triangle coT,const Vec3Df & corayDirection, const Vec3Df & corayOrigin, Material mat, Mesh comyMesh){
T = coT;
rayDirection = corayDirection;
rayOrigin = corayOrigin;
material = mat;
myMesh = comyMesh;
}
Hit ComplexObject::intersect(Vec3Df origin, Vec3Df dest){
// Our implementation is based on the proposed algorithm of Dan Sunday at: http://geomalgorithms.com/a06-_intersect-2.html
//std::cout<<T.v[0]<<" "<<T.v[1]<<" "<<T.v[2]<<std::endl;
Vertex v0 = myMesh.vertices[T.v[0]];
Vertex v1 = myMesh.vertices[T.v[1]];
Vertex v2 = myMesh.vertices[T.v[2]];
// Edge vectors
Vec3Df u = v1.p-v0.p;
Vec3Df v = v2.p-v0.p;
Vec3Df n = Vec3Df::crossProduct(u, v);
Vec3Df w0 = rayOrigin - v0.p;
float a = -Vec3Df::dotProduct(n, w0);
float b = Vec3Df::dotProduct(n, rayDirection);
// Use this as a threshold to avoid division overflow
float small = 0.00000001;
if (fabs(b) < small) { // ray is parallel to triangle plane
if (a == 0) // ray lies in triangle plane
return Hit(0, Vec3Df(0,0,0), Vec3Df(0,0,0), material);
else
return Hit(0, Vec3Df(0,0,0), Vec3Df(0,0,0), material); // ray is parallel and disjoint from plane
}
// Get intersection point of ray with triangle plane
float r = a / b;
if (r < 0.0) // ray goes away from triangle
return Hit(0, Vec3Df(0,0,0), Vec3Df(0,0,0), material); // Then there is no intersect
// intersect point of ray and plane
Vec3Df I = rayOrigin + (r * rayDirection);
// Next up; triangle text; is I inside T?
float uu, uv, vv, wu, wv, D;
uu = Vec3Df::dotProduct(u, u);
uv = Vec3Df::dotProduct(u, v);
vv = Vec3Df::dotProduct(v, v);
Vec3Df w = I - v0.p;
wu = Vec3Df::dotProduct(w, u);
wv = Vec3Df::dotProduct(w, v);
D = uv * uv - uu * vv;
// get and test parametric coords
float s, t;
s = (uv * wv - vv * wu) / D;
if (s < 0.0 || s > 1.0) // I is outside T
return Hit(0, Vec3Df(0,0,0), Vec3Df(0,0,0), material);
t = (uv * wu - uu * wv) / D;
if (t < 0.0 || (s + t) > 1.0) // I is outside T
return Hit(0, Vec3Df(0,0,0), Vec3Df(0,0,0), material);
Vec3Df normal = I;
normal.normalize();
return Hit(1, I, normal, material); // I is in T
}