-
Notifications
You must be signed in to change notification settings - Fork 4
/
kmf_struct.bt
331 lines (287 loc) · 10.7 KB
/
kmf_struct.bt
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
/* -*- Mode: C; -*- */
//--------------------------------------
//--- 010 Editor v6.0.3 Binary Template
//
// File: kmf_struct.bt
// Authors: ufdada
// Revision: 1.0
// Purpose: Dungeon Keeper 2 Meshes
// Original Code by Hoenir
//--------------------------------------
// v0.30
// - now correctly reads the faces
// v0.35
// - divided up the template into static meshes and animations
// v0.40
// - identified several unkown values
// - refactored lodEntries, so they should be now easier to read
#include "../util_struc.bt"
enum FormatType { MESH = 1, ANIM = 2, GROP = 3 };
struct MaterialFlags {
ubyte HAS_ALPHA : 1; // 0x0001
ubyte UNKNOWN2 : 1; // 0x0002 Shinyness?
ubyte ALPHA_ADDITIVE : 1; // 0x0004
ubyte UNKNOWN4 : 1; // 0x0008
ubyte UNKNOWN5 : 1; // 0x0010
ubyte UNKNOWN6 : 1; // 0x0020
ubyte UNKNOWN7 : 1; // 0x0040
ubyte UNKNOWN8 : 1; // 0x0080 Some sort of glow?
ubyte ENVIRONMENT_MAPPED : 1; // 0x0100 Environment mapped, invisible guys have this???
ubyte UNKNOWN10; // Probably unused
ubyte UNKNOWN11; // Probably unused
};
local string tKmsh = "KMSH";
local string tSphd = "SPHD";
local string tCtrl = "CTRL";
local string tMat2 = "MAT2";
local string tMatl = "MATL";
local string tHead = "HEAD";
local string tAnim = "ANIM";
local string tMesh = "MESH";
local string tGrop = "GROP";
local string tElem = "ELEM";
local string tGeom = "GEOM";
local string tVert = "VERT";
local string tVgeo = "VGEO";
local string tSprs = "SPRS";
local string tPoly = "POLY";
local string tItab = "ITAB";
struct UV {
short uv1;
short uv2;
};
typedef struct Triangle {
ubyte x;
ubyte y;
ubyte z;
};
struct Texture {
string name;
};
struct CTRL { // numCTRL only non-zero in ANIM files
char signature[4];
checkSignature(signature, tCtrl);
uint size;
uint numCTRL;
if (numCTRL > 0){
uint uk[numCTRL*2];
}
};
struct SPHD (int lodCount) {
char signature[4];
checkSignature(signature, tSphd);
uint size;
uint lodEntries[lodCount]; // one entry for each LOD
uint numVertsEx; // number of entries in the extended vertex list
float mmFactor;
};
struct MAT2 {
char signature[4];
checkSignature(signature, tMat2);
uint size;
string name;
uint numTextures;
Texture texturenames[numTextures] <optimize=false>;
MaterialFlags flags<open=true>;
float brightness;
float gamma;
string envmap;
};
struct MATL {
char signature[4];
checkSignature(signature, tMatl);
uint size;
uint numMaterials;
MAT2 materials[numMaterials] <optimize=false>;
};
struct Model {
char signature[4];
checkSignature(signature, tKmsh);
uint size;
uint version; // = 17
struct HEAD {
char signature[4];
checkSignature(signature, tHead);
uint size;
FormatType format;
uint uk; // = 1
} header;
if (header.format != GROP) {
MATL martials;
}
switch(header.format) {
case ANIM:
struct ANIM {
char signature[4];
checkSignature(signature, tAnim);
uint size;
struct ANIM_HEAD {
char signature[4];
checkSignature(signature, tHead);
uint size;
string meshname;
uint sprsCount;
uint frameCount;
uint indexCount;
uint geomCount;
uint frameFactorFunction;
Vec3f pos;
float cubeScale;
float scale;
uint lodCount;
} header;
CTRL ctrl;
struct ANIM_SPRS (ANIM_HEAD &header) {
char signature[4];
checkSignature(signature, tSprs);
uint size;
local uint j, n, k;
SPHD sprite(header.lodCount)[header.sprsCount] <optimize=false>;
for (j = 0; j < header.sprsCount; j++) {
struct SPRS_SPRS (uint lodCount) {
char signature[4];
checkSignature(signature, tSprs);
uint size;
uint index;
struct POLY (uint lodCount) { // only used in ANIM files
char signature[4];
checkSignature(signature, tPoly);
uint size;
for (n = 0; n < lodCount; n++) {
k = sprite[j].lodEntries[n];
if (k != 0) {
struct (uint triangleCount) {
Triangle lodVertIndices[triangleCount];
} lodEntries(k) <optimize=false>;
}
}
} poly(lodCount) <optimize=false>;
struct VERT {
char signature[4];
checkSignature(signature, tVert);
uint size;
struct AnimVertEx {
UV uv;
Vec3f normal;
ushort index; // index of the vertex in GEOM
} verts[sprite[j].numVertsEx]; // extended vertex list
} verts <optimize=false>;
} sprsSprs(header.lodCount) <optimize=false>;
}
struct ITAB {
char signature[4];
checkSignature(signature, tItab);
uint size;
local uint k = (int) ((header.frameCount - 1) / 128.0 + 1);
uint itab[k * header.indexCount];
} itab;
struct ANIM_GEOM (uint geomCount) {
char signature[4];
checkSignature(signature, tGeom);
uint size;
struct AnimGeom {
int coordinates;
ubyte frameBase;
} geom[geomCount] <read=getAnimGeom>;
} geometry(header.geomCount);
struct VGEO (uint indexCount, uint frameCount) {
char signature[4];
checkSignature(signature, tVgeo);
uint size;
ubyte offsets[indexCount * frameCount];
} vgeo(header.indexCount, header.frameCount);
} sprites(header);
} anim;
break;
case MESH:
struct MESH {
char signature[4];
checkSignature(signature, tMesh);
uint size; // incl. signature
struct MESH_HEAD {
char signature[4];
checkSignature(signature, tHead);
uint size;
string meshname;
uint sprsCount; // number of groups in SPRS
uint vertsCount; // total number of vertices defined in GEOM
Vec3f pos;
float scale;
uint lodCount; // number of total LOD entries
} header;
CTRL ctrl;
struct MESH_SPRS (MESH_HEAD &header) {
char signature[4];
checkSignature(signature, tSprs);
uint size;
local uint i, k, n;
SPHD sprite(header.lodCount)[header.sprsCount] <optimize=false>;
for (i = 0; i < header.sprsCount; i++) {
struct SPRS {
char signature[4];
checkSignature(signature, tSprs);
uint size;
uint index;
for (n = 0; n < header.lodCount; n++) {
k = sprite[i].lodEntries[n];
if (k != 0) {
struct (uint triangleCount) {
Triangle lodVertIndices[k];
} lodEntries(k) <optimize=false>;
}
}
struct MeshVertEx {
short index;
UV uv;
Vec3f normal;
} verts[sprite[i].numVertsEx] <optimize=false>;
} groups <optimize=false>;
}
} sprites(header) <optimize=false>;
struct MESH_GEOM (uint geomCount) {
char signature[4];
checkSignature(signature, tGeom);
uint size;
Vec3f geom[geomCount];
} geometry(header.vertsCount) <optimize=false>;
} mesh;
break;
case GROP:
struct GROP {
char signature[4];
checkSignature(signature, tGrop);
uint size; // incl. signature
struct GROP_HEAD {
char signature[4];
checkSignature(signature, tHead);
uint size;
uint elementCount;
} header;
struct GROP_ELEM {
char signature[4];
checkSignature(signature, tElem);
uint size;
string name;
Vec3f pos;
} element[header.elementCount] <optimize=false>;
} grop;
break;
default:
Printf("Unknown model format %i", header.format);
return -1;
}
} model;
string getAnimGeom(AnimGeom &g) {
string result;
float x = (((g.coordinates >> 20) & 0x3ff) - 0x200) / 511.0f;
float y = (((g.coordinates >> 10) & 0x3ff) - 0x200) / 511.0f;
float z = ((g.coordinates & 0x3ff) - 0x200) / 511.0f;
SPrintf(result, "(x=%lg, y=%lg, z=%lg)", x, y, z);
return result;
}
void checkSignature(char signature[], string expect) {
if (signature != expect) {
Printf("Wrong signature %s expect %s\n", signature, expect);
Exit(true);
}
}