-
Notifications
You must be signed in to change notification settings - Fork 0
/
Triangle.h
117 lines (90 loc) · 2.47 KB
/
Triangle.h
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#pragma once
#include "SceneObject.h"
class Triangle : public SceneObject
{
public:
Vec3 Corners[3]{ Vec3(0, 0, 5), Vec3(5, 5, 0), Vec3(10, 0, -5) }; //Triangle
Triangle() restrict(amp, cpu)
{
}
Triangle(Vec3 Corners[3]) restrict(amp, cpu)
{
this->Corners[0] = Corners[0];
this->Corners[1] = Corners[1];
this->Corners[2] = Corners[2];
}
Triangle(Vec3 Corners[3], Color c) restrict(amp, cpu) : Triangle(Corners)
{
this->color = c;
}
Triangle(Vec3 Corners[3], Color c, float reflectivity, float opacity) restrict(amp, cpu) : Triangle(Corners)
{
this->color = c;
this->opacity = opacity;
this->reflectivity = reflectivity;
}
Triangle(Vec3 c1, Vec3 c2, Vec3 c3) restrict(amp, cpu)
{
this->Corners[0] = c1;
this->Corners[1] = c2;
this->Corners[2] = c3;
}
Triangle(Vec3 c1, Vec3 c2, Vec3 c3, Color c) restrict(amp, cpu)
{
this->Corners[0] = c1;
this->Corners[1] = c2;
this->Corners[2] = c3;
this->color = c;
}
Triangle(Vec3 c1, Vec3 c2, Vec3 c3, Color c, float reflectivity, float opacity) restrict(amp, cpu)
{
this->Corners[0] = c1;
this->Corners[1] = c2;
this->Corners[2] = c3;
this->color = c;
this->opacity = opacity;
this->reflectivity = reflectivity;
}
Vec3 ApproxPosition() restrict(amp, cpu)
{
return (Corners[0] + Corners[1] + Corners[2]) * (1.0f / 3);
}
float RayHitDistance(Ray r) restrict(amp, cpu)
{
Vec3 E1 = Corners[1] - Corners[0], E2 = Corners[2] - Corners[0];
Vec3 h = r.Direction.cross(E2);
float a = E1.dot(h);
if (a > -0.0000001 && a < 0.0000001) return -1;
float f = 1.0f / a;
Vec3 s = r.Origin - Corners[0];
float u = f * s.dot(h);
if (u < 0 || u > 1) return -1;
Vec3 q = s.cross(E1);
float v = f * r.Direction.dot(q);
if (v < 0 || u + v > 1) return -1;
float t = f * E2.dot(q);
if (t > 0.0000001)
{
return t;
}
return -1;
}
/*float CorrectDistance(Ray r, float d) restrict(amp, cpu) {
return (r.Origin - IntersectionPoint(r, d)).norm() / r.Origin.norm();
}*/
bool RayHit(Ray r) restrict(amp, cpu)
{
return RayHitDistance(r) != -1;
}
Ray PointNormal(Vec3 Point, Ray oRay) restrict(amp, cpu)
{
Ray nRay;
Vec3 V = Corners[1] - Corners[0], W = Corners[2] - Corners[0];
Vec3 N(V.y * V.z - V.z * W.y, V.z * W.x - V.x * W.z, V.x * W.y - V.y * W.x);
nRay.Direction = oRay.Direction * 1;
nRay.Direction.normalise();
nRay.Direction = (nRay.Direction + (nRay.Direction * N * V) * 2) * -1;
nRay.Origin = Point /*+ (nRay.Direction * 0.0000001f)*/;
return nRay;
}
};