-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdff.cc
515 lines (404 loc) · 15.3 KB
/
dff.cc
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
// https://github.com/rwengine/openrw/blob/master/rwcore/loaders/LoaderDFF.cpp
#include "dff.h"
#include <algorithm>
#include <cctype>
#include <cstdint>
#include <cstring>
#include <cstdlib>
#include <memory>
#include <numeric>
#include <glm/glm.hpp>
#include "common.h"
#include "RWBinaryStream.hpp"
FrameList readFrameList(const RWBStream& stream);
GeometryList readGeometryList(const RWBStream& stream);
GeometryPtr readGeometry(const RWBStream& stream);
void readMaterialList(const GeometryPtr& geom, const RWBStream& stream);
void readMaterial(const GeometryPtr& geom, const RWBStream& stream);
void readTexture(Geometry::Material& material, const RWBStream& stream);
void readGeometryExtension(const GeometryPtr& geom, const RWBStream& stream);
void readBinMeshPLG(const GeometryPtr& geom, const RWBStream& stream);
AtomicPtr readAtomic(FrameList& framelist, GeometryList& geometrylist,
const RWBStream& stream);
enum DFFChunks {
CHUNK_STRUCT = 0x0001,
CHUNK_EXTENSION = 0x0003,
CHUNK_TEXTURE = 0x0006,
CHUNK_MATERIAL = 0x0007,
CHUNK_MATERIALLIST = 0x0008,
CHUNK_FRAMELIST = 0x000E,
CHUNK_GEOMETRY = 0x000F,
CHUNK_CLUMP = 0x0010,
CHUNK_ATOMIC = 0x0014,
CHUNK_GEOMETRYLIST = 0x001A,
CHUNK_BINMESHPLG = 0x050E,
CHUNK_NODENAME = 0x0253F2FE,
};
// These structs are used to interpret raw bytes from the stream.
/// @todo worry about endianness.
typedef glm::vec3 BSTVector3;
typedef glm::mat3 BSTMatrix;
typedef glm::i8vec4 BSTColour;
struct RWBSFrame {
BSTMatrix rotation;
BSTVector3 position;
int32_t index;
uint32_t matrixflags; // Not used
};
FrameList readFrameList(const RWBStream &stream) {
auto listStream = stream.getInnerStream();
auto listStructID = listStream.getNextChunk();
if (listStructID != CHUNK_STRUCT) {
die("Frame List missing struct chunk");
}
const char *headerPtr = listStream.getCursor();
unsigned int numFrames = *reinterpret_cast<const std::uint32_t *>(headerPtr);
headerPtr += sizeof(std::uint32_t);
FrameList framelist;
framelist.reserve(numFrames);
for (auto f = 0u; f < numFrames; ++f) {
auto data = reinterpret_cast<const RWBSFrame *>(headerPtr);
headerPtr += sizeof(RWBSFrame);
auto frame =
std::make_shared<ModelFrame>(f, data->rotation, data->position);
assert(data->index < static_cast<int>(framelist.size()) &&
"Frame parent out of bounds");
if (data->index != -1 &&
data->index < static_cast<int>(framelist.size())) {
framelist[data->index]->addChild(frame);
}
framelist.push_back(frame);
}
size_t namedFrames = 0;
/// @todo perhaps flatten this out a little
for (auto chunkID = listStream.getNextChunk(); chunkID != 0;
chunkID = listStream.getNextChunk()) {
switch (chunkID) {
case CHUNK_EXTENSION: {
auto extStream = listStream.getInnerStream();
for (auto chunkID = extStream.getNextChunk(); chunkID != 0;
chunkID = extStream.getNextChunk()) {
switch (chunkID) {
case CHUNK_NODENAME: {
std::string fname(extStream.getCursor(),
extStream.getCurrentChunkSize());
std::transform(fname.begin(), fname.end(),
fname.begin(), ::tolower);
if (namedFrames < framelist.size()) {
framelist[namedFrames++]->setName(fname);
}
} break;
default:
break;
}
}
} break;
default:
break;
}
}
return framelist;
}
GeometryList readGeometryList(const RWBStream &stream) {
auto listStream = stream.getInnerStream();
auto listStructID = listStream.getNextChunk();
if (listStructID != CHUNK_STRUCT) {
die("Geometry List missing struct chunk");
}
const char *headerPtr = listStream.getCursor();
unsigned int numGeometries = bit_cast<std::uint32_t>(*headerPtr);
headerPtr += sizeof(std::uint32_t);
std::vector<GeometryPtr> geometrylist;
geometrylist.reserve(numGeometries);
for (auto chunkID = listStream.getNextChunk(); chunkID != 0;
chunkID = listStream.getNextChunk()) {
switch (chunkID) {
case CHUNK_GEOMETRY: {
geometrylist.push_back(readGeometry(listStream));
} break;
default:
break;
}
}
return geometrylist;
}
GeometryPtr readGeometry(const RWBStream &stream) {
auto geomStream = stream.getInnerStream();
auto geomStructID = geomStream.getNextChunk();
if (geomStructID != CHUNK_STRUCT) {
die("Geometry missing struct chunk");
}
auto geom = std::make_shared<Geometry>();
const char *headerPtr = geomStream.getCursor();
geom->flags = bit_cast<std::uint16_t>(*headerPtr);
headerPtr += sizeof(std::uint16_t);
/*unsigned short numUVs = bit_cast<std::uint8_t>(*headerPtr);*/
headerPtr += sizeof(std::uint8_t);
/*unsigned short moreFlags = bit_cast<std::uint8_t>(*headerPtr);*/
headerPtr += sizeof(std::uint8_t);
unsigned int numTris = bit_cast<std::uint32_t>(*headerPtr);
headerPtr += sizeof(std::uint32_t);
unsigned int numVerts = bit_cast<std::uint32_t>(*headerPtr);
headerPtr += sizeof(std::uint32_t);
/*unsigned int numFrames = bit_cast<std::uint32_t>(*headerPtr);*/
headerPtr += sizeof(std::uint32_t);
geom->verts.resize(numVerts);
if (geomStream.getChunkVersion() < 0x1003FFFF) {
headerPtr += sizeof(RW::BSGeometryColor);
}
/// @todo extract magic numbers.
if ((geom->flags & 8) == 8) {
for (size_t v = 0; v < numVerts; ++v) {
geom->verts[v].colour = bit_cast<glm::u8vec4>(*headerPtr);
headerPtr += sizeof(glm::u8vec4);
}
} else {
for (size_t v = 0; v < numVerts; ++v) {
geom->verts[v].colour = {255, 255, 255, 255};
}
}
if ((geom->flags & 4) == 4 || (geom->flags & 128) == 128) {
for (size_t v = 0; v < numVerts; ++v) {
geom->verts[v].texcoord = bit_cast<glm::vec2>(*headerPtr);
headerPtr += sizeof(glm::vec2);
}
}
// Grab indicies data to generate normals (if applicable).
auto triangles = std::make_unique<RW::BSGeometryTriangle[]>(numTris);
memcpy(triangles.get(), headerPtr, sizeof(RW::BSGeometryTriangle) * numTris);
headerPtr += sizeof(RW::BSGeometryTriangle) * numTris;
geom->geometryBounds = bit_cast<RW::BSGeometryBounds>(*headerPtr);
geom->geometryBounds.radius = std::abs(geom->geometryBounds.radius);
headerPtr += sizeof(RW::BSGeometryBounds);
for (size_t v = 0; v < numVerts; ++v) {
geom->verts[v].position = bit_cast<glm::vec3>(*headerPtr);
headerPtr += sizeof(glm::vec3);
}
if ((geom->flags & 16) == 16) {
for (size_t v = 0; v < numVerts; ++v) {
geom->verts[v].normal = bit_cast<glm::vec3>(*headerPtr);
headerPtr += sizeof(glm::vec3);
}
} else {
// Use triangle data to calculate normals for each vert.
for (size_t t = 0; t < numTris; ++t) {
auto &triangle = triangles[t];
auto &A = geom->verts[triangle.first];
auto &B = geom->verts[triangle.second];
auto &C = geom->verts[triangle.third];
auto normal = glm::normalize(
glm::cross(C.position - A.position, B.position - A.position));
A.normal = normal;
B.normal = normal;
C.normal = normal;
}
}
// Process the geometry child sections
for (auto chunkID = geomStream.getNextChunk(); chunkID != 0;
chunkID = geomStream.getNextChunk()) {
switch (chunkID) {
case CHUNK_MATERIALLIST:
readMaterialList(geom, geomStream);
break;
case CHUNK_EXTENSION:
readGeometryExtension(geom, geomStream);
break;
default:
break;
}
}
return geom;
}
void readMaterialList(const GeometryPtr &geom, const RWBStream &stream) {
auto listStream = stream.getInnerStream();
auto listStructID = listStream.getNextChunk();
if (listStructID != CHUNK_STRUCT) {
die("MaterialList missing struct chunk");
}
unsigned int numMaterials = bit_cast<std::uint32_t>(*listStream.getCursor());
geom->materials.reserve(numMaterials);
RWBStream::ChunkID chunkID;
while ((chunkID = listStream.getNextChunk())) {
switch (chunkID) {
case CHUNK_MATERIAL:
readMaterial(geom, listStream);
break;
default:
break;
}
}
}
void readMaterial(const GeometryPtr &geom, const RWBStream &stream) {
auto materialStream = stream.getInnerStream();
auto matStructID = materialStream.getNextChunk();
if (matStructID != CHUNK_STRUCT) {
die("Material missing struct chunk");
}
const char *matData = materialStream.getCursor();
Geometry::Material material;
// Unkown
matData += sizeof(std::uint32_t);
material.colour = bit_cast<glm::u8vec4>(*matData);
matData += sizeof(std::uint32_t);
// Unkown
matData += sizeof(std::uint32_t);
/*bool usesTexture = bit_cast<std::uint32_t>(*matData);*/
matData += sizeof(std::uint32_t);
material.ambientIntensity = bit_cast<float>(*matData);
matData += sizeof(float);
/*float specular = bit_cast<float>(*matData);*/
matData += sizeof(float);
material.diffuseIntensity = bit_cast<float>(*matData);
matData += sizeof(float);
material.flags = 0;
RWBStream::ChunkID chunkID;
while ((chunkID = materialStream.getNextChunk())) {
switch (chunkID) {
case CHUNK_TEXTURE:
readTexture(material, materialStream);
break;
default:
break;
}
}
geom->materials.push_back(material);
}
void readTexture(Geometry::Material &material,
const RWBStream &stream) {
auto texStream = stream.getInnerStream();
auto texStructID = texStream.getNextChunk();
if (texStructID != CHUNK_STRUCT) {
die("Texture missing struct chunk");
}
// There's some data in the Texture's struct, but we don't know what it is.
/// @todo improve how these strings are read.
std::string name, alpha;
texStream.getNextChunk();
name = texStream.getCursor();
texStream.getNextChunk();
alpha = texStream.getCursor();
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
std::transform(alpha.begin(), alpha.end(), alpha.begin(), ::tolower);
material.textures.emplace_back(std::move(name), std::move(alpha));
}
void readGeometryExtension(const GeometryPtr &geom,
const RWBStream &stream) {
auto extStream = stream.getInnerStream();
RWBStream::ChunkID chunkID;
while ((chunkID = extStream.getNextChunk())) {
switch (chunkID) {
case CHUNK_BINMESHPLG:
readBinMeshPLG(geom, extStream);
break;
default:
break;
}
}
}
void readBinMeshPLG(const GeometryPtr &geom, const RWBStream &stream) {
auto data = stream.getCursor();
geom->facetype = static_cast<Geometry::FaceType>(bit_cast<std::uint32_t>(*data));
data += sizeof(std::uint32_t);
unsigned int numSplits = bit_cast<std::uint32_t>(*data);
data += sizeof(std::uint32_t);
// Number of triangles.
data += sizeof(std::uint32_t);
geom->subgeom.reserve(numSplits);
size_t start = 0;
for (size_t s = 0; s < numSplits; ++s) {
SubGeometry sg;
sg.numIndices = bit_cast<std::uint32_t>(*data);
data += sizeof(std::uint32_t);
sg.material = bit_cast<std::uint32_t>(*data);
data += sizeof(std::uint32_t);
sg.start = start;
start += sg.numIndices;
sg.indices.resize(sg.numIndices);
std::memcpy(sg.indices.data(), data,
sizeof(std::uint32_t) * sg.numIndices);
data += sizeof(std::uint32_t) * sg.numIndices;
geom->subgeom.push_back(std::move(sg));
}
}
AtomicPtr readAtomic(FrameList &framelist,
GeometryList &geometrylist,
const RWBStream &stream) {
auto atomicStream = stream.getInnerStream();
auto atomicStructID = atomicStream.getNextChunk();
if (atomicStructID != CHUNK_STRUCT) {
die("Atomic missing struct chunk");
}
auto data = atomicStream.getCursor();
std::uint32_t frame = bit_cast<std::uint32_t>(*data);
data += sizeof(std::uint32_t);
std::uint32_t geometry = bit_cast<std::uint32_t>(*data);
data += sizeof(std::uint32_t);
std::uint32_t flags = bit_cast<std::uint32_t>(*data);
// Verify the atomic's particulars
assert(frame < framelist.size() && "atomic frame out of bounds");
assert(geometry < geometrylist.size() &&
"atomic geometry out of bounds");
auto atomic = std::make_shared<Atomic>();
if (geometry < geometrylist.size()) {
atomic->setGeometry(geometrylist[geometry]);
}
if (frame < framelist.size()) {
atomic->setFrame(framelist[frame]);
}
atomic->setFlags(flags);
return atomic;
}
ClumpPtr loadDFF(const bytes &data) {
if (!data.size()) die("Empty file");
auto model = std::make_shared<Clump>();
RWBStream rootStream((const char *)data.data(), data.size());
auto rootID = rootStream.getNextChunk();
if (rootID != CHUNK_CLUMP) {
die("Invalid root section ID " + std::to_string(rootID));
}
RWBStream modelStream = rootStream.getInnerStream();
auto rootStructID = modelStream.getNextChunk();
if (rootStructID != CHUNK_STRUCT) {
die("Clump missing struct chunk");
}
// There is only one value in the struct section.
std::uint32_t numAtomics = bit_cast<std::uint32_t>(*rootStream.getCursor());
(void)numAtomics;
GeometryList geometrylist;
FrameList framelist;
// Process everything inside the clump stream.
RWBStream::ChunkID chunkID;
while ((chunkID = modelStream.getNextChunk())) {
switch (chunkID) {
case CHUNK_FRAMELIST:
framelist = readFrameList(modelStream);
if (!framelist.empty()) {
model->setFrame(framelist[0]);
}
break;
case CHUNK_GEOMETRYLIST:
geometrylist = readGeometryList(modelStream);
break;
case CHUNK_ATOMIC: {
auto atomic = readAtomic(framelist, geometrylist, modelStream);
if (!atomic) {
// Abort reading the rest of the clump
die("Failed to read atomic");
}
model->addAtomic(atomic);
} break;
default:
break;
}
}
// Ensure the model has cached metrics
model->recalculateMetrics();
return model;
}
#ifdef __EMSCRIPTEN__
#include <emscripten/bind.h>
using namespace emscripten;
EMSCRIPTEN_BINDINGS(dff) {
function("loadDFF", &loadDFF);
}
#endif