-
Notifications
You must be signed in to change notification settings - Fork 2
/
character.h
4595 lines (4272 loc) · 249 KB
/
character.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
// _____ _ _ _
// / ____| | | | /\ | |
// | | | |__| | / \ _ __ __ _ ___| |_ ___ _ __
// | | | __ | / /\ \ | '__/ _` |/ __| __/ _ \ '__|
// | |____| | | |/ ____ \| | | (_| | (__| || __/ |
// \_____|_| |_/_/ \_\_| \__,_|\___|\__\___|_|
//
// https://github.com/Flix01/Header-Only-GL-Helpers
/** MIT License
*
* Copyright (c) 2019 Flix (https://github.com/Flix01/)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
===================
WHAT's THIS:
===================
An header-only, plain C, animated character.
Code should be in ANSI C (-std=c89) with some notable exceptions:
-> single line comments are used.
-> initializer elements can be computable at load time.
-> some C99 math function are used (sinf,cosf,etc.). These can be removed by defining CHA_NO_C99_MATH.
So, to be strict, we should compile the code using -std=c99, but I prefer using -std=gnu89, that allows them.
Also, older versions of MSVC seem to work correctly out of the box with /DCHA_NO_C99_MATH
(even if they're not fully C99 compliant, and/or you can't specify the C dialect to use).
*/
/*
===================
BASIC USAGE:
===================
// Suppose you already have an OpenGL application (that supports the fixed-function-pipeline) working
// Make the following changes, and if everything goes well, you should see something on screen...
// (This is a cut-down version of 'test_character_standalone.c'.
// If you don't support the fixed function pipeline, please see 'test_character.c'.)
... // OpenGL must be available from here
#define CHA_HAS_OPENGL_SUPPORT
#define CHA_USE_VBO // optional: it just creates and uses 'vertex buffer objects' (vbo), instead of glBegin(GL_TRIANGLES)/glEnd()
#define CHA_HINT_USE_FFP_VBO // mandatory if CHA_USE_VBO (otherwise 'vbos' need to know shader attribute locations)
//#define CHA_HINT_USE_VAO // optional
#define CHARACTER_IMPLEMENTATION
#include <character.h>
static struct cha_character_group* group = NULL; // global
void InitGL(void) {
...
Character_Init();
group = Character_CreateGroup(3,3,1.85f,1.75f, 0.f,1,0.f);
...
}
void DestroyGL(void) {
...
if (group) {Character_DestroyGroup(group);group=NULL;}
Character_Destroy();
...
}
void DrawGL(void) {
static float vMatrix[16];
...
chm_Mat4LookAtf(vMatrix,0.f,3.f,15.f,0.f,1.5f,0.f,0.f,1.f,0.f);
//glLoadMatrixf(vMatrix);
//glLightfv(GL_LIGHT0,GL_POSITION,lightDirection); // Important call: the ffp must recalculate internally lightDirectionEyeSpace based on vMatrix [=> every frame]
// optionally move and aninate characters here (see the demo)... and then call
cha_character_group_updateMatrices(&group,1,vMatrix,NULL); // if 'pMatrixNormalizedFrustumPlanes==NULL' there's no frustum culling (you can calculate it in ResizeGL(), see the demo)
glPushMatrix();
glLoadIdentity();
// disable color material if you enabled it
Character_DrawGroupOpengl(&group,1,0);
// reenable it if necessary
glPopMatrix();
...
}
// That's all! Easy? Yes.
*/
/*
===================
A POSSIBLE ROADMAP:
===================
-> Move all structs to the declaration section so that they are exposed.
-> Choose the functions to be exposed and expose them (currently this file works only when the implementation is defined).
-> Decide if we need to merge 'character_inl.h' or not.
-> Should we elaborate some higher-level animation API or not? And how? (I've never used programs like Unity 3D and Unreal Engine, so I don't know what kind of API is better for the user).
-> Add other animations.
-> Add other animation 'tools' (for example to play actions backwards, or to mirror them in the X direction).
-> Remove all unnecessary chm_ math functions to slim file size a bit.
-> CHA_HINT_VERTEX_ATTRIBUTE_LOCATION and CHA_HINT_NORMAL_ATTRIBUTE_LOCATION are const... Must we set them at runtime?
-> consider if we can remove the CHA_BONE_SPACE_GRABBING and use only CHA_BONE_SPACE_ARMATURE
*/
#ifndef CHARACTER_H_
#define CHARACTER_H_
#define CHA_VERSION "0.1 ALPHA"
#define CHA_VERSION_NUM 0004
#ifndef CHA_API_INL
# define CHA_API_INL __inline
#endif
#ifndef CHA_API_DEC
# define CHA_API_DEC extern CHA_API_INL
#endif
#ifndef CHA_API_DEF
# define CHA_API_DEF /* no-op */
#endif
#ifndef CHA_API_PRIV
# define CHA_API_PRIV CHA_API_INL static
#endif
#ifdef CHA_HAS_OPENGL_SUPPORT
# ifndef GL_TEXTURE_2D
# error CHA_HAS_OPENGL_SUPPORT needs access to the OpenGL header files
# endif
# ifdef CHA_USE_VBO
# ifndef CHA_HINT_VERTEX_ATTRIBUTE_LOCATION /* vertex attribute locations should be made configurable at runtime (at init time) */
# define CHA_HINT_VERTEX_ATTRIBUTE_LOCATION 0
# endif
# ifndef CHA_HINT_NORMAL_ATTRIBUTE_LOCATION
# define CHA_HINT_NORMAL_ATTRIBUTE_LOCATION 1
# endif
# else /*CHA_USE_VBO*/
# undef CHA_HINT_USE_VAO
# undef CHA_HINT_USE_FFP_VBO
# endif /*CHA_USE_VBO*/
#else /*CHA_HAS_OPENGL_SUPPORT*/
# ifdef CHA_USE_VBO
# undef CHA_USE_VBO
# endif
#endif /*CHA_HAS_OPENGL_SUPPORT*/
#ifdef CHA_USE_DOUBLE_PRECISION
# undef CHA_DOUBLE_PRECISION
# define CHA_DOUBLE_PRECISION
#endif
#ifdef CHA_DOUBLE_PRECISION /* it affects mMatrices (and vMatrices) only. All the rest stays 'float'. 'choat' is CHaracter_flOAT */
# undef CHA_USE_DOUBLE_PRECISION
# define CHA_USE_DOUBLE_PRECISION
typedef double choat;
#else
typedef float choat;
#endif
#ifndef CHA_RESTRICT
# ifdef __restrict
# define CHA_RESTRICT __restrict
# else
# define CHA_RESTRICT /*no-op*/
# endif
#endif
#if (defined (NDEBUG) || defined (_NDEBUG))
# undef CHA_NO_ASSERT
# define CHA_NO_ASSERT
# undef CHA_NO_STDIO
# define CHA_NO_STDIO
# undef CHA_NO_STDLIB
# define CHA_NO_STDLIB
#endif
CHA_API_DEC void Character_Init(void);
CHA_API_DEC void Character_Destroy(void);
/* Last 3 arguments are:
'random_scaling_fraction' // if >0.f adds a small uniform scale variability. [suggested value: 0.0115f that scales +-2cm on a base scale of 1.75m (not very noticeable)]
'add_some_optional_meshes' // [0 or 1] aads some hats, glasses, etc.
'random_vertical_stretching_fraction_experimental' // if >0.f makes some characters a bit taller. [suggested value: 0.f, but if you really want to try it use 0.04f (not very noticeable)].
// Experimental: some artifacts can appear. USE IT AT YOUR OWN RISK.
*/
CHA_API_DEC struct cha_character_group* Character_CreateGroup(int num_men,int num_ladies,float men_scaling,float ladies_scaling,float random_scaling_fraction/*=0.f*/,int add_some_optional_meshes/*=1*/,float random_vertical_stretching_fraction_experimental/*=0.f*/);
CHA_API_DEC void Character_DestroyGroup(struct cha_character_group* p);
#if (defined(CHA_HAS_OPENGL_SUPPORT) && (!defined(CHA_USE_VBO) || defined(CHA_HINT_USE_FFP_VBO)))
CHA_API_DEC void Character_DrawGroupOpengl(struct cha_character_group*const* pp,int num_group_pointers,int no_materials/*=0*/);
#endif
#include "character_inl.h"
#ifndef CHA_NO_CHARACTER_INSTANCE_NAMES
enum ChaCharacterInstanceManNameEnum {
CHA_CHARACTER_INSTANCE_MAN_NAME_PETER=0, CHA_CHARACTER_INSTANCE_MAN_NAME_JOHN, CHA_CHARACTER_INSTANCE_MAN_NAME_ANDREW, CHA_CHARACTER_INSTANCE_MAN_NAME_JAMES, CHA_CHARACTER_INSTANCE_MAN_NAME_PHILIP,
CHA_CHARACTER_INSTANCE_MAN_NAME_THOMAS, CHA_CHARACTER_INSTANCE_MAN_NAME_MATTHEW,CHA_CHARACTER_INSTANCE_MAN_NAME_SIMON, CHA_CHARACTER_INSTANCE_MAN_NAME_BARTHOLOMEW,CHA_CHARACTER_INSTANCE_MAN_NAME_JUDE,
CHA_CHARACTER_INSTANCE_MAN_NAME_MATTHIAS, CHA_CHARACTER_INSTANCE_MAN_NAME_JOSEPH, CHA_CHARACTER_INSTANCE_MAN_NAME_PAUL, CHA_CHARACTER_INSTANCE_MAN_NAME_STEPHEN, CHA_CHARACTER_INSTANCE_MAN_NAME_FRANCIS,
CHA_CHARACTER_INSTANCE_MAN_NAME_BENEDICT, CHA_CHARACTER_INSTANCE_MAN_NAME_JOACHIM,CHA_CHARACTER_INSTANCE_MAN_NAME_CHARLES,CHA_CHARACTER_INSTANCE_MAN_NAME_AUGUSTINE, CHA_CHARACTER_INSTANCE_MAN_NAME_DOMINIC,
CHA_CHARACTER_INSTANCE_MAN_NAME_COUNT
};
enum ChaCharacterInstanceLadyNameEnum {
CHA_CHARACTER_INSTANCE_LADY_NAME_MARY=0, CHA_CHARACTER_INSTANCE_LADY_NAME_ANNE, CHA_CHARACTER_INSTANCE_LADY_NAME_MARTHA, CHA_CHARACTER_INSTANCE_LADY_NAME_SUSAN, CHA_CHARACTER_INSTANCE_LADY_NAME_JOAN,
CHA_CHARACTER_INSTANCE_LADY_NAME_LUCY, CHA_CHARACTER_INSTANCE_LADY_NAME_CATHERINE, CHA_CHARACTER_INSTANCE_LADY_NAME_MARGARET, CHA_CHARACTER_INSTANCE_LADY_NAME_ELIZABETH, CHA_CHARACTER_INSTANCE_LADY_NAME_THERESA,
CHA_CHARACTER_INSTANCE_LADY_NAME_RITA, CHA_CHARACTER_INSTANCE_LADY_NAME_MONICA, CHA_CHARACTER_INSTANCE_LADY_NAME_RACHEL, CHA_CHARACTER_INSTANCE_LADY_NAME_ROSE, CHA_CHARACTER_INSTANCE_LADY_NAME_HELENA,
CHA_CHARACTER_INSTANCE_LADY_NAME_BARBARA, CHA_CHARACTER_INSTANCE_LADY_NAME_BRIDGET, CHA_CHARACTER_INSTANCE_LADY_NAME_CHRISTINE, CHA_CHARACTER_INSTANCE_LADY_NAME_ALEXANDRA, CHA_CHARACTER_INSTANCE_LADY_NAME_PRISCILLA,
CHA_CHARACTER_INSTANCE_LADY_NAME_COUNT
};
#endif
#include <string.h> // memcpy
#include <math.h>
#ifndef CHA_NO_STDIO
# include <stdio.h> /*fprintf,printf,stderr,FILE*/
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef M_HALF_PI
#define M_HALF_PI (M_PI/2.0)
#endif
#ifndef M_PIOVER180
#define M_PIOVER180 (3.14159265358979323846/180.0)
#endif
#ifndef M_180OVERPI
#define M_180OVERPI (180.0/3.14159265358979323846)
#endif
#ifdef CHA_NO_C99_MATH
# define sqrtf(X) ((float)sqrt(X))
# define floorf(X) ((float)floor(X))
# define ceilf(X) ((float)ceil(X))
# define tanf(X) ((float)tan(X))
# define sinf(X) ((float)sin(X))
# define cosf(X) ((float)cos(X))
# define fabsf(X) ((float)abs(X))
# define atan2f(X,Y) ((float)atan2(X,Y))
# define fmodf(X,Y) ((float)fmod(X,Y))
#endif
#ifdef CHA_USE_MATH_MACROS
#ifndef CHA_DOUBLE_PRECISION
# define chm_sin(X) sinf(X)
# define chm_cos(X) cosf(X)
# define chm_Round(X) chm_Roundf(X)
# define chm_Vec3Dot(X,Y) chm_Vec3Dotf(X,Y)
# define chm_Vec3Normalize(X) chm_Vec3Normalizef(X)
# define chm_Vec3Cross(X,Y,Z) chm_Vec3Crossf(X,Y,Z)
# define chm_Vec3DistSquared(X,Y) chm_Vec3DistSquaredf(X,Y)
# define chm_Vec3Dist(X,Y) chm_Vec3Distf(X,Y)
# define chm_Mat4Identity(X) chm_Mat4Identityf(X)
# define chm_Mat4ClearRotation(X) chm_Mat4ClearRotationf(X)
# define chm_Mat4Copy(X,Y) chm_Mat4Copyf(X,Y)
# define chm_Mat4MulUncheckArgs(X,Y,Z) chm_Mat4MulUncheckArgsf(X,Y,Z)
# define chm_Mat4Mul(X,Y,Z) chm_Mat4Mulf(X,Y,Z)
# define chm_Mat4MulDir(M,DO,X,Y,Z) chm_Mat4MulDirf(M,DO,X,Y,Z)
# define chm_Mat4MulPos(M,PO,X,Y,Z) chm_Mat4MulPosf(M,PO,X,Y,Z)
# define chm_Mat4LookAt(M,EX,EY,EZ,CX,CY,CZ,UX,UY,UZ) chm_Mat4LookAtf(M,EX,EY,EZ,CX,CY,CZ,UX,UY,UZ)
# define chm_Mat4Perspective(M,DFY,AR,N,F) chm_Mat4Perspectivef(M,DFY,AR,N,F)
# define chm_Mat4Ortho(M,L,R,B,T,N,F) chm_Mat4Orthof((M,L,R,B,T,N,F)
# define chm_Mat4Ortho3D(M,D,DF,AR,N,F) chm_Mat4Ortho3Df(M,D,DF,AR,N,F)
# define chm_Mat4Translate(M,X,Y,Z) chm_Mat4Translatef(M,X,Y,Z)
# define chm_Mat4Rotate(M,DA,X,Y,Z) chm_Mat4Rotatef(M,DA,X,Y,Z)
# define chm_Mat4Scale(M,X,Y,Z) chm_Mat4Scalef(M,X,Y,Z)
# define chm_Mat4Invert(X,Y) chm_Mat4Invertf(X,Y)
# define chm_Mat4InvertTransformMatrix(X,Y) chm_Mat4InvertTransformMatrixf(X,Y)
# define chm_Mat4InvertTransformMatrixFast(X,Y) chm_Mat4InvertTransformMatrixFastf(X,Y)
# define chm_PlaneNormalize(P) chm_PlaneNormalizef(P)
# define chm_GetFrustumPlaneEquations(PE,M,N) chm_GetFrustumPlaneEquationsf(PE,M,N)
# define chm_IsOBBVisible(PE,M,AMINX,AMINY,AMINZ,AMAXX,AMAXY,AMAXZ) chm_IsOBBVisiblef(PE,M,AMINX,AMINY,AMINZ,AMAXX,AMAXY,AMAXZ)
# define chm_GetFrustumPoints(FP,MINV) chm_GetFrustumPointsf(FP,MINV)
# define chm_GetFrustumAabbCenterAndHalfExtents(CO,HEO,FP) chm_GetFrustumAabbCenterAndHalfExtentsf(CO,HEO,FP)
# define chm_GetFrustumRadiusAndCenterDistance(CDO,N,F,FD,AR,CTDN) chm_GetFrustumRadiusAndCenterDistancef(CDO,N,F,FD,AR,CTDN)
# define chm_GetFrustumCenterFromCenterDistance(CO,VINV,FDO) chm_GetFrustumCenterFromCenterDistancef(CO,VINV,FDO)
# define chm_GetLightViewProjectionMatrixExtra(LVPMO,VINV,C,R,LD,W,H,OPINV,OVO,OFPO,OLVPMOFC) chm_GetLightViewProjectionMatrixExtraf(LVPMO,VINV,C,R,LD,W,H,OPINV,OVO,OFPO,OLVPMOFC)
# define chm_GetLightViewProjectionMatrix(LVPMO,VINV,C,R,LD,W,H) chm_GetLightViewProjectionMatrixf(LVPMO,VINV,C,R,LD,W,H)
# ifndef CHA_NO_STDIO
# define chm_Mat4_fprintp(M,F,W,P) chm_Mat4_fprintpf(M,F,W,P)
# define chm_Mat4_fprint(M,F) chm_Mat4_fprintf(M,F)
# define chm_Mat4_printp(M,W,P) chm_Mat4_printpf(M,W,P)
# define chm_Mat4_print(M) chm_Mat4_printf(M)
# endif
#else /*CHA_DOUBLE_PRECISION*/
# define chm_sin(X) sin(X)
# define chm_cos(X) cos(X)
# define chm_Round(X) chm_Roundd(X)
# define chm_Vec3Dot(X,Y) chm_Vec3Dotd(X,Y)
# define chm_Vec3Normalize(X) chm_Vec3Normalized(X)
# define chm_Vec3Cross(X,Y,Z) chm_Vec3Crossd(X,Y,Z)
# define chm_Vec3DistSquared(X,Y) chm_Vec3DistSquaredd(X,Y)
# define chm_Vec3Dist(X,Y) chm_Vec3Distd(X,Y)
# define chm_Mat4Identity(X) chm_Mat4Identityd(X)
# define chm_Mat4ClearRotation(X) chm_Mat4ClearRotationd(X)
# define chm_Mat4Copy(X,Y) chm_Mat4Copyd(X,Y)
# define chm_Mat4MulUncheckArgs(X,Y,Z) chm_Mat4MulUncheckArgsd(X,Y,Z)
# define chm_Mat4Mul(X,Y,Z) chm_Mat4Muld(X,Y,Z)
# define chm_Mat4MulDir(M,DO,X,Y,Z) chm_Mat4MulDird(M,DO,X,Y,Z)
# define chm_Mat4MulPos(M,PO,X,Y,Z) chm_Mat4MulPosd(M,PO,X,Y,Z)
# define chm_Mat4LookAt(M,EX,EY,EZ,CX,CY,CZ,UX,UY,UZ) chm_Mat4LookAtd(M,EX,EY,EZ,CX,CY,CZ,UX,UY,UZ)
# define chm_Mat4Perspective(M,DFY,AR,N,F) chm_Mat4Perspectived(M,DFY,AR,N,F)
# define chm_Mat4Ortho(M,L,R,B,T,N,F) chm_Mat4Orthod((M,L,R,B,T,N,F)
# define chm_Mat4Ortho3D(M,D,DF,AR,N,F) chm_Mat4Ortho3Dd(M,D,DF,AR,N,F)
# define chm_Mat4Translate(M,X,Y,Z) chm_Mat4Translated(M,X,Y,Z)
# define chm_Mat4Rotate(M,DA,X,Y,Z) chm_Mat4Rotated(M,DA,X,Y,Z)
# define chm_Mat4Scale(M,X,Y,Z) chm_Mat4Scaled(M,X,Y,Z)
# define chm_Mat4Invert(X,Y) chm_Mat4Invertd(X,Y)
# define chm_Mat4InvertTransformMatrix(X,Y) chm_Mat4InvertTransformMatrixd(X,Y)
# define chm_Mat4InvertTransformMatrixFast(X,Y) chm_Mat4InvertTransformMatrixFastd(X,Y)
# define chm_PlaneNormalize(P) chm_PlaneNormalized(P)
# define chm_GetFrustumPlaneEquations(PE,M,N) chm_GetFrustumPlaneEquationsd(PE,M,N)
# define chm_IsOBBVisible(PE,M,AMINX,AMINY,AMINZ,AMAXX,AMAXY,AMAXZ) chm_IsOBBVisibled(PE,M,AMINX,AMINY,AMINZ,AMAXX,AMAXY,AMAXZ)
# define chm_GetFrustumPoints(FP,MINV) chm_GetFrustumPointsd(FP,MINV)
# define chm_GetFrustumAabbCenterAndHalfExtents(CO,HEO,FP) chm_GetFrustumAabbCenterAndHalfExtentsd(CO,HEO,FP)
# define chm_GetFrustumRadiusAndCenterDistance(CDO,N,F,FD,AR,CTDN) chm_GetFrustumRadiusAndCenterDistanced(CDO,N,F,FD,AR,CTDN)
# define chm_GetFrustumCenterFromCenterDistance(CO,VINV,FDO) chm_GetFrustumCenterFromCenterDistanced(CO,VINV,FDO)
# define chm_GetLightViewProjectionMatrixExtra(LVPMO,VINV,C,R,LD,W,H,OPINV,OVO,OFPO,OLVPMOFC) chm_GetLightViewProjectionMatrixExtrad(LVPMO,VINV,C,R,LD,W,H,OPINV,OVO,OFPO,OLVPMOFC)
# define chm_GetLightViewProjectionMatrix(LVPMO,VINV,C,R,LD,W,H) chm_GetLightViewProjectionMatrixd(LVPMO,VINV,C,R,LD,W,H)
# ifndef CHA_NO_STDIO
# define chm_Mat4_fprintp(M,F,W,P) chm_Mat4_fprintpd(M,F,W,P)
# define chm_Mat4_fprint(M,F) chm_Mat4_fprintd(M,F)
# define chm_Mat4_printp(M,W,P) chm_Mat4_printpd(M,W,P)
# define chm_Mat4_print(M) chm_Mat4_printd(M)
# endif
#endif /*CHA_DOUBLE_PRECISION*/
#endif //CHA_NO_MATH_MACROS
CHA_API_INL float chm_Vec3Dotf(const float* a3,const float* b3) {return a3[0]*b3[0]+a3[1]*b3[1]+a3[2]*b3[2];}
CHA_API_INL double chm_Vec3Dotd(const double* a3,const double* b3) {return a3[0]*b3[0]+a3[1]*b3[1]+a3[2]*b3[2];}
CHA_API_INL void chm_Vec3Normalizef(float* CHA_RESTRICT v3) {
float len = chm_Vec3Dotf(v3,v3);int i;
if (len!=0) {len = sqrt(len);for (i=0;i<3;i++) v3[i]/=len;}
}
CHA_API_INL void chm_Vec3Normalized(double* CHA_RESTRICT v3) {
double len = chm_Vec3Dotd(v3,v3);int i;
if (len!=0) {len = sqrt(len);for (i=0;i<3;i++) v3[i]/=len;}
}
CHA_API_INL void chm_Vec3Crossf(float* CHA_RESTRICT vOut3,const float* CHA_RESTRICT a3,const float* CHA_RESTRICT b3) {
vOut3[0] = a3[1] * b3[2] - a3[2] * b3[1];
vOut3[1] = a3[2] * b3[0] - a3[0] * b3[2];
vOut3[2] = a3[0] * b3[1] - a3[1] * b3[0];
}
CHA_API_INL void chm_Vec3Crossd(double* CHA_RESTRICT vOut3,const double* CHA_RESTRICT a3,const double* CHA_RESTRICT b3) {
vOut3[0] = a3[1] * b3[2] - a3[2] * b3[1];
vOut3[1] = a3[2] * b3[0] - a3[0] * b3[2];
vOut3[2] = a3[0] * b3[1] - a3[1] * b3[0];
}
CHA_API_INL float chm_Vec3DistSquaredf(const float* CHA_RESTRICT a3,const float* CHA_RESTRICT b3) {
const float rv[3] = {b3[0]-a3[0],b3[1]-a3[1],b3[2]-a3[2]};
return rv[0]*rv[0] + rv[1]*rv[1] + rv[2]*rv[2];
}
CHA_API_INL double chm_Vec3DistSquaredd(const double* CHA_RESTRICT a3,const double* CHA_RESTRICT b3) {
const double rv[3] = {b3[0]-a3[0],b3[1]-a3[1],b3[2]-a3[2]};
return rv[0]*rv[0] + rv[1]*rv[1] + rv[2]*rv[2];
}
CHA_API_INL float chm_Vec3Distf(const float* CHA_RESTRICT a3,const float* CHA_RESTRICT b3) {
const float res = chm_Vec3DistSquaredf(a3,b3);
return res!=0 ? sqrtf(res) : 0;
}
CHA_API_INL double chm_Vec3Distd(const double* CHA_RESTRICT a3,const double* CHA_RESTRICT b3) {
const double res = chm_Vec3DistSquaredd(a3,b3);
return res!=0 ? sqrt(res) : 0;
}
CHA_API_INL float chm_Roundf(float number) {return number < 0.0f ? ceilf(number - 0.5f) : floorf(number + 0.5f);}
CHA_API_INL float chm_Roundd(double number) {return number < 0.0 ? ceil(number - 0.5) : floor(number + 0.5);}
CHA_API_INL float* chm_Mat4Identityf(float* CHA_RESTRICT result16) {float* m = result16;m[0]=m[5]=m[10]=m[15]=1.f;m[1]=m[2]=m[3]=m[4]=m[6]=m[7]=m[8]=m[9]=m[11]=m[12]=m[13]=m[14]=0.f;return result16;}
CHA_API_INL double* chm_Mat4Identityd(double* CHA_RESTRICT result16) {double* m = result16;m[0]=m[5]=m[10]=m[15]=1.0;m[1]=m[2]=m[3]=m[4]=m[6]=m[7]=m[8]=m[9]=m[11]=m[12]=m[13]=m[14]=0.0;return result16;}
CHA_API_INL float* chm_Mat4ClearRotationf(float* CHA_RESTRICT result16) {float* m = result16;m[0]=m[5]=m[10]=m[15]=1.f;m[1]=m[2]=m[3]=m[4]=m[6]=m[7]=m[8]=m[9]=m[11]=0.f;return result16;}
CHA_API_INL double* chm_Mat4ClearRotationd(double* CHA_RESTRICT result16) {double* m = result16;m[0]=m[5]=m[10]=m[15]=1.0;m[1]=m[2]=m[3]=m[4]=m[6]=m[7]=m[8]=m[9]=m[11]=0.0;return result16;}
CHA_API_INL void chm_Mat4Copyf(float* CHA_RESTRICT dst16,const float* CHA_RESTRICT src16) {memcpy(dst16,src16,16*sizeof(float));}
CHA_API_INL void chm_Mat4Copyd(double* CHA_RESTRICT dst16,const double* CHA_RESTRICT src16) {memcpy(dst16,src16,16*sizeof(double));}
CHA_API_DEC float* chm_Mat4MulUncheckArgsf(float* CHA_RESTRICT result16,const float* CHA_RESTRICT ml16,const float* CHA_RESTRICT mr16);
CHA_API_DEC double* chm_Mat4MulUncheckArgsd(double* CHA_RESTRICT result16,const double* CHA_RESTRICT ml16,const double* CHA_RESTRICT mr16);
CHA_API_DEC float* chm_Mat4Mulf(float* CHA_RESTRICT result16,const float* CHA_RESTRICT ml16,const float* CHA_RESTRICT mr16);
CHA_API_DEC double* chm_Mat4Muld(double* CHA_RESTRICT result16,const double* CHA_RESTRICT ml16,const double* CHA_RESTRICT mr16);
CHA_API_INL void chm_Mat4MulDirf(const float* CHA_RESTRICT m16,float* CHA_RESTRICT dirOut3,const float dirX,float dirY,float dirZ) {
dirOut3[0] = dirX*m16[0] + dirY*m16[4] + dirZ*m16[8];
dirOut3[1] = dirX*m16[1] + dirY*m16[5] + dirZ*m16[9];
dirOut3[2] = dirX*m16[2] + dirY*m16[6] + dirZ*m16[10];
}
CHA_API_INL void chm_Mat4MulDird(const double* CHA_RESTRICT m16,double* CHA_RESTRICT dirOut3,const double dirX,double dirY,double dirZ) {
dirOut3[0] = dirX*m16[0] + dirY*m16[4] + dirZ*m16[8];
dirOut3[1] = dirX*m16[1] + dirY*m16[5] + dirZ*m16[9];
dirOut3[2] = dirX*m16[2] + dirY*m16[6] + dirZ*m16[10];
}
CHA_API_INL void chm_Mat4MulPosf(const float* CHA_RESTRICT m16,float* CHA_RESTRICT posOut3,const float posX,float posY,float posZ) {
posOut3[0] = posX*m16[0] + posY*m16[4] + posZ*m16[8] + m16[12];
posOut3[1] = posX*m16[1] + posY*m16[5] + posZ*m16[9] + m16[13];
posOut3[2] = posX*m16[2] + posY*m16[6] + posZ*m16[10]+ m16[14];
}
CHA_API_INL void chm_Mat4MulPosd(const double* CHA_RESTRICT m16,double* CHA_RESTRICT posOut3,const double posX,double posY,double posZ) {
posOut3[0] = posX*m16[0] + posY*m16[4] + posZ*m16[8] + m16[12];
posOut3[1] = posX*m16[1] + posY*m16[5] + posZ*m16[9] + m16[13];
posOut3[2] = posX*m16[2] + posY*m16[6] + posZ*m16[10]+ m16[14];
}
CHA_API_DEC void chm_Mat4LookAtf(float* CHA_RESTRICT mOut16,float eyeX,float eyeY,float eyeZ,float centerX,float centerY,float centerZ,float upX,float upY,float upZ);
CHA_API_DEC void chm_Mat4LookAtd(double* CHA_RESTRICT mOut16,double eyeX,double eyeY,double eyeZ,double centerX,double centerY,double centerZ,double upX,double upY,double upZ);
CHA_API_DEC void chm_Mat4Perspectivef(float* CHA_RESTRICT mOut16,float degfovy,float aspect, float zNear, float zFar);
CHA_API_DEC void chm_Mat4Perspectived(double* CHA_RESTRICT mOut16,double degfovy,double aspect, double zNear, double zFar);
CHA_API_DEC void chm_Mat4Orthof(float* CHA_RESTRICT mOut16,float left,float right, float bottom, float top,float nearVal,float farVal);
CHA_API_DEC void chm_Mat4Orthod(double* CHA_RESTRICT mOut16,double left,double right, double bottom, double top,double nearVal,double farVal);
CHA_API_DEC void chm_Mat4Ortho3Df(float* CHA_RESTRICT mOut16,float cameraTargetDistance,float degfovy,float aspect,float znear,float zfar);
CHA_API_DEC void chm_Mat4Ortho3Dd(double* CHA_RESTRICT mOut16,double cameraTargetDistance,double degfovy,double aspect,double znear,double zfar);
CHA_API_INL float* chm_Mat4Translatef(float* CHA_RESTRICT mInOut16,float x,float y,float z) {int i;for (i=0;i<3;i++) mInOut16[12+i]+=mInOut16[i]*x+mInOut16[4+i]*y+mInOut16[8+i]*z;return mInOut16;}
CHA_API_INL double* chm_Mat4Translated(double* CHA_RESTRICT mInOut16,double x,double y,double z) {int i;for (i=0;i<3;i++) mInOut16[12+i]+=mInOut16[i]*x+mInOut16[4+i]*y+mInOut16[8+i]*z;return mInOut16;}
CHA_API_DEC float* chm_Mat4Rotatef(float* CHA_RESTRICT mInOut16,float degAngle,float x,float y,float z);
CHA_API_DEC double* chm_Mat4Rotated(double* CHA_RESTRICT mInOut16,double degAngle,double x,double y,double z);
CHA_API_INL float* chm_Mat4Scalef(float* CHA_RESTRICT mInOut16,float x,float y,float z) {int i;for (i=0;i<3;i++) {mInOut16[i]*=x;mInOut16[4+i]*=y;mInOut16[8+i]*=z;} return mInOut16;}
CHA_API_INL double* chm_Mat4Scaled(double* CHA_RESTRICT mInOut16,double x,double y,double z) {int i;for (i=0;i<3;i++) {mInOut16[i]*=x;mInOut16[4+i]*=y;mInOut16[8+i]*=z;} return mInOut16;}
CHA_API_DEC int chm_Mat4Invertf(float* CHA_RESTRICT mOut16,const float* CHA_RESTRICT m16);
CHA_API_DEC int chm_Mat4Invertd(double* CHA_RESTRICT mOut16,const double* CHA_RESTRICT m16);
CHA_API_DEC void chm_Mat4InvertTransformMatrixf(float* CHA_RESTRICT mOut16,const float* CHA_RESTRICT m16);
CHA_API_DEC void chm_Mat4InvertTransformMatrixd(double* CHA_RESTRICT mOut16,const double* CHA_RESTRICT m16);
CHA_API_DEC void chm_Mat4InvertTransformMatrixFastf(float* CHA_RESTRICT mOut16,const float* CHA_RESTRICT m16);
CHA_API_DEC void chm_Mat4InvertTransformMatrixFastd(double* CHA_RESTRICT mOut16,const double* CHA_RESTRICT m16);
CHA_API_INL void chm_Mat4Convertd2f(float* CHA_RESTRICT result16,const double* CHA_RESTRICT m16) {int i;for(i = 0; i < 16; i++) result16[i]=(float)m16[i];}
CHA_API_INL void chm_Mat4Convertf2d(double* CHA_RESTRICT result16,const float* CHA_RESTRICT m16) {int i;for(i = 0; i < 16; i++) result16[i]=(double)m16[i];}
CHA_API_DEC void chm_PlaneNormalizef(float* CHA_RESTRICT p4);
CHA_API_DEC void chm_PlaneNormalized(double* CHA_RESTRICT p4);
CHA_API_DEC void chm_GetFrustumPlaneEquationsf(float planeEquationsOut[6][4],const float* CHA_RESTRICT vpMatrix16,int normalizePlanes);
CHA_API_DEC void chm_GetFrustumPlaneEquationsd(double planeEquationsOut[6][4],const double* CHA_RESTRICT vpMatrix16,int normalizePlanes);
CHA_API_DEC int chm_IsOBBVisiblef(const float frustumPlanes[6][4],const float* CHA_RESTRICT mfMatrix16,float aabbMinX,float aabbMinY,float aabbMinZ,float aabbMaxX,float aabbMaxY,float aabbMaxZ);
CHA_API_DEC int chm_IsOBBVisibled(const double frustumPlanes[6][4],const double* CHA_RESTRICT mfMatrix16,double aabbMinX,double aabbMinY,double aabbMinZ,double aabbMaxX,double aabbMaxY,double aabbMaxZ);
// shadow map helpers
CHA_API_DEC void chm_GetFrustumPointsf(float frustumPoints[8][4],const float* CHA_RESTRICT vpMatrixInverse16);
CHA_API_DEC void chm_GetFrustumPointsd(double frustumPoints[8][4],const double* CHA_RESTRICT vpMatrixInverse16);
CHA_API_DEC void chm_GetFrustumAabbCenterAndHalfExtentsf(float* CHA_RESTRICT frustumCenterOut3,float* CHA_RESTRICT frustumHalfExtentsOut3,const float frustumPoints[8][4]);
CHA_API_DEC void chm_GetFrustumAabbCenterAndHalfExtentsd(double* CHA_RESTRICT frustumCenterOut3,double* CHA_RESTRICT frustumHalfExtentsOut3,const double frustumPoints[8][4]);
CHA_API_DEC float chm_GetFrustumRadiusAndCenterDistancef(float* CHA_RESTRICT pFrustumCenterDistanceOut,float cameraNearClippingPlane,float cameraFarClippingPlane,float cameraFovyDeg,float cameraAspectRatio,float cameraTargetDistanceForUnstableOrtho3DModeOnly_or_zero);
CHA_API_DEC double chm_GetFrustumRadiusAndCenterDistanced(double* CHA_RESTRICT pFrustumCenterDistanceOut,double cameraNearClippingPlane,double cameraFarClippingPlane,double cameraFovyDeg,double cameraAspectRatio,double cameraTargetDistanceForUnstableOrtho3DModeOnly_or_zero);
CHA_API_DEC void chm_GetFrustumCenterFromCenterDistancef(float* CHA_RESTRICT frustumCenterOut3,const float* CHA_RESTRICT cameraVMatrixInverse16,float frustumCenterDistance);
CHA_API_DEC void chm_GetFrustumCenterFromCenterDistanced(double* CHA_RESTRICT frustumCenterOut3,const double* CHA_RESTRICT cameraVMatrixInverse16,double frustumCenterDistance);
CHA_API_DEC void chm_GetLightViewProjectionMatrixExtraf(float* CHA_RESTRICT lvpMatrixOut16
,const float* CHA_RESTRICT cameraVMatrixInverse16,const float* CHA_RESTRICT cameraFrustumCenterInWorldSpace,float cameraFrustumRadius
,const float* CHA_RESTRICT normalizedLightDirection3,unsigned short shadowMapWidth,unsigned short shadowMapHeight
,const float* CHA_RESTRICT optionalCameraPMatrixInverse16
,float* CHA_RESTRICT optionalLightViewportClippingOut4,float optionalCameraFrustumPointsInNDCLightSpaceOut[8][4]
,float* CHA_RESTRICT optionalLVPMatrixForFrustumCullingUsageOut16 // Highly experimental and untested
);
CHA_API_DEC void chm_GetLightViewProjectionMatrixExtrad(double* CHA_RESTRICT lvpMatrixOut16
,const double* CHA_RESTRICT cameraVMatrixInverse16,const double* CHA_RESTRICT cameraFrustumCenterInWorldSpace,double cameraFrustumRadius
,const double* CHA_RESTRICT normalizedLightDirection3,unsigned short shadowMapWidth,unsigned short shadowMapHeight
,const double* CHA_RESTRICT optionalCameraPMatrixInverse16
,double* CHA_RESTRICT optionalLightViewportClippingOut4,double optionalCameraFrustumPointsInNDCLightSpaceOut[8][4]
,double* CHA_RESTRICT optionalLVPMatrixForFrustumCullingUsageOut16 // Highly experimental and untested
);
CHA_API_DEC void chm_GetLightViewProjectionMatrixf(float* CHA_RESTRICT lvpMatrixOut16
,const float* CHA_RESTRICT cameraVMatrixInverse16,const float* CHA_RESTRICT cameraFrustumCenterInWorldSpace,float cameraFrustumRadius
,const float* CHA_RESTRICT normalizedLightDirection3,unsigned short shadowMapWidth,unsigned short shadowMapHeight);
CHA_API_DEC void chm_GetLightViewProjectionMatrixd(double* CHA_RESTRICT lvpMatrixOut16
,const double* CHA_RESTRICT cameraVMatrixInverse16,const double* CHA_RESTRICT cameraFrustumCenterInWorldSpace,double cameraFrustumRadius
,const double* CHA_RESTRICT normalizedLightDirection3,unsigned short shadowMapWidth,unsigned short shadowMapHeight);
// end shadow map helpers
//---------------- single-precision-only math -------------------------------------------------------------
CHA_API_INL void chm_Mat4MulPosWithWDivisionf(const float* CHA_RESTRICT m16,float* CHA_RESTRICT posOut3,const float posX,float posY,float posZ) {
float w;
posOut3[0] = posX*m16[0] + posY*m16[4] + posZ*m16[8] + m16[12];
posOut3[1] = posX*m16[1] + posY*m16[5] + posZ*m16[9] + m16[13];
posOut3[2] = posX*m16[2] + posY*m16[6] + posZ*m16[10]+ m16[14];
w = posX*m16[3] + posY*m16[7] + posZ*m16[11]+ m16[15];
if (w!=0 && w!=1) {posOut3[0]/=w;posOut3[1]/=w;posOut3[2]/=w;}
}
CHA_API_DEC int chm_UnProject_MvpMatrixInvf(float winX,float winY,float winZ,const float* CHA_RESTRICT mvpMatrixInv16,const int* viewport4,float* objX,float* objY,float* objZ);
CHA_API_DEC void chm_GetRayFromMouseCoordsf(float* rayOriginOut3,float* rayDirOut3,int mouseX,int mouseY,const float* vpMatrixInv,const int* viewport4);
CHA_API_INL float* chm_Mat4Sumf(float* CHA_RESTRICT result16,const float* CHA_RESTRICT ml16,const float* CHA_RESTRICT mr16) {int i;for(i=0;i<16;i++) {result16[i]=ml16[i]+mr16[i];}return result16;}
CHA_API_INL float* chm_Mat4SumInPlacef(float* CHA_RESTRICT mInOut16,const float* CHA_RESTRICT m16) {int i;for(i=0;i<16;i++) {mInOut16[i]+=m16[i];}return mInOut16;}
CHA_API_INL float* chm_Mat4Subf(float* CHA_RESTRICT result16,const float* CHA_RESTRICT ml16,const float* CHA_RESTRICT mr16) {int i;for(i=0;i<16;i++) {result16[i]=ml16[i]-mr16[i];}return result16;}
CHA_API_INL float* chm_Mat4SubInPlacef(float* CHA_RESTRICT mInOut16,const float* CHA_RESTRICT m16) {int i;for(i=0;i<16;i++) {mInOut16[i]-=m16[i];}return mInOut16;}
CHA_API_INL float* chm_Mat4FMAf(float* CHA_RESTRICT result16,const float* CHA_RESTRICT ml16,const float* CHA_RESTRICT mr16,float sca) {int i;for(i=0;i<16;i++) {result16[i]=ml16[i]+mr16[i]*sca;}return result16;}
CHA_API_INL float* chm_Mat4FMAInPlacef(float* CHA_RESTRICT mInOut16,const float* CHA_RESTRICT m16,float sca) {int i;for(i=0;i<16;i++) {mInOut16[i]+=m16[i]*sca;}return mInOut16;}
#ifndef CHA_NO_STDIO
CHA_API_DEC void chm_Mat4_fprintpf(const float* CHA_RESTRICT m16,FILE* stream,int width, int precision);
CHA_API_DEC void chm_Mat4_fprintf(const float* CHA_RESTRICT m16,FILE* stream);
CHA_API_DEC void chm_Mat4_printpf(const float* CHA_RESTRICT m16,int width, int precision);
CHA_API_DEC void chm_Mat4_printf(const float* CHA_RESTRICT m16);
CHA_API_DEC void chm_Mat4_fprintpd(const double* CHA_RESTRICT m16,FILE* stream,int width, int precision);
CHA_API_DEC void chm_Mat4_fprintd(const double* CHA_RESTRICT m16,FILE* stream);
CHA_API_DEC void chm_Mat4_printpd(const double* CHA_RESTRICT m16,int width, int precision);
CHA_API_DEC void chm_Mat4_printd(const double* CHA_RESTRICT m16);
#endif
#ifndef CHA_SLERP_EPSILON
# define CHA_SLERP_EPSILON (0.0001f)
#endif //CHA_SLERP_EPSILON
CHA_API_INL float chm_QuatLength(const float* CHA_RESTRICT q4) {return sqrtf(q4[0]*q4[0]+q4[1]*q4[1]+q4[2]*q4[2]+q4[3]*q4[3]);}
CHA_API_INL float* chm_QuatNormalized(float* CHA_RESTRICT result4,const float* CHA_RESTRICT q4) {float *r=result4;const float *q=q4;const float len=chm_QuatLength(q4);if (len>0) {r[0]=q[0]/len;r[1]=q[1]/len;r[2]=q[2]/len;r[3]=q[3]/len;} else {r[3]=(float)1;r[0]=r[1]=r[2]=(float)0;} return r;}
CHA_API_INL void chm_QuatNormalize(float* CHA_RESTRICT q4) {const float len=chm_QuatLength(q4);if (len>0) {q4[0]/=len;q4[1]/=len;q4[2]/=len;q4[3]/=len;} else {q4[0]=q4[1]=q4[2]=q4[3]=(float)0;}}
CHA_API_DEC float* chm_QuatSlerpEps(float* CHA_RESTRICT result4,const float* CHA_RESTRICT a4,const float* CHA_RESTRICT b4,float slerpTime_In_0_1,int normalizeResult4AfterLerp/*=1*/,float eps/*= CHA_SLERP_EPSILON*/);
CHA_API_DEC float* chm_QuatSlerp(float* CHA_RESTRICT result4,const float* CHA_RESTRICT a4,const float* CHA_RESTRICT b4,float slerpTime_In_0_1,int normalizeResult4AfterLerp/*=1*/);
CHA_API_DEC float* chm_QuatFromMat4(float* CHA_RESTRICT result4,const float* CHA_RESTRICT m16);
CHA_API_DEC float* chm_Mat4SetRotationFromQuat(float* CHA_RESTRICT result16,const float* CHA_RESTRICT q4);
//--------------------------------------------------------------------------
#endif /*CHARACTER_H_ */
#ifdef CHARACTER_IMPLEMENTATION
#ifndef CHA_IMPLEMENTATION_GUARD
#define CHA_IMPLEMENTATION_GUARD
#ifndef CHA_MALLOC
# undef CHA_NO_STDLIB /* stdlib cannot be avoided in this case */
# include <stdlib.h> /* malloc/realloc/free */
# define CHA_MALLOC(X) malloc(X)
#endif
#ifndef CHA_FREE
# define CHA_FREE(X) free(X)
#endif
#ifndef CHA_REALLOC
# define CHA_REALLOC(x,y) realloc((x),(y))
#endif
#include <stddef.h> /* size_t */
#ifndef CHA_ASSERT
# ifdef CHA_NO_ASSERT
# define CHA_ASSERT(X) /*no-op*/
# else
# include <assert.h>
# define CHA_ASSERT(X) assert((X))
# endif
#endif
#ifndef CHA_NO_STDLIB
# include <stdlib.h> /*exit*/
#endif
#include <string.h> /*memcpy,memmove,memset*/
#ifndef __cplusplus
#define CHA_ZERO_INIT {0}
#else
#define CHA_ZERO_INIT {}
#endif
#ifndef CHA_LIGHTNESS
# define CHA_LIGHTNESS 1.0f
#endif
/* base memory helpers */
CHA_API_PRIV void* cha_malloc(size_t size) {
void* p = CHA_MALLOC(size);
if (!p) {
CHA_ASSERT(0); /* No more memory error */
# ifndef CHA_NO_STDIO
fprintf(stderr,"CHA_ERROR: cha_malloc(...) failed. Not enough memory.\n");
# endif
# ifndef CHA_NO_STDLIB
exit(1);
# endif
}
return p;
}
CHA_API_PRIV void cha_free(void* p) {CHA_FREE(p);}
CHA_API_PRIV void* cha_safe_realloc(void** const ptr, size_t new_size) {
void *ptr2 = CHA_REALLOC(*ptr,new_size);
CHA_ASSERT(new_size!=0); /* undefined behaviour */
if (ptr2) *ptr=ptr2;
else {
CHA_FREE(*ptr);*ptr=NULL;
CHA_ASSERT(0); /* No more memory error */
# ifndef CHA_NO_STDIO
fprintf(stderr,"CHA_ERROR: cha_safe_realloc(...) failed. Not enough memory.\n");
# endif
# ifndef CHA_NO_STDLIB
exit(1);
# endif
}
return ptr2;
}
#include <math.h>
#ifdef CHA_USE_SIMD
#if (!defined(__SSE__) && (defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP>0))
#define __SSE__ // _MSC_VER does not always define it... (but it always defines __AVX__)
#endif // __SSE__
#ifndef CHA_DOUBLE_PRECISION
#ifdef __SSE__
#include <xmmintrin.h> // SSE
#endif //__SSE__
#else // CHA_DOUBLE_PRECISION
#ifdef __AVX__
#include <immintrin.h> // AVX (and everything)
#endif //__AVX__
#endif // CHA_DOUBLE_PRECISION
#endif //CHA_USE_SIMD
extern float chm_Roundf(float number);
extern float chm_Roundd(double number);
extern float chm_Vec3Dotf(const float* a3,const float* b3);
extern double chm_Vec3Dotd(const double* a3,const double* b3);
extern void chm_Vec3Normalizef(float* CHA_RESTRICT v3);
extern void chm_Vec3Normalized(double* CHA_RESTRICT v3);
extern void chm_Vec3Crossf(float* CHA_RESTRICT vOut3,const float* CHA_RESTRICT a3,const float* CHA_RESTRICT b3);
extern void chm_Vec3Crossd(double* CHA_RESTRICT vOut3,const double* CHA_RESTRICT a3,const double* CHA_RESTRICT b3);
extern float chm_Vec3DistSquaredf(const float* CHA_RESTRICT a3,const float* CHA_RESTRICT b3);
extern double chm_Vec3DistSquaredd(const double* CHA_RESTRICT a3,const double* CHA_RESTRICT b3);
extern float chm_Vec3Distf(const float* CHA_RESTRICT a3,const float* CHA_RESTRICT b3);
extern double chm_Vec3Distd(const double* CHA_RESTRICT a3,const double* CHA_RESTRICT b3);
extern void chm_Mat4Convertd2f(float* CHA_RESTRICT result16,const double* CHA_RESTRICT m16);
extern void chm_Mat4Convertf2d(double* CHA_RESTRICT result16,const float* CHA_RESTRICT m16);
extern float* chm_Mat4Identityf(float* CHA_RESTRICT result16);
extern double* chm_Mat4Identityd(double* CHA_RESTRICT result16);
extern float* chm_Mat4ClearRotationf(float* CHA_RESTRICT result16);
extern double* chm_Mat4ClearRotationd(double* CHA_RESTRICT result16);
extern void chm_Mat4Copyf(float* CHA_RESTRICT dst16,const float* CHA_RESTRICT src16);
extern void chm_Mat4Copyd(double* CHA_RESTRICT dst16,const double* CHA_RESTRICT src16);
CHA_API_DEF float* chm_Mat4MulUncheckArgsf(float* CHA_RESTRICT result16,const float* CHA_RESTRICT ml16,const float* CHA_RESTRICT mr16) {
int i,i4;
# if (defined(CHA_USE_SIMD) && defined(__SSE__))
__m128 row1 = _mm_loadu_ps(&ml16[0]);
__m128 row2 = _mm_loadu_ps(&ml16[4]);
__m128 row3 = _mm_loadu_ps(&ml16[8]);
__m128 row4 = _mm_loadu_ps(&ml16[12]);
for(i=0; i<4; i++) {
i4 = 4*i;
__m128 brod1 = _mm_set1_ps(mr16[i4]);
__m128 brod2 = _mm_set1_ps(mr16[i4 + 1]);
__m128 brod3 = _mm_set1_ps(mr16[i4 + 2]);
__m128 brod4 = _mm_set1_ps(mr16[i4 + 3]);
__m128 row = _mm_add_ps(
_mm_add_ps(
_mm_mul_ps(brod1, row1),
_mm_mul_ps(brod2, row2)),
_mm_add_ps(
_mm_mul_ps(brod3, row3),
_mm_mul_ps(brod4, row4)));
_mm_storeu_ps(&result16[i4], row);
}
# else
/* reference implementation */
float mri4plus0,mri4plus1,mri4plus2,mri4plus3;
for(i = 0; i < 4; i++) {
i4=4*i;mri4plus0=mr16[i4];mri4plus1=mr16[i4+1];mri4plus2=mr16[i4+2];mri4plus3=mr16[i4+3];
result16[ i4] = ml16[0]*mri4plus0 + ml16[4]*mri4plus1 + ml16[ 8]*mri4plus2 + ml16[12]*mri4plus3;
result16[1+i4] = ml16[1]*mri4plus0 + ml16[5]*mri4plus1 + ml16[ 9]*mri4plus2 + ml16[13]*mri4plus3;
result16[2+i4] = ml16[2]*mri4plus0 + ml16[6]*mri4plus1 + ml16[10]*mri4plus2 + ml16[14]*mri4plus3;
result16[3+i4] = ml16[3]*mri4plus0 + ml16[7]*mri4plus1 + ml16[11]*mri4plus2 + ml16[15]*mri4plus3;
}
# endif //CHA_DOUBLE_PRECISION
return result16;
}
CHA_API_DEF double* chm_Mat4MulUncheckArgsd(double* CHA_RESTRICT result16,const double* CHA_RESTRICT ml16,const double* CHA_RESTRICT mr16) {
int i,i4;
#if (defined(CHA_USE_SIMD) && defined(__AVX__))
__m256d row1 = _mm256_loadu_pd(&ml16[0]);
__m256d row2 = _mm256_loadu_pd(&ml16[4]);
__m256d row3 = _mm256_loadu_pd(&ml16[8]);
__m256d row4 = _mm256_loadu_pd(&ml16[12]);
for(i=0; i<4; i++) {
i4 = 4*i;
__m256d brod1 = _mm256_set1_pd(mr16[i4]);
__m256d brod2 = _mm256_set1_pd(mr16[i4 + 1]);
__m256d brod3 = _mm256_set1_pd(mr16[i4 + 2]);
__m256d brod4 = _mm256_set1_pd(mr16[i4 + 3]);
__m256d row = _mm256_add_pd(
_mm256_add_pd(
_mm256_mul_pd(brod1, row1),
_mm256_mul_pd(brod2, row2)),
_mm256_add_pd(
_mm256_mul_pd(brod3, row3),
_mm256_mul_pd(brod4, row4)));
_mm256_storeu_pd(&result16[i4], row);
}
# else
/* reference implementation */
double mri4plus0,mri4plus1,mri4plus2,mri4plus3;
for(i = 0; i < 4; i++) {
i4=4*i;mri4plus0=mr16[i4];mri4plus1=mr16[i4+1];mri4plus2=mr16[i4+2];mri4plus3=mr16[i4+3];
result16[ i4] = ml16[0]*mri4plus0 + ml16[4]*mri4plus1 + ml16[ 8]*mri4plus2 + ml16[12]*mri4plus3;
result16[1+i4] = ml16[1]*mri4plus0 + ml16[5]*mri4plus1 + ml16[ 9]*mri4plus2 + ml16[13]*mri4plus3;
result16[2+i4] = ml16[2]*mri4plus0 + ml16[6]*mri4plus1 + ml16[10]*mri4plus2 + ml16[14]*mri4plus3;
result16[3+i4] = ml16[3]*mri4plus0 + ml16[7]*mri4plus1 + ml16[11]*mri4plus2 + ml16[15]*mri4plus3;
}
# endif
return result16;
}
CHA_API_DEF float* chm_Mat4Mulf(float* CHA_RESTRICT result16,const float* CHA_RESTRICT ml16,const float* CHA_RESTRICT mr16) {
if (result16==ml16) {
float ML16[16];chm_Mat4Copyf(ML16,ml16);
return chm_Mat4MulUncheckArgsf(result16,ML16,mr16);
}
else if (result16==mr16) {
float MR16[16];chm_Mat4Copyf(MR16,mr16);
return chm_Mat4MulUncheckArgsf(result16,ml16,MR16);
}
return chm_Mat4MulUncheckArgsf(result16,ml16,mr16);
}
CHA_API_DEF double* chm_Mat4Muld(double* CHA_RESTRICT result16,const double* CHA_RESTRICT ml16,const double* CHA_RESTRICT mr16) {
if (result16==ml16) {
double ML16[16];chm_Mat4Copyd(ML16,ml16);
return chm_Mat4MulUncheckArgsd(result16,ML16,mr16);
}
else if (result16==mr16) {
double MR16[16];chm_Mat4Copyd(MR16,mr16);
return chm_Mat4MulUncheckArgsd(result16,ml16,MR16);
}
return chm_Mat4MulUncheckArgsd(result16,ml16,mr16);
}
extern void chm_Mat4MulDirf(const float* CHA_RESTRICT m16,float* CHA_RESTRICT dirOut3,const float dirX,float dirY,float dirZ);
extern void chm_Mat4MulDird(const double* CHA_RESTRICT m16,double* CHA_RESTRICT dirOut3,const double dirX,double dirY,double dirZ);
extern void chm_Mat4MulPosf(const float* CHA_RESTRICT m16,float* CHA_RESTRICT posOut3,const float posX,float posY,float posZ);
extern void chm_Mat4MulPosd(const double* CHA_RESTRICT m16,double* CHA_RESTRICT posOut3,const double posX,double posY,double posZ);
CHA_API_DEF void chm_Mat4LookAtf(float* CHA_RESTRICT mOut16,float eyeX,float eyeY,float eyeZ,float centerX,float centerY,float centerZ,float upX,float upY,float upZ) {
float* m = mOut16;
const float eps = 0.0001f;
float F[3] = {eyeX-centerX,eyeY-centerY,eyeZ-centerZ};
float length = F[0]*F[0]+F[1]*F[1]+F[2]*F[2]; // length2 now
float up[3] = {upX,upY,upZ};
float S[3] = {up[1]*F[2]-up[2]*F[1],up[2]*F[0]-up[0]*F[2],up[0]*F[1]-up[1]*F[0]};
float U[3] = {F[1]*S[2]-F[2]*S[1],F[2]*S[0]-F[0]*S[2],F[0]*S[1]-F[1]*S[0]};
if (length==0) length = eps;
length = sqrt(length);
F[0]/=length;F[1]/=length;F[2]/=length;
length = S[0]*S[0]+S[1]*S[1]+S[2]*S[2];if (length==0) length = eps;
length = sqrt(length);
S[0]/=length;S[1]/=length;S[2]/=length;
length = U[0]*U[0]+U[1]*U[1]+U[2]*U[2];if (length==0) length = eps;
length = sqrt(length);
U[0]/=length;U[1]/=length;U[2]/=length;
m[0] = S[0];
m[1] = U[0];
m[2] = F[0];
m[3]= 0;
m[4] = S[1];
m[5] = U[1];
m[6] = F[1];
m[7]= 0;
m[8] = S[2];
m[9] = U[2];
m[10]= F[2];
m[11]= 0;
m[12] = -S[0]*eyeX -S[1]*eyeY -S[2]*eyeZ;
m[13] = -U[0]*eyeX -U[1]*eyeY -U[2]*eyeZ;
m[14]= -F[0]*eyeX -F[1]*eyeY -F[2]*eyeZ;
m[15]= 1;
}
CHA_API_DEF void chm_Mat4LookAtd(double* CHA_RESTRICT mOut16,double eyeX,double eyeY,double eyeZ,double centerX,double centerY,double centerZ,double upX,double upY,double upZ) {
double* m = mOut16;
const double eps = 0.0001;
double F[3] = {eyeX-centerX,eyeY-centerY,eyeZ-centerZ};
double length = F[0]*F[0]+F[1]*F[1]+F[2]*F[2]; // length2 now
double up[3] = {upX,upY,upZ};
double S[3] = {up[1]*F[2]-up[2]*F[1],up[2]*F[0]-up[0]*F[2],up[0]*F[1]-up[1]*F[0]};
double U[3] = {F[1]*S[2]-F[2]*S[1],F[2]*S[0]-F[0]*S[2],F[0]*S[1]-F[1]*S[0]};
if (length==0) length = eps;
length = sqrt(length);
F[0]/=length;F[1]/=length;F[2]/=length;
length = S[0]*S[0]+S[1]*S[1]+S[2]*S[2];if (length==0) length = eps;
length = sqrt(length);
S[0]/=length;S[1]/=length;S[2]/=length;
length = U[0]*U[0]+U[1]*U[1]+U[2]*U[2];if (length==0) length = eps;
length = sqrt(length);
U[0]/=length;U[1]/=length;U[2]/=length;
m[0] = S[0];
m[1] = U[0];
m[2] = F[0];
m[3]= 0;
m[4] = S[1];
m[5] = U[1];
m[6] = F[1];
m[7]= 0;
m[8] = S[2];
m[9] = U[2];
m[10]= F[2];
m[11]= 0;
m[12] = -S[0]*eyeX -S[1]*eyeY -S[2]*eyeZ;
m[13] = -U[0]*eyeX -U[1]*eyeY -U[2]*eyeZ;
m[14]= -F[0]*eyeX -F[1]*eyeY -F[2]*eyeZ;
m[15]= 1;
}
CHA_API_DEF void chm_Mat4Perspectivef(float* CHA_RESTRICT mOut16,float degfovy,float aspect, float zNear, float zFar) {
float* res = mOut16;
const float eps = 0.0001f;
float f = 1.f/tanf(degfovy*1.5707963268f/180.0f); //cotg
float Dfn = (zFar-zNear);
if (Dfn==0) {zFar+=eps;zNear-=eps;Dfn=zFar-zNear;}
if (aspect==0) aspect = 1.f;
res[0] = f/aspect;
res[1] = 0;
res[2] = 0;
res[3] = 0;
res[4] = 0;
res[5] = f;
res[6] = 0;
res[7] = 0;
res[8] = 0;
res[9] = 0;
res[10] = -(zFar+zNear)/Dfn;
res[11] = -1;
res[12] = 0;
res[13] = 0;
res[14] = -2.f*zFar*zNear/Dfn;
res[15] = 0;
}
CHA_API_DEF void chm_Mat4Perspectived(double* CHA_RESTRICT mOut16,double degfovy,double aspect, double zNear, double zFar) {
double* res = mOut16;
const double eps = 0.0001;
double f = 1.f/tan(degfovy*1.5707963268/180.0); //cotg
double Dfn = (zFar-zNear);
if (Dfn==0) {zFar+=eps;zNear-=eps;Dfn=zFar-zNear;}
if (aspect==0) aspect = 1.f;
res[0] = f/aspect;
res[1] = 0;
res[2] = 0;
res[3] = 0;
res[4] = 0;
res[5] = f;
res[6] = 0;
res[7] = 0;
res[8] = 0;
res[9] = 0;
res[10] = -(zFar+zNear)/Dfn;
res[11] = -1;
res[12] = 0;
res[13] = 0;
res[14] = -2.f*zFar*zNear/Dfn;
res[15] = 0;
}
CHA_API_DEF void chm_Mat4Orthof(float* CHA_RESTRICT mOut16,float left,float right, float bottom, float top,float nearVal,float farVal) {
float* res = mOut16;
const float eps = 0.0001f;
float Drl = (right-left);
float Dtb = (top-bottom);
float Dfn = (farVal-nearVal);
if (Drl==0) {right+=eps;left-=eps;Drl=right-left;}
if (Dtb==0) {top+=eps;bottom-=eps;Dtb=top-bottom;}
if (Dfn==0) {farVal+=eps;nearVal-=eps;Dfn=farVal-nearVal;}
res[0] = 2.f/Drl;
res[1] = 0;
res[2] = 0;
res[3] = 0;
res[4] = 0;
res[5] = 2.f/Dtb;
res[6] = 0;
res[7] = 0;
res[8] = 0;
res[9] = 0;
res[10] = -2.f/Dfn;
res[11] = 0;
res[12] = -(right+left)/Drl;
res[13] = -(top+bottom)/Dtb;
res[14] = (farVal+nearVal)/Dfn;
res[15] = 1;
}
CHA_API_DEF void chm_Mat4Ortho3Df(float* CHA_RESTRICT mOut16,float cameraTargetDistance,float degfovy,float aspect,float znear,float zfar) {
// Warning: this function might be WRONG! Use it at your own risk!
const float FOVTG=tanf(degfovy*(float)(3.14159265358979323846/360.0));
float y=cameraTargetDistance*FOVTG;//=(zfar-znear)*0.5f;
//float y=(cameraTargetDistance<zfar?cameraTargetDistance:zfar)*FOVTG; // or maybe this?
float x=y*aspect;
//chm_Mat4Orthof(mOut16, -x, x, -y, y, znear, zfar); // I thought this was correct
//chm_Mat4Orthof(mOut16, -x, x, -y, y, -zfar, znear); // But this works better in my test-case
chm_Mat4Orthof(mOut16, -x, x, -y, y, -zfar, -znear); // Or maybe this?
}
CHA_API_DEF void chm_Mat4Orthod(double* CHA_RESTRICT mOut16,double left,double right, double bottom, double top,double nearVal,double farVal) {
double* res = mOut16;
const double eps = 0.0001;
double Drl = (right-left);
double Dtb = (top-bottom);
double Dfn = (farVal-nearVal);
if (Drl==0) {right+=eps;left-=eps;Drl=right-left;}
if (Dtb==0) {top+=eps;bottom-=eps;Dtb=top-bottom;}
if (Dfn==0) {farVal+=eps;nearVal-=eps;Dfn=farVal-nearVal;}
res[0] = 2.f/Drl;
res[1] = 0;
res[2] = 0;
res[3] = 0;
res[4] = 0;
res[5] = 2.f/Dtb;
res[6] = 0;
res[7] = 0;
res[8] = 0;
res[9] = 0;
res[10] = -2.f/Dfn;
res[11] = 0;
res[12] = -(right+left)/Drl;
res[13] = -(top+bottom)/Dtb;
res[14] = (farVal+nearVal)/Dfn;
res[15] = 1;
}
CHA_API_DEF void chm_Mat4Ortho3Dd(double* CHA_RESTRICT mOut16,double cameraTargetDistance,double degfovy,double aspect,double znear,double zfar) {
// Warning: this function might be WRONG! Use it at your own risk!
const double FOVTG=tanf(degfovy*(double)(3.14159265358979323846/360.0));
double y=cameraTargetDistance*FOVTG;//=(zfar-znear)*0.5f;
//double y=(cameraTargetDistance<zfar?cameraTargetDistance:zfar)*FOVTG; // or maybe this?
double x=y*aspect;
//chm_Mat4Orthod(mOut16, -x, x, -y, y, znear, zfar); // I thought this was correct
//chm_Mat4Orthod(mOut16, -x, x, -y, y, -zfar, znear); // But this works better in my test-case
chm_Mat4Orthod(mOut16, -x, x, -y, y, -zfar, -znear); // Or maybe this?
}
extern float* chm_Mat4Translatef(float* CHA_RESTRICT mInOut16,float x,float y,float z);
extern double* chm_Mat4Translated(double* CHA_RESTRICT mInOut16,double x,double y,double z);
CHA_API_DEF float* chm_Mat4Rotatef(float* CHA_RESTRICT mInOut16,float degAngle,float x,float y,float z) {
const float angle = degAngle*M_PIOVER180;
const float c = cosf(angle);
const float s = sinf(angle);
float len = x*x+y*y+z*z;
if (len<0.999f || len>1.001f) {len=sqrtf(len);x/=len;y/=len;z/=len;}
{
const float omc = (float)1-c, xs = x*s,ys = y*s,zs = z*s;int i;
const float xy = x*y,xz = x*z,yz = y*z, xx = x*x,yy = y*y,zz = z*z;
const float rot[9] = { /* 3x3 rotation matrix */
c+xx*omc, xy*omc+zs, xz*omc-ys,
xy*omc-zs, c+yy*omc, yz*omc+xs,
xz*omc+ys, yz*omc-xs, c+zz*omc
};
const float* m = mInOut16;
for (i=0;i<3;i++) { /* manual 3x3 rotation */
const float mi=m[i],mi4=m[i+4],mi8=m[i+8]; /* we cannot use references here */
mInOut16[i] = mi*rot[0] + mi4*rot[1] + mi8*rot[2];
mInOut16[4+i] = mi*rot[3] + mi4*rot[4] + mi8*rot[5];
mInOut16[8+i] = mi*rot[6] + mi4*rot[7] + mi8*rot[8];
}
return mInOut16;
}
}
CHA_API_DEF double* chm_Mat4Rotated(double* CHA_RESTRICT mInOut16,double degAngle,double x,double y,double z) {
const double angle = degAngle*M_PIOVER180;
const double c = cos(angle);
const double s = sin(angle);
double len = x*x+y*y+z*z;
if (len<0.9999 || len>1.0001) {len=sqrt(len);x/=len;y/=len;z/=len;}
{
const double omc = (double)1-c, xs = x*s,ys = y*s,zs = z*s;int i;
const double xy = x*y,xz = x*z,yz = y*z, xx = x*x,yy = y*y,zz = z*z;
const double rot[9] = { /* 3x3 rotation matrix */
c+xx*omc, xy*omc+zs, xz*omc-ys,
xy*omc-zs, c+yy*omc, yz*omc+xs,
xz*omc+ys, yz*omc-xs, c+zz*omc
};
const double* m = mInOut16;
for (i=0;i<3;i++) { /* manual 3x3 rotation */
const double mi=m[i],mi4=m[i+4],mi8=m[i+8]; /* we cannot use references here */
mInOut16[i] = mi*rot[0] + mi4*rot[1] + mi8*rot[2];
mInOut16[4+i] = mi*rot[3] + mi4*rot[4] + mi8*rot[5];
mInOut16[8+i] = mi*rot[6] + mi4*rot[7] + mi8*rot[8];
}
return mInOut16;
}
}
extern float* chm_Mat4Scalef(float* CHA_RESTRICT mInOut16,float x,float y,float z);