-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathRenderHelp.h
1489 lines (1277 loc) · 46.8 KB
/
RenderHelp.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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//=====================================================================
//
// RenderHelp.h - 可编程渲染管线实现,渲染器教学,着色程序学习
//
// By skywind3000 (at) gmail.com, 2020/08/08
//
// Features:
//
// - 单个头文件的渲染器实现,没有任何依赖
// - 模型标准,计算精确,类 Direct3D 接口
// - 包含一套精简何理的矢量/矩阵库
// - 包含一套位图 Bitmap 库,方便画点/画线,加载纹理,保存渲染结果
// - 支持二次线性插值纹理采样器
// - 支持深度缓存
// - 支持多种数据类型的 varying
// - 支持顶点着色器 (Vertex Shader) 和像素着色器 (Pixel Shader)
// - 支持加载 24 位和 32 位的 bmp 图片纹理
//
//=====================================================================
#ifndef _RENDER_HELP_H_
#define _RENDER_HELP_H_
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <vector>
#include <map>
#include <initializer_list>
#include <stdexcept>
#include <functional>
#include <ostream>
#include <sstream>
#include <iostream>
//---------------------------------------------------------------------
// 数学库:矢量定义
//---------------------------------------------------------------------
// 通用矢量:N 是矢量维度,T 为数据类型
template <size_t N, typename T> struct Vector {
T m[N]; // 元素数组
inline Vector() { for (size_t i = 0; i < N; i++) m[i] = T(); }
inline Vector(const T *ptr) { for (size_t i = 0; i < N; i++) m[i] = ptr[i]; }
inline Vector(const Vector<N, T> &u) { for (size_t i = 0; i < N; i++) m[i] = u.m[i]; }
inline Vector(const std::initializer_list<T> &u) {
auto it = u.begin(); for (size_t i = 0; i < N; i++) m[i] = *it++; }
inline const T& operator[] (size_t i) const { assert(i < N); return m[i]; }
inline T& operator[] (size_t i) { assert(i < N); return m[i]; }
inline void load(const T *ptr) { for (size_t i = 0; i < N; i++) m[i] = ptr[i]; }
inline void save(T *ptr) { for (size_t i = 0; i < N; i++) ptr[i] = m[i]; }
};
// 特化二维矢量
template <typename T> struct Vector<2, T> {
union {
struct { T x, y; }; // 元素别名
struct { T u, v; }; // 元素别名
T m[2]; // 元素数组
};
inline Vector(): x(T()), y(T()) {}
inline Vector(T X, T Y): x(X), y(Y) {}
inline Vector(const Vector<2, T> &u): x(u.x), y(u.y) {}
inline Vector(const T *ptr): x(ptr[0]), y(ptr[1]) {}
inline const T& operator[] (size_t i) const { assert(i < 2); return m[i]; }
inline T& operator[] (size_t i) { assert(i < 2); return m[i]; }
inline void load(const T *ptr) { for (size_t i = 0; i < 2; i++) m[i] = ptr[i]; }
inline void save(T *ptr) { for (size_t i = 0; i < 2; i++) ptr[i] = m[i]; }
inline Vector<2, T> xy() const { return *this; }
inline Vector<3, T> xy1() const { return Vector<3, T>(x, y, 1); }
inline Vector<4, T> xy11() const { return Vector<4, T>(x, y, 1, 1); }
};
// 特化三维矢量
template <typename T> struct Vector<3, T> {
union {
struct { T x, y, z; }; // 元素别名
struct { T r, g, b; }; // 元素别名
T m[3]; // 元素数组
};
inline Vector(): x(T()), y(T()), z(T()) {}
inline Vector(T X, T Y, T Z): x(X), y(Y), z(Z) {}
inline Vector(const Vector<3, T> &u): x(u.x), y(u.y), z(u.z) {}
inline Vector(const T *ptr): x(ptr[0]), y(ptr[1]), z(ptr[2]) {}
inline const T& operator[] (size_t i) const { assert(i < 3); return m[i]; }
inline T& operator[] (size_t i) { assert(i < 3); return m[i]; }
inline void load(const T *ptr) { for (size_t i = 0; i < 3; i++) m[i] = ptr[i]; }
inline void save(T *ptr) { for (size_t i = 0; i < 3; i++) ptr[i] = m[i]; }
inline Vector<2, T> xy() const { return Vector<2, T>(x, y); }
inline Vector<3, T> xyz() const { return *this; }
inline Vector<4, T> xyz1() const { return Vector<4, T>(x, y, z, 1); }
};
// 特化四维矢量
template <typename T> struct Vector<4, T> {
union {
struct { T x, y, z, w; }; // 元素别名
struct { T r, g, b, a; }; // 元素别名
T m[4]; // 元素数组
};
inline Vector(): x(T()), y(T()), z(T()), w(T()) {}
inline Vector(T X, T Y, T Z, T W): x(X), y(Y), z(Z), w(W) {}
inline Vector(const Vector<4, T> &u): x(u.x), y(u.y), z(u.z), w(u.w) {}
inline Vector(const T *ptr): x(ptr[0]), y(ptr[1]), z(ptr[2]), w(ptr[3]) {}
inline const T& operator[] (size_t i) const { assert(i < 4); return m[i]; }
inline T& operator[] (size_t i) { assert(i < 4); return m[i]; }
inline void load(const T *ptr) { for (size_t i = 0; i < 4; i++) m[i] = ptr[i]; }
inline void save(T *ptr) { for (size_t i = 0; i < 4; i++) ptr[i] = m[i]; }
inline Vector<2, T> xy() const { return Vector<2, T>(x, y); }
inline Vector<3, T> xyz() const { return Vector<3, T>(x, y, z); }
inline Vector<4, T> xyzw() const { return *this; }
};
//---------------------------------------------------------------------
// 数学库:矢量运算
//---------------------------------------------------------------------
// = (+a)
template <size_t N, typename T>
inline Vector<N, T> operator + (const Vector<N, T>& a) {
return a;
}
// = (-a)
template <size_t N, typename T>
inline Vector<N, T> operator - (const Vector<N, T>& a) {
Vector<N, T> b;
for (size_t i = 0; i < N; i++) b[i] = -a[i];
return b;
}
// = (a == b) ? true : false
template <size_t N, typename T>
inline bool operator == (const Vector<N, T>& a, const Vector<N, T>& b) {
for (size_t i = 0; i < N; i++) if (a[i] != b[i]) return false;
return true;
}
// = (a != b)? true : false
template <size_t N, typename T>
inline bool operator != (const Vector<N, T>& a, const Vector<N, T>& b) {
return !(a == b);
}
// = a + b
template <size_t N, typename T>
inline Vector<N, T> operator + (const Vector<N, T>& a, const Vector<N, T>& b) {
Vector<N, T> c;
for (size_t i = 0; i < N; i++) c[i] = a[i] + b[i];
return c;
}
// = a - b
template <size_t N, typename T>
inline Vector<N, T> operator - (const Vector<N, T>& a, const Vector<N, T>& b) {
Vector<N, T> c;
for (size_t i = 0; i < N; i++) c[i] = a[i] - b[i];
return c;
}
// = a * b,不是点乘也不是叉乘,而是各个元素分别相乘,色彩计算时有用
template <size_t N, typename T>
inline Vector<N, T> operator * (const Vector<N, T>& a, const Vector<N, T>& b) {
Vector<N, T> c;
for (size_t i = 0; i < N; i++) c[i] = a[i] * b[i];
return c;
}
// = a / b,各个元素相除
template <size_t N, typename T>
inline Vector<N, T> operator / (const Vector<N, T>& a, const Vector<N, T>& b) {
Vector<N, T> c;
for (size_t i = 0; i < N; i++) c[i] = a[i] / b[i];
return c;
}
// = a * x
template <size_t N, typename T>
inline Vector<N, T> operator * (const Vector<N, T>& a, T x) {
Vector<N, T> b;
for (size_t i = 0; i < N; i++) b[i] = a[i] * x;
return b;
}
// = x * a
template <size_t N, typename T>
inline Vector<N, T> operator * (T x, const Vector<N, T>& a) {
Vector<N, T> b;
for (size_t i = 0; i < N; i++) b[i] = a[i] * x;
return b;
}
// = a / x
template <size_t N, typename T>
inline Vector<N, T> operator / (const Vector<N, T>& a, T x) {
Vector<N, T> b;
for (size_t i = 0; i < N; i++) b[i] = a[i] / x;
return b;
}
// = x / a
template <size_t N, typename T>
inline Vector<N, T> operator / (T x, const Vector<N, T>& a) {
Vector<N, T> b;
for (size_t i = 0; i < N; i++) b[i] = x / a[i];
return b;
}
// a += b
template <size_t N, typename T>
inline Vector<N, T>& operator += (Vector<N, T>& a, const Vector<N, T>& b) {
for (size_t i = 0; i < N; i++) a[i] += b[i];
return a;
}
// a -= b
template <size_t N, typename T>
inline Vector<N, T>& operator -= (Vector<N, T>& a, const Vector<N, T>& b) {
for (size_t i = 0; i < N; i++) a[i] -= b[i];
return a;
}
// a *= b
template <size_t N, typename T>
inline Vector<N, T>& operator *= (Vector<N, T>& a, const Vector<N, T>& b) {
for (size_t i = 0; i < N; i++) a[i] *= b[i];
return a;
}
// a /= b
template <size_t N, typename T>
inline Vector<N, T>& operator /= (Vector<N, T>& a, const Vector<N, T>& b) {
for (size_t i = 0; i < N; i++) a[i] /= b[i];
return a;
}
// a *= x
template <size_t N, typename T>
inline Vector<N, T>& operator *= (Vector<N, T>& a, T x) {
for (size_t i = 0; i < N; i++) a[i] *= x;
return a;
}
// a /= x
template <size_t N, typename T>
inline Vector<N, T>& operator /= (Vector<N, T>& a, T x) {
for (size_t i = 0; i < N; i++) a[i] /= x;
return a;
}
//---------------------------------------------------------------------
// 数学库:矢量函数
//---------------------------------------------------------------------
// 不同维度的矢量转换
template<size_t N1, size_t N2, typename T>
inline Vector<N1, T> vector_convert(const Vector<N2, T>& a, T fill = 1) {
Vector<N1, T> b;
for (size_t i = 0; i < N1; i++)
b[i] = (i < N2)? a[i] : fill;
return b;
}
// = |a| ^ 2
template<size_t N, typename T>
inline T vector_length_square(const Vector<N, T>& a) {
T sum = 0;
for (size_t i = 0; i < N; i++) sum += a[i] * a[i];
return sum;
}
// = |a|
template<size_t N, typename T>
inline T vector_length(const Vector<N, T>& a) {
return sqrt(vector_length_square(a));
}
// = |a| , 特化 float 类型,使用 sqrtf
template<size_t N>
inline float vector_length(const Vector<N, float>& a) {
return sqrtf(vector_length_square(a));
}
// = a / |a|
template<size_t N, typename T>
inline Vector<N, T> vector_normalize(const Vector<N, T>& a) {
return a / vector_length(a);
}
// 矢量点乘
template<size_t N, typename T>
inline T vector_dot(const Vector<N, T>& a, const Vector<N, T>& b) {
T sum = 0;
for (size_t i = 0; i < N; i++) sum += a[i] * b[i];
return sum;
}
// 二维矢量叉乘,得到标量
template<typename T>
inline T vector_cross(const Vector<2, T>& a, const Vector<2, T>& b) {
return a.x * b.y - a.y * b.x;
}
// 三维矢量叉乘,得到新矢量
template<typename T>
inline Vector<3, T> vector_cross(const Vector<3, T>& a, const Vector<3, T>& b) {
return Vector<3, T>(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
}
// 四维矢量叉乘:前三维叉乘,后一位保留
template<typename T>
inline Vector<4, T> vector_cross(const Vector<4, T>& a, const Vector<4, T>& b) {
return Vector<4, T>(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x, a.w);
}
// = a + (b - a) * t
template<size_t N, typename T>
inline Vector<N, T> vector_lerp(const Vector<N, T>& a, const Vector<N, T>& b, float t) {
return a + (b - a) * t;
}
// 各个元素取最大值
template<size_t N, typename T>
inline Vector<N, T> vector_max(const Vector<N, T>& a, const Vector<N, T>& b) {
Vector<N, T> c;
for (size_t i = 0; i < N; i++) c[i] = (a[i] > b[i])? a[i] : b[i];
return c;
}
// 各个元素取最小值
template<size_t N, typename T>
inline Vector<N, T> vector_min(const Vector<N, T>& a, const Vector<N, T>& b) {
Vector<N, T> c;
for (size_t i = 0; i < N; i++) c[i] = (a[i] < b[i])? a[i] : b[i];
return c;
}
// 将矢量的值控制在 minx/maxx 范围内
template<size_t N, typename T>
inline Vector<N, T> vector_between(const Vector<N, T>& minx, const Vector<N, T>& maxx, const Vector<N, T>& x) {
return vector_min(vector_max(minx, x), maxx);
}
// 判断矢量是否接近
template<size_t N, typename T>
inline bool vector_near(const Vector<N, T>& a, const Vector<N, T>& b, T dist) {
return (vector_length_square(a - b) <= dist);
}
// 判断两个单精度矢量是否近似
template<size_t N>
inline bool vector_near_equal(const Vector<N, float>& a, const Vector<N, float>& b, float e = 0.0001) {
return vector_near(a, b, e);
}
// 判断两个双精度矢量是否近似
template<size_t N>
inline bool vector_near_equal(const Vector<N, double>& a, const Vector<N, double>& b, double e = 0.0000001) {
return vector_near(a, b, e);
}
// 矢量值元素范围裁剪
template<size_t N, typename T>
inline Vector<N, T> vector_clamp(const Vector<N, T>& a, T minx = 0, T maxx = 1) {
Vector<N, T> b;
for (size_t i = 0; i < N; i++) {
T x = (a[i] < minx)? minx : a[i];
b[i] = (x > maxx)? maxx : x;
}
return b;
}
// 输出到文本流
template<size_t N, typename T>
inline std::ostream& operator << (std::ostream& os, const Vector<N, T>& a) {
os << "[";
for (size_t i = 0; i < N; i++) {
os << a[i];
if (i < N - 1) os << ", ";
}
os << "]";
return os;
}
// 输出成字符串
template<size_t N, typename T>
inline std::string vector_repr(const Vector<N, T>& a) {
std::stringstream ss;
ss << a;
return ss.str();
}
//---------------------------------------------------------------------
// 数学库:矩阵
//---------------------------------------------------------------------
template<size_t ROW, size_t COL, typename T> struct Matrix {
T m[ROW][COL];
inline Matrix() {}
inline Matrix(const Matrix<ROW, COL, T>& src) {
for (size_t r = 0; r < ROW; r++) {
for (size_t c = 0; c < COL; c++)
m[r][c] = src.m[r][c];
}
}
inline Matrix(const std::initializer_list<Vector<COL, T>> &u) {
auto it = u.begin();
for (size_t i = 0; i < ROW; i++) SetRow(i, *it++);
}
inline const T* operator [] (size_t row) const { assert(row < ROW); return m[row]; }
inline T* operator [] (size_t row) { assert(row < ROW); return m[row]; }
// 取一行
inline Vector<COL, T> Row(size_t row) const {
assert(row < ROW);
Vector<COL, T> a;
for (size_t i = 0; i < COL; i++) a[i] = m[row][i];
return a;
}
// 取一列
inline Vector<ROW, T> Col(size_t col) const {
assert(col < COL);
Vector<ROW, T> a;
for (size_t i = 0; i < ROW; i++) a[i] = m[i][col];
return a;
}
// 设置一行
inline void SetRow(size_t row, const Vector<COL, T>& a) {
assert(row < ROW);
for (size_t i = 0; i < COL; i++) m[row][i] = a[i];
}
// 设置一列
inline void SetCol(size_t col, const Vector<ROW, T>& a) {
assert(col < COL);
for (size_t i = 0; i < ROW; i++) m[i][col] = a[i];
}
// 取得删除某行和某列的子矩阵:子式
inline Matrix<ROW-1, COL-1, T> GetMinor(size_t row, size_t col) const {
Matrix<ROW-1, COL-1, T> ret;
for (size_t r = 0; r < ROW - 1; r++) {
for (size_t c = 0; c < COL - 1; c++) {
ret.m[r][c] = m[r < row? r : r + 1][c < col? c : c + 1];
}
}
return ret;
}
// 取得转置矩阵
inline Matrix<COL, ROW, T> Transpose() const {
Matrix<COL, ROW, T> ret;
for (size_t r = 0; r < ROW; r++) {
for (size_t c = 0; c < COL; c++)
ret.m[c][r] = m[r][c];
}
return ret;
}
// 取得 0 矩阵
inline static Matrix<ROW, COL, T> GetZero() {
Matrix<ROW, COL, T> ret;
for (size_t r = 0; r < ROW; r++) {
for (size_t c = 0; c < COL; c++)
ret.m[r][c] = 0;
}
return ret;
}
// 取得单位矩阵
inline static Matrix<ROW, COL, T> GetIdentity() {
Matrix<ROW, COL, T> ret;
for (size_t r = 0; r < ROW; r++) {
for (size_t c = 0; c < COL; c++)
ret.m[r][c] = (r == c)? 1 : 0;
}
return ret;
}
};
//---------------------------------------------------------------------
// 数学库:矩阵运算
//---------------------------------------------------------------------
template<size_t ROW, size_t COL, typename T>
inline bool operator == (const Matrix<ROW, COL, T>& a, const Matrix<ROW, COL, T>& b) {
for (size_t r = 0; r < ROW; r++) {
for (size_t c = 0; c < COL; c++) {
if (a.m[r][c] != b.m[r][c]) return false;
}
}
return true;
}
template<size_t ROW, size_t COL, typename T>
inline bool operator != (const Matrix<ROW, COL, T>& a, const Matrix<ROW, COL, T>& b) {
return !(a == b);
}
template<size_t ROW, size_t COL, typename T>
inline Matrix<ROW, COL, T> operator + (const Matrix<ROW, COL, T>& src) {
return src;
}
template<size_t ROW, size_t COL, typename T>
inline Matrix<ROW, COL, T> operator - (const Matrix<ROW, COL, T>& src) {
Matrix<ROW, COL, T> out;
for (size_t j = 0; j < ROW; j++) {
for (size_t i = 0; i < COL; i++)
out.m[j][i] = -src.m[j][i];
}
return out;
}
template<size_t ROW, size_t COL, typename T>
inline Matrix<ROW, COL, T> operator + (const Matrix<ROW, COL, T>& a, const Matrix<ROW, COL, T>& b) {
Matrix<ROW, COL, T> out;
for (size_t j = 0; j < ROW; j++) {
for (size_t i = 0; i < COL; i++)
out.m[j][i] = a.m[j][i] + b.m[j][i];
}
return out;
}
template<size_t ROW, size_t COL, typename T>
inline Matrix<ROW, COL, T> operator - (const Matrix<ROW, COL, T>& a, const Matrix<ROW, COL, T>& b) {
Matrix<ROW, COL, T> out;
for (size_t j = 0; j < ROW; j++) {
for (size_t i = 0; i < COL; i++)
out.m[j][i] = a.m[j][i] - b.m[j][i];
}
return out;
}
template<size_t ROW, size_t COL, size_t NEWCOL, typename T>
inline Matrix<ROW, NEWCOL, T> operator * (const Matrix<ROW, COL, T>& a, const Matrix<COL, NEWCOL, T>& b) {
Matrix<ROW, NEWCOL, T> out;
for (size_t j = 0; j < ROW; j++) {
for (size_t i = 0; i < NEWCOL; i++) {
out.m[j][i] = vector_dot(a.Row(j), b.Col(i));
}
}
return out;
}
template<size_t ROW, size_t COL, typename T>
inline Matrix<ROW, COL, T> operator * (const Matrix<ROW, COL, T>& a, T x) {
Matrix<ROW, COL, T> out;
for (size_t j = 0; j < ROW; j++) {
for (size_t i = 0; i < COL; i++) {
out.m[j][i] = a.m[j][i] * x;
}
}
return out;
}
template<size_t ROW, size_t COL, typename T>
inline Matrix<ROW, COL, T> operator / (const Matrix<ROW, COL, T>& a, T x) {
Matrix<ROW, COL, T> out;
for (size_t j = 0; j < ROW; j++) {
for (size_t i = 0; i < COL; i++) {
out.m[j][i] = a.m[j][i] / x;
}
}
return out;
}
template<size_t ROW, size_t COL, typename T>
inline Matrix<ROW, COL, T> operator * (T x, const Matrix<ROW, COL, T>& a) {
return (a * x);
}
template<size_t ROW, size_t COL, typename T>
inline Matrix<ROW, COL, T> operator / (T x, const Matrix<ROW, COL, T>& a) {
Matrix<ROW, COL, T> out;
for (size_t j = 0; j < ROW; j++) {
for (size_t i = 0; i < COL; i++) {
out.m[j][i] = x / a.m[j][i];
}
}
return out;
}
template<size_t ROW, size_t COL, typename T>
inline Vector<COL, T> operator * (const Vector<ROW, T>& a, const Matrix<ROW, COL, T>& m) {
Vector<COL, T> b;
for (size_t i = 0; i < COL; i++)
b[i] = vector_dot(a, m.Col(i));
return b;
}
template<size_t ROW, size_t COL, typename T>
inline Vector<ROW, T> operator * (const Matrix<ROW, COL, T>& m, const Vector<COL, T>& a) {
Vector<ROW, T> b;
for (size_t i = 0; i < ROW; i++)
b[i] = vector_dot(a, m.Row(i));
return b;
}
//---------------------------------------------------------------------
// 数学库:行列式和逆矩阵等,光照计算有用
//---------------------------------------------------------------------
// 行列式求值:一阶
template<typename T>
inline T matrix_det(const Matrix<1, 1, T> &m) {
return m[0][0];
}
// 行列式求值:二阶
template<typename T>
inline T matrix_det(const Matrix<2, 2, T> &m) {
return m[0][0] * m[1][1] - m[0][1] * m[1][0];
}
// 行列式求值:多阶行列式,即第一行同他们的余子式相乘求和
template<size_t N, typename T>
inline T matrix_det(const Matrix<N, N, T> &m) {
T sum = 0;
for (size_t i = 0; i < N; i++) sum += m[0][i] * matrix_cofactor(m, 0, i);
return sum;
}
// 余子式:一阶
template<typename T>
inline T matrix_cofactor(const Matrix<1, 1, T> &m, size_t row, size_t col) {
return 0;
}
// 多阶余子式:即删除特定行列的子式的行列式值
template<size_t N, typename T>
inline T matrix_cofactor(const Matrix<N, N, T> &m, size_t row, size_t col) {
return matrix_det(m.GetMinor(row, col)) * (((row + col) % 2)? -1 : 1);
}
// 伴随矩阵:即余子式矩阵的转置
template<size_t N, typename T>
inline Matrix<N, N, T> matrix_adjoint(const Matrix<N, N, T> &m) {
Matrix<N, N, T> ret;
for (size_t j = 0; j < N; j++) {
for (size_t i = 0; i < N; i++) ret[j][i] = matrix_cofactor(m, i, j);
}
return ret;
}
// 求逆矩阵:使用伴随矩阵除以行列式的值得到
template<size_t N, typename T>
inline Matrix<N, N, T> matrix_invert(const Matrix<N, N, T> &m) {
Matrix<N, N, T> ret = matrix_adjoint(m);
T det = vector_dot(m.Row(0), ret.Col(0));
return ret / det;
}
// 输出到文本流
template<size_t ROW, size_t COL, typename T>
inline std::ostream& operator << (std::ostream& os, const Matrix<ROW, COL, T>& m) {
for (size_t r = 0; r < ROW; r++) {
Vector<COL, T> row = m.Row(r);
os << row << std::endl;
}
return os;
}
//---------------------------------------------------------------------
// 工具函数
//---------------------------------------------------------------------
template<typename T> inline T Abs(T x) { return (x < 0)? (-x) : x; }
template<typename T> inline T Max(T x, T y) { return (x < y)? y : x; }
template<typename T> inline T Min(T x, T y) { return (x > y)? y : x; }
template<typename T> inline bool NearEqual(T x, T y, T error) {
return (Abs(x - y) < error);
}
template<typename T> inline T Between(T xmin, T xmax, T x) {
return Min(Max(xmin, x), xmax);
}
// 截取 [0, 1] 的范围
template<typename T> inline T Saturate(T x) {
return Between<T>(0, 1, x);
}
// 类型别名
typedef Vector<2, float> Vec2f;
typedef Vector<2, double> Vec2d;
typedef Vector<2, int> Vec2i;
typedef Vector<3, float> Vec3f;
typedef Vector<3, double> Vec3d;
typedef Vector<3, int> Vec3i;
typedef Vector<4, float> Vec4f;
typedef Vector<4, double> Vec4d;
typedef Vector<4, int> Vec4i;
typedef Matrix<4, 4, float> Mat4x4f;
typedef Matrix<3, 3, float> Mat3x3f;
typedef Matrix<4, 3, float> Mat4x3f;
typedef Matrix<3, 4, float> Mat3x4f;
//---------------------------------------------------------------------
// 3D 数学运算
//---------------------------------------------------------------------
// 矢量转整数颜色
inline static uint32_t vector_to_color(const Vec4f& color) {
uint32_t r = (uint32_t)Between(0, 255, (int)(color.r * 255.0f));
uint32_t g = (uint32_t)Between(0, 255, (int)(color.g * 255.0f));
uint32_t b = (uint32_t)Between(0, 255, (int)(color.b * 255.0f));
uint32_t a = (uint32_t)Between(0, 255, (int)(color.a * 255.0f));
return (r << 16) | (g << 8) | b | (a << 24);
}
// 矢量转换整数颜色
inline static uint32_t vector_to_color(const Vec3f& color) {
return vector_to_color(color.xyz1());
}
// 整数颜色到矢量
inline static Vec4f vector_from_color(uint32_t rgba) {
Vec4f out;
out.r = ((rgba >> 16) & 0xff) / 255.0f;
out.g = ((rgba >> 8) & 0xff) / 255.0f;
out.b = ((rgba >> 0) & 0xff) / 255.0f;
out.a = ((rgba >> 24) & 0xff) / 255.0f;
return out;
}
// matrix set to zero
inline static Mat4x4f matrix_set_zero() {
Mat4x4f m;
m.m[0][0] = m.m[0][1] = m.m[0][2] = m.m[0][3] = 0.0f;
m.m[1][0] = m.m[1][1] = m.m[1][2] = m.m[1][3] = 0.0f;
m.m[2][0] = m.m[2][1] = m.m[2][2] = m.m[2][3] = 0.0f;
m.m[3][0] = m.m[3][1] = m.m[3][2] = m.m[3][3] = 0.0f;
return m;
}
// set to identity
inline static Mat4x4f matrix_set_identity() {
Mat4x4f m;
m.m[0][0] = m.m[1][1] = m.m[2][2] = m.m[3][3] = 1.0f;
m.m[0][1] = m.m[0][2] = m.m[0][3] = 0.0f;
m.m[1][0] = m.m[1][2] = m.m[1][3] = 0.0f;
m.m[2][0] = m.m[2][1] = m.m[2][3] = 0.0f;
m.m[3][0] = m.m[3][1] = m.m[3][2] = 0.0f;
return m;
}
// 平移变换
inline static Mat4x4f matrix_set_translate(float x, float y, float z) {
Mat4x4f m = matrix_set_identity();
m.m[3][0] = x;
m.m[3][1] = y;
m.m[3][2] = z;
return m;
}
// 缩放变换
inline static Mat4x4f matrix_set_scale(float x, float y, float z) {
Mat4x4f m = matrix_set_identity();
m.m[0][0] = x;
m.m[1][1] = y;
m.m[2][2] = z;
return m;
}
// 旋转变换,围绕 (x, y, z) 矢量旋转 theta 角度
inline static Mat4x4f matrix_set_rotate(float x, float y, float z, float theta) {
float qsin = (float)sin(theta * 0.5f);
float qcos = (float)cos(theta * 0.5f);
float w = qcos;
Vec3f vec = vector_normalize(Vec3f(x, y, z));
x = vec.x * qsin;
y = vec.y * qsin;
z = vec.z * qsin;
Mat4x4f m;
m.m[0][0] = 1 - 2 * y * y - 2 * z * z;
m.m[1][0] = 2 * x * y - 2 * w * z;
m.m[2][0] = 2 * x * z + 2 * w * y;
m.m[0][1] = 2 * x * y + 2 * w * z;
m.m[1][1] = 1 - 2 * x * x - 2 * z * z;
m.m[2][1] = 2 * y * z - 2 * w * x;
m.m[0][2] = 2 * x * z - 2 * w * y;
m.m[1][2] = 2 * y * z + 2 * w * x;
m.m[2][2] = 1 - 2 * x * x - 2 * y * y;
m.m[0][3] = m.m[1][3] = m.m[2][3] = 0.0f;
m.m[3][0] = m.m[3][1] = m.m[3][2] = 0.0f;
m.m[3][3] = 1.0f;
return m;
}
// 摄影机变换矩阵:eye/视点位置,at/看向哪里,up/指向上方的矢量
inline static Mat4x4f matrix_set_lookat(const Vec3f& eye, const Vec3f& at, const Vec3f& up) {
Vec3f zaxis = vector_normalize(at - eye);
Vec3f xaxis = vector_normalize(vector_cross(up, zaxis));
Vec3f yaxis = vector_cross(zaxis, xaxis);
Mat4x4f m;
m.SetCol(0, Vec4f(xaxis.x, xaxis.y, xaxis.z, -vector_dot(eye, xaxis)));
m.SetCol(1, Vec4f(yaxis.x, yaxis.y, yaxis.z, -vector_dot(eye, yaxis)));
m.SetCol(2, Vec4f(zaxis.x, zaxis.y, zaxis.z, -vector_dot(eye, zaxis)));
m.SetCol(3, Vec4f(0.0f, 0.0f, 0.0f, 1.0f));
return m;
}
// D3DXMatrixPerspectiveFovLH
inline static Mat4x4f matrix_set_perspective(float fovy, float aspect, float zn, float zf) {
float fax = 1.0f / (float)tan(fovy * 0.5f);
Mat4x4f m = matrix_set_zero();
m.m[0][0] = (float)(fax / aspect);
m.m[1][1] = (float)(fax);
m.m[2][2] = zf / (zf - zn);
m.m[3][2] = - zn * zf / (zf - zn);
m.m[2][3] = 1;
return m;
}
//---------------------------------------------------------------------
// 位图库:用于加载/保存图片,画点,画线,颜色读取
//---------------------------------------------------------------------
class Bitmap
{
public:
inline virtual ~Bitmap() { if (_bits) delete []_bits; _bits = NULL; }
inline Bitmap(int width, int height): _w(width), _h(height) {
_pitch = width * 4;
_bits = new uint8_t[_pitch * _h];
Fill(0);
}
inline Bitmap(const Bitmap& src): _w(src._w), _h(src._h), _pitch(src._pitch) {
_bits = new uint8_t[_pitch * _h];
memcpy(_bits, src._bits, _pitch * _h);
}
inline Bitmap(const char *filename) {
Bitmap *tmp = LoadFile(filename);
if (tmp == NULL) {
std::string msg = "load failed: ";
msg.append(filename);
throw std::runtime_error(msg);
}
_w = tmp->_w; _h = tmp->_h; _pitch = tmp->_pitch; _bits = tmp->_bits;
tmp->_bits = NULL;
delete tmp;
}
public:
inline int GetW() const { return _w; }
inline int GetH() const { return _h; }
inline int GetPitch() const { return _pitch; }
inline uint8_t *GetBits() { return _bits; }
inline const uint8_t *GetBits() const { return _bits; }
inline uint8_t *GetLine(int y) { return _bits + _pitch * y; }
inline const uint8_t *GetLine(int y) const { return _bits + _pitch * y; }
public:
inline void Fill(uint32_t color) {
for (int j = 0; j < _h; j++) {
uint32_t *row = (uint32_t*)(_bits + j * _pitch);
for (int i = 0; i < _w; i++, row++)
memcpy(row, &color, sizeof(uint32_t));
}
}
inline void SetPixel(int x, int y, uint32_t color) {
if (x >= 0 && x < _w && y >= 0 && y < _h) {
memcpy(_bits + y * _pitch + x * 4, &color, sizeof(uint32_t));
}
}
inline uint32_t GetPixel(int x, int y) const {
uint32_t color = 0;
if (x >= 0 && x < _w && y >= 0 && y < _h) {
memcpy(&color, _bits + y * _pitch + x * 4, sizeof(uint32_t));
}
return color;
}
inline void DrawLine(int x1, int y1, int x2, int y2, uint32_t color) {
int x, y;
if (x1 == x2 && y1 == y2) {
SetPixel(x1, y1, color);
return;
} else if (x1 == x2) {
int inc = (y1 <= y2)? 1 : -1;
for (y = y1; y != y2; y += inc) SetPixel(x1, y, color);
SetPixel(x2, y2, color);
} else if (y1 == y2) {
int inc = (x1 <= x2)? 1 : -1;
for (x = x1; x != x2; x += inc) SetPixel(x, y1, color);
SetPixel(x2, y2, color);
} else {
int dx = (x1 < x2)? x2 - x1 : x1 - x2;
int dy = (y1 < y2)? y2 - y1 : y1 - y2;
int rem = 0;
if (dx >= dy) {
if (x2 < x1) x = x1, y = y1, x1 = x2, y1 = y2, x2 = x, y2 = y;
for (x = x1, y = y1; x <= x2; x++) {
SetPixel(x, y, color);
rem += dy;
if (rem >= dx) { rem -= dx; y += (y2 >= y1)? 1 : -1; SetPixel(x, y, color); }
}
SetPixel(x2, y2, color);
} else {
if (y2 < y1) x = x1, y = y1, x1 = x2, y1 = y2, x2 = x, y2 = y;
for (x = x1, y = y1; y <= y2; y++) {
SetPixel(x, y, color);
rem += dx;
if (rem >= dy) { rem -= dy; x += (x2 >= x1)? 1 : -1; SetPixel(x, y, color); }
}
SetPixel(x2, y2, color);
}
}
}
struct BITMAPINFOHEADER { // bmih
uint32_t biSize;
uint32_t biWidth;
int32_t biHeight;
uint16_t biPlanes;
uint16_t biBitCount;
uint32_t biCompression;
uint32_t biSizeImage;
uint32_t biXPelsPerMeter;
uint32_t biYPelsPerMeter;
uint32_t biClrUsed;
uint32_t biClrImportant;
};
// 读取 BMP 图片,支持 24/32 位两种格式
inline static Bitmap* LoadFile(const char *filename) {
FILE *fp = fopen(filename, "rb");
if (fp == NULL) return NULL;
BITMAPINFOHEADER info;
uint8_t header[14];
int hr = (int)fread(header, 1, 14, fp);
if (hr != 14) { fclose(fp); return NULL; }
if (header[0] != 0x42 || header[1] != 0x4d) { fclose(fp); return NULL; }
hr = (int)fread(&info, 1, sizeof(info), fp);
if (hr != 40) { fclose(fp); return NULL; }
if (info.biBitCount != 24 && info.biBitCount != 32) { fclose(fp); return NULL; }
Bitmap *bmp = new Bitmap(info.biWidth, info.biHeight);
uint32_t offset;
memcpy(&offset, header + 10, sizeof(uint32_t));
fseek(fp, offset, SEEK_SET);
uint32_t pixelsize = (info.biBitCount + 7) / 8;
uint32_t pitch = (pixelsize * info.biWidth + 3) & (~3);
for (int y = 0; y < (int)info.biHeight; y++) {
uint8_t *line = bmp->GetLine(info.biHeight - 1 - y);
for (int x = 0; x < (int)info.biWidth; x++, line += 4) {
line[3] = 255;
fread(line, pixelsize, 1, fp);
}
fseek(fp, pitch - info.biWidth * pixelsize, SEEK_CUR);
}
fclose(fp);
return bmp;
}
// 保存 BMP 图片
inline bool SaveFile(const char *filename, bool withAlpha = false) const {
FILE *fp = fopen(filename, "wb");
if (fp == NULL) return false;
BITMAPINFOHEADER info;
uint32_t pixelsize = (withAlpha)? 4 : 3;
uint32_t pitch = (GetW() * pixelsize + 3) & (~3);
info.biSizeImage = pitch * GetH();
uint32_t bfSize = 54 + info.biSizeImage;
uint32_t zero = 0, offset = 54;
fputc(0x42, fp);
fputc(0x4d, fp);
fwrite(&bfSize, 4, 1, fp);
fwrite(&zero, 4, 1, fp);
fwrite(&offset, 4, 1, fp);
info.biSize = 40;
info.biWidth = GetW();
info.biHeight = GetH();
info.biPlanes = 1;
info.biBitCount = (withAlpha)? 32 : 24;