-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathShader.hpp
More file actions
573 lines (470 loc) · 16.7 KB
/
Copy pathShader.hpp
File metadata and controls
573 lines (470 loc) · 16.7 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
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
// Shader.hpp
#ifndef SHADER_HPP
#define SHADER_HPP
#include <cstdint>
#include <vector>
#include <cmath>
#include "FastMath.hpp"
#include "TrigLUT.hpp"
using namespace Renderer;
// Forward declarations for types used in shader parameters
struct Vector3
{
int32_t x, y, z;
// Empty constructor with 0 values
Vector3() : x(0), y(0), z(0) {}
Vector3(int32_t x, int32_t y, int32_t z) : x(x), y(y), z(z) {}
Vector3 add(const Vector3 &other) const
{
return Vector3(x + other.x, y + other.y, z + other.z);
}
void add(const Vector3 &other)
{
x += other.x;
y += other.y;
z += other.z;
}
Vector3 add(int32_t x, int32_t y, int32_t z) const
{
return Vector3(this->x + x, this->y + y, this->z + z);
}
void add(int32_t x, int32_t y, int32_t z)
{
this->x += x;
this->y += y;
this->z += z;
}
Vector3 multiply(const Vector3 &other) const
{
return Vector3(x * other.x, y * other.y, z * other.z);
}
Vector3 multiply(int32_t scalar) const
{
return Vector3(x * scalar, y * scalar, z * scalar);
}
Vector3 divide(int32_t scalar) const
{
return Vector3(x / scalar, y / scalar, z / scalar);
}
Vector3 divide(const Vector3 &other) const
{
return Vector3(x / other.x, y / other.y, z / other.z);
}
Vector3 subtract(const Vector3 &other) const
{
return Vector3(x - other.x, y - other.y, z - other.z);
}
Vector3 cross(const Vector3 &other) const
{
return Vector3(y * other.z - z * other.y,
z * other.x - x * other.z,
x * other.y - y * other.x);
}
// static cross product
static Vector3 cross(const Vector3 &a, const Vector3 &b)
{
return Vector3(a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x);
}
static int64_t dotProduct(const Vector3 &a, const Vector3 &b)
{
return FastMath::dotProduct(a.x, a.y, a.z, b.x, b.y, b.z);
}
Vector3 inverse() const
{
return Vector3(-x, -y, -z);
}
int32_t length() const
{
return static_cast<int32_t>(FastMath::approximateSqrt(x * x + y * y + z * z));
}
void assign(int32_t x, int32_t y, int32_t z)
{
this->x = x;
this->y = y;
this->z = z;
}
void assign(const Vector3 &other)
{
this->x = other.x;
this->y = other.y;
this->z = other.z;
}
Vector3 operator+(const Vector3 &other) const
{
return add(other);
}
Vector3 operator*(const Vector3 &other) const
{
return multiply(other);
}
Vector3 operator*(int32_t scalar) const
{
return multiply(scalar);
}
Vector3 operator-(const Vector3 &other) const
{
return subtract(other);
}
Vector3 operator/(int32_t scalar) const
{
return divide(scalar);
}
Vector3 operator/(const Vector3 &other) const
{
return divide(other);
}
Vector3 operator%(int32_t scalar) const
{
return Vector3(x % scalar, y % scalar, z % scalar);
}
};
struct Vector3_f
{
float x, y, z;
Vector3_f() : x(0.0f), y(0.0f), z(0.0f) {}
Vector3_f(float x, float y, float z) : x(x), y(y), z(z) {}
Vector3_f(const Vector3_f &other) : x(other.x), y(other.y), z(other.z) {}
Vector3_f(const Vector3 &other) : x(static_cast<float>(other.x)), y(static_cast<float>(other.y)), z(static_cast<float>(other.z)) {}
Vector3_f add(const Vector3_f &other) const { return Vector3_f(x + other.x, y + other.y, z + other.z); }
Vector3_f add(const Vector3 &other) const { return Vector3_f(x + other.x, y + other.y, z + other.z); }
Vector3_f add(float x, float y, float z) const { return Vector3_f(this->x + x, this->y + y, this->z + z); }
Vector3_f multiply(const Vector3_f &other) const { return Vector3_f(x * other.x, y * other.y, z * other.z); }
Vector3_f multiply(const Vector3 &other) const { return Vector3_f(x * other.x, y * other.y, z * other.z); }
Vector3_f multiply(float scalar) const { return Vector3_f(x * scalar, y * scalar, z * scalar); }
Vector3_f divide(float scalar) const { return Vector3_f(x / scalar, y / scalar, z / scalar); }
Vector3_f divide(const Vector3_f &other) const { return Vector3_f(x / other.x, y / other.y, z / other.z); }
Vector3_f divide(const Vector3 &other) const { return Vector3_f(x / other.x, y / other.y, z / other.z); }
Vector3_f subtract(const Vector3_f &other) const { return Vector3_f(x - other.x, y - other.y, z - other.z); }
Vector3_f cross(const Vector3_f &other) const { return Vector3_f(y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x); }
static Vector3_f cross(const Vector3_f &a, const Vector3_f &b) { return Vector3_f(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); }
static float dotProduct(const Vector3_f &a, const Vector3_f &b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
Vector3_f inverse() const { return Vector3_f(-x, -y, -z); }
float length() const { return std::sqrt(x * x + y * y + z * z); }
void assign(float x, float y, float z) { this->x = x; this->y = y; this->z = z; }
void assign(const Vector3_f &other) { x = other.x; y = other.y; z = other.z; }
Vector3_f operator+(const Vector3_f &other) const { return add(other); }
Vector3_f operator+(const Vector3 &other) const { return add(other); }
Vector3_f operator*(const Vector3_f &other) const { return multiply(other); }
Vector3_f operator*(const Vector3 &other) const { return multiply(other); }
Vector3_f operator*(float scalar) const { return multiply(scalar); }
Vector3_f operator-(const Vector3_f &other) const { return subtract(other); }
Vector3_f operator/(float scalar) const { return divide(scalar); }
Vector3_f operator/(const Vector3_f &other) const { return divide(other); }
Vector3_f operator/(const Vector3 &other) const { return divide(other); }
};
struct Vector3_64
{
int64_t x, y, z;
Vector3_64(int64_t x, int64_t y, int64_t z) : x(x), y(y), z(z) {}
Vector3_64 add(const Vector3_64 &other) const
{
return Vector3_64(x + other.x, y + other.y, z + other.z);
}
Vector3_64 add(int64_t x, int64_t y, int64_t z) const
{
return Vector3_64(this->x + x, this->y + y, this->z + z);
}
Vector3_64 multiply(const Vector3_64 &other) const
{
return Vector3_64(x * other.x, y * other.y, z * other.z);
}
Vector3_64 multiply(int64_t scalar) const
{
return Vector3_64(x * scalar, y * scalar, z * scalar);
}
void assign(int64_t x, int64_t y, int64_t z)
{
this->x = x;
this->y = y;
this->z = z;
}
void assign(const Vector3_64 &other)
{
this->x = other.x;
this->y = other.y;
this->z = other.z;
}
int64_t length() const
{
return static_cast<int64_t>(FastMath::approximateSqrt(x * x + y * y + z * z));
}
Vector3_64 cross(const Vector3_64 &other) const
{
return Vector3_64(y * other.z - z * other.y,
z * other.x - x * other.z,
x * other.y - y * other.x);
}
// static cross product
static Vector3_64 cross(const Vector3_64 &a, const Vector3_64 &b)
{
return Vector3_64(a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x);
}
Vector3_64 operator+(const Vector3_64 &other) const
{
return add(other);
}
Vector3_64 operator*(const Vector3_64 &other) const
{
return multiply(other);
}
Vector3_64 operator*(int64_t scalar) const
{
return multiply(scalar);
}
// Interoperability with Vector3
Vector3_64(const Vector3 &other) : x(other.x), y(other.y), z(other.z) {}
Vector3_64 add(const Vector3 &other) const
{
return Vector3_64(x + other.x, y + other.y, z + other.z);
}
Vector3_64 multiply(const Vector3 &other) const
{
return Vector3_64(x * other.x, y * other.y, z * other.z);
}
Vector3_64 multiply(int32_t scalar) const
{
return Vector3_64(x * scalar, y * scalar, z * scalar);
}
Vector3_64 divide(int32_t scalar) const
{
return Vector3_64(x / scalar, y / scalar, z / scalar);
}
Vector3_64 divide(const Vector3 &other) const
{
return Vector3_64(x / other.x, y / other.y, z / other.z);
}
Vector3_64 operator+(const Vector3 &other) const
{
return add(other);
}
Vector3_64 operator*(const Vector3 &other) const
{
return multiply(other);
}
Vector3_64 operator*(int32_t scalar) const
{
return multiply(scalar);
}
Vector3_64 operator/(int32_t scalar) const
{
return divide(scalar);
}
Vector3_64 operator/(const Vector3 &other) const
{
return divide(other);
}
static int64_t dotProduct(const Vector3_64 &a, const Vector3_64 &b)
{
return FastMath::dotProduct(a.x, a.y, a.z, b.x, b.y, b.z);
}
};
struct Vector2
{
int32_t x, y;
Vector2(int32_t x, int32_t y) : x(x), y(y) {}
Vector2() : x(0), y(0) {}
Vector2 add(const Vector2 &other) const
{
return Vector2(x + other.x, y + other.y);
}
Vector2 multiply(const Vector2 &other) const
{
return Vector2(x * other.x, y * other.y);
}
Vector2 multiply(int32_t scalar) const
{
return Vector2(x * scalar, y * scalar);
}
void assign(int32_t x, int32_t y)
{
this->x = x;
this->y = y;
}
};
struct Vector2_f {
float x, y;
Vector2_f(float x, float y) : x(x), y(y) {}
Vector2_f() : x(0.0f), y(0.0f) {}
Vector2_f add(const Vector2_f &other) const { return Vector2_f(x + other.x, y + other.y); }
Vector2_f multiply(const Vector2_f &other) const { return Vector2_f(x * other.x, y * other.y); }
Vector2_f multiply(float scalar) const { return Vector2_f(x * scalar, y * scalar); }
void assign(float x, float y) { this->x = x; this->y = y; }
};
struct Quaternion
{
int32_t w, x, y, z; // Fixed-point components
Quaternion() : w(FIXED_POINT_SCALE), x(0), y(0), z(0) {}
Quaternion(int32_t w, int32_t x, int32_t y, int32_t z) : w(w), x(x), y(y), z(z) {}
// Normalize the quaternion
void normalize()
{
int64_t magnitude = FastMath::approximateSqrt(w * w + x * x + y * y + z * z);
w = (w * FIXED_POINT_SCALE) / magnitude;
x = (x * FIXED_POINT_SCALE) / magnitude;
y = (y * FIXED_POINT_SCALE) / magnitude;
z = (z * FIXED_POINT_SCALE) / magnitude;
}
// Multiply two quaternions
Quaternion multiply(const Quaternion &other) const
{
Quaternion result;
result.w = ((w * other.w - x * other.x - y * other.y - z * other.z) / FIXED_POINT_SCALE);
result.x = ((w * other.x + x * other.w + y * other.z - z * other.y) / FIXED_POINT_SCALE);
result.y = ((w * other.y - x * other.z + y * other.w + z * other.x) / FIXED_POINT_SCALE);
result.z = ((w * other.z + x * other.y - y * other.x + z * other.w) / FIXED_POINT_SCALE);
return result;
}
// Rotate a vector by this quaternion
Vector3 rotate(const Vector3 &v) const
{
Quaternion p(0, v.x, v.y, v.z);
Quaternion q = multiply(p).multiply(conjugate());
return Vector3(q.x, q.y, q.z);
}
// Conjugate the quaternion
Quaternion conjugate() const
{
return Quaternion(w, -x, -y, -z);
}
static Quaternion fromEuler(const Vector3 &angles)
{
return fromEuler(angles.x, angles.y, angles.z);
}
// Convert from Euler angles (in degrees)
static Quaternion fromEuler(int32_t pitch, int32_t yaw, int32_t roll)
{
pitch %= 360;
yaw %= 360;
roll %= 360;
if (pitch < 0)
pitch += 360;
if (yaw < 0)
yaw += 360;
if (roll < 0)
roll += 360;
int32_t cy = lookupCosI(yaw / 2);
int32_t sy = lookupSinI(yaw / 2);
int32_t cp = lookupCosI(pitch / 2);
int32_t sp = lookupSinI(pitch / 2);
int32_t cr = lookupCosI(roll / 2);
int32_t sr = lookupSinI(roll / 2);
Quaternion q;
q.w = ((cy * cp * cr + sy * sp * sr) / FIXED_POINT_SCALE);
q.x = ((cy * cp * sr - sy * sp * cr) / FIXED_POINT_SCALE);
q.y = ((sy * cp * sr + cy * sp * cr) / FIXED_POINT_SCALE);
q.z = ((sy * cp * cr - cy * sp * sr) / FIXED_POINT_SCALE);
return q;
}
// Convert to Euler angles (in degrees)
void toEuler(int32_t &pitch, int32_t &yaw, int32_t &roll) const
{
int32_t sinr_cosp = 2 * (w * x + y * z) / FIXED_POINT_SCALE;
int32_t cosr_cosp = FIXED_POINT_SCALE - 2 * (x * x + y * y);
roll = static_cast<int32_t>(std::atan2(sinr_cosp, cosr_cosp) * 180.0 / M_PI);
int32_t sinp = 2 * (w * y - z * x) / FIXED_POINT_SCALE;
if (std::abs(sinp) >= FIXED_POINT_SCALE)
pitch = (sinp > 0) ? 90 : -90;
else
pitch = static_cast<int32_t>(std::asin(sinp) * 180.0 / M_PI);
int32_t siny_cosp = 2 * (w * z + x * y) / FIXED_POINT_SCALE;
int32_t cosy_cosp = FIXED_POINT_SCALE - 2 * (y * y + z * z);
yaw = static_cast<int32_t>(std::atan2(siny_cosp, cosy_cosp) * 180.0 / M_PI);
}
void toEuler(Vector3 &angles) const
{
toEuler(angles.x, angles.y, angles.z);
}
};
struct Quaternion_f {
float w, x, y, z;
Quaternion_f() : w(1.0f), x(0.0f), y(0.0f), z(0.0f) {}
Quaternion_f(float w, float x, float y, float z) : w(w), x(x), y(y), z(z) {}
void normalize() {
float magnitude = std::sqrt(w * w + x * x + y * y + z * z);
w /= magnitude;
x /= magnitude;
y /= magnitude;
z /= magnitude;
}
Quaternion_f multiply(const Quaternion_f &other) const {
return Quaternion_f(
w * other.w - x * other.x - y * other.y - z * other.z,
w * other.x + x * other.w + y * other.z - z * other.y,
w * other.y - x * other.z + y * other.w + z * other.x,
w * other.z + x * other.y - y * other.x + z * other.w
);
}
Vector3_f rotate(const Vector3_f &v) const {
Quaternion_f p(0, v.x, v.y, v.z);
Quaternion_f q = multiply(p).multiply(conjugate());
return Vector3_f(q.x, q.y, q.z);
}
Quaternion_f conjugate() const {
return Quaternion_f(w, -x, -y, -z);
}
static Quaternion_f fromEuler(const Vector3_f &angles) {
return fromEuler(angles.x, angles.y, angles.z);
}
static Quaternion_f fromEuler(float pitch, float yaw, float roll) {
pitch = fmodf(pitch, 360.0f);
yaw = fmodf(yaw, 360.0f);
roll = fmodf(roll, 360.0f);
float cy = cosf(yaw * 0.5f * M_PI / 180.0f);
float sy = sinf(yaw * 0.5f * M_PI / 180.0f);
float cp = cosf(pitch * 0.5f * M_PI / 180.0f);
float sp = sinf(pitch * 0.5f * M_PI / 180.0f);
float cr = cosf(roll * 0.5f * M_PI / 180.0f);
float sr = sinf(roll * 0.5f * M_PI / 180.0f);
return Quaternion_f(
cy * cp * cr + sy * sp * sr,
cy * cp * sr - sy * sp * cr,
sy * cp * sr + cy * sp * cr,
sy * cp * cr - cy * sp * sr
);
}
void toEuler(float &pitch, float &yaw, float &roll) const {
float sinr_cosp = 2.0f * (w * x + y * z);
float cosr_cosp = 1.0f - 2.0f * (x * x + y * y);
roll = atan2f(sinr_cosp, cosr_cosp) * 180.0f / M_PI;
float sinp = 2.0f * (w * y - z * x);
pitch = std::abs(sinp) >= 1.0f ?
copysignf(90.0f, sinp) :
asinf(sinp) * 180.0f / M_PI;
float siny_cosp = 2.0f * (w * z + x * y);
float cosy_cosp = 1.0f - 2.0f * (y * y + z * z);
yaw = atan2f(siny_cosp, cosy_cosp) * 180.0f / M_PI;
}
void toEuler(Vector3_f &angles) const {
toEuler(angles.x, angles.y, angles.z);
}
};
struct ShaderParameters
{
Vector2 screenPos;
int32_t zBuffer;
Vector3 localCoords;
Vector3 worldCoords;
uint32_t frameNumber;
};
class Shader
{
public:
virtual ~Shader() {}
bool screenSpace;
bool hasAlpha; // Indicates if an alpha color is used
uint16_t alphaColor; // The color to treat as transparent
/**
* @brief Computes the color of a pixel based on various parameters.
*
* @param u Texture coordinate U, 0-1023.
* @param v Texture coordinate V, 0-1023.
* @param params Additional parameters for shading.
* @return uint16_t The resulting color in RGB565 format.
*/
virtual uint16_t getPixel(uint32_t u, uint32_t v, ShaderParameters *params = nullptr) = 0;
};
#endif // SHADER_HPP