-
Notifications
You must be signed in to change notification settings - Fork 2
/
tc_vox.h
1635 lines (1390 loc) · 64.5 KB
/
tc_vox.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
/*
* tc_vox.h: MagicaVoxel *.vox file loader.
*
* DEPENDS:
* VERSION: 0.2.1 (2024-10-23)
* LICENSE: CC0 & Boost (dual-licensed)
* AUTHOR: Tim Čas
* URL: https://github.com/darkuranium/tclib
*
* VERSION HISTORY:
* 0.2.1 properly handle "simple" .vox files, i.e. those without nodes but only model(s)
* fix an issue with transforms not being properly reset when we finish iterating a node
* 0.2.0 added rOBJ & IMAP chunk handling
* fixed parsing of MATL chunks in some files (it appears that some files use a material ID of 0, and others use 256 for the same thing)
* reworked a number of datastructures, transform-related logic is now based on tcvox_transform_t
* added iterator, transform, and bounds APIs, to ease the loading of large (multi-model) files
* 0.1.0 initial public release
*
* TODOS:
* - Program & usage samples
* - Better handling of versions other than 150 & 200
* - Make the iterator API support animations
* - Parse out common rOBJ types
*/
#ifndef TC_VOX_H_
#define TC_VOX_H_
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#ifndef TC__STATIC_SIZE
#if __STDC_VERSION__ >= 199901
#define TC__STATIC_SIZE(N) static N
#else
#define TC__STATIC_SIZE(N) N
#endif
#endif
typedef enum tcvox_material_type
{
TCVOX_MATERIAL_TYPE_UNKNOWN,
TCVOX_MATERIAL_TYPE_DIFFUSE,// <default> (_diffuse?)
TCVOX_MATERIAL_TYPE_METAL, // _metal
TCVOX_MATERIAL_TYPE_GLASS, // _glass
TCVOX_MATERIAL_TYPE_EMIT, // _emit
TCVOX_MATERIAL_TYPE_BLEND, // _blend; combines all of the above
TCVOX_MATERIAL_TYPE_CLOUD, // _media
} tcvox_material_type_t;
typedef enum tcvox_media_type
{
TCVOX_MEDIA_TYPE_ABSORB,
TCVOX_MEDIA_TYPE_SCATTER,
TCVOX_MEDIA_TYPE_EMISSIVE,
TCVOX_MEDIA_TYPE_SUBSURFACE,
} tcvox_media_type_t;
typedef struct tcvox_attr
{
char* key;
char* value;
} tcvox_attr_t;
typedef union tcvox_ivec3
{
struct { int32_t x, y, z; };
int32_t xyz[3];
} tcvox_ivec3_t;
typedef struct tcvox_rotation_axis
{
uint8_t index : 2;
uint8_t sign : 1;
uint8_t : 5;
} tcvox_rotation_axis_t;
typedef union tcvox_rotation
{
struct { tcvox_rotation_axis_t x, y, z; };
tcvox_rotation_axis_t xyz[3];
} tcvox_rotation_t;
typedef struct tcvox_transform
{
tcvox_rotation_t r;
tcvox_ivec3_t t;
} tcvox_transform_t;
typedef struct tcvox_material
{
tcvox_material_type_t type; // _type
// roughness, index_of_refraction, and density seem to always be provided
// (probably an artefact of the materials preserving hidden values)
// METAL & GLASS:
float roughness; // _rough
float index_of_refraction; // 1 + _ior
// METAL:
float metalness; // _metal
float specular; // _sp
// GLASS:
float alpha; // 1 - _alpha [_trans?]
// GLASS & CLOUD:
tcvox_media_type_t media_type; // 0=ABSORB, 1=SCATTER, 2=
float scatter_phase; // _g; used if media_type==SCATTER
float density; // _d ; UI 0-100 is mapped to 0.0-0.1
// EMIT:
float emissiveness; // _emit * pow(10, 1 + _flux) + _ldr
struct
{
// EMIT:
float emit, flux, ldr;
} raw;
} tcvox_material_t;
typedef struct tcvox_palette
{
uint32_t abgr[256];
tcvox_material_t materials[256];
char* names[256];
} tcvox_palette_t;
typedef struct tcvox_frame
{
uint32_t nattrs;
tcvox_attr_t* attrs;
// extracted from `attrs`
tcvox_transform_t transform; // via r & t
uint32_t index; // frame index
} tcvox_frame_t;
typedef struct tcvox_voxel
{
uint8_t xyz[3];
uint8_t index;
} tcvox_voxel_t;
typedef struct tcvox_model
{
uint32_t id;
tcvox_ivec3_t size;
uint32_t nvoxels;
tcvox_voxel_t* voxels;
} tcvox_model_t;
typedef struct tcvox_layer
{
uint32_t id;
uint32_t nattrs;
tcvox_attr_t* attrs;
// extracted from `attrs`
char* name;
bool is_hidden;
uint32_t color_abgr;
} tcvox_layer_t;
typedef enum tcvox_camera_mode
{
TCVOX_CAMERA_MODE_UNKNOWN,
TCVOX_CAMERA_MODE_PERSPECTIVE, // _pers
} tcvox_camera_mode_t;
typedef struct tcvox_camera
{
uint32_t id;
uint32_t nattrs;
tcvox_attr_t* attrs;
// extracted from `attrs`
tcvox_camera_mode_t mode;
float focus[3];
float angle[3];
float radius;
float frustum;
float fov;
} tcvox_camera_t;
typedef struct tcvox_group_node
{
uint32_t id;
uint32_t nattrs;
tcvox_attr_t* attrs;
uint32_t nchildren;
struct tcvox_transform_node** children;
} tcvox_group_node_t;
typedef struct tcvox_shape_node_model
{
tcvox_model_t* model;
uint32_t nattrs;
tcvox_attr_t* attrs;
// extracted from `attrs`
uint32_t starting_frame;
} tcvox_shape_node_model_t;
typedef struct tcvox_shape_node
{
uint32_t id;
uint32_t nattrs;
tcvox_attr_t* attrs;
uint32_t nmodels;
tcvox_shape_node_model_t* models;
} tcvox_shape_node_t;
typedef struct tcvox_transform_node
{
uint32_t id;
uint32_t nattrs;
tcvox_attr_t* attrs;
// extracted from `attrs`
char* name;
bool is_hidden;
// only one of these two will be populated
struct
{
tcvox_group_node_t* group;
tcvox_shape_node_t* shape;
} child;
tcvox_layer_t* layer;
uint32_t nframes;
tcvox_frame_t* frames;
} tcvox_transform_node_t;
typedef enum tcvox_node_ref_type
{
TCVOX_NODE_REF_TYPE_NONE = 0,
TCVOX_NODE_REF_TYPE_GROUP = 1,
TCVOX_NODE_REF_TYPE_TRANSFORM = 2,
TCVOX_NODE_REF_TYPE_SHAPE = 3,
} tcvox_node_ref_type_t;
typedef struct tcvox_node_ref
{
uint32_t index : 30;
uint32_t type : 2;
} tcvox_node_ref_t;
typedef struct tcvox_object
{
uint32_t nattrs;
tcvox_attr_t* attrs;
// extracted from `attrs`
char* type;
} tcvox_object_t;
typedef struct tcvox_scene
{
const char* error;
tcvox_palette_t palette;
uint32_t nmodels;
tcvox_model_t* models;
uint32_t nlayers;
tcvox_layer_t* layers;
uint32_t ncameras;
tcvox_camera_t* cameras;
uint32_t nobjects;
tcvox_object_t* objects;
/*
`true` if file had no nodes whatsoever.
Note that if it had at least 1 model, we'll create a single shape (`index[0]` & `shapes[0]`) containing all the models.
This keeps the user access the same regardless of file layout.
*/
bool no_nodes_in_file;
struct
{
// root node sits at index[0]
uint32_t nindex;
tcvox_node_ref_t* index;
uint32_t ngroups;
tcvox_group_node_t* groups;
uint32_t nshapes;
tcvox_shape_node_t* shapes;
uint32_t ntransforms;
tcvox_transform_node_t* transforms;
} nodes;
} tcvox_scene_t;
typedef struct tcvox_iter
{
tcvox_scene_t* scene;
tcvox_shape_node_t* shape;
tcvox_transform_t transform;
bool include_hidden : 1;
bool is_finished : 1;
// private data follows
struct
{
uint32_t mem, len;
struct tcvox_iter_stack_entry* ptr;
} _stack;
tcvox_shape_node_t* _pending_shape;
} tcvox_iter_t;
// Provided publicly in case it's useful.
extern const tcvox_material_t tcvox_default_material_params; // Default parameters if omitted.
extern const tcvox_material_t tcvox_default_material; // Default material in palette.
extern const tcvox_palette_t tcvox_default_palette;
extern const tcvox_transform_t tcvox_transform_identity;
tcvox_scene_t* tcvox_load_memory(tcvox_scene_t* scene, const void* ptr, size_t length);
tcvox_scene_t* tcvox_load_fname(tcvox_scene_t* scene, const char* fname);
void tcvox_unload(const tcvox_scene_t* scene);
// Combine a transform of `parent` followed by `child`.
tcvox_transform_t tcvox_transform_combine(tcvox_transform_t parent, tcvox_transform_t child);
// Apply a transform in `frame` to `vector`.
tcvox_ivec3_t tcvox_transform_apply(tcvox_transform_t transform, tcvox_ivec3_t vector, bool apply_translation);
// The returned matrix is row-major, i.e. mat3[row][col].
void tcvox_rotation_to_mat3(tcvox_rotation_t rotation, int8_t mat3[TC__STATIC_SIZE(3)][3]);
tcvox_rotation_t tcvox_rotation_combine(tcvox_rotation_t first, tcvox_rotation_t second);
tcvox_iter_t tcvox_scene_iter_shapes(tcvox_scene_t* scene, bool include_hidden);
bool tcvox_iter_next(tcvox_iter_t* it);
void tcvox_iter_finish(tcvox_iter_t* it); // Only required to be called if we want to terminate the iterator before it ends.
// Compute a bounding box of the entire scene (min/max in each axis). Note that this does *not* inspect voxel data, but merely uses the outer boxes of each model/node.
bool tcvox_scene_compute_bounds(tcvox_scene_t* scene, tcvox_ivec3_t bounds[TC__STATIC_SIZE(2)], bool include_hidden);
#endif /* TC_VOX_H_ */
#ifdef TC_VOX_IMPLEMENTATION
#undef TC_VOX_IMPLEMENTATION
// Useful resources:
// http://paulbourke.net/dataformats/vox/
// https://github.com/ephtracy/voxel-model/blob/master/MagicaVoxel-file-format-vox.txt
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#ifdef __cplusplus
#if defined(_MSC_VER) || defined(__GNUC__)
#define restrict __restrict
#else
#define restrict
#endif
//#define TC__RESTRICT(T) T restrict
#endif
#ifdef __cplusplus
#define TC__VOID_CAST(T,x) static_cast<T>(x)
#else
#define TC__VOID_CAST(T,x) (x)
#endif
#define TCVOX_TAG_(A,B,C,D) ((uint32_t)(A)<<0 | (uint32_t)(B)<<8 | (uint32_t)(C)<<16 | (uint32_t)(D)<<24)
struct tcvox_chunk_
{
uint32_t tag;
uint32_t nbytes_data;
uint32_t nbytes_children;
// derived
uint32_t offset_data;
uint32_t offset_children;
uint32_t offset_end;
};
#define TCVOX_DEFAULT_MATERIAL_ \
{ \
.type = TCVOX_MATERIAL_TYPE_DIFFUSE, \
.roughness = 0.1f, \
.index_of_refraction = 0.3f, \
.density = 0.05f, \
}
const tcvox_material_t tcvox_default_material_params = {
.type = TCVOX_MATERIAL_TYPE_DIFFUSE,
// METAL & GLASS:
.roughness = 0.0f,
.index_of_refraction = 1.0f,
// METAL:
.metalness = 0.0f,
.specular = 1.0f,
// GLASS:
.alpha = 1.0f,
// GLASS & CLOUD:
.media_type = TCVOX_MEDIA_TYPE_ABSORB,
.scatter_phase = 0.0f,
.density = 0.0f,
// EMIT:
.emissiveness = 0.0f,
.raw = {
// EMIT:
.emit = 0.0f, .flux = 1.0f, .ldr = 0.0f,
},
};
const tcvox_material_t tcvox_default_material = TCVOX_DEFAULT_MATERIAL_;
const tcvox_palette_t tcvox_default_palette = {{
0x00000000, 0xffffffff, 0xffccffff, 0xff99ffff, 0xff66ffff, 0xff33ffff, 0xff00ffff, 0xffffccff,
0xffccccff, 0xff99ccff, 0xff66ccff, 0xff33ccff, 0xff00ccff, 0xffff99ff, 0xffcc99ff, 0xff9999ff,
0xff6699ff, 0xff3399ff, 0xff0099ff, 0xffff66ff, 0xffcc66ff, 0xff9966ff, 0xff6666ff, 0xff3366ff,
0xff0066ff, 0xffff33ff, 0xffcc33ff, 0xff9933ff, 0xff6633ff, 0xff3333ff, 0xff0033ff, 0xffff00ff,
0xffcc00ff, 0xff9900ff, 0xff6600ff, 0xff3300ff, 0xff0000ff, 0xffffffcc, 0xffccffcc, 0xff99ffcc,
0xff66ffcc, 0xff33ffcc, 0xff00ffcc, 0xffffcccc, 0xffcccccc, 0xff99cccc, 0xff66cccc, 0xff33cccc,
0xff00cccc, 0xffff99cc, 0xffcc99cc, 0xff9999cc, 0xff6699cc, 0xff3399cc, 0xff0099cc, 0xffff66cc,
0xffcc66cc, 0xff9966cc, 0xff6666cc, 0xff3366cc, 0xff0066cc, 0xffff33cc, 0xffcc33cc, 0xff9933cc,
0xff6633cc, 0xff3333cc, 0xff0033cc, 0xffff00cc, 0xffcc00cc, 0xff9900cc, 0xff6600cc, 0xff3300cc,
0xff0000cc, 0xffffff99, 0xffccff99, 0xff99ff99, 0xff66ff99, 0xff33ff99, 0xff00ff99, 0xffffcc99,
0xffcccc99, 0xff99cc99, 0xff66cc99, 0xff33cc99, 0xff00cc99, 0xffff9999, 0xffcc9999, 0xff999999,
0xff669999, 0xff339999, 0xff009999, 0xffff6699, 0xffcc6699, 0xff996699, 0xff666699, 0xff336699,
0xff006699, 0xffff3399, 0xffcc3399, 0xff993399, 0xff663399, 0xff333399, 0xff003399, 0xffff0099,
0xffcc0099, 0xff990099, 0xff660099, 0xff330099, 0xff000099, 0xffffff66, 0xffccff66, 0xff99ff66,
0xff66ff66, 0xff33ff66, 0xff00ff66, 0xffffcc66, 0xffcccc66, 0xff99cc66, 0xff66cc66, 0xff33cc66,
0xff00cc66, 0xffff9966, 0xffcc9966, 0xff999966, 0xff669966, 0xff339966, 0xff009966, 0xffff6666,
0xffcc6666, 0xff996666, 0xff666666, 0xff336666, 0xff006666, 0xffff3366, 0xffcc3366, 0xff993366,
0xff663366, 0xff333366, 0xff003366, 0xffff0066, 0xffcc0066, 0xff990066, 0xff660066, 0xff330066,
0xff000066, 0xffffff33, 0xffccff33, 0xff99ff33, 0xff66ff33, 0xff33ff33, 0xff00ff33, 0xffffcc33,
0xffcccc33, 0xff99cc33, 0xff66cc33, 0xff33cc33, 0xff00cc33, 0xffff9933, 0xffcc9933, 0xff999933,
0xff669933, 0xff339933, 0xff009933, 0xffff6633, 0xffcc6633, 0xff996633, 0xff666633, 0xff336633,
0xff006633, 0xffff3333, 0xffcc3333, 0xff993333, 0xff663333, 0xff333333, 0xff003333, 0xffff0033,
0xffcc0033, 0xff990033, 0xff660033, 0xff330033, 0xff000033, 0xffffff00, 0xffccff00, 0xff99ff00,
0xff66ff00, 0xff33ff00, 0xff00ff00, 0xffffcc00, 0xffcccc00, 0xff99cc00, 0xff66cc00, 0xff33cc00,
0xff00cc00, 0xffff9900, 0xffcc9900, 0xff999900, 0xff669900, 0xff339900, 0xff009900, 0xffff6600,
0xffcc6600, 0xff996600, 0xff666600, 0xff336600, 0xff006600, 0xffff3300, 0xffcc3300, 0xff993300,
0xff663300, 0xff333300, 0xff003300, 0xffff0000, 0xffcc0000, 0xff990000, 0xff660000, 0xff330000,
0xff0000ee, 0xff0000dd, 0xff0000bb, 0xff0000aa, 0xff000088, 0xff000077, 0xff000055, 0xff000044,
0xff000022, 0xff000011, 0xff00ee00, 0xff00dd00, 0xff00bb00, 0xff00aa00, 0xff008800, 0xff007700,
0xff005500, 0xff004400, 0xff002200, 0xff001100, 0xffee0000, 0xffdd0000, 0xffbb0000, 0xffaa0000,
0xff880000, 0xff770000, 0xff550000, 0xff440000, 0xff220000, 0xff110000, 0xffeeeeee, 0xffdddddd,
0xffbbbbbb, 0xffaaaaaa, 0xff888888, 0xff777777, 0xff555555, 0xff444444, 0xff222222, 0xff111111
}, {
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_, TCVOX_DEFAULT_MATERIAL_,
}};
const tcvox_transform_t tcvox_transform_identity = {
.r = {{
{0, 0},
{1, 0},
{2, 0},
}},
.t = {{0, 0, 0}},
};
#define TCVOX_CHECK_(cond, msg) do { if(!(cond)) { error = (msg); goto error; } } while(0)
#define TCVOX_ALLOC_CHECK_(ptr, num, name) \
do { \
(ptr) = calloc((num), sizeof(*(ptr))); \
TCVOX_CHECK_((ptr), "Cannot allocate " name ": Out of memory"); \
} while(0)
#define TCVOX_ALLOC_CHECK_NOINIT_(ptr, num, name) \
do { \
(ptr) = malloc((num) * sizeof(*(ptr))); \
TCVOX_CHECK_((ptr), "Cannot allocate " name ": Out of memory"); \
} while(0)
static const char* tcvox_make_attr_(tcvox_attr_t* restrict attr, const char* key, size_t klen, const char* value, size_t vlen)
{
const char* error = NULL;
TCVOX_ALLOC_CHECK_(attr->key, klen + 1 + vlen + 1, "attribute");
attr->value = attr->key + (klen + 1);
memcpy(attr->key, key, klen);
attr->key[klen] = 0;
memcpy(attr->value, value, vlen);
attr->value[vlen] = 0;
error:
return error;
}
#define TCVOX_MAKE_ATTR_(attr) do { if((error = tcvox_make_attr_(attr, key, klen, val, vlen))) goto error; } while(0)
static const char* tcvox_read_bytes_(void* restrict dst, size_t nbytes, const void* restrict ptr, size_t length, size_t offset)
{
if(!(offset + nbytes <= length))
return "Out of bounds read (truncated file?)";
const uint8_t* restrict uptr = TC__VOID_CAST(const uint8_t* restrict,ptr);
memcpy(dst, uptr + offset, nbytes);
return NULL;
}
#define TCVOX_READ_BYTES_(dst, nbytes, offset) do { if((error = tcvox_read_bytes_(dst, nbytes, ptr, length, offset))) goto error; } while(0)
static const char* tcvox_read_bytes_nocpy_(const uint8_t** restrict dst, size_t nbytes, const void* restrict ptr, size_t length, size_t offset)
{
if(!(offset + nbytes <= length))
return "Out of bounds read (truncated file?)";
const uint8_t* restrict cptr = TC__VOID_CAST(const uint8_t* restrict,ptr);
*dst = &cptr[offset];
return NULL;
}
#define TCVOX_READ_BYTES_NOCPY_(dst, nbytes, offset) do { if((error = tcvox_read_bytes_nocpy_(dst, nbytes, ptr, length, offset))) goto error; } while(0)
static const char* tcvox_read_32le_(uint32_t* restrict dst, const void* restrict ptr, size_t length, size_t offset)
{
if(!(offset + sizeof(*dst) <= length))
return "Out of bounds read (truncated file?)";
const uint8_t* restrict uptr = TC__VOID_CAST(const uint8_t* restrict,ptr);
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
memcpy(dst, uptr + offset, sizeof(*dst));
#else // unknown or BE
*dst = (uint32_t)uptr[offset+0] << 0
| (uint32_t)uptr[offset+1] << 8
| (uint32_t)uptr[offset+2] << 16
| (uint32_t)uptr[offset+3] << 24;
#endif
return NULL;
}
#define TCVOX_READ_32LE_(dst, offset) do { if((error = tcvox_read_32le_(dst, ptr, length, offset))) goto error; } while(0)
static const char* tcvox_read_string_nocpy_(const char** restrict dst, uint32_t* restrict dstlen, const void* restrict ptr, size_t length, size_t offset)
{
const char* error;
TCVOX_READ_32LE_(dstlen, offset);
if(!(offset + sizeof(*dstlen) + *dstlen <= length))
return "Out of bounds read (truncated file?)";
const char* restrict cptr = TC__VOID_CAST(const char* restrict,ptr);
*dst = &cptr[offset + 1 * sizeof(uint32_t)];
error:
return error;
}
#define TCVOX_READ_STRING_NOCPY_(dst, dstlen, offset) do { if((error = tcvox_read_string_nocpy_(dst, dstlen, ptr, length, offset))) goto error; } while(0)
static const char* tcvox_read_chunk_header_(struct tcvox_chunk_* restrict chunk, const void* restrict ptr, size_t length, size_t offset)
{
const char* error;
TCVOX_READ_32LE_(&chunk->tag, offset + 0 * sizeof(uint32_t));
TCVOX_READ_32LE_(&chunk->nbytes_data, offset + 1 * sizeof(uint32_t));
TCVOX_READ_32LE_(&chunk->nbytes_children, offset + 2 * sizeof(uint32_t));
chunk->offset_data = offset + 3 * sizeof(uint32_t);
chunk->offset_children = chunk->offset_data + chunk->nbytes_data;
chunk->offset_end = chunk->offset_children + chunk->nbytes_children;
error:
return error;
}
#define TCVOX_READ_CHUNK_HEADER_(dst, offset) do { if((error = tcvox_read_chunk_header_(dst, ptr, length, offset))) goto error; } while(0)
#define TCVOX_IS_KEY_(S) (klen == sizeof(#S) - 1U && !memcmp(key, #S, sizeof(#S) - 1U))
#define TCVOX_IS_VAL_(S) (vlen == sizeof(#S) - 1U && !memcmp(val, #S, sizeof(#S) - 1U))
// Main function.
static const char* tcvox_load_memory_(tcvox_scene_t* scene, const void* ptr, size_t length)
{
const char* error;
uint32_t tag;
TCVOX_READ_32LE_(&tag, 0 * sizeof(uint32_t));
TCVOX_CHECK_(tag == TCVOX_TAG_('V','O','X',' '), "Invalid VOX file (magic number mismatch)");
// TODO: We probably want to relax this at some point.
uint32_t version;
TCVOX_READ_32LE_(&version, 1 * sizeof(uint32_t));
TCVOX_CHECK_(version == 150 || version == 200, "Unknown VOX version");
struct tcvox_chunk_ c_MAIN;
TCVOX_READ_CHUNK_HEADER_(&c_MAIN, 2 * sizeof(uint32_t));
TCVOX_CHECK_(c_MAIN.tag == TCVOX_TAG_('M','A','I','N'), "Wrong initial file chunk");
TCVOX_CHECK_(c_MAIN.nbytes_data == 0, "Expected initial chunk to have no data");
// First, iterate over chunks, gather various counts.
uint32_t pack_nmodels = UINT32_MAX;
for(uint32_t offset = c_MAIN.offset_children; offset < length;)
{
struct tcvox_chunk_ c;
TCVOX_READ_CHUNK_HEADER_(&c, offset);
switch(c.tag)
{
case TCVOX_TAG_('P','A','C','K'):
TCVOX_CHECK_(c.nbytes_data == 1 * sizeof(uint32_t), "Incorrect PACK chunk length");
TCVOX_READ_32LE_(&pack_nmodels, c.offset_data + 0 * sizeof(uint32_t));
break;
case TCVOX_TAG_('S','I','Z','E'):
++scene->nmodels;
break;
case TCVOX_TAG_('n','T','R','N'):
++scene->nodes.ntransforms;
break;
case TCVOX_TAG_('n','G','R','P'):
++scene->nodes.ngroups;
break;
case TCVOX_TAG_('n','S','H','P'):
++scene->nodes.nshapes;
break;
case TCVOX_TAG_('L','A','Y','R'):
++scene->nlayers;
break;
case TCVOX_TAG_('r','C','A','M'):
++scene->ncameras;
break;
case TCVOX_TAG_('r','O','B','J'):
++scene->nobjects;
break;
}
offset = c.offset_end;
}
if(pack_nmodels != UINT32_MAX)
{
TCVOX_CHECK_(scene->nmodels == pack_nmodels, "Invalid number of specified models");
//scene->nmodels = pack_nmodels;
}
TCVOX_ALLOC_CHECK_(scene->models, scene->nmodels, "models");
TCVOX_ALLOC_CHECK_(scene->layers, scene->nlayers, "layers");
TCVOX_ALLOC_CHECK_(scene->cameras, scene->ncameras, "cameras");
TCVOX_ALLOC_CHECK_(scene->objects, scene->nobjects, "objects");
scene->nodes.nindex = scene->nodes.ngroups + scene->nodes.nshapes + scene->nodes.ntransforms;
TCVOX_ALLOC_CHECK_(scene->nodes.index, scene->nodes.nindex, "nodes index");
TCVOX_ALLOC_CHECK_(scene->nodes.groups, scene->nodes.ngroups, "group nodes");
TCVOX_ALLOC_CHECK_(scene->nodes.shapes, scene->nodes.nshapes, "shape nodes");
TCVOX_ALLOC_CHECK_(scene->nodes.transforms, scene->nodes.ntransforms, "transform nodes");
tcvox_model_t* model;
tcvox_object_t* object;
tcvox_group_node_t* group;
tcvox_shape_node_t* shape;
tcvox_transform_node_t* transform;
scene->palette = tcvox_default_palette;
// Populate node ID lookup table (`scene->nodes.index`).
group = &scene->nodes.groups[0];
shape = &scene->nodes.shapes[0];
transform = &scene->nodes.transforms[0];
for(uint32_t offset = c_MAIN.offset_children; offset < length;)
{
struct tcvox_chunk_ c;
TCVOX_READ_CHUNK_HEADER_(&c, offset);
switch(c.tag)
{
case TCVOX_TAG_('n','T','R','N'): {
// We count above, so this should always pass
assert(transform - scene->nodes.transforms < scene->nodes.ntransforms);
uint32_t id;
TCVOX_READ_32LE_(&id, c.offset_data + 0 * sizeof(uint32_t));
TCVOX_CHECK_(id < scene->nodes.nindex, "nTRN ID out of bounds");
TCVOX_CHECK_(!scene->nodes.index[id].type, "Duplicate node ID");
scene->nodes.index[id].index = transform - scene->nodes.transforms;
scene->nodes.index[id].type = TCVOX_NODE_REF_TYPE_TRANSFORM;
++transform;
} break;
case TCVOX_TAG_('n','G','R','P'): {
// We count above, so this should always pass
assert(group - scene->nodes.groups < scene->nodes.ngroups);
uint32_t id;
TCVOX_READ_32LE_(&id, c.offset_data + 0 * sizeof(uint32_t));
TCVOX_CHECK_(id < scene->nodes.nindex, "nGRP ID out of bounds");
TCVOX_CHECK_(!scene->nodes.index[id].type, "Duplicate node ID");
scene->nodes.index[id].index = group - scene->nodes.groups;
scene->nodes.index[id].type = TCVOX_NODE_REF_TYPE_GROUP;
++group;
} break;
case TCVOX_TAG_('n','S','H','P'): {
// We count above, so this should always pass
assert(shape - scene->nodes.shapes < scene->nodes.nshapes);
uint32_t id;
TCVOX_READ_32LE_(&id, c.offset_data + 0 * sizeof(uint32_t));
TCVOX_CHECK_(id < scene->nodes.nindex, "nSHP ID out of bounds");
TCVOX_CHECK_(!scene->nodes.index[id].type, "Duplicate node ID");
scene->nodes.index[id].index = shape - scene->nodes.shapes;
scene->nodes.index[id].type = TCVOX_NODE_REF_TYPE_SHAPE;
++shape;
} break;
default: break;
}
offset = c.offset_end;
}
// Handle IMAP chunks properly, by keeping track of a remapping table. The default table is a simple identity mapping.
static const uint8_t imap_identity[256] = {
0, 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,
};
const uint8_t* imap = imap_identity;
// Perform final read.
model = &scene->models[0];
object = &scene->objects[0];
group = &scene->nodes.groups[0];
shape = &scene->nodes.shapes[0];
transform = &scene->nodes.transforms[0];
for(uint32_t offset = c_MAIN.offset_children; offset < length;)
{
struct tcvox_chunk_ c;
TCVOX_READ_CHUNK_HEADER_(&c, offset);
switch(c.tag)
{
case TCVOX_TAG_('P','A','C','K'):
// nothing to do
break;
case TCVOX_TAG_('S','I','Z','E'):
// We count nmodels above, so this should always pass
assert(model - scene->models < scene->nmodels);
TCVOX_CHECK_(c.nbytes_data == 3 * sizeof(uint32_t), "Invalid SIZE chunk length");
for(size_t i = 0; i < 3; i++)
{
uint32_t s = 0;
TCVOX_READ_32LE_(&s, c.offset_data + i * sizeof(uint32_t));
model->size.xyz[i] = s;
TCVOX_CHECK_(model->size.xyz[i], "Zero-sized model"); // TODO: Should we simply ignore such models, instead of erroring on them?
}
model->id = model - scene->models;
break;
case TCVOX_TAG_('X','Y','Z','I'):
TCVOX_CHECK_(model - scene->models < scene->nmodels
&& !model->voxels, "Too many XYZI blocks in file");
TCVOX_CHECK_(model->size.x && model->size.y && model->size.z, "XYZI chunk must follow SIZE chunk");
TCVOX_READ_32LE_(&model->nvoxels, c.offset_data + 0 * sizeof(uint32_t));
TCVOX_CHECK_(c.nbytes_data >= sizeof(uint32_t) + model->nvoxels * sizeof(uint32_t), "Invalid XYZI chunk size (truncated chunk?)");
model->voxels = malloc(model->nvoxels * sizeof(*model->voxels));
TCVOX_READ_BYTES_(model->voxels, model->nvoxels * 4, c.offset_data + 1 * sizeof(uint32_t));
++model;
break;
case TCVOX_TAG_('n','T','R','N'): {
// We count above, so this should always pass
assert(transform - scene->nodes.transforms < scene->nodes.ntransforms);
TCVOX_READ_32LE_(&transform->id, c.offset_data + 0 * sizeof(uint32_t));
assert(scene->nodes.index[transform->id].type == TCVOX_NODE_REF_TYPE_TRANSFORM);
TCVOX_READ_32LE_(&transform->nattrs, c.offset_data + 1 * sizeof(uint32_t));
uint32_t ioffset = c.offset_data + 2 * sizeof(uint32_t);
TCVOX_ALLOC_CHECK_(transform->attrs, transform->nattrs, "transform attributes");
for(size_t a = 0; a < transform->nattrs; a++)
{
const char* key; uint32_t klen;
const char* val; uint32_t vlen;
TCVOX_READ_STRING_NOCPY_(&key, &klen, ioffset); ioffset += sizeof(uint32_t) + klen;
TCVOX_READ_STRING_NOCPY_(&val, &vlen, ioffset); ioffset += sizeof(uint32_t) + vlen;
TCVOX_MAKE_ATTR_(&transform->attrs[a]);
if(TCVOX_IS_KEY_(_name))
transform->name = transform->attrs[a].value;
else if(TCVOX_IS_KEY_(_hidden))
transform->is_hidden = strtoul(transform->attrs[a].value, NULL, 10);
}
uint32_t child_id;
TCVOX_READ_32LE_(&child_id, ioffset); ioffset += sizeof(uint32_t);
TCVOX_CHECK_(child_id < scene->nodes.nindex, "Child ID out of bounds");
switch(scene->nodes.index[child_id].type)
{
case TCVOX_NODE_REF_TYPE_GROUP: transform->child.group = &scene->nodes.groups[scene->nodes.index[child_id].index]; break;
case TCVOX_NODE_REF_TYPE_SHAPE: transform->child.shape = &scene->nodes.shapes[scene->nodes.index[child_id].index]; break;
default: TCVOX_CHECK_(false, "Invalid child type for transform");
}
uint32_t reserved_id;
TCVOX_READ_32LE_(&reserved_id, ioffset); ioffset += sizeof(uint32_t);
TCVOX_CHECK_(reserved_id == UINT32_MAX, "Reserved ID must be `-1` / `UINT32_MAX`.");
uint32_t layer_id;
TCVOX_READ_32LE_(&layer_id, ioffset); ioffset += sizeof(uint32_t);
if(layer_id == UINT32_MAX)
transform->layer = NULL;
else
{
TCVOX_CHECK_(layer_id < scene->nlayers, "Layer ID out of bounds");
transform->layer = &scene->layers[layer_id];
}
TCVOX_READ_32LE_(&transform->nframes, ioffset); ioffset += sizeof(uint32_t);
TCVOX_CHECK_(transform->nframes > 0, "Number of frames in nTRN must be at least 1");
TCVOX_ALLOC_CHECK_(transform->frames, transform->nframes, "transform frames");
for(size_t f = 0; f < transform->nframes; f++)
{
tcvox_frame_t* frame = &transform->frames[f];
TCVOX_READ_32LE_(&frame->nattrs, ioffset); ioffset += sizeof(uint32_t);
TCVOX_ALLOC_CHECK_(frame->attrs, frame->nattrs, "frame attributes");
// initialize values to defaults
for(size_t i = 0; i < 3; i++)
frame->transform.r.xyz[i].index = i;
//frame->r.xyz[i].sign is already okay (0)
//frame->t is already okay (0 0 0)
//frame->index is already okay (0)
for(size_t a = 0; a < frame->nattrs; a++)
{
const char* key; uint32_t klen;
const char* val; uint32_t vlen;
TCVOX_READ_STRING_NOCPY_(&key, &klen, ioffset); ioffset += sizeof(uint32_t) + klen;
TCVOX_READ_STRING_NOCPY_(&val, &vlen, ioffset); ioffset += sizeof(uint32_t) + vlen;
TCVOX_MAKE_ATTR_(&frame->attrs[a]);
if(TCVOX_IS_KEY_(_r))
{
uint8_t r = strtoul(frame->attrs[a].value, NULL, 10);
frame->transform.r.x.index = (r >> 0) & 3;
frame->transform.r.y.index = (r >> 2) & 3;
// this computes the remaining index:
// ~(0 | 1) == ~(0b00 | 0b01) == 0b10 == 2
// ~(0 | 2) == ~(0b00 | 0b10) == 0b01 == 1
// ~(1 | 2) == ~(0b01 | 0b10) == 0b00 == 0
frame->transform.r.z.index = ~(frame->transform.r.x.index | frame->transform.r.y.index);
frame->transform.r.x.sign = (r >> 4) & 1;
frame->transform.r.y.sign = (r >> 5) & 1;
frame->transform.r.z.sign = (r >> 6) & 1;
}
else if(TCVOX_IS_KEY_(_t))
{
char* end = frame->attrs[a].value;
for(size_t i = 0; i < 3; i++)
frame->transform.t.xyz[i] = strtol(end, &end, 10);
}
else if(TCVOX_IS_KEY_(_f))
frame->index = strtoul(frame->attrs[a].value, NULL, 10);
}
}
++transform;
} break;
case TCVOX_TAG_('n','G','R','P'): {
// We count above, so this should always pass
assert(group - scene->nodes.groups < scene->nodes.ngroups);
TCVOX_READ_32LE_(&group->id, c.offset_data + 0 * sizeof(uint32_t));
assert(scene->nodes.index[group->id].type == TCVOX_NODE_REF_TYPE_GROUP);
TCVOX_READ_32LE_(&group->nattrs, c.offset_data + 1 * sizeof(uint32_t));
uint32_t ioffset = c.offset_data + 2 * sizeof(uint32_t);
TCVOX_ALLOC_CHECK_(group->attrs, group->nattrs, "group attributes");
for(size_t a = 0; a < group->nattrs; a++)
{
const char* key; uint32_t klen;
const char* val; uint32_t vlen;
TCVOX_READ_STRING_NOCPY_(&key, &klen, ioffset); ioffset += sizeof(uint32_t) + klen;
TCVOX_READ_STRING_NOCPY_(&val, &vlen, ioffset); ioffset += sizeof(uint32_t) + vlen;
TCVOX_MAKE_ATTR_(&group->attrs[a]);
}
TCVOX_READ_32LE_(&group->nchildren, ioffset); ioffset += sizeof(uint32_t);
TCVOX_ALLOC_CHECK_(group->children, group->nchildren, "group children");
for(size_t c = 0; c < group->nchildren; c++)
{
uint32_t child_id;
TCVOX_READ_32LE_(&child_id, ioffset); ioffset += sizeof(uint32_t);
TCVOX_CHECK_(child_id < scene->nodes.nindex, "Child ID out of bounds");
TCVOX_CHECK_(scene->nodes.index[child_id].type == TCVOX_NODE_REF_TYPE_TRANSFORM, "Invalid child type for group");
group->children[c] = &scene->nodes.transforms[scene->nodes.index[child_id].index];
}
++group;
} break;
case TCVOX_TAG_('n','S','H','P'): {
// We count above, so this should always pass
assert(shape - scene->nodes.shapes < scene->nodes.nshapes);
TCVOX_READ_32LE_(&shape->id, c.offset_data + 0 * sizeof(uint32_t));
assert(scene->nodes.index[shape->id].type == TCVOX_NODE_REF_TYPE_SHAPE);
TCVOX_READ_32LE_(&shape->nattrs, c.offset_data + 1 * sizeof(uint32_t));
uint32_t ioffset = c.offset_data + 2 * sizeof(uint32_t);
TCVOX_ALLOC_CHECK_(shape->attrs, shape->nattrs, "shape attributes");
for(size_t a = 0; a < shape->nattrs; a++)
{
const char* key; uint32_t klen;
const char* val; uint32_t vlen;
TCVOX_READ_STRING_NOCPY_(&key, &klen, ioffset); ioffset += sizeof(uint32_t) + klen;
TCVOX_READ_STRING_NOCPY_(&val, &vlen, ioffset); ioffset += sizeof(uint32_t) + vlen;
TCVOX_MAKE_ATTR_(&shape->attrs[a]);
}
TCVOX_READ_32LE_(&shape->nmodels, ioffset); ioffset += sizeof(uint32_t);
TCVOX_CHECK_(shape->nmodels > 0, "A shape must have at least 1 model");
TCVOX_ALLOC_CHECK_(shape->models, shape->nmodels, "shape models");
for(size_t m = 0; m < shape->nmodels; m++)
{
tcvox_shape_node_model_t* smodel = &shape->models[m];
uint32_t model_id;
TCVOX_READ_32LE_(&model_id, ioffset); ioffset += sizeof(uint32_t);
TCVOX_CHECK_(model_id < scene->nmodels, "Shape model ID out of bounds");
smodel->model = &scene->models[model_id];
TCVOX_READ_32LE_(&smodel->nattrs, ioffset); ioffset += sizeof(uint32_t);
TCVOX_ALLOC_CHECK_(smodel->attrs, smodel->nattrs, "shape model attributes");
for(size_t a = 0; a < smodel->nattrs; a++)
{
const char* key; uint32_t klen;
const char* val; uint32_t vlen;
TCVOX_READ_STRING_NOCPY_(&key, &klen, ioffset); ioffset += sizeof(uint32_t) + klen;
TCVOX_READ_STRING_NOCPY_(&val, &vlen, ioffset); ioffset += sizeof(uint32_t) + vlen;
TCVOX_MAKE_ATTR_(&smodel->attrs[a]);
if(TCVOX_IS_KEY_(_f))
smodel->starting_frame = strtoul(shape->attrs[a].value, NULL, 10);
}