-
Notifications
You must be signed in to change notification settings - Fork 2
/
teapot.h
4236 lines (3816 loc) · 307 KB
/
teapot.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
//
/* LICENSE: Made by @Flix01. MIT license on my part.
* But I don't know the license of the original Teapot mesh data,
* and of the Bunny and Torus meshes (these two from the Bullet Physics Engine's GImpact Old Demo)
* I've made the other meshes myself.
* You can disable some meshes with something like TEAPOT_NO_MESH_BUNNY (never tested).
*
* However:
* -> The teapot mesh data is based on the data contained in the freeglut library (see glutSolidTeapot(...)),
* with some modifications to display it better when GL_CULL_FACE is enabled. AFAIK, the freeglut library has a liberal
* license that is compatible with MIT.
* -> The bunny and torus meshes are based on the BunnyMesh.h and TorusMesh.h contained in the GImpactTestDemo that was first
* available from here: http://gimpact.sourceforge.net. GIMPACT itself is licensed under either LGPL, BSD or ZLib-style license.
* However there is no note about the license of the two mesh data...
*/
/* WHAT'S THIS?
* A plain C (--std=gnu89) header-only file that can be used to display the teapot mesh (and other meshes),
* in a (modern) openGL program, with a very simple API and no other dependencies (everything is in the teapot.h file).
*/
/* USAGE:
* Please see the comments of the functions below and try to follow their order when you call them.
* Define TEAPOT_IMPLEMENTATION in one of your .c (or .cpp) files before the inclusion of this file.
*/
// OPTIONAL DEFINITIONS:
//
//#define TEAPOT_CENTER_MESHES_ON_FLOOR // By default meshes are centered in their model location (usually their aabb center)
//#define TEAPOT_INVERT_MESHES_Z_AXIS // Makes meshes look in the opposite Z direction
//
//#define TEAPOT_SHADER_SPECULAR // Adds specular light component in the shader and Teapot_SetColorSpecular(...) function
//#define TEAPOT_SHADER_FOG // Adds linear fog in the shader and Teapot_SetFogColor(...) Teapot_SetFogDistances(...) functions
//#define TEAPOT_SHADER_FOG_HINT_FRAGMENT_SHADER // Changes the fog quality a bit (used only when TEAPOT_SHADER_FOG is defined)
//#define TEAPOT_SHADER_USE_ACCURATE_NORMALS // (a bit slower). It replaces TEAPOT_SHADER_USE_NORMAL_MATRIX from version 1.2 (with code based on https://lxjk.github.io/2017/10/01/Stop-Using-Normal-Matrix.html).
//#define TEAPOT_SHADER_HINT_ACCURATE_NORMALS_GPU // used only when TEAPOT_SHADER_USE_ACCURATE_NORMALS is defined. Not sure if it's faster or not.
//#define TEAPOT_SHADER_USE_SHADOW_MAP // Eases shadow mapping by supporting the second pass of the algorithm.
//#define TEAPOT_SHADER_SHADOW_MAP_PCF 4 // (optional, but needs a value>0, otherwise will be set to zero). Basically when TEAPOT_SHADER_USE_SHADOW_MAP is defined, PCF filter used (dynamic_resolution.h can automatically set this value when DYNAMIC_RESOLUTION_SHADOW_USE_PCF is used).
// // Warning: when TEAPOT_SHADER_SHADOW_MAP_PCF is used with emscripten, it needs: -s USE_WEBGL2=1
//
//#define TEAPOT_MAX_NUM_USER_MESH_VERTICES (3000) // (experimental) default is 1000. Needed when, using Teapot_Set_Init_UserMeshCallback(...), you overflow the number of vertices. Warning: the max number of vertices for ALL meshes (= embedded + user) can't be bigger than 65535, because the indices are unsigned short.
//#define TEAPOT_MAX_NUM_USER_MESH_INDICES (15000) // (experimental) default is 5000. Needed when, using Teapot_Set_Init_UserMeshCallback(...), you overflow the number of indices.
//
//#define TEAPOT_ENABLE_FRUSTUM_CULLING // (experimental) it does not cull 100% objects, and performance might be slower rather than faster...
//
//#define TEAPOT_USE_OPENMP // (experimental) ATM is only used in Teapot_MeshData_CalculateMvMatrixFromArray(...) and never tested => one more dependency and no gain: DO NOT USE!
//
//#define TEAPOT_USE_SIMD // (experimental) speeds up Teapot_Helper_MultMatrix(...) using SIMD (about 1.5x-2x when compiled with -O3 -DNDEBUG -march=native), Requires -msse (OR -mavx when using double precision).
//
//#define TEAPOT_MESHDATA_HAS_MMATRIX_PTR // (untested) handy when using Teapot_MeshData + some kind of physic engine that already stores a mMatrix16 somewhere.
#ifndef TEAPOT_H_
#define TEAPOT_H_
#ifdef __cplusplus
extern "C" {
#endif
#ifndef TEAPOT_VERSION
# define TEAPOT_VERSION 1.23
#endif //TEAPOT_VERSION
/* The __restrict and __restrict__ keywords are recognized in both C, at all language levels, and C++, at LANGLVL(EXTENDED).*/
#ifdef TEAPOT_NO_RESTRICT // please define it globally if the keyword __restrict is not present
# ifndef __restrict
# define __restrict /*no-op*/
# endif
#endif //TEAPOT_NO_RESTRICT
#ifdef TEAPOT_USE_DOUBLE_PRECISION // from version TEAPOT_VERSION 1.1: TEAPOT_MATRIX_USE_DOUBLE_PRECISION and TEAPOT_USE_DOUBLE_PRECISION are the same
#undef TEAPOT_MATRIX_USE_DOUBLE_PRECISION
#define TEAPOT_MATRIX_USE_DOUBLE_PRECISION
#endif //TEAPOT_USE_DOUBLE_PRECISION
#ifndef TEAPOT_MATRIX_USE_DOUBLE_PRECISION
typedef float tpoat; /* short form of tEApOT_FLoat */
#else
typedef double tpoat;
#undef TEAPOT_USE_DOUBLE_PRECISION
#define TEAPOT_USE_DOUBLE_PRECISION
#endif
#ifdef TEAPOT_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 TEAPOT_MATRIX_USE_DOUBLE_PRECISION
#ifdef __SSE__
#include <xmmintrin.h> // SSE
#endif //__SSE__
#else // TEAPOT_MATRIX_USE_DOUBLE_PRECISION
#ifdef __AVX__
#include <immintrin.h> // AVX (and everything)
#endif //__AVX__
#endif // TEAPOT_MATRIX_USE_DOUBLE_PRECISION
#endif //TEAPOT_USE_SIMD
#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
#ifndef TEAPOT_SHADER_SHADOW_MAP_PCF
# ifdef DYNAMIC_RESOLUTION_SHADOW_USE_PCF
# define TEAPOT_SHADER_SHADOW_MAP_PCF DYNAMIC_RESOLUTION_SHADOW_USE_PCF
# else //DYNAMIC_RESOLUTION_SHADOW_USE_PCF
# define TEAPOT_SHADER_SHADOW_MAP_PCF 0
# endif //DYNAMIC_RESOLUTION_SHADOW_USE_PCF
#endif //TEAPOT_SHADER_SHADOW_MAP_PCF
#ifdef TEAPOT_SHADER_USE_NORMAL_MATRIX // deprecated from version 1.2
#undef TEAPOT_SHADER_USE_ACCURATE_NORMALS
#define TEAPOT_SHADER_USE_ACCURATE_NORMALS
#endif
typedef enum {
TEAPOT_MESH_TEAPOT=0,
TEAPOT_MESH_ARROW,
TEAPOT_MESH_BUNNY,
TEAPOT_MESH_CAR, // TEAPOT_NO_MESH_CAR_WHEELS can be defined to strip wheels
TEAPOT_MESH_CHAIR,
TEAPOT_MESH_CHARACTER,
TEAPOT_MESH_GHOST,
TEAPOT_MESH_SKITTLE,
TEAPOT_MESH_FLIPPER_RIGHT,
TEAPOT_MESH_FLIPPER_LEFT,
TEAPOT_MESH_SLEDGE,
TEAPOT_MESH_TABLE,
TEAPOT_MESH_TORUS,
TEAPOT_MESH_USER_00, // TEAPOT_MESH_USER_XX meshes must be entered through Teapot_Set_Init_UserMeshCallback(...) before calling Teapot_Init(...)
TEAPOT_MESH_USER_01,
TEAPOT_MESH_USER_02,
TEAPOT_MESH_USER_03,
TEAPOT_MESH_USER_04,
TEAPOT_MESH_USER_05,
TEAPOT_MESH_USER_06,
TEAPOT_MESH_USER_07,
TEAPOT_MESH_USER_08,
TEAPOT_MESH_USER_09,
TEAPOT_MESH_USER_10,
TEAPOT_MESH_USER_11,
TEAPOT_MESH_USER_12,
TEAPOT_MESH_USER_13,
TEAPOT_MESH_USER_14,
TEAPOT_MESH_USER_15,
TEAPOT_MESH_USER_16,
TEAPOT_MESH_USER_17,
TEAPOT_MESH_USER_18,
TEAPOT_MESH_USER_19,
TEAPOT_MESH_USER_20,
TEAPOT_MESH_USER_21,
TEAPOT_MESH_USER_22,
TEAPOT_MESH_USER_23,
TEAPOT_MESH_USER_24,
TEAPOT_MESH_USER_25,
TEAPOT_MESH_USER_26,
TEAPOT_MESH_USER_27,
TEAPOT_MESH_USER_28,
TEAPOT_MESH_USER_29,
TEAPOT_MESH_CUBE,
TEAPOT_MESH_CUBIC_GROUND, // Top face is made by 5x5 vertices
TEAPOT_MESH_CUBE_ROUNDED,
TEAPOT_MESH_PLANE_X,
TEAPOT_MESH_PLANE_Y,
TEAPOT_MESH_PLANE_Z,
TEAPOT_MESH_CYLINDER,
TEAPOT_MESH_CONE1,
TEAPOT_MESH_CONE2, // Better quality
TEAPOT_MESH_PYRAMID,
TEAPOT_MESH_ROOF,
TEAPOT_MESH_SPHERE1,
TEAPOT_MESH_SPHERE2, // Better quality
TEAPOT_MESH_CAPSULE, // Warning: this is special: itsHeight=scaling.y+(scaling.x+scaling.z)*0.5f; itsDiameter=(scaling.x+scaling.z)*0.5f; (this avoids non-uniform scaling)
TEAPOT_MESH_CYLINDER_LATERAL_SURFACE,
TEAPOT_MESH_HALF_SPHERE_UP, // Warning: These is not centered when TEAPOT_CENTER_MESHES_ON_FLOOR is defined
TEAPOT_MESH_HALF_SPHERE_DOWN, // Warning: These is not centered when TEAPOT_CENTER_MESHES_ON_FLOOR is defined
TEAPOT_MESH_PIVOT3D, // Warning: These is not centered when TEAPOT_CENTER_MESHES_ON_FLOOR is defined
TEAPOT_MESH_TEXT_X, // Warning: These is not centered when TEAPOT_CENTER_MESHES_ON_FLOOR is defined
TEAPOT_MESH_TEXT_Y, // Warning: These is not centered when TEAPOT_CENTER_MESHES_ON_FLOOR is defined
TEAPOT_MESH_TEXT_Z, // Warning: These is not centered when TEAPOT_CENTER_MESHES_ON_FLOOR is defined
// GL_LINES starts here:
TEAPOT_MESHLINES_CUBE_EDGES, // Experimental GL_LINES
TEAPOT_MESH_COUNT
} TeapotMeshEnum;
#define TEAPOT_FIRST_MESHLINES_INDEX (TEAPOT_MESHLINES_CUBE_EDGES)
#define TEAPOT_LAST_CONCAVE_MESH_INDEX (TEAPOT_MESH_TORUS)
// This callback can be useful when users want to add some simple meshes: just verts + inds, all sharing the same color, and nothing else [= NO TEXTURE!]
typedef void (*TeapotInitUserMeshCallback)(TeapotMeshEnum meshId,const float** ppverts,int* numVerts,const unsigned short** ppinds,int* numInds); // numVerts is the number of vertices (each vertex is 3 floats)
void Teapot_Set_Init_UserMeshCallback(TeapotInitUserMeshCallback callback); // (Optional/Advanced Users) to be called before Teapot_Init(void)
// This callback can be useful when users need to do something with the verts/inds of the meshes
typedef void (*TeapotInitCallback)(TeapotMeshEnum meshId,const float* pverts,int numVerts,const unsigned short* pinds,int numInds); // numVerts is the number of vertices (each vertex is 3 floats)
void Teapot_Set_Init_Callback(TeapotInitCallback callback); // (Optional/Advanced Users) to be called before Teapot_Init(void)
void Teapot_Init(void); // In your InitGL() method
void Teapot_Destroy(void); // In your DestroyGL() method (cleanup)
// In your InitGL() and ResizeGL() methods:
void Teapot_SetProjectionMatrix(const tpoat pMatrix[16]); // Sets the projection matrix [Warning: it calls glUseProgram(0); at the end => call it outside Teapot_PreDraw()/Teapot_PostDraw()]
void Teapot_SetProjectionMatrixf(const float pMatrix[16]); // Sets a float projection matrix [Warning: it calls glUseProgram(0); at the end => call it outside Teapot_PreDraw()/Teapot_PostDraw()]
#ifdef TEAPOT_SHADER_FOG
void Teapot_SetFogColor(float R, float G, float B); // it should be the same as glClearColor() [Warning: it calls glUseProgram(0); at the end => call it outside Teapot_PreDraw()/Teapot_PostDraw()]
void Teapot_SetFogDistances(float startDistance,float endDistance); // endDistance should be equal to the far clipping plane [Warning: it calls glUseProgram(0); at the end => call it outside Teapot_PreDraw()/Teapot_PostDraw()]
#endif //TEAPOT_SHADER_FOG
// In your DrawGL() method:
void Teapot_SetViewMatrixAndLightDirection(const tpoat vMatrix[16],tpoat lightDirectionWorldSpace[3]); // vMatrix CAN'T HAVE any scaling! Sets the camera view matrix (= gluLookAt matrix) and the directional light in world space [Warning: it calls glUseProgram(0); at the end => call it outside Teapot_PreDraw()/Teapot_PostDraw()]
// 6 Helper Functions. If used, they must be called AFTER Teapot_SetViewMatrixAndLightDirection(...)
void Teapot_GetViewMatrix(tpoat* res16); // camera view matrix (16 tpoat)
const tpoat* Teapot_GetViewMatrixConstReference(); // camera view matrix (16 tpoat)
void Teapot_GetNormalizedLightDirection(tpoat* res3); // directional light in world space (3 tpoat)
const tpoat* Teapot_GetNormalizedLightDirectionConstReference(); // directional light in world space (3 tpoat)
void Teapot_GetNormalizedLightDirectionInViewSpace(tpoat* res3); // directional light in view space (3 tpoat)
const tpoat* Teapot_GetNormalizedLightDirectionInViewSpaceConstReference(); // directional light in view space (3 tpoat)
#ifdef TEAPOT_SHADER_USE_SHADOW_MAP
void Teapot_SetShadowVpMatrix(const tpoat unbiasedShadowVpMatrix[16]); // MANDATORY: Must be called AFTER Teapot_SetViewMatrixAndLightDirection(...) Warning: Do NOT call inside Teapot_PreDraw()/Teapot_PostDraw(): it sets glUseProgram(0) before exiting.
void Teapot_SetShadowDarkening(float darkeningFactorIn_0_80,float shadowMinIntensityIn_0_1); // Optional. Default values are (40.0f,0.75f). Warning: Do NOT call inside Teapot_PreDraw()/Teapot_PostDraw(): it sets glUseProgram(0) before exiting.
void Teapot_GetViewMatrixInverse(tpoat* res16); // Helper: (it's the camera matrix). Must be called AFTER Teapot_SetViewMatrixAndLightDirection(...)
const tpoat* Teapot_GetViewMatrixInverseConstReference(); // Helper: (it's the camera matrix). Must be called AFTER Teapot_SetViewMatrixAndLightDirection(...)
void Teapot_LowLevel_SetMvMatrixUniformWithShadowSupport(const tpoat mvMatrix[16]);
void Teapot_LowLevel_SetMvMatrixUniformWithShadowSupportFloat(const float mvMatrix[16]);
#endif //TEAPOT_SHADER_USE_SHADOW_MAP
//------------------------------------------------------------------------------------
void Teapot_PreDraw(void); // sets program and buffers for drawing
//------------------------------------------------------------------------------------
void Teapot_SetColor(float R, float G, float B, float A); // Optional (last set is used). A<1.0 requires glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); and glEnable(GL_BLEND); to make transparency work
void Teapot_SetColorAmbient(float R, float G, float B); // Optional (last set is used).
void Teapot_SetColorAmbientAndDiffuse(const float ambient[3],const float diffuse[4]);// Optional (last set is used).
#ifdef TEAPOT_SHADER_SPECULAR
void Teapot_SetColorSpecular(float R, float G, float B, float SHI); // Optional (last set is used). If SHI<0, last valid call value is used
void Teapot_SetColorAmbientDiffuseAndSpecular(const float ambient[3],const float diffuse[4],const float specular_plus_shininess[4]);// Optional (last set is used). If specular_plus_shininess[3]<0, last valid call value is used.
#endif //TEAPOT_SHADER_SPECULAR
void Teapot_SetScaling(float scalingX,float scalingY,float scalingZ); // Optional (last set is used): it scales all the vertices, leaving the normals unaffected (a good perfomance/quality compromise)
// There are two Teapot_Draw functions:
void Teapot_Draw(const tpoat mMatrix[16],TeapotMeshEnum meshId); // (optionally repeat this call for multiple draws)
void Teapot_Draw_Mv(const tpoat mvMatrix[16],TeapotMeshEnum meshId); // (optionally repeat this call for multiple draws) Note that this is not the model matrix, but: Teapot_Helper_MultMatrix(mvMatrix,vMatrix,mMatrix)
void Teapot_Draw_MvFloat(const float mvMatrix[16],TeapotMeshEnum meshId); // [enforces float] (optionally repeat this call for multiple draws) Note that this is not the model matrix, but: Teapot_Helper_MultMatrix(mvMatrix,vMatrix,mMatrix)
// These draw a wireframe AABB around the mesh (scaling3 can be NULL)
void Teapot_DrawAabb(const tpoat mMatrix[16], TeapotMeshEnum meshId, const float *scaling3);
void Teapot_DrawAabb_Mv(const tpoat mvMatrix[16],TeapotMeshEnum meshId,const float* scaling3);
void Teapot_DrawAabb_MvFloat(const float mvMatrix[16],TeapotMeshEnum meshId,const float* scaling3);
// There are two Teapot_DrawMulti functions:
typedef struct _Teapot_MeshData {
# ifdef TEAPOT_MESHDATA_HAS_MMATRIX_PTR
const tpoat* mMatrix; // Input for Teapot_DrawMulti(...) [Useful if your physic engine already provides a tpoat mMatrix[16] somewhere]
# else
tpoat mMatrix[16]; // Input for Teapot_DrawMulti(...)
# endif
tpoat mvMatrix[16]; // Output for Teapot_DrawMulti(...), but Input if Teapot_DrawMulti_Mv(...) is used [Teapot_Helper_MultMatrix(mvMatrix,vMatrix,mMatrix)];
// Input:
TeapotMeshEnum meshId;
float scaling[3];
float color[4];
float colorAmbient[3]; // Skipped when Teapot_Color_Material is enabled
float colorSpecular[4]; // Skipped when Teapot_Color_Material is enabled. Used only when TEAPOT_SHADER_SPECULAR is defined
int outlineEnabled; // 0 or 1
int active; // 0 or 1
# ifdef TEAPOT_MESHDATA_STRUCT_EXTRA_FIELDS
TEAPOT_MESHDATA_STRUCT_EXTRA_FIELDS
# else
void* userPtr; // yours
# endif
# ifdef __cplusplus
_Teapot_MeshData();
virtual ~_Teapot_MeshData() {} // In case we want to inherit from it in C++
# endif
} Teapot_MeshData;
void Teapot_MeshData_Clear(Teapot_MeshData* md);
#ifndef TEAPOT_MESHDATA_HAS_MMATRIX_PTR
void Teapot_MeshData_SetMMatrix(Teapot_MeshData* md,const tpoat* mMatrix16);
#endif //TEAPOT_MESHDATA_HAS_MMATRIX_PTR
void Teapot_MeshData_SetMvMatrix(Teapot_MeshData* md,const tpoat* mvMatrix16);
static __inline void Teapot_MeshData_SetScaling(Teapot_MeshData* md,float scalingX,float scalingY,float scalingZ) {md->scaling[0]=scalingX;md->scaling[1]=scalingY;md->scaling[2]=scalingZ;}
static __inline void Teapot_MeshData_SetColor(Teapot_MeshData* md,float R,float G,float B,float A) {md->color[0]=R;md->color[1]=G;md->color[2]=B;md->color[3]=A;}
static __inline void Teapot_MeshData_SetColorAmbient(Teapot_MeshData* md,float R,float G,float B) {md->colorAmbient[0]=R;md->colorAmbient[1]=G;md->colorAmbient[2]=B;}
#ifdef TEAPOT_SHADER_SPECULAR
static __inline void Teapot_MeshData_SetColorSpecular(Teapot_MeshData* md,float R, float G, float B, float SHI) {md->colorSpecular[0]=R;md->colorSpecular[1]=G;md->colorSpecular[2]=B;md->colorSpecular[3]=SHI;}
#endif //TEAPOT_SHADER_SPECULAR
void Teapot_MeshData_GetAabbHalfExtents(const Teapot_MeshData* md,float* halfAabb);
void Teapot_MeshData_GetAabbExtents(const Teapot_MeshData* md,float* aabb);
void Teapot_MeshData_GetAabbCenter(const Teapot_MeshData* md,float* center);
static __inline void Teapot_MeshData_SetOutlineEnabled(Teapot_MeshData* md,int meshOutlineEnabled) {md->outlineEnabled=meshOutlineEnabled;}
static __inline void Teapot_MeshData_SetMeshId(Teapot_MeshData* md,TeapotMeshEnum meshId) {md->meshId = meshId;}
void Teapot_MeshData_CalculateMvMatrix(Teapot_MeshData* md); // From mMatrix (called internally when Teapot_DrawMulti(...) is used)
void Teapot_MeshData_CalculateMvMatrixFromArray(Teapot_MeshData** meshes,int numMeshes); // From mMatrix (called internally when Teapot_DrawMulti(...) is used)
Teapot_MeshData* Teapot_MeshData_GetMeshUnderMouse(Teapot_MeshData* const* meshes,int numMeshes,int mouseX,int mouseY,const int* viewport4,tpoat* pOptionalDistanceOut);
Teapot_MeshData* Teapot_MeshData_GetMeshUnderMouseFromRay(Teapot_MeshData* const* meshes, int numMeshes, const tpoat* rayOrigin3, const tpoat* rayDir3, tpoat* pOptionalDistanceOut); /* ray in world space */
tpoat* Teapot_Helper_ExtractMatrix3x3(tpoat* __restrict m9Out,const tpoat* __restrict m16,tpoat* __restrict optionalTranslation3Out/*=NULL*/);
void Teapot_Helper_ExtractScalingFromTransformMatrix(const tpoat* __restrict m16,tpoat* __restrict scaOut3,tpoat* __restrict optionalMOut16);
void Teapot_DrawMulti(Teapot_MeshData** meshes,int numMeshes,int mustSortObjectsForTransparency); // 'mustSortObjectsForTransparency' requires glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); and glDisable(GL_BLEND); At the end it restores glDisable(GL_BLEND); if used.
void Teapot_DrawMulti_Mv(Teapot_MeshData* const* meshes,int numMeshes,int mustSortObjectsForTransparency); // Same as above, but use it only if you set or calculate all the Teapot_MeshData::mvMatrix[16] manually
void Teapot_MeshData_DrawAabb(const Teapot_MeshData* mesh);
int Teapot_MeshData_Depth_Sorter(const void* pmd0,const void* pmd1); // helper function used internally by Teapot_DrawMulti(...) when mustSortObjectsForTransparency==1 (for qsort)
//----------------------------------------------------------------------------------------
void Teapot_PostDraw(void); // unsets program and buffers for drawing
//----------------------------------------------------------------------------------------
void Teapot_GetMeshAabbCenter(TeapotMeshEnum meshId,float center[3]);
void Teapot_GetMeshAabbHalfExtents(TeapotMeshEnum meshId,float halfAabb[3]);
void Teapot_GetMeshAabbExtents(TeapotMeshEnum meshId,float aabb[3]);
void Teapot_GetMeshAabbMinAndMax(TeapotMeshEnum meshId,float aabbMin[3],float aabbMax[3]);
void Teapot_GetMeshScaledAabbHalfExtents(TeapotMeshEnum meshId,float halfAabb[3],const float scaling[3]);
void Teapot_GetMeshScaledAabbExtents(TeapotMeshEnum meshId,float aabb[3],const float scaling[3]);
void Teapot_Enable_ColorMaterial(void); // (*)
void Teapot_Disable_ColorMaterial(void);
int Teapot_Get_ColorMaterial_Enabled(void); // returns 0 or 1
// (*) When Teapot_Color_Material is enabled, the only change is that:
/* Teapot_SetColor(float R, float G, float B, float A)
internally adds the following calls:
Teapot_SetColorAmbient(...);
Teapot_SetColorSpecular(...);
with values derived from R,G,B,A.
*/
void Teapot_Enable_MeshOutline(void); // Needs glEnable(GL_CULL_FACE). Adds an outline around the mesh.
void Teapot_Disable_MeshOutline(void);
int Teapot_Get_MeshOutline_Enabled(void); // returns 0 or 1
void Teapot_Set_MeshOutline_Color(float R,float G, float B, float A); // A<1.0 requires glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); and glEnable(GL_BLEND); to make transparency work
void Teapot_Set_MeshOutline_Scaling(float scalingGreaterOrEqualThanOne); // default is 1.015f
void Teapot_Set_MeshOutline_Params(float polygonOffsetSlope, float polygonOffsetConstant); // defaults are -1.f, -250.f
static __inline tpoat* Teapot_Helper_IdentityMatrix(tpoat* __restrict result16) {tpoat* m = result16;m[0]=m[5]=m[10]=m[15]=1;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;return result16;}
static __inline tpoat* Teapot_Helper_IdentityMatrix3x3(tpoat* __restrict result9) {tpoat* m = result9;m[0]=m[4]=m[8]=1;m[1]=m[2]=m[3]=m[5]=m[6]=m[7]==0;return result9;}
static __inline void Teapot_Helper_CopyMatrix(tpoat* __restrict dst16,const tpoat* __restrict src16) {int i;for (i=0;i<16;i++) dst16[i]=src16[i];}
static __inline void Teapot_Helper_CopyMatrix3x3(tpoat* __restrict dst9,const tpoat* __restrict src9) {int i;for (i=0;i<9;i++) dst9[i]=src9[i];}
static __inline tpoat* Teapot_Helper_MultMatrixUncheckArgs(tpoat* __restrict result16,const tpoat* __restrict ml16,const tpoat* __restrict mr16) {
int i,i4;
# if (defined(TEAPOT_USE_SIMD) && !defined(TEAPOT_MATRIX_USE_DOUBLE_PRECISION) && 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);
}
# elif (defined(TEAPOT_USE_SIMD) && defined(TEAPOT_MATRIX_USE_DOUBLE_PRECISION) && 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 //MOL_USE_DOUBLE_PRECISION
/* reference implementation */
tpoat 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 //MOL_USE_DOUBLE_PRECISION
return result16;
}
static __inline tpoat* Teapot_Helper_MultMatrix(tpoat* __restrict result16,const tpoat* __restrict ml16,const tpoat* __restrict mr16) {
if (result16==ml16) {
tpoat ML16[16];Teapot_Helper_CopyMatrix(ML16,ml16);
return Teapot_Helper_MultMatrixUncheckArgs(result16,ML16,mr16);
}
else if (result16==mr16) {
tpoat MR16[16];Teapot_Helper_CopyMatrix(MR16,mr16);
return Teapot_Helper_MultMatrixUncheckArgs(result16,ml16,MR16);
}
return Teapot_Helper_MultMatrixUncheckArgs(result16,ml16,mr16);
}
void Teapot_Helper_LookAt(tpoat* __restrict mOut16,tpoat eyeX,tpoat eyeY,tpoat eyeZ,tpoat centerX,tpoat centerY,tpoat centerZ,tpoat upX,tpoat upY,tpoat upZ);
void Teapot_Helper_Perspective(tpoat* __restrict mOut16,tpoat degfovy,tpoat aspect, tpoat zNear, tpoat zFar);
void Teapot_Helper_Ortho(tpoat* __restrict mOut16,tpoat left,tpoat right, tpoat bottom, tpoat top,tpoat nearVal,tpoat farVal);
void Teapot_Helper_Ortho3D(tpoat* __restrict mOut16,tpoat cameraTargetDistance,tpoat degfovy,tpoat aspect,tpoat znear,tpoat zfar);
tpoat* Teapot_Helper_TranslateMatrix(tpoat* __restrict mInOut16,tpoat x,tpoat y,tpoat z);
tpoat* Teapot_Helper_RotateMatrix(tpoat* __restrict mInOut16,tpoat degAngle,tpoat x,tpoat y,tpoat z);
tpoat* Teapot_Helper_ScaleMatrix(tpoat* __restrict mInOut16,tpoat x,tpoat y,tpoat z);
int Teapot_Helper_InvertMatrix(tpoat* __restrict mOut16,const tpoat* __restrict m16);
void Teapot_Helper_InvertTransformMatrix(tpoat* __restrict mOut16,const tpoat* __restrict m16);
void Teapot_Helper_InvertTransformMatrixFast(tpoat* __restrict mOut16,const tpoat* __restrict m16);
tpoat* Teapot_Helper_ExtractMatrix3x3Inverse(tpoat* __restrict m9Out,const tpoat* __restrict m16);
tpoat* Teapot_Helper_ExtractMatrix3x3InverseFast(tpoat* __restrict m9Out,const tpoat* __restrict m16);
void Teapot_Helper_InvertTransformMatrixXZAxisInPlace(tpoat* __restrict m16);
tpoat* Teapot_Helper_InvertTransformMatrixXZAxis(tpoat* __restrict mOut16,const tpoat* __restrict m16);
void Teapot_Helper_GetFrustumPlaneEquations(tpoat planeEquationsOut[6][4],const tpoat* __restrict vpMatrix16,int normalizePlanes);
void Teapot_Helper_GetFrustumPoints(tpoat frustumPoints[8][4],const tpoat* __restrict vpMatrixInverse16); // frustumPoints[i][3]==1
// returns the frustum radius (by value) and the scalar (positive) distance from the the camera eye to the frustum center (by 'pFrustumCenterDistanceOut').
// 'cameraTargetDistanceForUnstableOrtho3DModeOnly_or_zero': the arg name is correct when the result is passed to 'Teapot_Helper_GetLightViewProjectionMatrix' functions.
// Otherwise simply set it when using an ortho3D camera frustum (that needs a camera-target distance), or just set it to zero.
tpoat Teapot_Helper_GetFrustumRadiusAndCenterDistance(tpoat* __restrict pFrustumCenterDistanceOut,tpoat cameraNearClippingPlane,tpoat cameraFarClippingPlane,tpoat cameraFovyDeg,tpoat cameraAspectRatio,tpoat cameraTargetDistanceForUnstableOrtho3DModeOnly_or_zero);
// 'frustumCenterOut3' is in world space (mandatory arg).
// 'cameraVMatrixInverse16' must be orthonormal (no scaling).
void Teapot_Helper_GetFrustumCenterFromCenterDistance(tpoat* __restrict frustumCenterOut3,const tpoat* __restrict cameraVMatrixInverse16,tpoat frustumCenterDistance);
// 'optionalPMatrixInverse16' is required only if you need to retrieve (one or more of) the arguments that follow them (otherwise their value is untouched).
// 'normalizedLightDirection3' must be in world space.
void Teapot_Helper_GetLightViewProjectionMatrixExtra(tpoat* __restrict lvpMatrixOut16
,const tpoat* __restrict cameraVMatrixInverse16,const tpoat* __restrict cameraFrustumCenterInWorldSpace3,tpoat cameraFrustumRadius
,const tpoat* __restrict normalizedLightDirection3,unsigned short shadowMapWidth,unsigned short shadowMapHeight
,const tpoat* __restrict optionalCameraPMatrixInverse16
,tpoat* __restrict optionalLightViewportClippingOut4,tpoat optionalCameraFrustumPointsInNDCLightSpaceOut[8][4]
,tpoat* __restrict optionalLVPMatrixForFrustumCullingUsageOut16 // Highly experimental and untested
);
// 'normalizedLightDirection3' must be in world space.
void Teapot_Helper_GetLightViewProjectionMatrix(tpoat* __restrict lvpMatrixOut16
,const tpoat* __restrict cameraVMatrixInverse16,const tpoat* __restrict cameraFrustumCenterInWorldSpace3,tpoat cameraFrustumRadius
,const tpoat* __restrict normalizedLightDirection3,unsigned short shadowMapWidth,unsigned short shadowMapHeight);
static __inline void Teapot_Helper_MatrixMulDir(const tpoat* __restrict m16,tpoat* __restrict dirOut3,const tpoat dirX,tpoat dirY,tpoat dirZ) {
//tpoat w;
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];
//w = dirX*m16[3] + dirY*m16[7] + dirZ*m16[11]; // + m[15] ?
//if (w!=0 && w!=1) {dirOut3[0]/=w;dirOut3[1]/=w;dirOut3[2]/=w;}
}
static __inline void Teapot_Helper_MatrixMulPos(const tpoat* __restrict m16,tpoat* __restrict posOut3,const tpoat posX,tpoat posY,tpoat 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];
}
static __inline void Teapot_Helper_MatrixMulPosWithWDivision(const tpoat* __restrict m16,tpoat* __restrict posOut3,const tpoat posX,tpoat posY,tpoat posZ) {
tpoat 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;}
}
static __inline void Teapot_Helper_NormalizePlane(tpoat* __restrict p4) {
tpoat len = p4[0]*p4[0]+p4[1]*p4[1]+p4[2]*p4[2];
if (len!=0) {
int i;len = sqrt(len);
for (i=0;i<4;i++) p4[i]/=len;
}
}
static __inline tpoat Teapot_Helper_Vector3Dot(const tpoat* a3,const tpoat* b3) {return a3[0]*b3[0]+a3[1]*b3[1]+a3[2]*b3[2];}
static __inline void Teapot_Helper_Vector3Normalize(tpoat* __restrict v3) {
tpoat len = Teapot_Helper_Vector3Dot(v3,v3);int i;
if (len!=0) {len = sqrt(len);for (i=0;i<3;i++) v3[i]/=len;}
}
static __inline void Teapot_Helper_Vector3Cross(tpoat* __restrict vOut3,const tpoat* __restrict a3,const tpoat* __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];
}
static __inline tpoat Teapot_Helper_Vector3DistSquared(const tpoat* __restrict a3,const tpoat* __restrict b3) {
const tpoat 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];
}
static __inline tpoat Teapot_Helper_Vector3Dist(const tpoat* __restrict a3,const tpoat* __restrict b3) {
const tpoat res = Teapot_Helper_Vector3DistSquared(a3,b3);
return res!=0 ? sqrt(res) : 0;
}
static __inline tpoat Teapot_Helper_Round(tpoat number) {return number < 0.0 ? ceil(number - 0.5) : floor(number + 0.5);}
// It "should" performs AABB test. mfMatrix16 is the matrix M so that: F*M = mvpMatrix (F being the matrix used to extract the frustum planes). Here we use: F=pMatrix and M=mvMatrix, but it could be: F=vpMatrix and M=mMatrix too.
int Teapot_Helper_IsVisible(const tpoat frustumPlanes[6][4],const tpoat*__restrict mfMatrix16,tpoat aabbMinX,tpoat aabbMinY,tpoat aabbMinZ,tpoat aabbMaxX,tpoat aabbMaxY,tpoat aabbMaxZ);
int Teapot_Helper_UnProject_MvpMatrixInv(tpoat winX,tpoat winY,tpoat winZ,const tpoat* __restrict mvpMatrixInv16,const int* viewport4,tpoat* objX,tpoat* objY,tpoat* objZ);
// Maps the specified window coordinates into object coordinates using mvMatrix16, pMatrix16, and viewport4.
// The result is stored in objX, objY, and objZ. A return value of 1 indicates success; a return value of 0 indicates failure.
// viewport4 Specifies the current viewport (as from a glGetIntegerv call).
int Teapot_Helper_UnProject(tpoat winX,tpoat winY,tpoat winZ,const tpoat* __restrict mvMatrix16,const tpoat* __restrict pMatrix16,const int* viewport4,tpoat* objX,tpoat* objY,tpoat* objZ);
void Teapot_Helper_UnProjectMouseCoords(tpoat* rayOriginOut3,tpoat* rayDirOut3,int mouseX,int mouseY,const tpoat* vpMatrixInv,const int* viewport4);
int Teapot_Helper_GetMeshUnderMouseFromRayGeneric(int numMeshes,void (*getMeshDataCallback)(int idx,tpoat* __restrict mMatrix16InOut,tpoat aabbMinOut[3],tpoat aabbMaxOut[3],void* userData),const tpoat* rayOrigin3,const tpoat* rayDir3,tpoat* pOptionalDistanceOut,void* userData);
// This should be equivalent to old: glLoadIdentity();gluPickMatrix(x,y,width,height,viewport); [plus glGet(...matrixmode...)]
tpoat* Teapot_Helper_GetPickMatrix(tpoat* __restrict mOut16,tpoat x,tpoat y,tpoat width,tpoat height,const int* viewport4);
static __inline void Teapot_Helper_ConvertMatrixd2f16(float* __restrict result16,const double* __restrict m16) {int i;for(i = 0; i < 16; i++) result16[i]=(float)m16[i];}
static __inline void Teapot_Helper_ConvertMatrixf2d16(double* __restrict result16,const float* __restrict m16) {int i;for(i = 0; i < 16; i++) result16[i]=(double)m16[i];}
static __inline void Teapot_Helper_ConvertMatrixd2f9(float* __restrict result9,const double* __restrict m9) {int i;for(i = 0; i < 9; i++) result9[i]=(float)m9[i];}
static __inline void Teapot_Helper_ConvertMatrixf2d9(double* __restrict result9,const float* __restrict m9) {int i;for(i = 0; i < 9; i++) result9[i]=(double)m9[i];}
// These draw an armature bone with the 'tail' placed in the matrix origin and 'length' directed in the 'y' relative axis (not affected by the TEAPOT_CENTER_MESHES_ON_FLOOR definition):
void Teapot_Helper_DrawArmatureBone(const tpoat mMatrix[16],tpoat length); // (optionally repeat this call for multiple draws)
void Teapot_Helper_DrawArmatureBone_Mv(const tpoat mvMatrix[16],tpoat length); // (optionally repeat this call for multiple draws) Note that this is not the model matrix, but: Teapot_Helper_MultMatrix(mvMatrix,vMatrix,mMatrix)
// Low-level API (use it at your own risk, or never use it at all!)
// These can be used to replace Teapot_PreDraw()
void Teapot_LowLevel_EnableVertexAttributes(int enableVertexAttribArray,int enableNormalAttribArray); // args are bool variables (0 or 1)
void Teapot_LowLevel_BindVertexBufferObject(void);
void Teapot_LowLevel_BindVertexBufferObjectAndEnableVertexAttributes(int enableVertexAttribArray,int enableNormalAttribArray); // args are bool variables (0 or 1)
void Teapot_LowLevel_BindShaderProgram(void);
void Teapot_LowLevel_StartDisablingLighting(void); // Use Teapot_SetAmbientColor() to set the color then (but ALPHA is still set through last call to Teapot_SetColor(...))
void Teapot_LowLevel_SetMvMatrixUniform(const tpoat mvMatrix[16]); // This just sets the uniform matrix
void Teapot_LowLevel_SetMvMatrixUniformFloat(const float mvMatrix[16]); // Same as above, but enforces single precision
void Teapot_LowLevel_DrawElements(TeapotMeshEnum meshId); // This just calls glDrawElements(...)
void Teapot_LowLevel_StopDisablingLighting(void);
// These can be used to replace Teapot_PostDraw()
void Teapot_LowLevel_UnbindShaderProgram(void);
void Teapot_LowLevel_UnbindVertexBufferObject(void);
void Teapot_LowLevel_DisableVertexAttributes(int disableVertexAttribArray,int disableNormalAttribArray); // args are bool variables (0 or 1)
void Teapot_LowLevel_UnbindVertexBufferObjectAndDisableVertexAttributes(int disableVertexAttribArray,int disableNormalAttribArray); // args are bool variables (0 or 1)
// These are just the two internal functions that compose Teapot_Helper_IsVisible(...), for advanced users only
void Teapot_Helper_LowLevel_OBB2AABB(tpoat* __restrict aabbMinMax6Out,const tpoat*__restrict mfMatrix16,tpoat aabbMinX,tpoat aabbMinY,tpoat aabbMinZ,tpoat aabbMaxX,tpoat aabbMaxY,tpoat aabbMaxZ);
int Teapot_Helper_LowLevel_IsAABBVisible(const tpoat frustumPlanes[6][4],const tpoat* __restrict aabbMinMax6);
#if (defined(DYNAMIC_RESOLUTION_H) && defined(TEAPOT_SHADER_USE_SHADOW_MAP))
#if ((defined(DYNAMIC_RESOLUTION_USE_DOUBLE_PRECISION) && defined(TEAPOT_USE_DOUBLE_PRECISION)) || (!defined(DYNAMIC_RESOLUTION_USE_DOUBLE_PRECISION) && !defined(TEAPOT_USE_DOUBLE_PRECISION)))
// lpvMatrix16 = lpMatrix16 * lvMatrix16 (light projection and view matrices)
void Teapot_HiLevel_DrawMulti_ShadowMap_Vp(Teapot_MeshData* const* pMeshData,int numMeshData,const tpoat* lvpMatrix16, float transparent_threshold, void (*optionalAdditionalObjectsCallback)(void* userData)/*=NULL*/,void* userData/*=NULL*/);
// Not always faster (overhead + no BVH). Anyway use: Teapot_Helper_GetFrustumPlaneEquations(vpMatrixFrustumPlaneEquations,lvpMatrix16,0);
void Teapot_HiLevel_DrawMulti_ShadowMap_Vp_WithFrustumCulling(Teapot_MeshData* const* pMeshData,int numMeshData,const tpoat* lvpMatrix16, const tpoat lvpMatrixFrustumPlaneEquations[6][4], float transparent_threshold, void (*optionalAdditionalObjectsCallback)(void* userData)/*=NULL*/,void* userData/*=NULL*/);
static __inline void Teapot_HiLevel_DrawMulti_ShadowMap(Teapot_MeshData* const* pMeshData,int numMeshData,const tpoat* lpMatrix16, const tpoat* lvMatrix16, float transparent_threshold, void (*optionalAdditionalObjectsCallback)(void* userData)/*=NULL*/,void* userData/*=NULL*/) {
tpoat lpvMatrix16[16];Teapot_Helper_MultMatrix(lpvMatrix16,lpMatrix16,lvMatrix16);
Teapot_HiLevel_DrawMulti_ShadowMap_Vp(pMeshData,numMeshData,lpvMatrix16,transparent_threshold,optionalAdditionalObjectsCallback,userData);
}
#endif
#endif
#ifdef __cplusplus
}
#endif
#endif //TEAPOT_H_
#ifdef TEAPOT_IMPLEMENTATION
#ifndef TEAPOT_IMPLEMENTATION_H
#define TEAPOT_IMPLEMENTATION_H // Additional guard
#ifndef COMPILER_SUPPORTS_GCC_DIAGNOSTIC // We define this
# if (defined(__GNUC__) || defined(__MINGW__) || defined(__clang__))
# define COMPILER_SUPPORTS_GCC_DIAGNOSTIC
# endif
#endif
#ifdef COMPILER_SUPPORTS_GCC_DIAGNOSTIC
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wpragmas"
# pragma GCC diagnostic ignored "-Wunknown-warning-option"
# pragma GCC diagnostic ignored "-Wrestrict"
#endif //COMPILER_SUPPORTS_GCC_DIAGNOSTIC
#include <math.h> // sqrt
#include <stdlib.h> // qsort
#include <string.h> // memcpy
#ifdef TEAPOT_USE_OPENMP
#include <omp.h>
#endif //TEAPOT_USE_OPENMP
#ifndef TEAPOT_SHADER_SHADOW_MAP_PCF
# ifdef DYNAMIC_RESOLUTION_SHADOW_USE_PCF
# define TEAPOT_SHADER_SHADOW_MAP_PCF DYNAMIC_RESOLUTION_SHADOW_USE_PCF
# else //DYNAMIC_RESOLUTION_SHADOW_USE_PCF
# define TEAPOT_SHADER_SHADOW_MAP_PCF 0
# endif //DYNAMIC_RESOLUTION_SHADOW_USE_PCF
#endif //TEAPOT_SHADER_SHADOW_MAP_PCF
#ifndef STR_MACRO
# define STR_MACRO(s) #s
#endif //STR_MACRO
#ifndef XSTR_MACRO
# define XSTR_MACRO(s) STR_MACRO(s)
#endif //XSTR_MACRO
#ifdef __cplusplus
extern "C" {
#endif
__inline static void Teapot_Helper_GlUniformMatrix4v(GLint location,GLsizei count,GLboolean transpose,const tpoat* value) {
const float* fvalue = NULL;
# ifndef TEAPOT_MATRIX_USE_DOUBLE_PRECISION
fvalue = value;
# else
float val[16];Teapot_Helper_ConvertMatrixd2f16(val,value);fvalue=val;
# endif
glUniformMatrix4fv(location,count,transpose,fvalue);
}
__inline static void Teapot_Helper_GlUniformMatrix3v(GLint location,GLsizei count,GLboolean transpose,const tpoat* value) {
const float* fvalue = NULL;
# ifndef TEAPOT_MATRIX_USE_DOUBLE_PRECISION
fvalue = value;
# else
float val[9];Teapot_Helper_ConvertMatrixd2f9(val,value);fvalue=val;
# endif
glUniformMatrix3fv(location,count,transpose,fvalue);
}
__inline static void Teapot_Helper_GlUniform3v(GLint location,GLsizei count,const tpoat* value) {
const float* fvalue = NULL;
# ifndef TEAPOT_MATRIX_USE_DOUBLE_PRECISION
fvalue = value;
# else
const float val[3] = {(float)value[0],(float)value[1],(float)value[2]};fvalue = val;
# endif
glUniform3fv(location,count,fvalue);
}
static const char* TeapotVS[] = {
# if (defined(__EMSCRIPTEN__) && defined(TEAPOT_SHADER_USE_SHADOW_MAP) && (TEAPOT_SHADER_SHADOW_MAP_PCF>0))
"#version 300 es\n" // emscripten needs: -s USE_WEBGL2=1
"#define attribute in\n"
"#define varying out\n"
# endif //__EMSCRIPTEN__ && TEAPOT_SHADER_USE_SHADOW_MAP && TEAPOT_SHADER_SHADOW_MAP_PCF
"#ifdef GL_ES\n"
"precision highp float;\n"
"#endif\n"
"attribute vec4 a_vertex;\n"
"attribute vec3 a_normal;\n"
"uniform mat4 u_mvMatrix;\n"
"uniform mat4 u_pMatrix;\n"
# ifdef TEAPOT_SHADER_USE_ACCURATE_NORMALS
# ifndef TEAPOT_SHADER_HINT_ACCURATE_NORMALS_GPU
" uniform vec3 u_nCoefficients;\n"
# endif
# endif //TEAPOT_SHADER_USE_ACCURATE_NORMALS
"uniform vec4 u_scaling;\n"
"uniform vec3 u_lightVector;\n"
# ifndef TEAPOT_SHADER_SPECULAR
"uniform vec4 u_colorData[2];\n" // RGBA diffuse + RGBA ambient
# else //TEAPOT_SHADER_SPECULAR
"uniform vec4 u_colorData[3];\n" // RGBA diffuse + RGBA ambient + RGBS specular (s=SHININESS)
"#define u_colorSpecular u_colorData[2]\n"
# endif //TEAPOT_SHADER_SPECULAR
"#define u_color u_colorData[0]\n"
"#define u_colorAmbient u_colorData[1]\n"
# ifdef TEAPOT_SHADER_FOG
"uniform vec4 u_fogDistances;\n"
# ifndef TEAPOT_SHADER_FOG_HINT_FRAGMENT_SHADER
"uniform vec3 u_fogColor;\n"
# else //TEAPOT_SHADER_FOG_HINT_FRAGMENT_SHADER
"varying float v_fog;\n"
# endif // TEAPOT_SHADER_FOG_HINT_FRAGMENT_SHADER
# endif // TEAPOT_SHADER_FOG
# ifdef TEAPOT_SHADER_USE_SHADOW_MAP
"uniform mat4 u_biasedShadowMvpMatrix;\n"
"varying vec4 v_shadowCoord;\n"
# endif //TEAPOT_SHADER_USE_SHADOW_MAP
"varying vec4 v_color;\n"
"\n"
"void main() {\n"
# ifndef TEAPOT_SHADER_USE_ACCURATE_NORMALS
# ifndef TEAPOT_SHADER_NORMALIZE_NORMALS
" vec3 normalEyeSpace = vec3(u_mvMatrix * vec4(a_normal, 0.0));\n"
# else
" vec3 normalEyeSpace = normalize(vec3(u_mvMatrix * vec4(a_normal, 0.0)));\n"
# endif
# else //TEAPOT_SHADER_USE_ACCURATE_NORMALS
// https://lxjk.github.io/2017/10/01/Stop-Using-Normal-Matrix.html
# ifdef TEAPOT_SHADER_HINT_ACCURATE_NORMALS_GPU
" vec3 u_nCoefficients=vec3(1.0/(dot(u_mvMatrix[0].xyz,u_mvMatrix[0].xyz)*u_scaling[0]),1.0/(dot(u_mvMatrix[1].xyz,u_mvMatrix[1].xyz)*u_scaling[1]),1.0/(dot(u_mvMatrix[2].xyz,u_mvMatrix[2].xyz)*u_scaling[2]));\n"
# endif
" //vec3 normalEyeSpace = normalize(mat3(u_mvMatrix)*(a_normal*u_nCoefficients));\n"
" vec3 normalEyeSpace = normalize(vec3(u_mvMatrix * vec4(a_normal*u_nCoefficients, 0.0)));\n"
# endif //TEAPOT_SHADER_USE_ACCURATE_NORMALS
" float fDot = max(0.0, dot(normalEyeSpace,u_lightVector));\n"
" vec4 vertexScaledWorldSpace = a_vertex * u_scaling;\n"
" vec4 vertexScaledEyeSpace = u_mvMatrix*vertexScaledWorldSpace;\n"
" //vertexScaledEyeSpace.xyz/vertexScaledEyeSpace.w;\n" // is this necessary ?
# ifdef TEAPOT_SHADER_USE_SHADOW_MAP
" v_shadowCoord = u_biasedShadowMvpMatrix*vertexScaledWorldSpace;\n"
//" v_shadowCoord = u_biasedShadowMvpMatrix*(u_mvMatrix*vertexScaledWorldSpace);\n"
# endif //TEAPOT_SHADER_USE_SHADOW_MAP
# ifndef TEAPOT_SHADER_SPECULAR
" v_color = vec4(u_colorAmbient.rgb + u_color.rgb*(fDot*u_colorAmbient.a),u_color.a);\n"
# else // TEAPOT_SHADER_SPECULAR
" vec3 E = normalize(-vertexScaledEyeSpace.xyz);\n"
" vec3 halfVector = normalize(u_lightVector + E);\n"
" float nxHalf = max(0.005,dot(normalEyeSpace, halfVector));\n"
" vec3 specularColor = u_colorSpecular.rgb * pow(nxHalf,u_colorSpecular.a);\n"
" v_color = vec4(u_colorAmbient.rgb + (u_color.rgb*fDot+specularColor.rgb)*u_colorAmbient.a,u_color.a);\n"
# endif // TEAPOT_SHADER_SPECULAR
# ifdef TEAPOT_SHADER_FOG
# ifdef TEAPOT_SHADER_FOG_HINT_FRAGMENT_SHADER
" v_fog = \n"
# else //TEAPOT_SHADER_FOG_HINT_FRAGMENT_SHADER
" float v_fog = \n"
# endif //TEAPOT_SHADER_FOG_HINT_FRAGMENT_SHADER
//" 1.0 - (u_fogDistances.y-abs(vertexScaledEyeSpace.z))*u_fogDistances.w;\n"
" 1.0 - min((u_fogDistances.y+vertexScaledEyeSpace.z)*u_fogDistances.w,1.0);\n"
# ifndef TEAPOT_SHADER_FOG_HINT_FRAGMENT_SHADER
" v_color.rgb = mix(v_color.rgb,u_fogColor.rgb,v_fog);\n"
# endif //TEAPOT_SHADER_FOG_HINT_FRAGMENT_SHADER
# endif //TEAPOT_SHADER_FOG
" gl_Position = u_pMatrix * vertexScaledEyeSpace;\n"
"}\n"
};
// 0.2 is the global ambient factor
static const char* TeapotFS[] = {
# if (defined(__EMSCRIPTEN__) && defined(TEAPOT_SHADER_USE_SHADOW_MAP) && (TEAPOT_SHADER_SHADOW_MAP_PCF>0))
"#version 300 es\n" // emscripten needs: -s USE_WEBGL2=1
"#define varying in\n"
"#define shadow2D texture\n"
"#define gl_FragColor FragColor\n"
# endif //__EMSCRIPTEN__ && TEAPOT_SHADER_USE_SHADOW_MAP && TEAPOT_SHADER_SHADOW_MAP_PCF
"#define TABSSQRT " XSTR_MACRO(TEAPOT_SHADER_SHADOW_MAP_PCF) "\n"
"#ifdef GL_ES\n"
"precision mediump float;\n"
"precision lowp int;\n"
"#endif\n"
"varying vec4 v_color;\n"
# ifdef TEAPOT_SHADER_FOG
# ifdef TEAPOT_SHADER_FOG_HINT_FRAGMENT_SHADER
"uniform vec3 u_fogColor;\n"
"varying float v_fog;\n"
# endif //TEAPOT_SHADER_FOG_HINT_FRAGMENT_SHADER
# endif
# if (defined(__EMSCRIPTEN__) && defined(TEAPOT_SHADER_USE_SHADOW_MAP) && (TEAPOT_SHADER_SHADOW_MAP_PCF>0))
"out vec4 gl_FragColor;\n"
# endif //__EMSCRIPTEN__ && TEAPOT_SHADER_USE_SHADOW_MAP && TEAPOT_SHADER_SHADOW_MAP_PCF
# ifdef TEAPOT_SHADER_USE_SHADOW_MAP
# if (TEAPOT_SHADER_SHADOW_MAP_PCF<1)
" uniform sampler2D u_shadowMap;\n"
# else // TEAPOT_SHADER_SHADOW_MAP_PCF
# ifdef __EMSCRIPTEN__
" uniform lowp sampler2DShadow u_shadowMap;\n"
# else //__EMSCRIPTEN__
" uniform sampler2DShadow u_shadowMap;\n"
# endif //__EMSCRIPTEN__
# endif // TEAPOT_SHADER_SHADOW_MAP_PCF
"uniform vec2 u_shadowDarkening;\n" // .x = fDarkeningFactor [10.0-80.0], .y = min value clamp [0.0-1.0]
"uniform vec4 u_ambientColor;\n"
"uniform float u_shadowMapFactor; // in [0,1]: default: 1\n"
"uniform vec2 u_shadowMapTexelIncrement;\n"
"varying vec4 v_shadowCoord;\n"
# endif //TEAPOT_SHADER_USE_SHADOW_MAP
"\n"
"void main() {\n"
"float shadowFactor = 1.0;\n"
# ifdef TEAPOT_SHADER_USE_SHADOW_MAP
"vec4 shadowCoordinateWdivide = v_shadowCoord/v_shadowCoord.w;\n"
"\n"
# if (TEAPOT_SHADER_SHADOW_MAP_PCF<1)
" shadowFactor = clamp(exp(u_shadowDarkening.x*(1.0-u_ambientColor.a)*(texture2D(u_shadowMap,(shadowCoordinateWdivide.st*u_shadowMapFactor)).r - shadowCoordinateWdivide.z)),u_shadowDarkening.y,1.0);\n"
# elif (TEAPOT_SHADER_SHADOW_MAP_PCF==1)
" shadowFactor = shadow2D(u_shadowMap,vec3(shadowCoordinateWdivide.st,shadowCoordinateWdivide.z-u_shadowDarkening.x));\n"
" shadowFactor = u_shadowDarkening.y + (1.0-u_shadowDarkening.y)*shadowFactor;\n"
# else //TEAPOT_SHADER_SHADOW_MAP_PCF
" shadowFactor=0.0;\n"
" float biasedShadowCoordinateZ = shadowCoordinateWdivide.z-u_shadowDarkening.x;\n;"
# if (TEAPOT_SHADER_SHADOW_MAP_PCF== (TEAPOT_SHADER_SHADOW_MAP_PCF/2)*2) // even
" const float edgeVal = 0.5+float((TABSSQRT-1)/2);\n"
" const float startVal = -edgeVal;\n"
" const float endVal = edgeVal+0.5;\n" // we use +0.5 and < instead of <= in the for loop (more robust)
# else // TEAPOT_SHADER_SHADOW_MAP_PCF odd
" const float edgeVal = float((TABSSQRT-1)/2);\n"
" const float startVal = -edgeVal;\n"
" const float endVal = edgeVal+0.5;\n" // we use +0.5 and < instead of <= in the for loop (more robust)
# endif // TEAPOT_SHADER_SHADOW_MAP_PCF
" float x,y;shadowFactor=0.0;\n;"
" for (y=startVal; y<endVal; y+=1.0) {\n"
" for (x=startVal; x<endVal; x+=1.0) {\n"
" shadowFactor += shadow2D(u_shadowMap,vec3(shadowCoordinateWdivide.st+vec2(x*u_shadowMapTexelIncrement.x,y*u_shadowMapTexelIncrement.y),biasedShadowCoordinateZ));\n"
" }\n"
" }\n"
" shadowFactor/=float(TABSSQRT*TABSSQRT);\n"
" shadowFactor = u_shadowDarkening.y + (1.0-u_shadowDarkening.y)*shadowFactor;\n"
# endif //TEAPOT_SHADER_SHADOW_MAP_PCF
# endif //TEAPOT_SHADER_USE_SHADOW_MAP
# ifdef TEAPOT_SHADER_FOG
# ifdef TEAPOT_SHADER_FOG_HINT_FRAGMENT_SHADER
" //float v_fog = 1.0 - (u_fogDistances.y-gl_FragCoord.z/gl_FragCoord.w)*u_fogDistances.w;\n" // Best quality but expensive...
" gl_FragColor = vec4(mix(v_color.rgb*shadowFactor,u_fogColor.rgb,v_fog).rgb,v_color.a);\n"
" return;\n"
# endif //TEAPOT_SHADER_FOG_HINT_FRAGMENT_SHADER
# endif //TEAPOT_SHADER_FOG
" gl_FragColor = vec4(v_color.rgb*shadowFactor,v_color.a);\n"
"}\n"
};
typedef struct {
float color[4];
float colorAmbient[4];
float colorSpecular[4];
GLuint programId;
GLuint vertexBuffer,elementBuffer;
int startInds[TEAPOT_MESH_COUNT];
int numInds[TEAPOT_MESH_COUNT];
float halfExtents[TEAPOT_MESH_COUNT][3];
float centerPoint[TEAPOT_MESH_COUNT][3];
float aabbMin[TEAPOT_MESH_COUNT][3];
float aabbMax[TEAPOT_MESH_COUNT][3];
GLint aLoc_vertex,aLoc_normal;
GLint uLoc_mvMatrix,uLoc_pMatrix,uLoc_nCoefficients,uLoc_scaling,
uLoc_lightVector;
GLint uLoc_color,uLoc_colorAmbient,uLoc_colorSpecular;
GLint uLoc_fogColor,uLoc_fogDistances,
uLoc_biasedShadowMvpMatrix,uLoc_shadowMap,uLoc_shadowDarkening,uLoc_shadowMapFactor,uLoc_shadowMapTexelIncrement;
tpoat lightDirectionWorldSpace[3];
tpoat lightDirectionViewSpace[3];
tpoat pMatrix[16];
tpoat pMatrixFrustum[6][4];
tpoat vMatrix[16];
float scaling[3];
tpoat vMatrixInverse[16];
tpoat biasedShadowVpMatrix[16]; // actually what we store here is: biasedShadowVpMatrix * vMatrixInverse (so that we must multiply it per mvMatrix, instead of mMatrix)
float shadowDarkening,shadowClamp;
int colorMaterialEnabled;
int meshOutlineEnabled;
float colorMeshOutline[4];
float scalingMeshOutline;
float polygonOffsetSlope;
float polygonOffsetConstant;
} Teapot_Inner_Struct;
static Teapot_Inner_Struct TIS;
static TeapotInitCallback gTeapotInitCallback=NULL;
static TeapotInitUserMeshCallback gTeapotInitUserMeshCallback=NULL;
static __inline GLuint Teapot_LoadShaderProgramFromSource(const char* vs,const char* fs);
void Teapot_Helper_LookAt(tpoat* __restrict mOut16,tpoat eyeX,tpoat eyeY,tpoat eyeZ,tpoat centerX,tpoat centerY,tpoat centerZ,tpoat upX,tpoat upY,tpoat upZ) {
tpoat* m = mOut16;
const tpoat eps = 0.0001;
tpoat F[3] = {eyeX-centerX,eyeY-centerY,eyeZ-centerZ};
tpoat length = F[0]*F[0]+F[1]*F[1]+F[2]*F[2]; // length2 now
tpoat up[3] = {upX,upY,upZ};
tpoat 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]};
tpoat 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;
/*
S0 S1 S2 0 1 0 0 -ex
U0 U1 U2 0 * 0 1 0 -ey
F0 F1 F2 0 0 0 1 -ez
0 0 0 1 0 0 0 1
*/
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;
}
void Teapot_Helper_Perspective(tpoat* __restrict mOut16,tpoat degfovy,tpoat aspect, tpoat zNear, tpoat zFar) {
tpoat* res = mOut16;
const tpoat eps = 0.0001;
tpoat f = 1.f/tan(degfovy*1.5707963268f/180.0); //cotg
tpoat 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;
}
void Teapot_Helper_Ortho(tpoat* __restrict mOut16,tpoat left,tpoat right, tpoat bottom, tpoat top,tpoat nearVal,tpoat farVal) {
tpoat* res = mOut16;
const tpoat eps = 0.0001;
tpoat Drl = (right-left);
tpoat Dtb = (top-bottom);
tpoat 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;
}
void Teapot_Helper_Ortho3D(tpoat* __restrict mOut16,tpoat cameraTargetDistance,tpoat degfovy,tpoat aspect,tpoat znear,tpoat zfar) {
// Warning: this function might be WRONG! Use it at your own risk!
const tpoat FOVTG=tan(degfovy*(tpoat)(3.14159265358979323846/360.0));
tpoat y=cameraTargetDistance*FOVTG;//=(zfar-znear)*0.5f;
//tpoat y=(cameraTargetDistance<zfar?cameraTargetDistance:zfar)*FOVTG; // or maybe this?
tpoat x=y*aspect;
//Teapot_Helper_Ortho(mOut16, -x, x, -y, y, znear, zfar); // I thought this was correct
//Teapot_Helper_Ortho(mOut16, -x, x, -y, y, -zfar, znear); // But this works better in my test-case
Teapot_Helper_Ortho(mOut16, -x, x, -y, y, -zfar, -znear); // Or maybe this?
}
tpoat* Teapot_Helper_TranslateMatrix(tpoat* __restrict mInOut16,tpoat x,tpoat y,tpoat z) {
#ifdef TEAPOT_USE_LEGACY_CODE
const tpoat m[16] = {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
x, y, z, 1};
return Teapot_Helper_MultMatrix(mInOut16,mInOut16,m);
#else
int i;for (i=0;i<3;i++) mInOut16[12+i]+=mInOut16[i]*x+mInOut16[4+i]*y+mInOut16[8+i]*z;
return mInOut16;