-
Notifications
You must be signed in to change notification settings - Fork 3
/
llighttypes.h
740 lines (611 loc) · 16.8 KB
/
llighttypes.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
#pragma once
#include "core/color.h"
#include "core/math/vector2.h"
#include "core/math/vector3.h"
#include "lvector.h"
#include "scene/3d/mesh_instance.h"
namespace LM {
struct MiniList {
uint32_t first;
uint32_t num;
};
class Vec3i {
public:
Vec3i() {}
Vec3i(int xx, int yy, int zz) {
x = xx;
y = yy;
z = zz;
}
int32_t x, y, z;
Vec3i &operator-=(const Vec3i &v) {
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
int SquareLength() const { return (x * x) + (y * y) + (z * z); }
float Length() const { return sqrtf(SquareLength()); }
void Set(int xx, int yy, int zz) {
x = xx;
y = yy;
z = zz;
}
void Set_Round(const Vector3 &p) {
x = (int)(p.x + 0.5f);
y = (int)(p.y + 0.5f);
z = (int)(p.z + 0.5f);
}
void To(Vector3 &p) const {
p.x = x;
p.y = y;
p.z = z;
}
String ToString() const { return itos(x) + ", " + itos(y) + ", " + itos(z); }
};
class Vec2_i16 {
public:
Vec2_i16() {}
Vec2_i16(int xx, int yy) {
x = xx;
y = yy;
}
int16_t x;
int16_t y;
void Set(int xx, int yy) {
x = xx;
y = yy;
}
bool IsZero() const { return (x == 0) && (y == 0); }
bool IsNonZero() const { return !IsZero(); }
};
struct EmissionTri {
uint32_t tri_id;
float area;
};
class Tri {
public:
Vector3 pos[3];
Vector3 GetCentre() const { return (pos[0] + pos[1] + pos[2]) / 3.0f; }
void FlipWinding() {
Vector3 temp = pos[0];
pos[0] = pos[2];
pos[2] = temp;
}
void FindBarycentric(const Vector3 &pt, float &u, float &v, float &w) const;
void InterpolateBarycentric(Vector3 &pt, const Vector3 &bary) const { InterpolateBarycentric(pt, bary.x, bary.y, bary.z); }
void InterpolateBarycentric(Vector3 &pt, float u, float v, float w) const;
void FindNormal_EdgeForm(Vector3 &norm) const {
norm = -pos[0].cross(pos[1]);
norm.normalize();
}
void FindNormal(Vector3 &norm) const {
Vector3 e0 = pos[1] - pos[0];
Vector3 e1 = pos[2] - pos[1];
norm = -e0.cross(e1);
norm.normalize();
}
void ConvertToEdgeForm() {
Tri t = *this;
// b - a
pos[0] = t.pos[1] - t.pos[0];
// c - a
pos[1] = t.pos[2] - t.pos[0];
// a
pos[2] = t.pos[0];
}
float CalculateArea() const {
return sqrtf(CalculateTwiceAreaSquared()) * 0.5f;
}
float CalculateTwiceAreaSquared() const {
// compute the area squared. Which is the result of the cross product of two edges. If it's near zero, bingo
Vector3 edge1 = pos[1] - pos[0];
Vector3 edge2 = pos[2] - pos[0];
Vector3 vec = edge1.cross(edge2);
return vec.length_squared();
}
};
class Ray {
public:
Vector3 o; // origin
Vector3 d; // direction
bool TestIntersect(const Tri &tri, float &t) const;
bool TestIntersect_EdgeForm(const Tri &tri_edge, float &t) const;
void FindIntersect(const Tri &tri, float t, float &u, float &v, float &w) const;
bool IntersectAAPlane(int axis, Vector3 &pt, float epsilon = 0.0001f) const;
};
// a ray in flight .. in the ray bank to be processed
struct FHit {
void SetNoHit() { tx = UINT16_MAX; }
bool IsNoHit() const { return tx == UINT16_MAX; }
// texel hit points
uint16_t tx, ty;
};
struct FColor {
float r, g, b;
void Set(float v) {
r = v;
g = v;
b = v;
}
void Set(float rr, float gg, float bb) {
r = rr;
g = gg;
b = bb;
}
void Set(const Color &col) {
r = col.r;
g = col.g;
b = col.b;
}
float Max() const { return MAX(r, MAX(g, b)); }
FColor operator*(float v) const {
FColor s;
s.r = r * v;
s.g = g * v;
s.b = b * v;
return s;
}
FColor operator/(float v) const {
FColor s;
s.r = r / v;
s.g = g / v;
s.b = b / v;
return s;
}
FColor &operator+=(const FColor &v) {
r += v.r;
g += v.g;
b += v.b;
return *this;
}
FColor &operator-=(const FColor &v) {
r -= v.r;
g -= v.g;
b -= v.b;
return *this;
}
FColor &operator*=(float v) {
r *= v;
g *= v;
b *= v;
return *this;
}
FColor operator*(const FColor &o) const {
FColor s;
s.r = r * o.r;
s.g = g * o.g;
s.b = b * o.b;
return s;
}
void Lerp(const FColor &o, float f) {
float mf = 1.0f - f;
r = (r * mf) + (o.r * f);
g = (g * mf) + (o.g * f);
b = (b * mf) + (o.b * f);
}
void To_u8(uint8_t &rr, uint8_t &gg, uint8_t &bb, float m) const {
rr = _FloatToU8(r, m);
gg = _FloatToU8(g, m);
bb = _FloatToU8(b, m);
}
void To(Color &col) const {
col.r = r;
col.g = g;
col.b = b;
col.a = 1.0f;
}
private:
uint8_t _FloatToU8(float f, float m) const {
f *= m;
f *= 255.0f;
int i = f;
i = CLAMP(i, 0, 255);
return i;
}
};
struct FRay {
Ray ray;
// the color of the ray at the moment
FColor color;
// the color of the ray that is reflected after hitting a surface
FColor bounce_color;
int num_rays_left;
// hit texel
FHit hit;
};
class Line2 {
public:
Vector2 a;
Vector2 b;
float Distance(const Vector2 &pt) const {
Vector2 dir = b - a;
Vector2 norm = Vector2(dir.y, -dir.x);
norm.normalize();
Vector2 test_vec = pt - a;
return test_vec.dot(norm);
}
};
class UVTri {
public:
Vector2 uv[3];
void FlipWinding() {
Vector2 temp = uv[0];
uv[0] = uv[2];
uv[2] = temp;
}
void FindUVBarycentric(Vector2 &res, const Vector3 &bary) const { FindUVBarycentric(res, bary.x, bary.y, bary.z); }
void FindUVBarycentric(Vector2 &res, float u, float v, float w) const;
bool ContainsPoint(const Vector2 &pt, float epsilon = 0.0f) const;
bool ContainsTexel(int tx, int ty, int width, int height) const;
void FindBarycentricCoords(const Vector2 &pt, float &u, float &v, float &w) const;
void FindBarycentricCoords(const Vector2 &pt, Vector3 &bary) const { FindBarycentricCoords(pt, bary.x, bary.y, bary.z); }
bool IsWindingCW() const { return CalculateTwiceArea() < 0.0f; }
float CalculateTwiceArea() const {
const Vector2 &a = uv[0];
const Vector2 &b = uv[1];
const Vector2 &c = uv[2];
return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y);
}
};
bool BarycentricInside(float u, float v, float w) {
if ((u < 0.0f) || (u > 1.0f) ||
(v < 0.0f) || (v > 1.0f) ||
(w < 0.0f) || (w > 1.0f))
return false;
return true;
}
bool BarycentricInside(const Vector3 &bary) {
return BarycentricInside(bary.x, bary.y, bary.z);
}
bool IsMeshInstanceSuitable(const MeshInstance &mi) {
// must be set to bake in lightmap
if (!mi.get_flag(GeometryInstance::FLAG_USE_BAKED_LIGHT)) {
print_line("\tunsuitable : " + mi.get_name() + " (use_baked_light unset)");
return false;
}
Ref<Mesh> rmesh = mi.get_mesh();
if (rmesh->get_surface_count() == 0) {
print_line("\tunsuitable : " + mi.get_name() + " (no surfaces)");
return false;
}
Array arrays = rmesh->surface_get_arrays(0);
if (!arrays.size()) {
print_line("\tunsuitable : " + mi.get_name() + " (no arrays)");
return false;
}
PoolVector<Vector3> verts = arrays[VS::ARRAY_VERTEX];
if (!verts.size()) {
print_line("\tunsuitable : " + mi.get_name() + " (no positions)");
return false;
}
PoolVector<Vector3> norms = arrays[VS::ARRAY_NORMAL];
if (!norms.size()) {
print_line("\tunsuitable : " + mi.get_name() + " (no normals)");
return false;
}
// PoolVector<int> indices = arrays[VS::ARRAY_INDEX];
// if (!indices.size())
// return false;
PoolVector<Vector2> uv1s = arrays[VS::ARRAY_TEX_UV];
PoolVector<Vector2> uv2s = arrays[VS::ARRAY_TEX_UV2];
bool uv1 = uv1s.size() != 0;
bool uv2 = uv2s.size() != 0;
if (!uv1 && !uv2) {
print_line("\tunsuitable : " + mi.get_name() + " (no uvs at all)");
return false;
}
return true;
}
float Triangle_CalculateTwiceAreaSquared(const Vector3 &a, const Vector3 &b, const Vector3 &c) {
// compute the area squared. Which is the result of the cross product of two edges. If it's near zero, bingo
Vector3 edge1 = b - a;
Vector3 edge2 = c - a;
Vector3 vec = edge1.cross(edge2);
return vec.length_squared();
}
bool Triangle_IsDegenerate(const Vector3 &a, const Vector3 &b, const Vector3 &c, float epsilon) {
// not interested in the actual area, but numerical stability
Vector3 edge1 = b - a;
Vector3 edge2 = c - a;
edge1 *= 1024.0f;
edge2 *= 1024.0f;
Vector3 vec = edge1.cross(edge2);
float sl = vec.length_squared();
if (sl <= epsilon)
return true;
return false;
}
// check for invalid tris, or make a list of the valid triangles, depending on whether pOutputInds is set
bool CheckForValidIndices(const PoolVector<int> &inds, const PoolVector<Vector3> &verts, LVector<int> *pOutputInds) {
int nTris = inds.size();
nTris /= 3;
int indCount = 0;
for (int t = 0; t < nTris; t++) {
int i0 = inds[indCount++];
int i1 = inds[indCount++];
int i2 = inds[indCount++];
bool ok = true;
// if the indices are the same, the triangle is invalid
if (i0 == i1) ok = false;
if (i1 == i2) ok = false;
if (i0 == i2) ok = false;
// check positions
if (ok) {
// vertex positions
const Vector3 &p0 = verts[i0];
const Vector3 &p1 = verts[i1];
const Vector3 &p2 = verts[i2];
// if the area is zero, the triangle is invalid (will crash xatlas)
if (Triangle_IsDegenerate(p0, p1, p2, 0.00001f)) {
print_line("\t\tdetected zero area triangle, ignoring");
ok = false;
}
}
if (ok) {
// if the triangle is ok, we will output it if we are outputting
if (pOutputInds) {
pOutputInds->push_back(i0);
pOutputInds->push_back(i1);
pOutputInds->push_back(i2);
}
} else {
// if triangle not ok, return failed check if we are not outputting
if (!pOutputInds)
return false;
}
}
return true;
}
bool EnsureIndicesValid(PoolVector<int> &indices, const PoolVector<Vector3> &verts) {
// no indices? create some
if (!indices.size()) {
// indices are blank!! let's create some, assuming the mesh is using triangles
indices.resize(verts.size());
PoolVector<int>::Write write = indices.write();
int *pi = write.ptr();
// this is assuming each triangle vertex is unique
for (int n = 0; n < verts.size(); n++) {
*pi = n;
pi++;
}
}
if (!CheckForValidIndices(indices, verts, nullptr)) {
LVector<int> new_inds;
CheckForValidIndices(indices, verts, &new_inds);
// copy the new indices
indices.resize(new_inds.size());
PoolVector<int>::Write write = indices.write();
int *pi = write.ptr();
for (int n = 0; n < new_inds.size(); n++) {
pi[n] = new_inds[n];
}
return false;
}
return true;
}
// somewhat more complicated test to try and get all the tris that hit any part of the texel.
// really this should be a box / tri test.
inline bool UVTri::ContainsTexel(int tx, int ty, int width, int height) const {
float unit_x = 1.0f / width;
float unit_y = 1.0f / height;
float fx = tx * unit_x;
float fy = ty * unit_y;
float half_x = unit_x * 0.5f;
float half_y = unit_y * 0.5f;
// centre first as most likely
if (ContainsPoint(Vector2(fx + half_x, fy + half_y)))
return true;
// 4 corners
if (ContainsPoint(Vector2(fx, fy)))
return true;
if (ContainsPoint(Vector2(fx + unit_x, fy + unit_y)))
return true;
if (ContainsPoint(Vector2(fx + unit_x, fy)))
return true;
if (ContainsPoint(Vector2(fx, fy + unit_y)))
return true;
return false;
}
inline bool UVTri::ContainsPoint(const Vector2 &pt, float epsilon) const {
const Vector2 &a = uv[0];
const Vector2 &b = uv[1];
const Vector2 &c = uv[2];
// if the point is in front of any of the edges, it is outside the tri,
// if not, it must be inside
// N.B. the triangle must be ordered clockwise for this to work
Line2 edge;
edge.a = b;
edge.b = a;
if (edge.Distance(pt) < epsilon)
return false;
edge.a = c;
edge.b = b;
if (edge.Distance(pt) < epsilon)
return false;
edge.a = a;
edge.b = c;
if (edge.Distance(pt) < epsilon)
return false;
return true;
}
inline void UVTri::FindBarycentricCoords(const Vector2 &pt, float &u, float &v, float &w) const {
const Vector2 &a = uv[0];
const Vector2 &b = uv[1];
const Vector2 &c = uv[2];
Vector2 v0 = b - a, v1 = c - a, v2 = pt - a;
float d00 = v0.dot(v0);
float d01 = v0.dot(v1);
float d11 = v1.dot(v1);
float d20 = v2.dot(v0);
float d21 = v2.dot(v1);
// degenerate tris will produce division by zero
float denom = d00 * d11 - d01 * d01;
if (denom == 0.0f) {
// panic mode, return something reasonable.
// THIS DOES HAPPEN
u = 0.0f;
v = 0.0f;
w = 0.0f;
return;
}
float invDenom = 1.0f / denom;
v = (d11 * d20 - d01 * d21) * invDenom;
w = (d00 * d21 - d01 * d20) * invDenom;
u = 1.0f - v - w;
}
inline void UVTri::FindUVBarycentric(Vector2 &res, float u, float v, float w) const {
res = (uv[0] * u) + (uv[1] * v) + (uv[2] * w);
}
//////////////////////////////////////////////////////////
inline void Tri::FindBarycentric(const Vector3 &pt, float &u, float &v, float &w) const {
const Vector3 &a = pos[0];
const Vector3 &b = pos[1];
const Vector3 &c = pos[2];
// we can alternatively precache
Vector3 v0 = b - a, v1 = c - a, v2 = pt - a;
float d00 = v0.dot(v0);
float d01 = v0.dot(v1);
float d11 = v1.dot(v1);
float d20 = v2.dot(v0);
float d21 = v2.dot(v1);
float invDenom = 1.0f / (d00 * d11 - d01 * d01);
v = (d11 * d20 - d01 * d21) * invDenom;
w = (d00 * d21 - d01 * d20) * invDenom;
u = 1.0f - v - w;
}
inline void Tri::InterpolateBarycentric(Vector3 &pt, float u, float v, float w) const {
const Vector3 &a = pos[0];
const Vector3 &b = pos[1];
const Vector3 &c = pos[2];
pt.x = (a.x * u) + (b.x * v) + (c.x * w);
pt.y = (a.y * u) + (b.y * v) + (c.y * w);
pt.z = (a.z * u) + (b.z * v) + (c.z * w);
}
// returns barycentric, assumes we have already tested and got a collision
inline void Ray::FindIntersect(const Tri &tri, float t, float &u, float &v, float &w) const {
Vector3 pt = o + (d * t);
tri.FindBarycentric(pt, u, v, w);
}
// returns false if doesn't cut x.
// the relevant axis should be filled on entry (0 is x, 1 is y, 2 is z) with the axis aligned plane constant.
bool Ray::IntersectAAPlane(int axis, Vector3 &pt, float epsilon) const {
// x(t) = o.x + (d.x * t)
// y(t) = o.y + (d.y * t)
// z(t) = o.z + (d.z * t)
// solve for x
// find t
// d.x * t = x(t) - o.x
// t = (x(t) - o.x) / d.x
// just aliases
// const Vector3 &o = ray.o;
// const Vector3 &d = ray.d;
switch (axis) {
case 0: {
if (fabsf(d.x) < epsilon)
return false;
float t = pt.x - o.x;
t /= d.x;
pt.y = o.y + (d.y * t);
pt.z = o.z + (d.z * t);
} break;
case 1: {
if (fabsf(d.y) < epsilon)
return false;
float t = pt.y - o.y;
t /= d.y;
pt.x = o.x + (d.x * t);
pt.z = o.z + (d.z * t);
} break;
case 2: {
if (fabsf(d.z) < epsilon)
return false;
float t = pt.z - o.z;
t /= d.z;
// now we have t, we can calculate y and z
pt.x = o.x + (d.x * t);
pt.y = o.y + (d.y * t);
} break;
default:
break;
}
return true;
}
// same as below but with edges pre calced
inline bool Ray::TestIntersect_EdgeForm(const Tri &tri_edge, float &t) const {
//Edge1, Edge2
const Vector3 &e1 = tri_edge.pos[0];
const Vector3 &e2 = tri_edge.pos[1];
const Vector3 &a = tri_edge.pos[2];
const float cfEpsilon = 0.000001f;
Vector3 P, Q, T;
float det, inv_det, u, v;
//Begin calculating determinant - also used to calculate u parameter
P = d.cross(e2);
//if determinant is near zero, ray lies in plane of triangle
det = e1.dot(P);
//NOT CULLING
if (det > -cfEpsilon && det < cfEpsilon) return false; //IR_NONE;
inv_det = 1.f / det;
//calculate distance from V1 to ray origin
T = o - a;
//Calculate u parameter and test bound
u = T.dot(P) * inv_det;
//The intersection lies outside of the triangle
if (u < 0.f || u > 1.f) return false; // IR_NONE;
//Prepare to test v parameter
Q = T.cross(e1);
//Calculate V parameter and test bound
v = d.dot(Q) * inv_det;
//The intersection lies outside of the triangle
if (v < 0.f || u + v > 1.f) return false; //IR_NONE;
t = e2.dot(Q) * inv_det;
if (t > cfEpsilon) { //ray intersection
// *out = t;
return true; //IR_HIT;
}
// No hit, no win
return false; //IR_BEHIND;
}
// moller and trumbore test (actually calculated uv and t but we are not returning uv)
inline bool Ray::TestIntersect(const Tri &tri, float &t) const {
const Vector3 &a = tri.pos[0];
const Vector3 &b = tri.pos[1];
const Vector3 &c = tri.pos[2];
const float cfEpsilon = 0.000001f;
//#define EPSILON 0.000001
Vector3 e1, e2; //Edge1, Edge2
Vector3 P, Q, T;
float det, inv_det, u, v;
// float t;
//Find vectors for two edges sharing V1
e1 = b - a;
e2 = c - a;
//Begin calculating determinant - also used to calculate u parameter
P = d.cross(e2);
//if determinant is near zero, ray lies in plane of triangle
det = e1.dot(P);
//NOT CULLING
if (det > -cfEpsilon && det < cfEpsilon) return false; //IR_NONE;
inv_det = 1.f / det;
//calculate distance from V1 to ray origin
T = o - a;
//Calculate u parameter and test bound
u = T.dot(P) * inv_det;
//The intersection lies outside of the triangle
if (u < 0.f || u > 1.f) return false; // IR_NONE;
//Prepare to test v parameter
Q = T.cross(e1);
//Calculate V parameter and test bound
v = d.dot(Q) * inv_det;
//The intersection lies outside of the triangle
if (v < 0.f || u + v > 1.f) return false; //IR_NONE;
t = e2.dot(Q) * inv_det;
if (t > cfEpsilon) { //ray intersection
// *out = t;
return true; //IR_HIT;
}
// No hit, no win
return false; //IR_BEHIND;
}
} // namespace LM