-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathm3d.h
6547 lines (6286 loc) · 298 KB
/
m3d.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
/*
* m3d.h
* https://gitlab.com/bztsrc/model3d
*
* Copyright (C) 2020 bzt (bztsrc@gitlab)
*
* 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.
*
* @brief ANSI C89 / C++11 single header importer / exporter SDK for the Model 3D (.M3D) format
* https://gitlab.com/bztsrc/model3d
*
* PNG decompressor included from (with minor modifications to make it C89 valid):
* stb_image - v2.13 - public domain image loader - http://nothings.org/stb_image.h
*
* @version: 1.0.0
*/
#ifndef _M3D_H_
#define _M3D_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
/*** configuration ***/
#ifndef M3D_MALLOC
# define M3D_MALLOC(sz) malloc(sz)
#endif
#ifndef M3D_REALLOC
# define M3D_REALLOC(p,nsz) realloc(p,nsz)
#endif
#ifndef M3D_FREE
# define M3D_FREE(p) free(p)
#endif
#ifndef M3D_LOG
# define M3D_LOG(x)
#endif
#ifndef M3D_APIVERSION
#define M3D_APIVERSION 0x0100
#ifndef M3D_DOUBLE
typedef float M3D_FLOAT;
#ifndef M3D_EPSILON
/* carefully choosen for IEEE 754 don't change */
#define M3D_EPSILON ((M3D_FLOAT)1e-7)
#endif
#else
typedef double M3D_FLOAT;
#ifndef M3D_EPSILON
#define M3D_EPSILON ((M3D_FLOAT)1e-14)
#endif
#endif
#if !defined(M3D_SMALLINDEX)
typedef uint32_t M3D_INDEX;
typedef uint16_t M3D_VOXEL;
#define M3D_UNDEF 0xffffffff
#define M3D_INDEXMAX 0xfffffffe
#define M3D_VOXUNDEF 0xffff
#define M3D_VOXCLEAR 0xfffe
#else
typedef uint16_t M3D_INDEX;
typedef uint8_t M3D_VOXEL;
#define M3D_UNDEF 0xffff
#define M3D_INDEXMAX 0xfffe
#define M3D_VOXUNDEF 0xff
#define M3D_VOXCLEAR 0xfe
#endif
#define M3D_NOTDEFINED 0xffffffff
#ifndef M3D_NUMBONE
#define M3D_NUMBONE 4
#endif
#ifndef M3D_BONEMAXLEVEL
#define M3D_BONEMAXLEVEL 64
#endif
#ifndef _MSC_VER
#ifndef _inline
#define _inline __inline__
#endif
#define _pack __attribute__((packed))
#define _unused __attribute__((unused))
#else
#define _inline
#define _pack
#define _unused __pragma(warning(suppress:4100))
#endif
#ifndef __cplusplus
#define _register register
#else
#define _register
#endif
/*** File format structures ***/
/**
* M3D file format structure
* 3DMO m3dchunk_t file header chunk, may followed by compressed data
* PRVW preview chunk (optional)
* HEAD m3dhdr_t model header chunk
* n x m3dchunk_t more chunks follow
* CMAP color map chunk (optional)
* TMAP texture map chunk (optional)
* VRTS vertex data chunk (optional if it's a material library)
* BONE bind-pose skeleton, bone hierarchy chunk (optional)
* n x m3db_t contains propably more, but at least one bone
* n x m3ds_t skin group records
* MTRL* material chunk(s), can be more (optional)
* n x m3dp_t each material contains propapbly more, but at least one property
* the properties are configurable with a static array, see m3d_propertytypes
* n x m3dchunk_t at least one, but maybe more face chunks
* PROC* procedural face, or
* MESH* triangle mesh (vertex index list) or
* VOXT, VOXD* voxel image (converted to mesh) or
* SHPE* mathematical shapes like parameterized surfaces
* LBLS* annotation label chunks, can be more (optional)
* ACTN* action chunk(s), animation-pose skeletons, can be more (optional)
* n x m3dfr_t each action contains probably more, but at least one frame
* n x m3dtr_t each frame contains probably more, but at least one transformation
* ASET* inlined asset chunk(s), can be more (optional)
* OMD3 end chunk
*
* Typical chunks for a game engine: 3DMO, HEAD, CMAP, TMAP, VRTS, BONE, MTRL, MESH, ACTN, OMD3
* Typical chunks for distibution: 3DMO, PRVW, HEAD, CMAP, TMAP, VRTS, BONE, MTRL, MESH, ACTN, ASET, OMD3
* Typical chunks for voxel image: 3DMO, HEAD, CMAP, MTRL, VOXT, VOXD, VOXD, VOXD, OMD3
* Typical chunks for CAD software: 3DMO, PRVW, HEAD, CMAP, TMAP, VRTS, MTRL, SHPE, LBLS, OMD3
*/
#ifdef _MSC_VER
#pragma pack(push)
#pragma pack(1)
#endif
typedef struct {
char magic[4];
uint32_t length;
float scale; /* deliberately not M3D_FLOAT */
uint32_t types;
} _pack m3dhdr_t;
typedef struct {
char magic[4];
uint32_t length;
} _pack m3dchunk_t;
#ifdef _MSC_VER
#pragma pack(pop)
#endif
/*** in-memory model structure ***/
/* textmap entry */
typedef struct {
M3D_FLOAT u;
M3D_FLOAT v;
} m3dti_t;
#define m3d_textureindex_t m3dti_t
/* texture */
typedef struct {
char *name; /* texture name */
uint8_t *d; /* pixels data */
uint16_t w; /* width */
uint16_t h; /* height */
uint8_t f; /* format, 1 = grayscale, 2 = grayscale+alpha, 3 = rgb, 4 = rgba */
} m3dtx_t;
#define m3d_texturedata_t m3dtx_t
typedef struct {
M3D_INDEX vertexid;
M3D_FLOAT weight;
} m3dw_t;
#define m3d_weight_t m3dw_t
/* bone entry */
typedef struct {
M3D_INDEX parent; /* parent bone index */
char *name; /* name for this bone */
M3D_INDEX pos; /* vertex index position */
M3D_INDEX ori; /* vertex index orientation (quaternion) */
M3D_INDEX numweight; /* number of controlled vertices */
m3dw_t *weight; /* weights for those vertices */
M3D_FLOAT mat4[16]; /* transformation matrix */
} m3db_t;
#define m3d_bone_t m3db_t
/* skin: bone per vertex entry */
typedef struct {
M3D_INDEX boneid[M3D_NUMBONE];
M3D_FLOAT weight[M3D_NUMBONE];
} m3ds_t;
#define m3d_skin_t m3ds_t
/* vertex entry */
typedef struct {
M3D_FLOAT x; /* 3D coordinates and weight */
M3D_FLOAT y;
M3D_FLOAT z;
M3D_FLOAT w;
uint32_t color; /* default vertex color */
M3D_INDEX skinid; /* skin index */
#ifdef M3D_VERTEXTYPE
uint8_t type;
#endif
} m3dv_t;
#define m3d_vertex_t m3dv_t
/* material property formats */
enum {
m3dpf_color,
m3dpf_uint8,
m3dpf_uint16,
m3dpf_uint32,
m3dpf_float,
m3dpf_map
};
typedef struct {
uint8_t format;
uint8_t id;
#ifdef M3D_ASCII
#define M3D_PROPERTYDEF(f,i,n) { (f), (i), (char*)(n) }
char *key;
#else
#define M3D_PROPERTYDEF(f,i,n) { (f), (i) }
#endif
} m3dpd_t;
/* material property types */
/* You shouldn't change the first 8 display and first 4 physical property. Assign the rest as you like. */
enum {
m3dp_Kd = 0, /* scalar display properties */
m3dp_Ka,
m3dp_Ks,
m3dp_Ns,
m3dp_Ke,
m3dp_Tf,
m3dp_Km,
m3dp_d,
m3dp_il,
m3dp_Pr = 64, /* scalar physical properties */
m3dp_Pm,
m3dp_Ps,
m3dp_Ni,
m3dp_Nt,
m3dp_map_Kd = 128, /* textured display map properties */
m3dp_map_Ka,
m3dp_map_Ks,
m3dp_map_Ns,
m3dp_map_Ke,
m3dp_map_Tf,
m3dp_map_Km, /* bump map */
m3dp_map_D,
m3dp_map_N, /* normal map */
m3dp_map_Pr = 192, /* textured physical map properties */
m3dp_map_Pm,
m3dp_map_Ps,
m3dp_map_Ni,
m3dp_map_Nt
};
enum { /* aliases */
m3dp_bump = m3dp_map_Km,
m3dp_map_il = m3dp_map_N,
m3dp_refl = m3dp_map_Pm
};
/* material property */
typedef struct {
uint8_t type; /* property type, see "m3dp_*" enumeration */
union {
uint32_t color; /* if value is a color, m3dpf_color */
uint32_t num; /* if value is a number, m3dpf_uint8, m3pf_uint16, m3dpf_uint32 */
float fnum; /* if value is a floating point number, m3dpf_float */
M3D_INDEX textureid; /* if value is a texture, m3dpf_map */
} value;
} m3dp_t;
#define m3d_property_t m3dp_t
/* material entry */
typedef struct {
char *name; /* name of the material */
uint8_t numprop; /* number of properties */
m3dp_t *prop; /* properties array */
} m3dm_t;
#define m3d_material_t m3dm_t
/* face entry */
typedef struct {
M3D_INDEX materialid; /* material index */
M3D_INDEX vertex[3]; /* 3D points of the triangle in CCW order */
M3D_INDEX normal[3]; /* normal vectors */
M3D_INDEX texcoord[3]; /* UV coordinates */
#ifdef M3D_VERTEXMAX
M3D_INDEX paramid; /* parameter index */
M3D_INDEX vertmax[3]; /* maximum 3D points of the triangle in CCW order */
#endif
} m3df_t;
#define m3d_face_t m3df_t
typedef struct {
uint16_t count;
char *name;
} m3dvi_t;
#define m3d_voxelitem_t m3dvi_t
#define m3d_parameter_t m3dvi_t
/* voxel types (voxel palette) */
typedef struct {
char *name; /* technical name of the voxel */
uint8_t rotation; /* rotation info */
uint16_t voxshape; /* voxel shape */
M3D_INDEX materialid; /* material index */
uint32_t color; /* default voxel color */
M3D_INDEX skinid; /* skin index */
uint8_t numitem; /* number of sub-voxels */
m3dvi_t *item; /* list of sub-voxels */
} m3dvt_t;
#define m3d_voxeltype_t m3dvt_t
/* voxel data blocks */
typedef struct {
char *name; /* name of the block */
int32_t x, y, z; /* position */
uint32_t w, h, d; /* dimension */
uint8_t uncertain; /* probability */
uint8_t groupid; /* block group id */
M3D_VOXEL *data; /* voxel data, indices to voxel type */
} m3dvx_t;
#define m3d_voxel_t m3dvx_t
/* shape command types. must match the row in m3d_commandtypes */
enum {
/* special commands */
m3dc_use = 0, /* use material */
m3dc_inc, /* include another shape */
m3dc_mesh, /* include part of polygon mesh */
/* approximations */
m3dc_div, /* subdivision by constant resolution for both u, v */
m3dc_sub, /* subdivision by constant, different for u and v */
m3dc_len, /* spacial subdivision by maxlength */
m3dc_dist, /* subdivision by maxdistance and maxangle */
/* modifiers */
m3dc_degu, /* degree for both u, v */
m3dc_deg, /* separate degree for u and v */
m3dc_rangeu, /* range for u */
m3dc_range, /* range for u and v */
m3dc_paru, /* u parameters (knots) */
m3dc_parv, /* v parameters */
m3dc_trim, /* outer trimming curve */
m3dc_hole, /* inner trimming curve */
m3dc_scrv, /* spacial curve */
m3dc_sp, /* special points */
/* helper curves */
m3dc_bez1, /* Bezier 1D */
m3dc_bsp1, /* B-spline 1D */
m3dc_bez2, /* bezier 2D */
m3dc_bsp2, /* B-spline 2D */
/* surfaces */
m3dc_bezun, /* Bezier 3D with control, UV, normal */
m3dc_bezu, /* with control and UV */
m3dc_bezn, /* with control and normal */
m3dc_bez, /* control points only */
m3dc_nurbsun, /* B-spline 3D */
m3dc_nurbsu,
m3dc_nurbsn,
m3dc_nurbs,
m3dc_conn, /* connect surfaces */
/* geometrical */
m3dc_line,
m3dc_polygon,
m3dc_circle,
m3dc_cylinder,
m3dc_shpere,
m3dc_torus,
m3dc_cone,
m3dc_cube
};
/* shape command argument types */
enum {
m3dcp_mi_t = 1, /* material index */
m3dcp_hi_t, /* shape index */
m3dcp_fi_t, /* face index */
m3dcp_ti_t, /* texture map index */
m3dcp_vi_t, /* vertex index */
m3dcp_qi_t, /* vertex index for quaternions */
m3dcp_vc_t, /* coordinate or radius, float scalar */
m3dcp_i1_t, /* int8 scalar */
m3dcp_i2_t, /* int16 scalar */
m3dcp_i4_t, /* int32 scalar */
m3dcp_va_t /* variadic arguments */
};
#define M3D_CMDMAXARG 8 /* if you increase this, add more arguments to the macro below */
typedef struct {
#ifdef M3D_ASCII
#define M3D_CMDDEF(t,n,p,a,b,c,d,e,f,g,h) { (char*)(n), (p), { (a), (b), (c), (d), (e), (f), (g), (h) } }
char *key;
#else
#define M3D_CMDDEF(t,n,p,a,b,c,d,e,f,g,h) { (p), { (a), (b), (c), (d), (e), (f), (g), (h) } }
#endif
uint8_t p;
uint8_t a[M3D_CMDMAXARG];
} m3dcd_t;
/* shape command */
typedef struct {
uint16_t type; /* shape type */
uint32_t *arg; /* arguments array */
} m3dc_t;
#define m3d_shapecommand_t m3dc_t
/* shape entry */
typedef struct {
char *name; /* name of the mathematical shape */
M3D_INDEX group; /* group this shape belongs to or -1 */
uint32_t numcmd; /* number of commands */
m3dc_t *cmd; /* commands array */
} m3dh_t;
#define m3d_shape_t m3dh_t
/* label entry */
typedef struct {
char *name; /* name of the annotation layer or NULL */
char *lang; /* language code or NULL */
char *text; /* the label text */
uint32_t color; /* color */
M3D_INDEX vertexid; /* the vertex the label refers to */
} m3dl_t;
#define m3d_label_t m3dl_t
/* frame transformations / working copy skeleton entry */
typedef struct {
M3D_INDEX boneid; /* selects a node in bone hierarchy */
M3D_INDEX pos; /* vertex index new position */
M3D_INDEX ori; /* vertex index new orientation (quaternion) */
} m3dtr_t;
#define m3d_transform_t m3dtr_t
/* animation frame entry */
typedef struct {
uint32_t msec; /* frame's position on the timeline, timestamp */
M3D_INDEX numtransform; /* number of transformations in this frame */
m3dtr_t *transform; /* transformations */
} m3dfr_t;
#define m3d_frame_t m3dfr_t
/* model action entry */
typedef struct {
char *name; /* name of the action */
uint32_t durationmsec; /* duration in millisec (1/1000 sec) */
M3D_INDEX numframe; /* number of frames in this animation */
m3dfr_t *frame; /* frames array */
} m3da_t;
#define m3d_action_t m3da_t
/* inlined asset */
typedef struct {
char *name; /* asset name (same pointer as in texture[].name) */
uint8_t *data; /* compressed asset data */
uint32_t length; /* compressed data length */
} m3di_t;
#define m3d_inlinedasset_t m3di_t
/*** in-memory model structure ***/
#define M3D_FLG_FREERAW (1<<0)
#define M3D_FLG_FREESTR (1<<1)
#define M3D_FLG_MTLLIB (1<<2)
#define M3D_FLG_GENNORM (1<<3)
typedef struct {
m3dhdr_t *raw; /* pointer to raw data */
char flags; /* internal flags */
signed char errcode; /* returned error code */
char vc_s, vi_s, si_s, ci_s, ti_s, bi_s, nb_s, sk_s, fc_s, hi_s, fi_s, vd_s, vp_s; /* decoded sizes for types */
char *name; /* name of the model, like "Utah teapot" */
char *license; /* usage condition or license, like "MIT", "LGPL" or "BSD-3clause" */
char *author; /* nickname, email, homepage or github URL etc. */
char *desc; /* comments, descriptions. May contain '\n' newline character */
M3D_FLOAT scale; /* the model's bounding cube's size in SI meters */
M3D_INDEX numcmap;
uint32_t *cmap; /* color map */
M3D_INDEX numtmap;
m3dti_t *tmap; /* texture map indices */
M3D_INDEX numtexture;
m3dtx_t *texture; /* uncompressed textures */
M3D_INDEX numbone;
m3db_t *bone; /* bone hierarchy */
M3D_INDEX numvertex;
m3dv_t *vertex; /* vertex data */
M3D_INDEX numskin;
m3ds_t *skin; /* skin data */
M3D_INDEX nummaterial;
m3dm_t *material; /* material list */
#ifdef M3D_VERTEXMAX
M3D_INDEX numparam;
m3dvi_t *param; /* parameters and their values list */
#endif
M3D_INDEX numface;
m3df_t *face; /* model face, polygon (triangle) mesh */
M3D_INDEX numvoxtype;
m3dvt_t *voxtype; /* model face, voxel types */
M3D_INDEX numvoxel;
m3dvx_t *voxel; /* model face, cubes compressed into voxels */
M3D_INDEX numshape;
m3dh_t *shape; /* model face, shape commands */
M3D_INDEX numlabel;
m3dl_t *label; /* annotation labels */
M3D_INDEX numaction;
m3da_t *action; /* action animations */
M3D_INDEX numinlined;
m3di_t *inlined; /* inlined assets */
M3D_INDEX numextra;
m3dchunk_t **extra; /* unknown chunks, application / engine specific data probably */
m3di_t preview; /* preview chunk */
} m3d_t;
/*** export parameters ***/
#define M3D_EXP_INT8 0
#define M3D_EXP_INT16 1
#define M3D_EXP_FLOAT 2
#define M3D_EXP_DOUBLE 3
#define M3D_EXP_NOCMAP (1<<0)
#define M3D_EXP_NOMATERIAL (1<<1)
#define M3D_EXP_NOFACE (1<<2)
#define M3D_EXP_NONORMAL (1<<3)
#define M3D_EXP_NOTXTCRD (1<<4)
#define M3D_EXP_FLIPTXTCRD (1<<5)
#define M3D_EXP_NORECALC (1<<6)
#define M3D_EXP_IDOSUCK (1<<7)
#define M3D_EXP_NOBONE (1<<8)
#define M3D_EXP_NOACTION (1<<9)
#define M3D_EXP_INLINE (1<<10)
#define M3D_EXP_EXTRA (1<<11)
#define M3D_EXP_NOZLIB (1<<14)
#define M3D_EXP_ASCII (1<<15)
#define M3D_EXP_NOVRTMAX (1<<16)
/*** error codes ***/
#define M3D_SUCCESS 0
#define M3D_ERR_ALLOC -1
#define M3D_ERR_BADFILE -2
#define M3D_ERR_UNIMPL -65
#define M3D_ERR_UNKPROP -66
#define M3D_ERR_UNKMESH -67
#define M3D_ERR_UNKIMG -68
#define M3D_ERR_UNKFRAME -69
#define M3D_ERR_UNKCMD -70
#define M3D_ERR_UNKVOX -71
#define M3D_ERR_TRUNC -72
#define M3D_ERR_CMAP -73
#define M3D_ERR_TMAP -74
#define M3D_ERR_VRTS -75
#define M3D_ERR_BONE -76
#define M3D_ERR_MTRL -77
#define M3D_ERR_SHPE -78
#define M3D_ERR_VOXT -79
#define M3D_ERR_ISFATAL(x) ((x) < 0 && (x) > -65)
/* callbacks */
typedef unsigned char *(*m3dread_t)(char *filename, unsigned int *size); /* read file contents into buffer */
typedef void (*m3dfree_t)(void *buffer); /* free file contents buffer */
typedef int (*m3dtxsc_t)(const char *name, const void *script, uint32_t len, m3dtx_t *output); /* interpret texture script */
typedef int (*m3dprsc_t)(const char *name, const void *script, uint32_t len, m3d_t *model); /* interpret surface script */
#endif /* ifndef M3D_APIVERSION */
/*** C prototypes ***/
/* import / export */
m3d_t *m3d_load(unsigned char *data, m3dread_t readfilecb, m3dfree_t freecb, m3d_t *mtllib);
unsigned char *m3d_save(m3d_t *model, int quality, int flags, unsigned int *size);
void m3d_free(m3d_t *model);
/* generate animation pose skeleton */
m3dtr_t *m3d_frame(m3d_t *model, M3D_INDEX actionid, M3D_INDEX frameid, m3dtr_t *skeleton);
m3db_t *m3d_pose(m3d_t *model, M3D_INDEX actionid, uint32_t msec);
/* private prototypes used by both importer and exporter */
char *_m3d_safestr(char *in, int morelines);
/*** C implementation ***/
#ifdef M3D_IMPLEMENTATION
#if !defined(M3D_NOIMPORTER) || defined(M3D_EXPORTER)
/* material property definitions */
static m3dpd_t m3d_propertytypes[] = {
M3D_PROPERTYDEF(m3dpf_color, m3dp_Kd, "Kd"), /* diffuse color */
M3D_PROPERTYDEF(m3dpf_color, m3dp_Ka, "Ka"), /* ambient color */
M3D_PROPERTYDEF(m3dpf_color, m3dp_Ks, "Ks"), /* specular color */
M3D_PROPERTYDEF(m3dpf_float, m3dp_Ns, "Ns"), /* specular exponent */
M3D_PROPERTYDEF(m3dpf_color, m3dp_Ke, "Ke"), /* emissive (emitting light of this color) */
M3D_PROPERTYDEF(m3dpf_color, m3dp_Tf, "Tf"), /* transmission color */
M3D_PROPERTYDEF(m3dpf_float, m3dp_Km, "Km"), /* bump strength */
M3D_PROPERTYDEF(m3dpf_float, m3dp_d, "d"), /* dissolve (transparency) */
M3D_PROPERTYDEF(m3dpf_uint8, m3dp_il, "il"), /* illumination model (informational, ignored by PBR-shaders) */
M3D_PROPERTYDEF(m3dpf_float, m3dp_Pr, "Pr"), /* roughness */
M3D_PROPERTYDEF(m3dpf_float, m3dp_Pm, "Pm"), /* metallic, also reflection */
M3D_PROPERTYDEF(m3dpf_float, m3dp_Ps, "Ps"), /* sheen */
M3D_PROPERTYDEF(m3dpf_float, m3dp_Ni, "Ni"), /* index of refraction (optical density) */
M3D_PROPERTYDEF(m3dpf_float, m3dp_Nt, "Nt"), /* thickness of face in millimeter, for printing */
/* aliases, note that "map_*" aliases are handled automatically */
M3D_PROPERTYDEF(m3dpf_map, m3dp_map_Km, "bump"),
M3D_PROPERTYDEF(m3dpf_map, m3dp_map_N, "map_N"),/* as normal map has no scalar version, it's counterpart is 'il' */
M3D_PROPERTYDEF(m3dpf_map, m3dp_map_Pm, "refl")
};
/* shape command definitions. if more commands start with the same string, the longer must come first */
static m3dcd_t m3d_commandtypes[] = {
/* technical */
M3D_CMDDEF(m3dc_use, "use", 1, m3dcp_mi_t, 0, 0, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_inc, "inc", 3, m3dcp_hi_t, m3dcp_vi_t, m3dcp_qi_t, m3dcp_vi_t, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_mesh, "mesh", 1, m3dcp_fi_t, m3dcp_fi_t, m3dcp_vi_t, m3dcp_qi_t, m3dcp_vi_t, 0, 0, 0),
/* approximations */
M3D_CMDDEF(m3dc_div, "div", 1, m3dcp_vc_t, 0, 0, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_sub, "sub", 2, m3dcp_vc_t, m3dcp_vc_t, 0, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_len, "len", 1, m3dcp_vc_t, 0, 0, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_dist, "dist", 2, m3dcp_vc_t, m3dcp_vc_t, 0, 0, 0, 0, 0, 0),
/* modifiers */
M3D_CMDDEF(m3dc_degu, "degu", 1, m3dcp_i1_t, 0, 0, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_deg, "deg", 2, m3dcp_i1_t, m3dcp_i1_t, 0, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_rangeu, "rangeu", 1, m3dcp_ti_t, 0, 0, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_range, "range", 2, m3dcp_ti_t, m3dcp_ti_t, 0, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_paru, "paru", 2, m3dcp_va_t, m3dcp_vc_t, 0, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_parv, "parv", 2, m3dcp_va_t, m3dcp_vc_t, 0, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_trim, "trim", 3, m3dcp_va_t, m3dcp_ti_t, m3dcp_i2_t, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_hole, "hole", 3, m3dcp_va_t, m3dcp_ti_t, m3dcp_i2_t, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_scrv, "scrv", 3, m3dcp_va_t, m3dcp_ti_t, m3dcp_i2_t, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_sp, "sp", 2, m3dcp_va_t, m3dcp_vi_t, 0, 0, 0, 0, 0, 0),
/* helper curves */
M3D_CMDDEF(m3dc_bez1, "bez1", 2, m3dcp_va_t, m3dcp_vi_t, 0, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_bsp1, "bsp1", 2, m3dcp_va_t, m3dcp_vi_t, 0, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_bez2, "bez2", 2, m3dcp_va_t, m3dcp_vi_t, 0, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_bsp2, "bsp2", 2, m3dcp_va_t, m3dcp_vi_t, 0, 0, 0, 0, 0, 0),
/* surfaces */
M3D_CMDDEF(m3dc_bezun, "bezun", 4, m3dcp_va_t, m3dcp_vi_t, m3dcp_ti_t, m3dcp_vi_t, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_bezu, "bezu", 3, m3dcp_va_t, m3dcp_vi_t, m3dcp_ti_t, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_bezn, "bezn", 3, m3dcp_va_t, m3dcp_vi_t, m3dcp_vi_t, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_bez, "bez", 2, m3dcp_va_t, m3dcp_vi_t, 0, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_nurbsun, "nurbsun", 4, m3dcp_va_t, m3dcp_vi_t, m3dcp_ti_t, m3dcp_vi_t, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_nurbsu, "nurbsu", 3, m3dcp_va_t, m3dcp_vi_t, m3dcp_ti_t, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_nurbsn, "nurbsn", 3, m3dcp_va_t, m3dcp_vi_t, m3dcp_vi_t, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_nurbs, "nurbs", 2, m3dcp_va_t, m3dcp_vi_t, 0, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_conn, "conn", 6, m3dcp_i2_t, m3dcp_ti_t, m3dcp_i2_t, m3dcp_i2_t, m3dcp_ti_t, m3dcp_i2_t, 0, 0),
/* geometrical */
M3D_CMDDEF(m3dc_line, "line", 2, m3dcp_va_t, m3dcp_vi_t, 0, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_polygon, "polygon", 2, m3dcp_va_t, m3dcp_vi_t, 0, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_circle, "circle", 3, m3dcp_vi_t, m3dcp_qi_t, m3dcp_vc_t, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_cylinder,"cylinder",6, m3dcp_vi_t, m3dcp_qi_t, m3dcp_vc_t, m3dcp_vi_t, m3dcp_qi_t, m3dcp_vc_t, 0, 0),
M3D_CMDDEF(m3dc_shpere, "shpere", 2, m3dcp_vi_t, m3dcp_vc_t, 0, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_torus, "torus", 4, m3dcp_vi_t, m3dcp_qi_t, m3dcp_vc_t, m3dcp_vc_t, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_cone, "cone", 3, m3dcp_vi_t, m3dcp_vi_t, m3dcp_vi_t, 0, 0, 0, 0, 0),
M3D_CMDDEF(m3dc_cube, "cube", 3, m3dcp_vi_t, m3dcp_vi_t, m3dcp_vi_t, 0, 0, 0, 0, 0)
};
#endif
#include <stdlib.h>
#include <string.h>
#if !defined(M3D_NOIMPORTER) && !defined(STBI_INCLUDE_STB_IMAGE_H)
/* PNG decompressor from
stb_image - v2.23 - public domain image loader - http://nothings.org/stb_image.h
*/
static const char *_m3dstbi__g_failure_reason;
enum
{
STBI_default = 0,
STBI_grey = 1,
STBI_grey_alpha = 2,
STBI_rgb = 3,
STBI_rgb_alpha = 4
};
enum
{
STBI__SCAN_load=0,
STBI__SCAN_type,
STBI__SCAN_header
};
typedef unsigned short _m3dstbi_us;
typedef uint16_t _m3dstbi__uint16;
typedef int16_t _m3dstbi__int16;
typedef uint32_t _m3dstbi__uint32;
typedef int32_t _m3dstbi__int32;
typedef struct
{
_m3dstbi__uint32 img_x, img_y;
int img_n, img_out_n;
void *io_user_data;
int read_from_callbacks;
int buflen;
unsigned char buffer_start[128];
unsigned char *img_buffer, *img_buffer_end;
unsigned char *img_buffer_original, *img_buffer_original_end;
} _m3dstbi__context;
typedef struct
{
int bits_per_channel;
int num_channels;
int channel_order;
} _m3dstbi__result_info;
#define STBI_ASSERT(v)
#define STBI_NOTUSED(v) (void)sizeof(v)
#define STBI__BYTECAST(x) ((unsigned char) ((x) & 255))
#define STBI_MALLOC(sz) M3D_MALLOC(sz)
#define STBI_REALLOC(p,newsz) M3D_REALLOC(p,newsz)
#define STBI_FREE(p) M3D_FREE(p)
#define STBI_REALLOC_SIZED(p,oldsz,newsz) STBI_REALLOC(p,newsz)
_inline static unsigned char _m3dstbi__get8(_m3dstbi__context *s)
{
if (s->img_buffer < s->img_buffer_end)
return *s->img_buffer++;
return 0;
}
_inline static int _m3dstbi__at_eof(_m3dstbi__context *s)
{
return s->img_buffer >= s->img_buffer_end;
}
static void _m3dstbi__skip(_m3dstbi__context *s, int n)
{
if (n < 0) {
s->img_buffer = s->img_buffer_end;
return;
}
s->img_buffer += n;
}
static int _m3dstbi__getn(_m3dstbi__context *s, unsigned char *buffer, int n)
{
if (s->img_buffer+n <= s->img_buffer_end) {
memcpy(buffer, s->img_buffer, n);
s->img_buffer += n;
return 1;
} else
return 0;
}
static int _m3dstbi__get16be(_m3dstbi__context *s)
{
int z = _m3dstbi__get8(s);
return (z << 8) + _m3dstbi__get8(s);
}
static _m3dstbi__uint32 _m3dstbi__get32be(_m3dstbi__context *s)
{
_m3dstbi__uint32 z = _m3dstbi__get16be(s);
return (z << 16) + _m3dstbi__get16be(s);
}
#define _m3dstbi__err(x,y) _m3dstbi__errstr(y)
static int _m3dstbi__errstr(const char *str)
{
_m3dstbi__g_failure_reason = str;
return 0;
}
_inline static void *_m3dstbi__malloc(size_t size)
{
return STBI_MALLOC(size);
}
static int _m3dstbi__addsizes_valid(int a, int b)
{
if (b < 0) return 0;
return a <= 2147483647 - b;
}
static int _m3dstbi__mul2sizes_valid(int a, int b)
{
if (a < 0 || b < 0) return 0;
if (b == 0) return 1;
return a <= 2147483647/b;
}
static int _m3dstbi__mad2sizes_valid(int a, int b, int add)
{
return _m3dstbi__mul2sizes_valid(a, b) && _m3dstbi__addsizes_valid(a*b, add);
}
static int _m3dstbi__mad3sizes_valid(int a, int b, int c, int add)
{
return _m3dstbi__mul2sizes_valid(a, b) && _m3dstbi__mul2sizes_valid(a*b, c) &&
_m3dstbi__addsizes_valid(a*b*c, add);
}
static void *_m3dstbi__malloc_mad2(int a, int b, int add)
{
if (!_m3dstbi__mad2sizes_valid(a, b, add)) return NULL;
return _m3dstbi__malloc(a*b + add);
}
static void *_m3dstbi__malloc_mad3(int a, int b, int c, int add)
{
if (!_m3dstbi__mad3sizes_valid(a, b, c, add)) return NULL;
return _m3dstbi__malloc(a*b*c + add);
}
static unsigned char _m3dstbi__compute_y(int r, int g, int b)
{
return (unsigned char) (((r*77) + (g*150) + (29*b)) >> 8);
}
static unsigned char *_m3dstbi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y)
{
int i,j;
unsigned char *good;
if (req_comp == img_n) return data;
STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
good = (unsigned char *) _m3dstbi__malloc_mad3(req_comp, x, y, 0);
if (good == NULL) {
STBI_FREE(data);
_m3dstbi__err("outofmem", "Out of memory");
return NULL;
}
for (j=0; j < (int) y; ++j) {
unsigned char *src = data + j * x * img_n ;
unsigned char *dest = good + j * x * req_comp;
#define STBI__COMBO(a,b) ((a)*8+(b))
#define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
switch (STBI__COMBO(img_n, req_comp)) {
STBI__CASE(1,2) { dest[0]=src[0], dest[1]=255; } break;
STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=255; } break;
STBI__CASE(2,1) { dest[0]=src[0]; } break;
STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; } break;
STBI__CASE(3,4) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=255; } break;
STBI__CASE(3,1) { dest[0]=_m3dstbi__compute_y(src[0],src[1],src[2]); } break;
STBI__CASE(3,2) { dest[0]=_m3dstbi__compute_y(src[0],src[1],src[2]), dest[1] = 255; } break;
STBI__CASE(4,1) { dest[0]=_m3dstbi__compute_y(src[0],src[1],src[2]); } break;
STBI__CASE(4,2) { dest[0]=_m3dstbi__compute_y(src[0],src[1],src[2]), dest[1] = src[3]; } break;
STBI__CASE(4,3) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; } break;
default: STBI_ASSERT(0);
}
#undef STBI__CASE
}
STBI_FREE(data);
return good;
}
static _m3dstbi__uint16 _m3dstbi__compute_y_16(int r, int g, int b)
{
return (_m3dstbi__uint16) (((r*77) + (g*150) + (29*b)) >> 8);
}
static _m3dstbi__uint16 *_m3dstbi__convert_format16(_m3dstbi__uint16 *data, int img_n, int req_comp, unsigned int x, unsigned int y)
{
int i,j;
_m3dstbi__uint16 *good;
if (req_comp == img_n) return data;
STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
good = (_m3dstbi__uint16 *) _m3dstbi__malloc(req_comp * x * y * 2);
if (good == NULL) {
STBI_FREE(data);
_m3dstbi__err("outofmem", "Out of memory");
return NULL;
}
for (j=0; j < (int) y; ++j) {
_m3dstbi__uint16 *src = data + j * x * img_n ;
_m3dstbi__uint16 *dest = good + j * x * req_comp;
#define STBI__COMBO(a,b) ((a)*8+(b))
#define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
switch (STBI__COMBO(img_n, req_comp)) {
STBI__CASE(1,2) { dest[0]=src[0], dest[1]=0xffff; } break;
STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=0xffff; } break;
STBI__CASE(2,1) { dest[0]=src[0]; } break;
STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; } break;
STBI__CASE(3,4) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=0xffff; } break;
STBI__CASE(3,1) { dest[0]=_m3dstbi__compute_y_16(src[0],src[1],src[2]); } break;
STBI__CASE(3,2) { dest[0]=_m3dstbi__compute_y_16(src[0],src[1],src[2]), dest[1] = 0xffff; } break;
STBI__CASE(4,1) { dest[0]=_m3dstbi__compute_y_16(src[0],src[1],src[2]); } break;
STBI__CASE(4,2) { dest[0]=_m3dstbi__compute_y_16(src[0],src[1],src[2]), dest[1] = src[3]; } break;
STBI__CASE(4,3) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; } break;
default: STBI_ASSERT(0);
}
#undef STBI__CASE
}
STBI_FREE(data);
return good;
}
#define STBI__ZFAST_BITS 9
#define STBI__ZFAST_MASK ((1 << STBI__ZFAST_BITS) - 1)
typedef struct
{
_m3dstbi__uint16 fast[1 << STBI__ZFAST_BITS];
_m3dstbi__uint16 firstcode[16];
int maxcode[17];
_m3dstbi__uint16 firstsymbol[16];
unsigned char size[288];
_m3dstbi__uint16 value[288];
} _m3dstbi__zhuffman;
_inline static int _m3dstbi__bitreverse16(int n)
{
n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1);
n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2);
n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4);
n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8);
return n;
}
_inline static int _m3dstbi__bit_reverse(int v, int bits)
{
STBI_ASSERT(bits <= 16);
return _m3dstbi__bitreverse16(v) >> (16-bits);
}
static int _m3dstbi__zbuild_huffman(_m3dstbi__zhuffman *z, unsigned char *sizelist, int num)
{
int i,k=0;
int code, next_code[16], sizes[17];
memset(sizes, 0, sizeof(sizes));
memset(z->fast, 0, sizeof(z->fast));
for (i=0; i < num; ++i)
++sizes[sizelist[i]];
sizes[0] = 0;
for (i=1; i < 16; ++i)
if (sizes[i] > (1 << i))
return _m3dstbi__err("bad sizes", "Corrupt PNG");
code = 0;
for (i=1; i < 16; ++i) {
next_code[i] = code;
z->firstcode[i] = (_m3dstbi__uint16) code;
z->firstsymbol[i] = (_m3dstbi__uint16) k;
code = (code + sizes[i]);
if (sizes[i])
if (code-1 >= (1 << i)) return _m3dstbi__err("bad codelengths","Corrupt PNG");
z->maxcode[i] = code << (16-i);
code <<= 1;
k += sizes[i];
}
z->maxcode[16] = 0x10000;
for (i=0; i < num; ++i) {
int s = sizelist[i];
if (s) {
int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s];
_m3dstbi__uint16 fastv = (_m3dstbi__uint16) ((s << 9) | i);
z->size [c] = (unsigned char ) s;
z->value[c] = (_m3dstbi__uint16) i;
if (s <= STBI__ZFAST_BITS) {
int j = _m3dstbi__bit_reverse(next_code[s],s);
while (j < (1 << STBI__ZFAST_BITS)) {
z->fast[j] = fastv;
j += (1 << s);
}
}
++next_code[s];
}
}
return 1;
}
typedef struct
{