-
Notifications
You must be signed in to change notification settings - Fork 2
/
m3d.odin
578 lines (527 loc) · 16.3 KB
/
m3d.odin
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
package m3d
APIVERSION :: 0x0100
DOUBLE :: #config(M3D_DOUBLE, false)
when DOUBLE
{
FLOAT :: f64
}
else
{
FLOAT :: f32
}
SMALLINDEX :: #config(M3D_SMALLINDEX, false)
when !SMALLINDEX
{
INDEX :: u32
VOXEL :: u16
UNDEF :: 0xffffffff
INDEXMAX :: 0xfffffffe
VOXUNDEF :: 0xffff
VOXCLEAR :: 0xfffe
}
else
{
INDEX :: u16
VOXEL :: u8
UNDEF :: 0xffff
INDEXMAX :: 0xfffe
VOXUNDEF :: 0xff
VOXCLEAR :: 0xfe
}
NOTDEFINED :: 0xffffffff
NUMBONE :: #config(MD3_NUMBONE, 4)
BONEMAXLEVEL :: #config(MD3_BONEMAXLEVEL, 64)
VERTEXTYPE :: #config(M3D_VERTEXTYPE, false)
ASCII :: #config(M3D_ASCII, false)
VERTEXMAX :: #config(M3D_VERTEXMAX, false)
CMDMAXARG :: #config(M3D_CMDMAXARG, 8) /* if you increase this, add more arguments to the macro below */
hdr_t :: struct #packed
{
magic: [4]u8,
length: u32,
scale: f32, /* deliberately not M3D_FLOAT */
types: u32,
}
chunk_t :: struct #packed
{
magic: [4]u8,
length: u32,
}
/* textmap entry */
ti_t :: struct
{
u, v: FLOAT,
}
textureindex_t :: ti_t
/* texture */
tx_t :: struct
{
name: cstring, /* texture name */
d: [^]u8, /* pixels data */
w: u16, /* width */
h: u16, /* height */
f: u8, /* format, 1 = grayscale, 2 = grayscale+alpha, 3 = rgb, 4 = rgba */
}
texturedata_t :: tx_t
w_t :: struct
{
vertexid: INDEX,
weight: FLOAT,
}
weight_t :: w_t
/* bone entry */
b_t :: struct
{
parent: INDEX, /* parent bone index */
name: cstring, /* name for this bone */
pos: INDEX, /* vertex index position */
ori: INDEX, /* vertex index orientation (quaternion) */
numweight: INDEX, /* number of controlled vertices */
weight: [^]w_t, /* weights for those vertices */
mat4: [16]FLOAT, /* transformation matrix */
}
bone_t :: b_t
/* skin: bone per vertex entry */
s_t :: struct
{
boneid: [NUMBONE]INDEX,
weight: [NUMBONE]FLOAT,
}
skin_t :: s_t
when VERTEXTYPE
{
/* vertex entry */
v_t :: struct
{
x: FLOAT, /* 3D coordinates and weight */
y: FLOAT,
z: FLOAT,
w: FLOAT,
color: u32, /* default vertex color */
skinid: INDEX, /* skin index */
type: u8,
}
}
else
{
/* vertex entry */
v_t :: struct
{
x: FLOAT, /* 3D coordinates and weight */
y: FLOAT,
z: FLOAT,
w: FLOAT,
color: u32, /* default vertex color */
skinid: INDEX, /* skin index */
}
}
vertex_t :: v_t
/* material property formats */
pf_t :: enum u8
{
color,
uint8,
uint16,
uint32,
float,
map_
}
when ASCII
{
pd_t :: struct
{
format: pf_t,
id: u8,
key: cstring,
}
}
else
{
pd_t :: struct
{
format: pf_t,
id: u8,
}
}
/* material property types */
/* You shouldn't change the first 8 display and first 4 physical property. Assign the rest as you like. */
p_e :: enum u8
{
Kd = 0, /* scalar display properties */
Ka,
Ks,
Ns,
Ke,
Tf,
Km,
d,
il,
Pr = 64, /* scalar physical properties */
Pm,
Ps,
Ni,
Nt,
map_Kd = 128, /* textured display map properties */
map_Ka,
map_Ks,
map_Ns,
map_Ke,
map_Tf,
map_Km, /* bump map */
map_D,
map_N, /* normal map */
map_Pr = 192, /* textured physical map properties */
map_Pm,
map_Ps,
map_Ni,
map_Nt
}
/* aliases */
p_bump :: p_e.map_Km
p_map_il :: p_e.map_N
p_refl :: p_e.map_Pm
/* material property */
p_t :: struct
{
type: p_e, /* property type, see "m3dp_*" enumeration */
value: struct #raw_union
{
color: u32, /* if value is a color, m3dpf_color */
num: u32, /* if value is a number, m3dpf_uint8, m3pf_uint16, m3dpf_uint32 */
fnum: f32, /* if value is a floating point number, m3dpf_float */
textureid: INDEX, /* if value is a texture, m3dpf_map */
},
}
property_t :: p_t
/* material entry */
m_t :: struct
{
name: cstring, /* name of the material */
numprop: u8, /* number of properties */
prop: [^]p_t, /* properties array */
}
material_t :: m_t
when VERTEXMAX
{
/* face entry */
f_t :: struct
{
materialid: INDEX, /* material index */
vertex: [3]INDEX, /* 3D points of the triangle in CCW order */
normal: [3]INDEX, /* normal vectors */
texcoord: [3]INDEX, /* UV coordinates */
paramid: INDEX, /* parameter index */
vertmax: [3]INDEX, /* maximum 3D points of the triangle in CCW order */
}
}
else
{
/* face entry */
f_t :: struct
{
materialid: INDEX, /* material index */
vertex: [3]INDEX, /* 3D points of the triangle in CCW order */
normal: [3]INDEX, /* normal vectors */
texcoord: [3]INDEX, /* UV coordinates */
}
}
face_t :: f_t
vi_t :: struct
{
count: u16,
name: cstring,
}
voxelitem_t :: vi_t
parameter_t :: vi_t
/* voxel types (voxel palette) */
vt_t :: struct
{
name: cstring, /* technical name of the voxel */
rotation: u8, /* rotation info */
voxshape: u16, /* voxel shape */
materialid: INDEX, /* material index */
color: u32, /* default voxel color */
skinid: INDEX, /* skin index */
numitem: u8, /* number of sub-voxels */
item: [^]vi_t, /* list of sub-voxels */
}
voxeltype_t :: vt_t
/* voxel data blocks */
vx_t :: struct
{
name: cstring, /* name of the block */
x, y, z: i32, /* position */
w, h, d: u32, /* dimension */
uncertain: u8, /* probability */
groupid: u8, /* block group id */
data: [^]VOXEL, /* voxel data, indices to voxel type */
}
voxel_t :: vx_t
/* shape command types. must match the row in m3d_commandtypes */
c_e :: enum u8
{
/* special commands */
use = 0, /* use material */
inc, /* include another shape */
mesh, /* include part of polygon mesh */
/* approximations */
div, /* subdivision by constant resolution for both u, v */
sub, /* subdivision by constant, different for u and v */
len, /* spacial subdivision by maxlength */
dist, /* subdivision by maxdistance and maxangle */
/* modifiers */
degu, /* degree for both u, v */
deg, /* separate degree for u and v */
rangeu, /* range for u */
range, /* range for u and v */
paru, /* u parameters (knots) */
parv, /* v parameters */
trim, /* outer trimming curve */
hole, /* inner trimming curve */
scrv, /* spacial curve */
sp, /* special points */
/* helper curves */
bez1, /* Bezier 1D */
bsp1, /* B-spline 1D */
bez2, /* bezier 2D */
bsp2, /* B-spline 2D */
/* surfaces */
bezun, /* Bezier 3D with control, UV, normal */
bezu, /* with control and UV */
bezn, /* with control and normal */
bez, /* control points only */
nurbsun, /* B-spline 3D */
nurbsu,
nurbsn,
nurbs,
conn, /* connect surfaces */
/* geometrical */
line,
polygon,
circle,
cylinder,
shpere,
torus,
cone,
cube,
}
/* shape command argument types */
cp_e :: enum u8
{
mi_t = 1, /* material index */
hi_t, /* shape index */
fi_t, /* face index */
ti_t, /* texture map index */
vi_t, /* vertex index */
qi_t, /* vertex index for quaternions */
vc_t, /* coordinate or radius, float scalar */
i1_t, /* int8 scalar */
i2_t, /* int16 scalar */
i4_t, /* int32 scalar */
va_t, /* variadic arguments */
}
when ASCII
{
cd_t :: struct
{
key: cstring,
p: u8,
a: [CMDMAXARG]u8,
}
}
else
{
cd_t :: struct
{
p: u8,
a: [CMDMAXARG]u8,
}
}
/* shape command */
c_t :: struct
{
type: u16, /* shape type */
arg: [^]u32, /* arguments array */
}
shapecommand_t :: c_t
/* shape entry */
h_t :: struct
{
name: cstring, /* name of the mathematical shape */
group: INDEX, /* group this shape belongs to or -1 */
numcmd: u32, /* number of commands */
cmd: [^]c_t, /* commands array */
}
shape_t :: h_t
/* label entry */
l_t :: struct
{
name: cstring, /* name of the annotation layer or NULL */
lang: cstring, /* language code or NULL */
text: cstring, /* the label text */
color: u32, /* color */
vertexid: INDEX, /* the vertex the label refers to */
}
label_t :: l_t
/* frame transformations / working copy skeleton entry */
tr_t :: struct
{
boneid: INDEX, /* selects a node in bone hierarchy */
pos: INDEX, /* vertex index new position */
ori: INDEX, /* vertex index new orientation (quaternion) */
}
transform_t :: tr_t
/* animation frame entry */
fr_t :: struct
{
msec: u32, /* frame's position on the timeline, timestamp */
numtransform: INDEX, /* number of transformations in this frame */
transform: [^]tr_t, /* transformations */
}
frame_t :: fr_t
/* model action entry */
a_t :: struct
{
name: cstring, /* name of the action */
durationmsec: u32, /* duration in millisec (1/1000 sec) */
numframe: INDEX, /* number of frames in this animation */
frame: [^]fr_t, /* frames array */
}
action_t :: a_t
/* inlined asset */
i_t :: struct
{
name: cstring, /* asset name (same pointer as in texture[].name) */
data: [^]u8, /* compressed asset data */
length: u32, /* compressed data length */
}
inlinedasset_t :: i_t
/*** in-memory model structure ***/
FLG_FREERAW :: (1<<0)
FLG_FREESTR :: (1<<1)
FLG_MTLLIB :: (1<<2)
FLG_GENNORM :: (1<<3)
when VERTEXMAX
{
m3d_t :: struct
{
raw: [^]hdr_t, /* pointer to raw data */
flags: u8, /* internal flags */
errcode: i8, /* returned error code */
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: u8, /* decoded sizes for types */
name: cstring, /* name of the model, like "Utah teapot" */
license: cstring, /* usage condition or license, like "MIT", "LGPL" or "BSD-3clause" */
author: cstring, /* nickname, email, homepage or github URL etc. */
desc: cstring, /* comments, descriptions. May contain '\n' newline character */
scale: FLOAT, /* the model's bounding cube's size in SI meters */
numcmap: INDEX,
cmap: [^]u32, /* color map */
numtmap: INDEX,
tmap: [^]ti_t, /* texture map indices */
numtexture: INDEX,
texture: [^]tx_t, /* uncompressed textures */
numbone: INDEX,
bone: [^]b_t, /* bone hierarchy */
numvertex: INDEX,
vertex: [^]v_t, /* vertex data */
numskin: INDEX,
skin: [^]s_t, /* skin data */
nummaterial: INDEX,
material: [^]m_t, /* material list */
numparam: INDEX,
param: [^]vi_t, /* parameters and their values list */
numface: INDEX,
face: [^]f_t, /* model face, polygon (triangle) mesh */
numvoxtype: INDEX,
voxtype: [^]vt_t, /* model face, voxel types */
numvoxel: INDEX,
voxel: [^]vx_t, /* model face, cubes compressed into voxels */
numshape: INDEX,
shape: [^]h_t, /* model face, shape commands */
numlabel: INDEX,
label: [^]l_t, /* annotation labels */
numaction: INDEX,
action: [^]a_t, /* action animations */
numinlined: INDEX,
inlined: [^]i_t, /* inlined assets */
numextra: INDEX,
extra: [^]^chunk_t, /* unknown chunks, application / engine specific data probably */
preview: i_t, /* preview chunk */
}
}
else
{
m3d_t :: struct
{
raw: [^]hdr_t, /* pointer to raw data */
flags: u8, /* internal flags */
errcode: i8, /* returned error code */
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: u8, /* decoded sizes for types */
name: cstring, /* name of the model, like "Utah teapot" */
license: cstring, /* usage condition or license, like "MIT", "LGPL" or "BSD-3clause" */
author: cstring, /* nickname, email, homepage or github URL etc. */
desc: cstring, /* comments, descriptions. May contain '\n' newline character */
scale: FLOAT, /* the model's bounding cube's size in SI meters */
numcmap: INDEX,
cmap: [^]u32, /* color map */
numtmap: INDEX,
tmap: [^]ti_t, /* texture map indices */
numtexture: INDEX,
texture: [^]tx_t, /* uncompressed textures */
numbone: INDEX,
bone: [^]b_t, /* bone hierarchy */
numvertex: INDEX,
vertex: [^]v_t, /* vertex data */
numskin: INDEX,
skin: [^]s_t, /* skin data */
nummaterial: INDEX,
material: [^]m_t, /* material list */
numface: INDEX,
face: [^]f_t, /* model face, polygon (triangle) mesh */
numvoxtype: INDEX,
voxtype: [^]vt_t, /* model face, voxel types */
numvoxel: INDEX,
voxel: [^]vx_t, /* model face, cubes compressed into voxels */
numshape: INDEX,
shape: [^]h_t, /* model face, shape commands */
numlabel: INDEX,
label: [^]l_t, /* annotation labels */
numaction: INDEX,
action: [^]a_t, /* action animations */
numinlined: INDEX,
inlined: [^]i_t, /* inlined assets */
numextra: INDEX,
extra: [^]^chunk_t, /* unknown chunks, application / engine specific data probably */
preview: i_t, /* preview chunk */
}
}
/* read file contents into buffer */
read_t :: #type proc "c" (filename: cstring, size: ^u32) -> [^]u8
/* free file contents buffer */
free_t :: #type proc "c" (buffer: rawptr)
/* interpret texture script */
txsc_t :: #type proc "c" (name: cstring, script: rawptr, len: u32, output: ^tx_t) -> i32
/* interpret surface script */
prsc_t :: #type proc "c" (name: cstring, script: rawptr, len: u32, model: ^m3d_t) -> i32
when ODIN_OS == .Windows && ODIN_ARCH == .amd64
{
foreign import m3d "m3d_windows_amd64.lib"
}
@(default_calling_convention="c")
foreign m3d
{
@(link_name="m3d_load")
load :: proc(data: [^]u8, readfilecb: read_t, freecb: free_t, mtllib: ^m3d_t) -> ^m3d_t ---
@(link_name="m3d_save")
save :: proc(model: ^m3d_t, quality, flags: i32, size: ^u32) -> [^]u8 ---
@(link_name="m3d_free")
free :: proc(model: ^m3d_t) ---
/* generate animation pose skeleton */
@(link_name="m3d_frame")
frame :: proc(model: ^m3d_t, actionid: INDEX, frameid: INDEX, skeleton: ^tr_t) -> ^tr_t ---
@(link_name="m3d_pose")
pose :: proc(model: ^m3d_t, actionid: INDEX, msec: u32) -> ^b_t ---
/* private prototypes used by both importer and exporter */
@(link_name="_m3d_safestr")
_m3d_safestr :: proc(in_: cstring, morelines: i32) -> cstring ---
}