-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShape.cpp
More file actions
299 lines (217 loc) · 8.06 KB
/
Shape.cpp
File metadata and controls
299 lines (217 loc) · 8.06 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <list>
#include <algorithm>
#include "Shape.hpp"
#include "Debug.hpp"
extern "C" {
#include "print.h"
}
Hit::Hit(Shape*s) {
this->shape = s;
}
Hit::Hit() : Hit(nullptr) {
}
Hit::operator bool() const {
return this->shape;
}
Sphere::Sphere(Vector3 position, fixed32 radius) {
this->transform = Transform(position);
this->radius = radius;
}
Sphere::Sphere(Vector3 position) : Sphere(position, 1) {
}
Sphere::Sphere(fixed32 radius) : Sphere(Vector3(0), radius) {
}
Sphere::Sphere() : Sphere(1) {
}
Hit Sphere::intersectRay(Ray r) {
Vector3 o = r.origin;
Vector3 d = r.direction;
Vector3 center = this->transform.position - o;
fixed32 a = d.dot(d);
fixed32 b = d.dot(center) * -2;
fixed32 c = center.dot(center) - this->radius * this->radius;
fixed32 discrim = b*b - (a*4)*c;
if (debugPrintingEnabled) {
mgba_printf("Debugging shape with color (%x, %x, %x)", this->material->diffuseColor.x, this->material->diffuseColor.y, this->material->diffuseColor.z);
mgba_printf("Direction: (%x, %x, %x)", d.x, d.y, d.z);
mgba_printf("Direction Magnitude: %x", d.magnitude());
mgba_printf("A: %x", a);
mgba_printf("B: %x", b);
mgba_printf("C: %x", c);
mgba_printf("b*b: %x", b*b);
mgba_printf("4a: %x", a*4);
mgba_printf("4ac: %x", (a*4)*c);
mgba_printf("Discrim: %x", discrim);
}
Hit result = Hit();
result.shape = NULL;
if (discrim < 0) return result;
fixed32 root = discrim.sqrt();
fixed32 t1 = (-b + root)/(a*2);
fixed32 t2 = (-b - root)/(a*2);
if (debugPrintingEnabled) {
mgba_printf("Root: %x", root);
mgba_printf("2a: %x", a*2);
mgba_printf("-b: %x", -b);
mgba_printf("-b+root: %x", -b + root);
mgba_printf("-b-root: %x", -b - root);
mgba_printf("T1: %x", t1);
mgba_printf("T2: %x", t2);
}
std::list<fixed32> tOptions = std::list<fixed32>{t1, t2};
std::remove_if(tOptions.begin(), tOptions.end(), [](fixed32 t) {return t < 0;});
if (tOptions.empty()) return result;
fixed32 finalT = *std::min_element(tOptions.begin(), tOptions.end());
if (debugPrintingEnabled) {
mgba_printf("final T: %x", finalT);
}
result.shape = this;
result.t = finalT;
result.position = r.evaluateT(finalT);
result.normal = (result.position - this->transform.position).normalized();
if (debugPrintingEnabled) {
mgba_printf("Normal: (%x, %x, %x)", result.normal.x, result.normal.y, result.normal.z);
}
result.ray = r;
return result;
}
void Sphere::generateBoundingBox() {
this->boundingBox.a = this->transform.position - radius;
this->boundingBox.b = this->transform.position + radius;
}
BoundingBox3 Sphere::getBoundingBox() const {
return this->boundingBox;
}
Triangle::Triangle(Vector3 v1, Vector3 v2, Vector3 v3) : Plane(v1, (v2-v1).normalized().cross((v3-v1).normalized()).normalized()) {
this->v1 = v1;
this->v2 = v2;
this->v3 = v3;
}
Triangle::Triangle() : Triangle(Vector3(0,1,0), Vector3(1,-1,0), Vector3(-1,-1,0)) {
}
Hit Triangle::intersectRay(Ray r) {
Hit result = Plane::intersectRay(r);
Vector3 intersection = result.position;
Vector3 v1 = this->v1;
Vector3 v2 = this->v2;
Vector3 v3 = this->v3;
fixed32 areaABC = this->normal.dot((v1-v2).cross(v3-v1));
fixed32 areaPBC = this->normal.dot((v2-intersection).cross(v3-intersection));
fixed32 areaPCA = this->normal.dot((v3-intersection).cross(v1-intersection));
fixed32 areaPAB = this->normal.dot((v1-intersection).cross(v2-intersection));
Vector3 bary;
bary.x = areaPBC/areaABC;
bary.y = areaPCA/areaABC;
bary.z = areaPAB/areaABC;
if (debugPrintingEnabled) {
mgba_printf("Bary: (%x, %x, %x)", bary.x, bary.y, bary.z);
}
if (!((bary.x <= 0 && bary.y <= 0 && bary.z <= 0) || (bary.x >= 0 && bary.y >= 0 && bary.z >= 0))) {
return Hit();
}
return result;
}
Plane::Plane(Vector3 pos, Vector3 norm) {
this->position = pos;
this->normal = norm.normalized();
}
Plane::Plane(Vector3 pos) : Plane(pos, Vector3(0,1,0)) {
}
Plane::Plane() : Plane(Vector3(0)) {
}
Hit Plane::intersectRay(Ray r) {
Vector3 u = position-r.origin;
if ((this->normal.dot(r.direction)) == 0) {
return Hit();
}
fixed32 t = this->normal.dot(u) / this->normal.dot(r.direction);
Vector3 intersection = r.evaluateT(t);
Hit result = Hit(this);
result.position = intersection;
result.t = t;
result.ray = r;
if (this->normal.dot(r.direction) < 0) {
result.normal = this->normal;
} else {
result.normal = -this->normal;
}
return result;
}
Disc::Disc(Vector3 pos, Vector3 norm, fixed32 rad) : Plane(pos, norm) {
this->radius = rad;
}
Disc::Disc(Vector3 pos, fixed32 rad) : Disc(pos, Vector3(0,1,0), rad) {}
Disc::Disc(Vector3 pos, Vector3 norm) : Disc(pos, norm, 1) {}
Disc::Disc(Vector3 pos) : Disc(pos, 1) {}
Disc::Disc(fixed32 rad) : Disc(Vector3(0), rad) {}
Disc::Disc() : Disc(1) {}
Hit Disc::intersectRay(Ray r) {
Hit result = Plane::intersectRay(r);
if ((result.position - this->position).magnitude() > this->radius) {
return Hit();
}
return result;
}
Cylinder::Cylinder(Vector3 pos, Vector3 norm, fixed32 rad, fixed32 height) : Disc(pos, norm, rad) {
this->height = height;
}
Cylinder::Cylinder(Vector3 pos, Vector3 norm, fixed32 rad) : Cylinder(pos, norm, rad, 1) {}
Cylinder::Cylinder(Vector3 pos, Vector3 norm) : Cylinder(pos, norm, 1) {}
Cylinder::Cylinder(Vector3 pos, fixed32 rad, fixed32 height) : Cylinder(pos, Vector3(0,1,0), rad, height) {}
Cylinder::Cylinder(Vector3 pos, fixed32 rad) : Cylinder(pos, rad, 1) {}
Cylinder::Cylinder(Vector3 pos) : Cylinder(pos, 1) {}
Cylinder::Cylinder(fixed32 rad, fixed32 height) : Cylinder(Vector3(0), rad, height) {}
Cylinder::Cylinder(fixed32 rad) : Cylinder(rad, 1) {}
Cylinder::Cylinder() : Cylinder(1) {}
Hit Cylinder::intersectRay(Ray r) {
std::list<Hit> hitOptions = std::list<Hit>();
Hit d1Hit = d1.intersectRay(r);
if (d1Hit) hitOptions.push_front(d1Hit);
Hit d2Hit = d2.intersectRay(r);
if (d2Hit) hitOptions.push_front(d2Hit);
Vector3 o = r.origin;
Vector3 d = r.direction;
Vector3 cVec = this->position - o;
fixed32 a = d.x*d.x + d.z*d.z;
fixed32 b = d.x*cVec.x*-2 + d.z*cVec.z*-2;
fixed32 c = cVec.x*cVec.x + cVec.z*cVec.z - this->radius*this->radius;
fixed32 discrim = b*b - a * c * 4;
if (discrim >= 0) {
fixed32 root = discrim.sqrt();
std::list<fixed32> tOptions = std::list<fixed32>{(-b + root)/(a*2), (-b - root)/(a*2)};
for (fixed32 t : tOptions) {
if (t<0) continue;
Hit h = Hit(this);
h.t = t;
h.position = r.evaluateT(t);
if (!(h.position.y < this->position.y + this->height/2 && h.position.y > this->position.y - this->height/2)) continue;
h.normal = h.position - this->position;
h.normal.y = 0;
h.normal = h.normal.normalized();
h.ray = r;
hitOptions.push_back(h);
}
}
if (hitOptions.size() <= 0) return Hit();
Hit outHit;
outHit = *std::min_element(hitOptions.begin(), hitOptions.end(), [](Hit a, Hit b){
return b.t > a.t;
});
outHit.shape = this;
return outHit;
}
void Cylinder::generateSubshapes() {
d1 = Disc(this->position - this->normal * this->height / 2, this->normal, this->radius);
d2 = Disc(this->position + this->normal * this->height / 2, this->normal, this->radius);
}
void Cylinder::generateBoundingBox() {
this->boundingBox.a.y = this->position - this->height / 2;
this->boundingBox.b.y = this->position + this->height / 2;
this->boundingBox.a.x = this->position.x - this->radius;
this->boundingBox.a.y = this->position.y - this->radius;
this->boundingBox.b.x = this->position.x + this->radius;
this->boundingBox.b.y = this->position.y + this->radius;
}
BoundingBox3 Cylinder::getBoundingBox() const {
return this->boundingBox;
}