forked from kaitai-io/kaitai_struct_formats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquake2_md2.ksy
433 lines (426 loc) · 15.5 KB
/
quake2_md2.ksy
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
meta:
id: quake2_md2
title: Quake II player model (version 8)
application: Quake II
file-extension: md2
xref:
justsolve: MD2
tags:
- 3d
- game
license: CC0-1.0
endian: le
doc: |
The MD2 format is used for 3D animated models in id Sofware's Quake II.
A model consists of named `frames`, each with the same number of `vertices`
(`vertices_per_frame`). Each such vertex has a `position` and `normal` in
model space. Each vertex has the same topological "meaning" across frames, in
terms of triangle and texture info; it just varies in position and normal for
animation purposes.
How the vertices form triangles is defined via disjoint `triangles` or via
`gl_cmds` (which allows strip and fan topology). Each triangle contains three
`vertex_indices` into frame vertices, and three `tex_point_indices` into
global `tex_coords`. Each texture point has pixel coords `s_px` and `t_px`
ranging from 0 to `skin_{width,height}_px` respectively, along with
`{s,t}_normalized` ranging from 0 to 1 for your convenience.
A GL command has a `primitive` type (`TRIANGLE_FAN` or `TRIANGLE_STRIP`) along
with some `vertices`. Each GL vertex contains `tex_coords_normalized` from 0
to 1, and a `vertex_index` into frame vertices.
A model may also contain `skins`, which are just file paths to PCX images.
However, this is empty for many models, in which case it is up to the client
(e.g. Q2PRO) to offer skins some other way (e.g. by similar filename in the
current directory).
There are 198 `frames` in total, partitioned into a fixed set of ranges used
for different animations. Each frame has a standard `name` for humans, but the
client just uses their index and the name can be arbitrary. The name, start
frame index and frame count of each animation can be looked up in the arrays
`anim_names`, `anim_start_indices`, and `anim_num_frames` respectively. This
information is summarized in the following table:
```
| INDEX | NAME | SUFFIX | NOTES |
|:--------:|--------:|:-------|:-------------------------------------------------------|
| 0-39 | stand | 01-40 | Idle animation |
| 40-45 | run | 1-6 | Full run cycle |
| 46-53 | attack | 1-8 | Shoot, reload; some weapons just repeat 1st few frames |
| 54-57 | pain1 | 01-04 | Q2Pro also uses this for switching weapons |
| 58-61 | pain2 | 01-04 | |
| 62-65 | pain3 | 01-04 | |
| 66-71 | jump | 1-6 | Starts at height and lands on feet |
| 72-83 | flip | 01-12 | Flipoff, i.e. middle finger |
| 84-94 | salute | 01-11 | |
| 95-111 | taunt | 01-17 | |
| 112-122 | wave | 01-11 | Q2Pro plays this backwards for a handgrenade toss |
| 123-134 | point | 01-12 | |
| 135-153 | crstnd | 01-19 | Idle while crouching |
| 154-159 | crwalk | 1-6 | |
| 160-168 | crattak | 1-9 | |
| 169-172 | crpain | 1-4 | |
| 173-177 | crdeath | 1-5 | |
| 178-183 | death1 | 01-06 | |
| 184-189 | death2 | 01-06 | |
| 190-197 | death3 | 01-08 | |
```
The above are filled in for player models; for the separate weapon models,
the final frame is 173 "g_view" (unknown purpose) since weapons aren't shown
during death animations. `a_grenades.md2`, the handgrenade weapon model, is
the same except that the `wave` frames are blank (according to the default
female model files). This is likely due to its dual use as a grenade throw
animation where this model must leave the player's model.
doc-ref:
- https://icculus.org/~phaethon/q3a/formats/md2-schoenblum.html
- http://tfc.duke.free.fr/coding/md2-specs-en.html
- http://tastyspleen.net/~panjoo/downloads/quake2_model_frames.html
- http://wiki.polycount.com/wiki/OldSiteResourcesQuake2FramesList
seq:
- id: magic
contents: IDP2
- id: version
type: u4
valid: 8
- id: skin_width_px
type: u4
- id: skin_height_px
type: u4
- id: bytes_per_frame
type: u4
- id: num_skins
type: u4
- id: vertices_per_frame
type: u4
- id: num_tex_coords
type: u4
- id: num_triangles
type: u4
- id: num_gl_cmds
type: u4
- id: num_frames
type: u4
- id: ofs_skins
type: u4
- id: ofs_tex_coords
type: u4
- id: ofs_triangles
type: u4
- id: ofs_frames
type: u4
- id: ofs_gl_cmds
type: u4
- id: ofs_eof
type: u4
instances:
skins:
pos: ofs_skins
size: 64
type: strz
encoding: ascii
repeat: expr
repeat-expr: num_skins
tex_coords:
pos: ofs_tex_coords
type: tex_point
repeat: expr
repeat-expr: num_tex_coords
triangles:
pos: ofs_triangles
type: triangle
repeat: expr
repeat-expr: num_triangles
frames:
pos: ofs_frames
size: bytes_per_frame
type: frame
repeat: expr
repeat-expr: num_frames
gl_cmds:
pos: ofs_gl_cmds
# in http://tfc.duke.free.fr/coding/src/md2.c initially read as `int *`
# (a pool of `int`s) => hence `s4`
size: 4 * num_gl_cmds # FIXME: replace `4` with `sizeof<s4>` once `sizeof` is implemented for GraphViz
type: gl_cmds_list
anim_names:
value: |
['stand', 'run', 'attack', 'pain1', 'pain2', 'pain3', 'jump', 'flip',
'salute', 'taunt', 'wave', 'point', 'crstnd', 'crwalk', 'crattak',
'crpain', 'crdeath', 'death1', 'death2', 'death3']
anim_start_indices:
value: |
[0, 40, 46, 54, 58, 62, 66, 72,
84, 95, 112, 123, 135, 154, 160,
169, 173, 178, 184, 190]
anim_num_frames:
value: |
[40, 6, 8, 4, 4, 4, 6, 12,
11, 17, 11, 12, 19, 6, 9,
4, 5, 6, 6, 8]
anorms_table:
doc-ref: |
https://github.com/skullernet/q2pro/blob/f4faabd/src/common/math.c#L80
from Quake anorms.h
value: |
[
[-0.525731, 0.000000, 0.850651],
[-0.442863, 0.238856, 0.864188],
[-0.295242, 0.000000, 0.955423],
[-0.309017, 0.500000, 0.809017],
[-0.162460, 0.262866, 0.951056],
[0.000000, 0.000000, 1.000000],
[0.000000, 0.850651, 0.525731],
[-0.147621, 0.716567, 0.681718],
[0.147621, 0.716567, 0.681718],
[0.000000, 0.525731, 0.850651],
[0.309017, 0.500000, 0.809017],
[0.525731, 0.000000, 0.850651],
[0.295242, 0.000000, 0.955423],
[0.442863, 0.238856, 0.864188],
[0.162460, 0.262866, 0.951056],
[-0.681718, 0.147621, 0.716567],
[-0.809017, 0.309017, 0.500000],
[-0.587785, 0.425325, 0.688191],
[-0.850651, 0.525731, 0.000000],
[-0.864188, 0.442863, 0.238856],
[-0.716567, 0.681718, 0.147621],
[-0.688191, 0.587785, 0.425325],
[-0.500000, 0.809017, 0.309017],
[-0.238856, 0.864188, 0.442863],
[-0.425325, 0.688191, 0.587785],
[-0.716567, 0.681718, -0.147621],
[-0.500000, 0.809017, -0.309017],
[-0.525731, 0.850651, 0.000000],
[0.000000, 0.850651, -0.525731],
[-0.238856, 0.864188, -0.442863],
[0.000000, 0.955423, -0.295242],
[-0.262866, 0.951056, -0.162460],
[0.000000, 1.000000, 0.000000],
[0.000000, 0.955423, 0.295242],
[-0.262866, 0.951056, 0.162460],
[0.238856, 0.864188, 0.442863],
[0.262866, 0.951056, 0.162460],
[0.500000, 0.809017, 0.309017],
[0.238856, 0.864188, -0.442863],
[0.262866, 0.951056, -0.162460],
[0.500000, 0.809017, -0.309017],
[0.850651, 0.525731, 0.000000],
[0.716567, 0.681718, 0.147621],
[0.716567, 0.681718, -0.147621],
[0.525731, 0.850651, 0.000000],
[0.425325, 0.688191, 0.587785],
[0.864188, 0.442863, 0.238856],
[0.688191, 0.587785, 0.425325],
[0.809017, 0.309017, 0.500000],
[0.681718, 0.147621, 0.716567],
[0.587785, 0.425325, 0.688191],
[0.955423, 0.295242, 0.000000],
[1.000000, 0.000000, 0.000000],
[0.951056, 0.162460, 0.262866],
[0.850651, -0.525731, 0.000000],
[0.955423, -0.295242, 0.000000],
[0.864188, -0.442863, 0.238856],
[0.951056, -0.162460, 0.262866],
[0.809017, -0.309017, 0.500000],
[0.681718, -0.147621, 0.716567],
[0.850651, 0.000000, 0.525731],
[0.864188, 0.442863, -0.238856],
[0.809017, 0.309017, -0.500000],
[0.951056, 0.162460, -0.262866],
[0.525731, 0.000000, -0.850651],
[0.681718, 0.147621, -0.716567],
[0.681718, -0.147621, -0.716567],
[0.850651, 0.000000, -0.525731],
[0.809017, -0.309017, -0.500000],
[0.864188, -0.442863, -0.238856],
[0.951056, -0.162460, -0.262866],
[0.147621, 0.716567, -0.681718],
[0.309017, 0.500000, -0.809017],
[0.425325, 0.688191, -0.587785],
[0.442863, 0.238856, -0.864188],
[0.587785, 0.425325, -0.688191],
[0.688191, 0.587785, -0.425325],
[-0.147621, 0.716567, -0.681718],
[-0.309017, 0.500000, -0.809017],
[0.000000, 0.525731, -0.850651],
[-0.525731, 0.000000, -0.850651],
[-0.442863, 0.238856, -0.864188],
[-0.295242, 0.000000, -0.955423],
[-0.162460, 0.262866, -0.951056],
[0.000000, 0.000000, -1.000000],
[0.295242, 0.000000, -0.955423],
[0.162460, 0.262866, -0.951056],
[-0.442863, -0.238856, -0.864188],
[-0.309017, -0.500000, -0.809017],
[-0.162460, -0.262866, -0.951056],
[0.000000, -0.850651, -0.525731],
[-0.147621, -0.716567, -0.681718],
[0.147621, -0.716567, -0.681718],
[0.000000, -0.525731, -0.850651],
[0.309017, -0.500000, -0.809017],
[0.442863, -0.238856, -0.864188],
[0.162460, -0.262866, -0.951056],
[0.238856, -0.864188, -0.442863],
[0.500000, -0.809017, -0.309017],
[0.425325, -0.688191, -0.587785],
[0.716567, -0.681718, -0.147621],
[0.688191, -0.587785, -0.425325],
[0.587785, -0.425325, -0.688191],
[0.000000, -0.955423, -0.295242],
[0.000000, -1.000000, 0.000000],
[0.262866, -0.951056, -0.162460],
[0.000000, -0.850651, 0.525731],
[0.000000, -0.955423, 0.295242],
[0.238856, -0.864188, 0.442863],
[0.262866, -0.951056, 0.162460],
[0.500000, -0.809017, 0.309017],
[0.716567, -0.681718, 0.147621],
[0.525731, -0.850651, 0.000000],
[-0.238856, -0.864188, -0.442863],
[-0.500000, -0.809017, -0.309017],
[-0.262866, -0.951056, -0.162460],
[-0.850651, -0.525731, 0.000000],
[-0.716567, -0.681718, -0.147621],
[-0.716567, -0.681718, 0.147621],
[-0.525731, -0.850651, 0.000000],
[-0.500000, -0.809017, 0.309017],
[-0.238856, -0.864188, 0.442863],
[-0.262866, -0.951056, 0.162460],
[-0.864188, -0.442863, 0.238856],
[-0.809017, -0.309017, 0.500000],
[-0.688191, -0.587785, 0.425325],
[-0.681718, -0.147621, 0.716567],
[-0.442863, -0.238856, 0.864188],
[-0.587785, -0.425325, 0.688191],
[-0.309017, -0.500000, 0.809017],
[-0.147621, -0.716567, 0.681718],
[-0.425325, -0.688191, 0.587785],
[-0.162460, -0.262866, 0.951056],
[0.442863, -0.238856, 0.864188],
[0.162460, -0.262866, 0.951056],
[0.309017, -0.500000, 0.809017],
[0.147621, -0.716567, 0.681718],
[0.000000, -0.525731, 0.850651],
[0.425325, -0.688191, 0.587785],
[0.587785, -0.425325, 0.688191],
[0.688191, -0.587785, 0.425325],
[-0.955423, 0.295242, 0.000000],
[-0.951056, 0.162460, 0.262866],
[-1.000000, 0.000000, 0.000000],
[-0.850651, 0.000000, 0.525731],
[-0.955423, -0.295242, 0.000000],
[-0.951056, -0.162460, 0.262866],
[-0.864188, 0.442863, -0.238856],
[-0.951056, 0.162460, -0.262866],
[-0.809017, 0.309017, -0.500000],
[-0.864188, -0.442863, -0.238856],
[-0.951056, -0.162460, -0.262866],
[-0.809017, -0.309017, -0.500000],
[-0.681718, 0.147621, -0.716567],
[-0.681718, -0.147621, -0.716567],
[-0.850651, 0.000000, -0.525731],
[-0.688191, 0.587785, -0.425325],
[-0.587785, 0.425325, -0.688191],
[-0.425325, 0.688191, -0.587785],
[-0.425325, -0.688191, -0.587785],
[-0.587785, -0.425325, -0.688191],
[-0.688191, -0.587785, -0.425325],
]
types:
tex_point:
seq:
- id: s_px
type: u2
- id: t_px
type: u2
instances:
s_normalized:
value: (s_px + 0.0) / _root.skin_width_px
t_normalized:
value: (t_px + 0.0) / _root.skin_height_px
triangle:
seq:
- id: vertex_indices
type: u2
repeat: expr
repeat-expr: 3
doc: indices to `_root.frames[i].vertices` (for each frame with index `i`)
- id: tex_point_indices
type: u2
repeat: expr
repeat-expr: 3
doc: indices to `_root.tex_coords`
vec3f:
seq:
- id: x
type: f4
- id: y
type: f4
- id: z
type: f4
compressed_vec:
seq:
- id: x_compressed
type: u1
- id: y_compressed
type: u1
- id: z_compressed
type: u1
instances:
x:
value: x_compressed * _parent._parent.scale.x + _parent._parent.translate.x
y:
value: y_compressed * _parent._parent.scale.y + _parent._parent.translate.y
z:
value: z_compressed * _parent._parent.scale.z + _parent._parent.translate.z
vertex:
seq:
- id: position
type: compressed_vec
- id: normal_index
type: u1
instances:
normal:
value: _root.anorms_table[normal_index]
frame:
seq:
- id: scale
type: vec3f
- id: translate
type: vec3f
- id: name
size: 16
type: strz
encoding: ascii
- id: vertices
type: vertex
repeat: expr
repeat-expr: _root.vertices_per_frame
gl_cmds_list:
seq:
- id: items
type: gl_cmd
repeat: until
repeat-until: _.cmd_num_vertices == 0
if: not _io.eof
gl_cmd:
seq:
- id: cmd_num_vertices
type: s4
- id: vertices
type: gl_vertex
repeat: expr
repeat-expr: num_vertices
instances:
num_vertices:
value: 'cmd_num_vertices < 0 ? -cmd_num_vertices : cmd_num_vertices'
primitive:
value: 'cmd_num_vertices < 0 ? gl_primitive::triangle_fan : gl_primitive::triangle_strip'
gl_vertex:
seq:
- id: tex_coords_normalized
type: f4
repeat: expr
repeat-expr: 2
- id: vertex_index
type: u4
doc: index to `_root.frames[i].vertices` (for each frame with index `i`)
enums:
gl_primitive:
0: triangle_strip
1: triangle_fan