-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb3dlib.c
2968 lines (2717 loc) · 111 KB
/
b3dlib.c
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
//camera function is modified from small3dlib
//tri draw function is modified from adafruit gfx lib
//math function sin is from cmsis dsp lib
//arcsin function is copy from nvidia cg fast math lib
//invert sqrtf is copy from id soft quake
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
#include "b3dlib.h"
#ifndef __ASM
#define __ASM __asm
#endif
#ifdef WIN32
#define __attribute__(A)
#endif
#pragma GCC optimize("-O3")
//config the ram position if necessary
u32 B3L_seed = 0x31415926;
screen3f_t vectBuff[VECT_BUFF_SIZE]; //8KB
__attribute__((section(".zbuff"))) zBuff_t zBuff[Z_BUFF_LENTH]; //
#ifdef B3L_USING_PARTICLE
B3L_Particle_t particleBuff[B3L_PARTICLE_BUFF_DEPTH];//18KB
#endif
#if Z_BUFF_LEVEL == 2
#define Z_LIMIT_NUM (1.0f)
#endif
#if Z_BUFF_LEVEL == 1
#define Z_LIMIT_NUM (65535u)
#endif
#if Z_BUFF_LEVEL == 0
#define Z_LIMIT_NUM (255u)
#endif
#ifndef _swap_f32_t
#define _swap_f32_t(a, b) { f32 t = a; a = b; b = t; }
#endif
#ifndef _swap_int32_t
#define _swap_int32_t(a, b) { int32_t t = a; a = b; b = t; }
#endif
/*Private Fuction declare ---------------------------------------------------*/
/*-----------------------------------------------------------------------------
Math functions
-----------------------------------------------------------------------------*/
#if B3L_ARM == 1
__attribute__((always_inline)) static inline f32 B3L_Sqrtf(f32 in);
__attribute__((always_inline)) static inline f32 B3L_Absf(f32 in);
#else
#define B3L_Sqrtf sqrtf
#define B3L_Absf fabsf
#endif
__attribute__((always_inline)) static inline s32 B3L_RoundingToS(f32 in);
__attribute__((always_inline)) static inline s32 B3L_RoundingToU(f32 in);
__attribute__((always_inline)) static inline f32 FastInvertSqrt(f32 x);
__attribute__((always_inline)) static inline s32 VcvtF32ToS32_Fix(f32 in);
__attribute__((always_inline)) static inline f32 VcvtS32ToF32_Fix(s32 in);
__attribute__((always_inline)) static inline u32 SatToU8(u32 in);
__attribute__((always_inline)) static inline u32 SatToU16(u32 in);
__attribute__((always_inline)) static inline f32 Interp_f(f32 x1, f32 x2, f32 t);
__attribute__((always_inline)) static inline s32 Clamp_i(s32 v, s32 v1, s32 v2);
__attribute__((always_inline)) static inline f32 Clamp_f(f32 v, f32 v1, f32 v2);
/*-----------------------------------------------------------------------------
Vector and matrix functions
-----------------------------------------------------------------------------*/
__attribute__((always_inline)) static inline void Vect3_Add(vect3_t *pV1,vect3_t *pV2,vect3_t *pResult);
__attribute__((always_inline)) static inline void MakeClipMatrix(u32 state,f32 near_plane,f32 far_plane,f32 focalLength, f32 aspectRatio,mat4_t *pMat);
__attribute__((always_inline)) static inline void Vect3Xmat4(vect3_t *pV, mat4_t *pMat, vect4_t *pResult);
__attribute__((always_inline)) static inline void Vect3Xmat4WithTestToScreen4(vect3_t *pV, mat4_t *pMat, screen4_t *pResult);
__attribute__((always_inline)) static inline void Vect3Xmat4WithTest_f(vect3_t *pV, mat4_t *pMat, screen3f_t *pResult);
__attribute__((always_inline)) static inline bool Vect4BoundTest(vect4_t *pV);
/*-----------------------------------------------------------------------------
Z buff functions
-----------------------------------------------------------------------------*/
__attribute__((always_inline)) static inline zBuff_t CalZbuffValue(f32 z);
/*-----------------------------------------------------------------------------
Light functions
-----------------------------------------------------------------------------*/
__attribute__((always_inline)) static inline fBuff_t LightBlend(u32 inputPixel, u8 r, u8 g, u8 b);
__attribute__((always_inline)) static inline u32 CalLightFactor(f32 normalDotLight, f32 lightFactor0,f32 lightFactor1);
/*-----------------------------------------------------------------------------
Testing functions
-----------------------------------------------------------------------------*/
static bool Vect3InClipSpace(vect3_t *pV, mat4_t *pMat);
static bool BoundBoxTest(f32 *pMaxMin,mat4_t *pMat);
__attribute__((always_inline)) static inline bool TriangleFaceToViewer_f(f32 x0, f32 y0, f32 x1, f32 y1, f32 x2, f32 y2);
__attribute__((always_inline)) static inline bool TriVisable(u32 r0,u32 r1,u32 r2);
/*-----------------------------------------------------------------------------
Draw functions
-----------------------------------------------------------------------------*/
__attribute__((always_inline)) static inline fBuff_t GetFinalColor(fBuff_t color,u32 lightFactor);
__attribute__((always_inline)) static inline fBuff_t GetColorValue(texLUT_t *lut,u8 colorIdx,u32 lightFactor);
__attribute__((always_inline)) static inline void DrawPixel(fBuff_t color,s32 x,s32 y,f32 z,
fBuff_t *pFrameBuff,zBuff_t *pZbuff);
__attribute__((always_inline)) static inline void DrawPixelWithTest(fBuff_t color,s32 x,s32 y,f32 z,
fBuff_t *pFrameBuff,zBuff_t *pZbuff);
__attribute__((always_inline)) static inline void DrawTriTexture(
f32 x0,f32 y0,f32 u0,f32 v0,f32 z0,
f32 x1,f32 y1,f32 u1,f32 v1,f32 z1,
f32 x2,f32 y2,f32 u2,f32 v2,f32 z2,
u32 renderLevel,u32 lightFactor,B3L_texture_t *pTexture,
fBuff_t *pFrameBuff,zBuff_t *pZbuff);
__attribute__((always_inline)) static inline void DrawTriColor(
f32 x0,f32 y0,f32 z0,
f32 x1,f32 y1,f32 z1,
f32 x2,f32 y2,f32 z2,
u32 renderLevel,u32 lightFactor,fBuff_t color,
fBuff_t *pFrameBuff,zBuff_t *pZbuff);
__attribute__((always_inline)) static inline void DrawTexHLine(f32 x,s32 y,f32 b, f32 aZ, f32 bZ,
f32 aU,f32 aV,f32 bU,f32 bV,u32 lightFactor,
fBuff_t *pFrameBuff,zBuff_t *pZbuff,
B3L_texture_t *pTexture);
__attribute__((always_inline)) static inline void DrawColorHLine(f32 x,s32 y,f32 b, f32 aZ, f32 bZ,
fBuff_t finalColor,
fBuff_t *pFrameBuff,zBuff_t *pZbuff);
__attribute__((always_inline)) static inline void DrawDepthLineNoClip(s32 Ax,s32 Ay,f32 Az,s32 Bx,s32 By,f32 Bz,
texLUT_t color,fBuff_t *pFrameBuff,zBuff_t *pZbuff);
__attribute__((always_inline)) static inline void DrawDepthLineClip(s32 Ax,s32 Ay,f32 Az,s32 Bx,s32 By,f32 Bz,
texLUT_t color,fBuff_t *pFrameBuff,zBuff_t *pZbuff);
/*-----------------------------------------------------------------------------
Camera functions
-----------------------------------------------------------------------------*/
static void UpdateCam(render_t *pRender);
static void GenerateW2CMatrix(camera_t *pCam);
static void CamCalNewTrackPosition(camera_t *pCam);
static void CamCalNewTrackQuaternion(camera_t *pCam);
/*-----------------------------------------------------------------------------
Obj list functions
-----------------------------------------------------------------------------*/
static void ResetObjList(scene_t *pScene);
static void AddObjToTwoWayList(B3LObj_t *pObj, B3LObj_t **pStart);
/*-----------------------------------------------------------------------------
Particle list functions
-----------------------------------------------------------------------------*/
static void ResetParticleList(B3L_Particle_t *pPool,B3L_Particle_t **pStart,u32 num);
/*-----------------------------------------------------------------------------
Buffer functions
-----------------------------------------------------------------------------*/
static void ClearFrameBuff(fBuff_t *pFramebuff,fBuff_t value,u32 lineNum,u32 lineLength,u32 lineSkip);
static void ClearZbuff(zBuff_t *pZbuff,u32 length);
/*-----------------------------------------------------------------------------
Obj render functions
-----------------------------------------------------------------------------*/
static void RenderMeshObjs(render_t *pRender);
#ifdef B3L_USING_PARTICLE
static void UpdateParticleObjs(render_t *pRender, u32 time);
static void RenderParticleObjs(render_t *pRender);
#endif
static void RenderTexMesh(B3LMeshObj_t *pObj,render_t *pRender, mat4_t *pMat,u32 renderLevel);
static void RenderNoTexMesh(B3LMeshNoTexObj_t *pObj,render_t *pRender, mat4_t *pMat,u32 renderLevel);
static void RenderPolygon(B3LPolygonObj_t *pObj,render_t *pRender, mat4_t *pMat);
/*Function defines-----------------------------------------------------------*/
/*-----------------------------------------------------------------------------
Math function
-----------------------------------------------------------------------------*/
#if B3L_ARM == 1
__attribute__((always_inline)) static inline f32 B3L_Sqrtf(f32 in){
f32 result;
__ASM("vsqrt.f32 %0,%1" : "=t"(result) : "t"(in));
return (result);
}
__attribute__((always_inline)) static inline f32 B3L_Absf(f32 in){
f32 result;
__ASM("vabs.f32 %0,%1" : "=t"(result) : "t"(in));
return (result);
}
__attribute__((always_inline)) static inline s32 VcvtF32ToS32_Fix(f32 in){
s32 result;
__ASM ("vcvt.s32.f32 %0,%1,#B3L_FIX_BITS" : "=t"(result) : "t"(in));
return result;
}
__attribute__((always_inline)) static inline f32 VcvtS32ToF32_Fix(s32 in){
s32 result;
__ASM ("vcvt.f32.s32 %0,%1,#B3L_FIX_BITS" : "=t"(result) : "t"(in));
return result;
}
__attribute__((always_inline)) static inline u32 SatToU8(u32 in){
u32 result;
__ASM ("usat %0,#8,%1" : "=t"(result) : "t"(in));
return result;
}
__attribute__((always_inline)) static inline u32 SatToU16(u32 in){
u32 result;
__ASM ("usat %0,#16,%1" : "=t"(result) : "t"(in));
return result;
}
__attribute__((always_inline)) static inline s32 B3L_RoundingToS(f32 in){
s32 result;
__ASM ("vcvtr.s32.f32 %0,%1" : "=t"(result) : "t"(in));
return result;
}
__attribute__((always_inline)) static inline s32 B3L_RoundingToU(f32 in){
u32 result;
__ASM ("vcvtr.u32.f32 %0,%1" : "=t"(result) : "t"(in));
return result;
}
#else
__attribute__((always_inline)) static inline s32 VcvtF32ToS32_Fix(f32 in){
return ((s32)(in*((f32)(1<<B3L_FIX_BITS))));
}
__attribute__((always_inline)) static inline f32 VcvtS32ToF32_Fix(s32 in){
return ((f32)in)/((f32)(1<<B3L_FIX_BITS));
}
__attribute__((always_inline)) static inline u32 SatToU8(u32 in){
const uint32_t max = ((1U << 8) - 1U);
if (in > max){
return max;
}else{
return in;
}
}
__attribute__((always_inline)) static inline u32 SatToU16(u32 in){
const uint32_t max = ((1U << 16) - 1U);
if (in > max){
return max;
}else{
return in;
}
}
__attribute__((always_inline)) static inline s32 B3L_RoundingToS(f32 in){
return (s32)roundf(in);
}
__attribute__((always_inline)) static inline s32 B3L_RoundingToU(f32 in){
return (u32)roundf(in);
}
#endif //end of B3L_ARM
__attribute__((always_inline)) static inline f32 Clamp_f(f32 v, f32 v1, f32 v2){
return v > v1 ? (v < v2 ? v : v2) : v1;
}
__attribute__((always_inline)) static inline s32 Clamp_i(s32 v, s32 v1, s32 v2)
{
return v >= v1 ? (v <= v2 ? v : v2) : v1;
}
//inv sqrt black magic from quake
__attribute__((always_inline)) static inline f32 FastInvertSqrt(f32 x){
f32 xhalf = 0.5f * x;
int i = *(int*)&x; // store floating-point bits in integer
i = 0x5f3759df - (i >> 1); // initial guess for Newton's method
x = *(float*)&i; // convert new bits into float
x = x*(1.5f - xhalf*x*x); // One round of Newton's method
return x;
}
__attribute__((always_inline)) static inline f32 Interp_f(f32 x1, f32 x2, f32 t){
return x1 + (x2 - x1) * t;
}
__attribute__((always_inline)) static inline void Vect3_Add(vect3_t *pV1,vect3_t *pV2,vect3_t *pResult){
pResult->x = pV1->x + pV2->x;
pResult->y = pV1->y + pV2->y;
pResult->z = pV1->z + pV2->z;
}
/*
Create 3*3 matrix for rotation in x axis
*/
void B3L_Mat3XRotate(mat3_t *pMat,f32 angle){
f32 cosp = B3L_cos(angle);
f32 sinp = B3L_sin(angle);
pMat->m00=1.0f; pMat->m01=0.0f; pMat->m02=0.0f;
pMat->m10=0.0f; pMat->m11=cosp; pMat->m12=-sinp;
pMat->m20=0.0f; pMat->m21=sinp; pMat->m22=cosp;
}
/*
Create 3*3 matrix for rotation in y axis
*/
void B3L_Mat3YRotate(mat3_t *pMat,f32 angle){
f32 cosh = B3L_cos(angle);
f32 sinh = B3L_sin(angle);
pMat->m00=cosh; pMat->m01=0.0f; pMat->m02=sinh;
pMat->m10=0.0f; pMat->m11=1.0f; pMat->m12=0.0f;
pMat->m20=-sinh; pMat->m21=0.0f; pMat->m22=cosh;
}
/*
Create 3*3 matrix for rotation in z axis
*/
void B3L_Mat3ZRotate(mat3_t *pMat,f32 angle){
f32 cosb = B3L_cos(angle);
f32 sinb = B3L_sin(angle);
pMat->m00=cosb; pMat->m01=-sinb; pMat->m02=0.0f;
pMat->m10=sinb; pMat->m11=cosb; pMat->m12=0.0f;
pMat->m20=0.0f; pMat->m21=0.0f; pMat->m22=1.0f;
}
/*
Rotate obj matrix in obj space by x axis
*/
void B3L_RotateObjInOX(quat4_t *pQuat,f32 angle){
quat4_t rqat;
B3L_QuatCreateXRotate(&rqat,angle);
B3L_QuatMult(pQuat,&rqat, pQuat);
}
/*
Rotate obj matrix in obj space by y axis
*/
void B3L_RotateObjInOY(quat4_t *pQuat,f32 angle){
quat4_t rqat;
B3L_QuatCreateYRotate(&rqat,angle);
B3L_QuatMult(pQuat,&rqat, pQuat);
}
/*
Rotate obj matrix in obj space by z axis
*/
void B3L_RotateObjInOZ(quat4_t *pQuat,f32 angle){
quat4_t rqat;
B3L_QuatCreateZRotate(&rqat,angle);
B3L_QuatMult(pQuat,&rqat, pQuat);
}
/*
Rotate obj matrix in world space by x axis
*/
void B3L_RotateObjInWX(quat4_t *pQuat,f32 angle){
quat4_t rqat;
B3L_QuatCreateXRotate(&rqat,angle);
B3L_QuatMult(&rqat, pQuat,pQuat);
}
/*
Rotate obj matrix in world space by y axis
*/
void B3L_RotateObjInWY(quat4_t *pQuat,f32 angle){
quat4_t rqat;
B3L_QuatCreateYRotate(&rqat,angle);
B3L_QuatMult(&rqat, pQuat,pQuat);
}
/*
Rotate obj matrix in world space by z axis ?? may should use matrix type ABA
*/
void B3L_RotateObjInWZ(quat4_t *pQuat,f32 angle){
quat4_t rqat;
B3L_QuatCreateZRotate(&rqat,angle);
B3L_QuatMult(&rqat, pQuat,pQuat);
}
__attribute__((always_inline)) static inline void Vect3Xmat4(vect3_t *pV, mat4_t *pMat, vect4_t *pResult){
f32 x = pV->x; f32 y = pV->y; f32 z = pV->z;
#define dotCol(col)\
((x*(pMat->m##col##0)) +\
(y*(pMat->m##col##1)) +\
(z*(pMat->m##col##2)) +\
(pMat->m##col##3))
pResult->x = dotCol(0);
pResult->y = dotCol(1);
pResult->z = dotCol(2);
pResult->w = dotCol(3);
#undef dotCol
}
__attribute__((always_inline)) static inline void Vect3Xmat4WithTest_f(vect3_t *pV, mat4_t *pMat, screen3f_t *pResult){
f32 x = pV->x; f32 y = pV->y; f32 z = pV->z;
f32 rx,ry,rz,rw;
u32 testResult=0;
#define dotCol(col)\
((x*(pMat->m##col##0)) +\
(y*(pMat->m##col##1)) +\
(z*(pMat->m##col##2)) +\
(pMat->m##col##3))
rx = dotCol(0);
ry = dotCol(1);
rz = dotCol(2);
rw = dotCol(3);
if (rz<0.0f){//if the near plane clip, then don't do the calculation, set bit and return directly
B3L_SET(testResult,B3L_NEAR_PLANE_CLIP);
pResult->test = testResult;
return;
}else{
if((rx<=rw)&&(rx>=-rw)&&(ry<=rw)&&(ry>=-rw)&&(rz<=rw)){
B3L_SET(testResult,B3L_IN_SPACE);
}
}
f32 factor=1.0f / (rw);//rw won't be zero due we returned already rz<0 (rz>0, rw must >0)
f32 intX = (HALF_RESOLUTION_X + rx *factor* HALF_RESOLUTION_X);
f32 intY = (HALF_RESOLUTION_Y - ry *factor* HALF_RESOLUTION_Y);
rz = rz*factor;
pResult->test = testResult;
pResult->x = intX;
pResult->y = intY;
pResult->z = rz;
#undef dotCol
}
__attribute__((always_inline)) static inline void Vect3Xmat4WithTestToScreen4(vect3_t *pV, mat4_t *pMat, screen4_t *pResult){
f32 x = pV->x; f32 y = pV->y; f32 z = pV->z;
f32 rx,ry,rz,rw;
u32 testResult=0;
#define dotCol(col)\
((x*(pMat->m##col##0)) +\
(y*(pMat->m##col##1)) +\
(z*(pMat->m##col##2)) +\
(pMat->m##col##3))
rx = dotCol(0);
ry = dotCol(1);
rz = dotCol(2);
rw = dotCol(3);
if (rz<0.0f){//if the near plane clip, then don't do the calculation, set bit and return directly
B3L_SET(testResult,B3L_NEAR_PLANE_CLIP);
pResult->test = testResult;
return;
}else{
if((rx<=rw)&&(rx>=-rw)&&(ry<=rw)&&(ry>=-rw)&&(rz<=rw)){
B3L_SET(testResult,B3L_IN_SPACE);
}
}
f32 factor=1.0f / (rw);//rw won't be zero due we returned already rz<0 (rz>0, rw must >0)
s32 intX = (int32_t)(HALF_RESOLUTION_X + rx *factor* HALF_RESOLUTION_X);
s32 intY = (int32_t)(HALF_RESOLUTION_Y - ry *factor* HALF_RESOLUTION_Y);
rz = rz*factor;
pResult->test = testResult;
pResult->x = intX;
pResult->y = intY;
pResult->z = rz;
pResult->w = rw;
#undef dotCol
}
void B3L_Vect3MulMat3(vect3_t *pV, mat3_t *pMat, vect3_t *pResult){
f32 x = pV->x; f32 y = pV->y; f32 z = pV->z;
f32 rx,ry,rz;
#define dotCol(col)\
(x*(pMat->m##col##0)) +\
(y*(pMat->m##col##1)) +\
(z*(pMat->m##col##2))
rx = dotCol(0);
ry = dotCol(1);
rz = dotCol(2);
pResult->x = rx;
pResult->y = ry;
pResult->z = rz;
#undef dotCol
}
void B3L_Point3MulMat4(vect3_t *pV, mat4_t *pMat, vect3_t *pResult){
f32 x = pV->x; f32 y = pV->y; f32 z = pV->z;
f32 rx,ry,rz;
#define dotCol(col)\
(x*(pMat->m##col##0)) +\
(y*(pMat->m##col##1)) +\
(z*(pMat->m##col##2)) +\
pMat->m##col##3
rx = dotCol(0);
ry = dotCol(1);
rz = dotCol(2);
pResult->x = rx;
pResult->y = ry;
pResult->z = rz;
#undef dotCol
}
__attribute__((always_inline)) static inline bool Vect4BoundTest(vect4_t *pV){
f32 x=pV->x;f32 y=pV->y;f32 z=pV->z;f32 w=pV->w;
if((x<=w)&&(x>=-w)&&(y<=w)&&(y>=-w)&&(z>=0)&&(z<=w)){
return true;
}else{
return false;
}
}
/*-----------------------------------------------------------------------------
Math functions
-----------------------------------------------------------------------------*/
f32 B3L_sin(f32 in){
in = in-(f32)((s32)in);//scale to -1~1
if (in>0.25f){in = 0.5f - in;}
if (in<-0.25f){in = -0.5f -in;}
if (in>0.25f){in = 0.5f - in;}
f32 v2 = in*in;
f32 v3 = in*v2;
f32 v5 = v3*v2;
f32 v7 = v5*v2;
return 6.28316394434f*in-41.3371315018f*v3+81.3404239211f*v5-70.99090501333f*v7;
}
f32 B3L_cos(f32 in){
return B3L_sin(in + 0.25f);
}
f32 B3L_asin(f32 in){
in = Clamp_f(in, -1.0f,1.0f);
float negate = (f32)(in < 0);
if (in < 0.0f){
in = -1.0f*in;
}
float ret = -0.0187293f;
ret *= in;
ret += 0.0742610f;
ret *= in;
ret -= 0.2121144f;
ret *= in;
ret += 1.5707288f;
ret = 3.14159265358979f*0.5f - B3L_Sqrtf(1.0f - in)*ret;
ret = (ret - 2 * negate * ret)*0.15915494309f;
return ret;
}
f32 B3L_atan2(f32 y,f32 x){
f32 t0, t1, t3, t4;
t3 = B3L_Absf(x);
t1 = B3L_Absf(y);
t0 = B3L_MAX(t3, t1);
t1 = B3L_MIN(t3, t1);
t3 = 1.0f / t0;
t3 = t1 * t3;
t4 = t3 * t3;
t0 = - 0.013480470f;
t0 = t0 * t4 + 0.057477314f;
t0 = t0 * t4 - 0.121239071f;
t0 = t0 * t4 + 0.195635925f;
t0 = t0 * t4 - 0.332994597f;
t0 = t0 * t4 + 0.999995630f;
t3 = t0 * t3 * 0.159154943f;
t3 = (B3L_Absf(y) > B3L_Absf(x)) ? 0.25f - t3 : t3;
t3 = (x < 0) ? 0.5f - t3 : t3;
t3 = (y < 0) ? -t3 : t3;
return t3;
}
//random function
void B3L_SetSeed(u32 seed){
B3L_seed = seed|1;
}
//using rand xorshift algorithm from george marsaglia's paper
u32 B3L_Random(void){
B3L_seed ^=(B3L_seed<<13);
B3L_seed ^=(B3L_seed>>17);
B3L_seed ^=(B3L_seed<<5);
return B3L_seed;
}
u32 B3L_Rnd(u32 range){
return B3L_Random()%range;
}
/*-----------------------------------------------------------------------------
Vector functions
-----------------------------------------------------------------------------*/
vect2_t B3L_Vect2(f32 x,f32 y){
vect2_t output = {.x=x,.y=y};
return output;
}
vect3_t B3L_Vect3(f32 x,f32 y,f32 z){
vect3_t output = {.x=x,.y=y,.z=z};
return output;
}
vect4_t B3L_Vect4(f32 x,f32 y,f32 z,f32 w){
vect4_t output = {.x=x,.y=y,.z=z,.w=w};
return output;
}
f32 B3L_Vec2Length(vect2_t *pV){
return B3L_Sqrtf(pV->x*pV->x+pV->y*pV->y);
}
void B3L_Vect2Normalize(vect2_t *pV,vect2_t *pResult){
f32 x = pV->x;f32 y= pV->y;
f32 factor = FastInvertSqrt(x*x+y*y);
pResult->x = (x*factor);
pResult->y = (y*factor);
}
void B3L_Vect3Normalize(vect3_t *pV,vect3_t *pResult){
f32 x = pV->x;f32 y= pV->y;f32 z= pV->z;
//f32 factor = FastInvertSqrt(x*x+y*y+z*z);
f32 factor = 1.0f/B3L_Sqrtf(x*x+y*y+z*z);
pResult->x = (x*factor);
pResult->y = (y*factor);
pResult->z = (z*factor);
}
f32 B3L_Vect3Length(vect3_t *pV){
f32 x = pV->x;f32 y= pV->y;f32 z= pV->z;
return B3L_Sqrtf(x * x + y*y + z*z);
}
void B3L_Vect3Add(vect3_t *pVa,vect3_t *pVb,vect3_t *pResult){
pResult->x = pVa->x + pVb->x;
pResult->y = pVa->y + pVb->y;
pResult->z = pVa->z + pVb->z;
}
void B3L_Vect3Sub(vect3_t *pVa,vect3_t *pVb,vect3_t *pResult){
pResult->x = pVa->x - pVb->x;
pResult->y = pVa->y - pVb->y;
pResult->z = pVa->z - pVb->z;
}
void B3L_Vect3Cross(vect3_t *pA, vect3_t *pB, vect3_t *pResult){
pResult->x = pA->y * pB->z - pA->z * pB->y;
pResult->y = pA->z * pB->x - pA->x * pB->z;
pResult->z = pA->x * pB->y - pA->y * pB->x;
}
f32 B3L_Vect3Dot(vect3_t *pA, vect3_t *pB){
return (pA->x*pB->x+pA->y*pB->y+pA->z*pB->z);
}
void B3L_Vect3Scale(vect3_t *pV,f32 scale,vect3_t *pResult){
pResult->x = scale*pV->x;
pResult->y = scale*pV->y;
pResult->z = scale*pV->z;
}
void B3L_Vect3Interp(vect3_t *pVa,vect3_t *pVb,vect3_t *pResult,f32 t){
pResult->x = Interp_f(pVa->x,pVb->x,t);
pResult->y = Interp_f(pVa->y,pVb->y,t);
pResult->z = Interp_f(pVa->z,pVb->z,t);
}
/*-----------------------------------------------------------------------------
Matrix functions
-----------------------------------------------------------------------------*/
void B3L_InitUnitMat3(mat3_t *pMat){
pMat->m00 = 1.0f;pMat->m01 = 0.0f;pMat->m02 = 0.0f;
pMat->m10 = 0.0f;pMat->m11 = 1.0f;pMat->m12 = 0.0f;
pMat->m20 = 0.0f;pMat->m21 = 0.0f;pMat->m22 = 1.0f;
}
void B3L_CreateO2WMat(mat3_t *pRMat, vect3_t *pTranslation, vect3_t *pScale, mat4_t *pResult){
f32 sx = pScale->x;f32 sy = pScale->y;f32 sz = pScale->z;
pResult->m03 = pTranslation->x; pResult->m13 = pTranslation->y; pResult->m23 = pTranslation->z;
pResult->m00 = pRMat->m00*sx; pResult->m01 = pRMat->m01*sy; pResult->m02 = pRMat->m02*sz;
pResult->m10 = pRMat->m10*sx; pResult->m11 = pRMat->m11*sy; pResult->m12 = pRMat->m12*sz;
pResult->m20 = pRMat->m20*sx; pResult->m21 = pRMat->m21*sy; pResult->m22 = pRMat->m22*sz;
pResult->m30 = 0.0f; pResult->m31 = 0.0f; pResult->m32 = 0.0f; pResult->m33 = 1.0f;
}
__attribute__((always_inline)) static inline void MakeClipMatrix(u32 state,f32 near_plane,f32 far_plane,
f32 focalLength, f32 aspectRatio,mat4_t *pMat){
f32 zero = 0.0f;
f32 one = 1.0f;
#define M(x,y) (pMat)->m##x##y
if(B3L_TEST(state,B3L_PROJECT_MODE)==PERSPECTIVE_PROJECT){
M(0,0) = focalLength; M(1,0) = zero; M(2,0) = zero; M(3,0) = zero;
M(0,1) = zero; M(1,1) = focalLength*aspectRatio; M(2,1) = zero; M(3,1) = zero;
M(0,2) = zero; M(1,2) = zero; M(2,2) = far_plane/(far_plane-near_plane); M(3,2) = one;
M(0,3) = zero; M(1,3) = zero; M(2,3) =-near_plane*far_plane/(far_plane-near_plane); M(3,3) = zero;
}else{
M(0,0) = focalLength; M(1,0) = zero; M(2,0) = zero; M(3,0) = zero;
M(0,1) = zero; M(1,1) = focalLength*aspectRatio; M(2,1) = zero; M(3,1) = zero;
M(0,2) = zero; M(1,2) = zero; M(2,2) = one/(far_plane-near_plane); M(3,2) = zero;
M(0,3) = zero; M(1,3) = zero; M(2,3) =near_plane/(far_plane-near_plane); M(3,3) = one;
}
#undef M
}
void B3L_InitMat4One(mat4_t *pMat){
#define M(x,y) (pMat)->m##x##y
M(0,0) = 1.0f; M(1,0) = 0.0f; M(2,0) = 0.0f; M(3,0) = 0.0f;
M(0,1) = 0.0f; M(1,1) = 1.0f; M(2,1) = 0.0f; M(3,1) = 0.0f;
M(0,2) = 0.0f; M(1,2) = 0.0f; M(2,2) = 1.0f; M(3,2) = 0.0f;
M(0,3) = 0.0f; M(1,3) = 0.0f; M(2,3) = 0.0f; M(3,3) = 1.0f;
#undef M
}
void B3L_TransposeMat4(mat4_t *pMat){
f32 temp;
temp = ((f32 *)pMat)[1];
((f32 *)pMat)[1]=((f32 *)pMat)[4];
((f32 *)pMat)[4] = temp;
temp = ((f32 *)pMat)[2];
((f32 *)pMat)[2]=((f32 *)pMat)[8];
((f32 *)pMat)[8] = temp;
temp = ((f32 *)pMat)[6];
((f32 *)pMat)[6]=((f32 *)pMat)[9];
((f32 *)pMat)[9] = temp;
temp = ((f32 *)pMat)[3];
((f32 *)pMat)[3]=((f32 *)pMat)[12];
((f32 *)pMat)[12] = temp;
temp = ((f32 *)pMat)[7];
((f32 *)pMat)[7]=((f32 *)pMat)[13];
((f32 *)pMat)[13] = temp;
temp = ((f32 *)pMat)[11];
((f32 *)pMat)[11]=((f32 *)pMat)[14];
((f32 *)pMat)[14] = temp;
}
//mat1 * mat2 -> mat3, it is safe to set mat1 same as mat3
void B3L_Mat4XMat4(mat4_t *pMat1,mat4_t *pMat2, mat4_t *pMat3){
f32 t0,t1,t2,t3;
f32 s0,s1,s2,s3;
#define M(x,y) (pMat1)->m##x##y
#define N(x,y) (pMat2)->m##x##y
#define O(x,y) (pMat3)->m##x##y
s0=M(0,0);s1=M(1,0);s2=M(2,0);s3=M(3,0);
t0 = s0*N(0,0)+s1*N(0,1)+s2*N(0,2)+s3*N(0,3);
t1 = s0*N(1,0)+s1*N(1,1)+s2*N(1,2)+s3*N(1,3);
t2 = s0*N(2,0)+s1*N(2,1)+s2*N(2,2)+s3*N(2,3);
t3 = s0*N(3,0)+s1*N(3,1)+s2*N(3,2)+s3*N(3,3);
O(0,0) = t0;O(1,0) = t1;O(2,0) = t2;O(3,0) = t3;
s0=M(0,1);s1=M(1,1);s2=M(2,1);s3=M(3,1);
t0 =s0*N(0,0)+s1*N(0,1)+s2*N(0,2)+s3*N(0,3);
t1 =s0*N(1,0)+s1*N(1,1)+s2*N(1,2)+s3*N(1,3);
t2 =s0*N(2,0)+s1*N(2,1)+s2*N(2,2)+s3*N(2,3);
t3 =s0*N(3,0)+s1*N(3,1)+s2*N(3,2)+s3*N(3,3);
O(0,1) = t0;O(1,1) = t1;O(2,1) = t2;O(3,1) = t3;
s0=M(0,2);s1=M(1,2);s2=M(2,2);s3=M(3,2);
t0 =s0*N(0,0)+s1*N(0,1)+s2*N(0,2)+s3*N(0,3);
t1 =s0*N(1,0)+s1*N(1,1)+s2*N(1,2)+s3*N(1,3);
t2 =s0*N(2,0)+s1*N(2,1)+s2*N(2,2)+s3*N(2,3);
t3 =s0*N(3,0)+s1*N(3,1)+s2*N(3,2)+s3*N(3,3);
O(0,2) = t0;O(1,2) = t1;O(2,2) = t2;O(3,2) = t3;
s0=M(0,3);s1=M(1,3);s2=M(2,3);s3=M(3,3);
t0 = s0*N(0,0)+s1*N(0,1)+s2*N(0,2)+s3*N(0,3);
t1 = s0*N(1,0)+s1*N(1,1)+s2*N(1,2)+s3*N(1,3);
t2 = s0*N(2,0)+s1*N(2,1)+s2*N(2,2)+s3*N(2,3);
t3 = s0*N(3,0)+s1*N(3,1)+s2*N(3,2)+s3*N(3,3);
O(0,3) = t0;O(1,3) = t1;O(2,3) = t2;O(3,3) = t3;
#undef M
#undef N
#undef O
}
/*
matrix 3*3 B = AB
*/
void B3L_Mat3MultMat3ABB(mat3_t *pMatA,mat3_t *pMatB){
f32 t0,t1,t2;
f32 s0,s1,s2;
#define M(x,y) (pMatA)->m##x##y
#define N(x,y) (pMatB)->m##x##y
s0=N(0,0);s1=N(0,1);s2=N(0,2);
t0 = s0*M(0,0)+s1*M(1,0)+s2*M(2,0);
t1 = s0*M(0,1)+s1*M(1,1)+s2*M(2,1);
t2 = s0*M(0,2)+s1*M(1,2)+s2*M(2,2);
N(0,0) = t0;N(0,1) = t1;N(0,2) = t2;
s0=N(1,0);s1=N(1,1);s2=N(1,2);
t0 = s0*M(0,0)+s1*M(1,0)+s2*M(2,0);
t1 = s0*M(0,1)+s1*M(1,1)+s2*M(2,1);
t2 = s0*M(0,2)+s1*M(1,2)+s2*M(2,2);
N(1,0) = t0;N(1,1) = t1;N(1,2) = t2;
s0=N(2,0);s1=N(2,1);s2=N(2,2);
t0 = s0*M(0,0)+s1*M(1,0)+s2*M(2,0);
t1 = s0*M(0,1)+s1*M(1,1)+s2*M(2,1);
t2 = s0*M(0,2)+s1*M(1,2)+s2*M(2,2);
N(2,0) = t0;N(2,1) = t1;N(2,2) = t2;
#undef M
#undef N
}
/*
matrix 3*3 A = AB
*/
void B3L_Mat3MultMat3ABA(mat3_t *pMatA,mat3_t *pMatB){
f32 t0,t1,t2;
f32 s0,s1,s2;
#define M(x,y) (pMatA)->m##x##y
#define N(x,y) (pMatB)->m##x##y
s0=M(0,0);s1=M(1,0);s2=M(2,0);
t0 = s0*N(0,0)+s1*N(0,1)+s2*N(0,2);
t1 = s0*N(1,0)+s1*N(1,1)+s2*N(1,2);
t2 = s0*N(2,0)+s1*N(2,1)+s2*N(2,2);
M(0,0) = t0;M(1,0) = t1;M(2,0) = t2;
s0=M(0,1);s1=M(1,1);s2=M(2,1);
t0 =s0*N(0,0)+s1*N(0,1)+s2*N(0,2);
t1 =s0*N(1,0)+s1*N(1,1)+s2*N(1,2);
t2 =s0*N(2,0)+s1*N(2,1)+s2*N(2,2);
M(0,1) = t0;M(1,1) = t1;M(2,1) = t2;
s0=M(0,2);s1=M(1,2);s2=M(2,2);
t0 =s0*N(0,0)+s1*N(0,1)+s2*N(0,2);
t1 =s0*N(1,0)+s1*N(1,1)+s2*N(1,2);
t2 =s0*N(2,0)+s1*N(2,1)+s2*N(2,2);
M(0,2) = t0;M(1,2) = t1;M(2,2) = t2;
#undef M
#undef N
}
void B3L_MakeScaleMatrix(f32 scaleX,f32 scaleY,f32 scaleZ,mat4_t *pMat){
#define M(x,y) (pMat)->m##x##y
M(0,0) = scaleX; M(1,0) = 0.0f; M(2,0) = 0.0f; M(3,0) = 0.0f;
M(0,1) = 0.0f; M(1,1) = scaleY; M(2,1) = 0.0f; M(3,1) = 0.0f;
M(0,2) = 0.0f; M(1,2) = 0.0f; M(2,2) = scaleZ; M(3,2) = 0.0f;
M(0,3) = 0.0f; M(1,3) = 0.0f; M(2,3) = 0.0f; M(3,3) = 1.0f;
#undef M
}
void B3L_MakeTranslationMat(f32 offsetX,f32 offsetY,f32 offsetZ,mat4_t *pMat){
#define M(x,y) (pMat)->m##x##y
f32 one = 1.0f;
M(0,0) = one; M(1,0) = 0.0f; M(2,0) = 0.0f; M(3,0) = 0.0f;
M(0,1) = 0.0f; M(1,1) = one; M(2,1) = 0.0f; M(3,1) = 0.0f;
M(0,2) = 0.0f; M(1,2) = 0.0f; M(2,2) = one; M(3,2) = 0.0f;
M(0,3) = offsetX; M(1,3) = offsetY; M(2,3) = offsetZ; M(3,3) = one;
#undef M
}
void B3L_MakeO2CMatrix(mat3_t *pRMat,vect3_t *pScale,vect3_t *pTrans,mat4_t *pCamMat, mat4_t *pResult){
f32 t0,t1,t2,t3;
f32 s0,s1,s2;
#define M(x,y) (pRMat)->m##x##y
#define N(x,y) (pCamMat)->m##x##y
#define O(x,y) (pResult)->m##x##y
f32 scaleVal = pScale->x;
s0=M(0,0)*scaleVal;s1=M(1,0)*scaleVal;s2=M(2,0)*scaleVal;
t0 = s0*N(0,0)+s1*N(0,1)+s2*N(0,2);
t1 = s0*N(1,0)+s1*N(1,1)+s2*N(1,2);
t2 = s0*N(2,0)+s1*N(2,1)+s2*N(2,2);
t3 = s0*N(3,0)+s1*N(3,1)+s2*N(3,2);
O(0,0) = t0;O(1,0) = t1;O(2,0) = t2;O(3,0) = t3;
scaleVal = pScale->y;
s0=M(0,1)*scaleVal;s1=M(1,1)*scaleVal;s2=M(2,1)*scaleVal;
t0 =s0*N(0,0)+s1*N(0,1)+s2*N(0,2);
t1 =s0*N(1,0)+s1*N(1,1)+s2*N(1,2);
t2 =s0*N(2,0)+s1*N(2,1)+s2*N(2,2);
t3 =s0*N(3,0)+s1*N(3,1)+s2*N(3,2);
O(0,1) = t0;O(1,1) = t1;O(2,1) = t2;O(3,1) = t3;
scaleVal = pScale->z;
s0=M(0,2)*scaleVal;s1=M(1,2)*scaleVal;s2=M(2,2)*scaleVal;
t0 =s0*N(0,0)+s1*N(0,1)+s2*N(0,2);
t1 =s0*N(1,0)+s1*N(1,1)+s2*N(1,2);
t2 =s0*N(2,0)+s1*N(2,1)+s2*N(2,2);
t3 =s0*N(3,0)+s1*N(3,1)+s2*N(3,2);
O(0,2) = t0;O(1,2) = t1;O(2,2) = t2;O(3,2) = t3;
s0=pTrans->x;s1=pTrans->y;s2=pTrans->z;
t0 = s0*N(0,0)+s1*N(0,1)+s2*N(0,2)+N(0,3);
t1 = s0*N(1,0)+s1*N(1,1)+s2*N(1,2)+N(1,3);
t2 = s0*N(2,0)+s1*N(2,1)+s2*N(2,2)+N(2,3);
t3 = s0*N(3,0)+s1*N(3,1)+s2*N(3,2)+N(3,3);
O(0,3) = t0;O(1,3) = t1;O(2,3) = t2;O(3,3) = t3;
#undef M
#undef N
#undef O
}
__attribute__((always_inline)) static inline zBuff_t CalZbuffValue(f32 z){
#if (Z_BUFF_LEVEL == 0)
u32 tempZ = B3L_RoundingToU(z*255.0f);
u8 compZ = SatToU8(tempZ);
#endif
#if (Z_BUFF_LEVEL == 1)
u32 tempZ = B3L_RoundingToU(z*65535.0f);
u16 compZ = SatToU16(tempZ);
#endif
#if (Z_BUFF_LEVEL == 2)
f32 compZ = z;
#endif
return compZ;
}
/*-----------------------------------------------------------------------------
Triangle testing functions
-----------------------------------------------------------------------------*/
__attribute__((always_inline)) static inline bool TriangleFaceToViewer_f(f32 x0, f32 y0, f32 x1, f32 y1, f32 x2, f32 y2){
f32 winding =
(x1 - x0) * (y2 - y0) - (y1 - y0) * (x2 - x0) ;
// ^ cross product for points with z == 0
return winding >= 0.0f ? true : false;
}
__attribute__((always_inline)) static inline bool TriVisable(u32 r0,u32 r1,u32 r2){
bool returnVal=true;
//u32 nearPlaneCount = 0;
//u32 inSpaceCount = 0;
if(B3L_TEST(r0,B3L_IN_SPACE)||B3L_TEST(r1,B3L_IN_SPACE)||B3L_TEST(r2,B3L_IN_SPACE)){
//test near plan
if (B3L_TEST(r0,B3L_NEAR_PLANE_CLIP)||B3L_TEST(r1,B3L_NEAR_PLANE_CLIP)||B3L_TEST(r2,B3L_NEAR_PLANE_CLIP)){
returnVal= false;
}
}else{
returnVal= false;
}
return returnVal;
}
/*-----------------------------------------------------------------------------
light functions
-----------------------------------------------------------------------------*/
void B3L_ResetLight(light_t *pLight){
B3L_CLR(pLight->state,LIGHT_TYPE_BIT); //parallel light
B3L_VECT3_SET(pLight->lightVect,0.0f,1.0f,0.0f);
pLight->color = 0xFF000000;
pLight->factor_0 = 1.01f; //make sure it is larger than 0, now the range is 0.01~2.01
pLight->factor_1 =126.7f;
}
__attribute__((always_inline)) static inline u32 CalLightFactor(f32 normalDotLight, f32 lightFactor0,f32 lightFactor1){
s32 lightValue;
normalDotLight += lightFactor0;
lightValue =B3L_RoundingToS(normalDotLight*lightFactor1);
lightValue = B3L_MAX(lightValue,0);
#if FRAME_BUFF_COLOR_TYPE == 1
lightValue = lightValue>>4; //only use high 4 bit
#endif
return lightValue;
}
void B3L_SetLightType(render_t *pRender,lightType_e type){
if (parallelLight == type ){
B3L_CLR(pRender->light.state,LIGHT_TYPE_BIT);
}
if (dotLight == type ){
B3L_SET(pRender->light.state,LIGHT_TYPE_BIT);
}
}
void B3L_SetLightVect(render_t *pRender, f32 x,f32 y,f32 z){
if (B3L_TEST(pRender->light.state,LIGHT_TYPE_BIT) == PARALLEL_LIGHT){//parallel light
//normalized the vector
f32 invSqrt = FastInvertSqrt(x*x+y*y+z*z);
x = x*invSqrt;
y = y*invSqrt;
z = z*invSqrt;
}
B3L_VECT3_SET(pRender->light.lightVect,x,y,z);
}
/*-----------------------------------------------------------------------------
Camera functions
-----------------------------------------------------------------------------*/
void B3L_InitCamera(render_t *pRender){
camera_t *pCam = &(pRender->camera);
pCam->aspectRate = DEFAULT_ASPECT_RATIO;
pCam->focalLength = DEFAULT_FOCUS_LENGTH;
B3L_VECT3_SET(pCam->transform.scale,1.0f,1.0f,1.0f);
B3L_VECT3_SET(pCam->transform.translation,0.0f,0.0f,0.0f);
B3L_InitUnitMat3(&(pCam->mat));
B3L_VECT4_SET(pCam->transform.quaternion,0.0f,0.0f,0.0f,1.0f);
pCam->pTrackObj = (B3LObj_t *)NULL;
MakeClipMatrix(pCam->state,pRender->nearPlane,pRender->farPlane,pCam->focalLength,pCam->aspectRate,&(pCam->clipMat));
//printf("after init clip matrix:\n");
//B3L_logMat4(pCam->clipMat);
pCam->state = 0; //default is PERSPECTIVE_PROJECT
}
void B3L_SetOrthographicProject(render_t *pRender){
B3L_SET(pRender->camera.state,B3L_PROJECT_MODE);
B3L_UpdateClipMatrix(pRender);
}
void B3L_SetPerspectiveProject(render_t *pRender){
B3L_CLR(pRender->camera.state,B3L_PROJECT_MODE);
B3L_UpdateClipMatrix(pRender);
}
void B3L_UpdateClipMatrix(render_t *pRender){
camera_t *pCam = &(pRender->camera);
MakeClipMatrix(pCam->state,pRender->nearPlane,pRender->farPlane,pCam->focalLength,pCam->aspectRate,&(pCam->clipMat));
}
void B3L_CamSetFocusLengthByFOV(render_t *pRender, f32 fov){
f32 halfFOV = 0.5f*fov;