2017-10-30 23:44:40 +01:00
|
|
|
#include "loaders/LoaderDFF.hpp"
|
2013-07-02 08:06:03 +02:00
|
|
|
|
2013-07-26 07:40:21 +02:00
|
|
|
#include <algorithm>
|
2017-10-30 23:44:40 +01:00
|
|
|
#include <cctype>
|
|
|
|
#include <cstdint>
|
2016-09-09 22:13:21 +02:00
|
|
|
#include <cstring>
|
2017-10-30 23:44:40 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <memory>
|
2014-08-15 23:58:03 +02:00
|
|
|
#include <numeric>
|
2017-10-30 23:44:40 +01:00
|
|
|
|
|
|
|
#include <glm/glm.hpp>
|
|
|
|
|
|
|
|
#include "data/Clump.hpp"
|
|
|
|
#include "gl/gl_core_3_3.h"
|
|
|
|
#include "loaders/RWBinaryStream.hpp"
|
|
|
|
#include "platform/FileHandle.hpp"
|
2018-06-21 16:49:05 +02:00
|
|
|
#include "rw/debug.hpp"
|
2013-07-02 08:06:03 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
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,
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
CHUNK_ATOMIC = 0x0014,
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
CHUNK_GEOMETRYLIST = 0x001A,
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
CHUNK_BINMESHPLG = 0x050E,
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
CHUNK_NODENAME = 0x0253F2FE,
|
2014-08-04 23:21:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
struct RWBSFrame {
|
|
|
|
BSTMatrix rotation;
|
|
|
|
BSTVector3 position;
|
|
|
|
int32_t index;
|
|
|
|
uint32_t matrixflags; // Not used
|
2014-08-04 23:21:01 +02:00
|
|
|
};
|
|
|
|
|
2017-01-04 22:08:21 +01:00
|
|
|
LoaderDFF::FrameList LoaderDFF::readFrameList(const RWBStream &stream) {
|
2016-09-09 22:13:21 +02:00
|
|
|
auto listStream = stream.getInnerStream();
|
|
|
|
|
|
|
|
auto listStructID = listStream.getNextChunk();
|
|
|
|
if (listStructID != CHUNK_STRUCT) {
|
|
|
|
throw DFFLoaderException("Frame List missing struct chunk");
|
|
|
|
}
|
|
|
|
|
|
|
|
char *headerPtr = listStream.getCursor();
|
|
|
|
|
2018-07-27 22:53:35 +02:00
|
|
|
unsigned int numFrames = *reinterpret_cast<std::uint32_t *>(headerPtr);
|
2016-09-09 22:13:21 +02:00
|
|
|
headerPtr += sizeof(std::uint32_t);
|
|
|
|
|
2017-01-04 22:08:21 +01:00
|
|
|
FrameList framelist;
|
|
|
|
framelist.reserve(numFrames);
|
2016-09-09 22:13:21 +02:00
|
|
|
|
2018-08-29 22:46:00 +02:00
|
|
|
for (auto f = 0u; f < numFrames; ++f) {
|
2018-07-27 22:53:35 +02:00
|
|
|
auto data = reinterpret_cast<RWBSFrame *>(headerPtr);
|
2016-09-09 22:13:21 +02:00
|
|
|
headerPtr += sizeof(RWBSFrame);
|
2017-01-04 22:08:21 +01:00
|
|
|
auto frame =
|
|
|
|
std::make_shared<ModelFrame>(f, data->rotation, data->position);
|
2016-09-09 22:13:21 +02:00
|
|
|
|
2018-07-27 22:53:35 +02:00
|
|
|
RW_CHECK(data->index < static_cast<int>(framelist.size()),
|
2017-01-04 22:08:21 +01:00
|
|
|
"Frame parent out of bounds");
|
2018-07-27 22:53:35 +02:00
|
|
|
if (data->index != -1 &&
|
|
|
|
data->index < static_cast<int>(framelist.size())) {
|
2017-01-04 22:08:21 +01:00
|
|
|
framelist[data->index]->addChild(frame);
|
2016-09-09 22:13:21 +02:00
|
|
|
}
|
|
|
|
|
2017-01-04 22:08:21 +01:00
|
|
|
framelist.push_back(frame);
|
2016-09-09 22:13:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2017-01-04 22:08:21 +01:00
|
|
|
if (namedFrames < framelist.size()) {
|
|
|
|
framelist[namedFrames++]->setName(fname);
|
2016-09-09 22:13:21 +02:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-01-04 22:08:21 +01:00
|
|
|
|
|
|
|
return framelist;
|
2014-08-04 23:21:01 +02:00
|
|
|
}
|
2013-07-02 08:06:03 +02:00
|
|
|
|
2017-01-04 22:08:21 +01:00
|
|
|
LoaderDFF::GeometryList LoaderDFF::readGeometryList(const RWBStream &stream) {
|
2016-09-09 22:13:21 +02:00
|
|
|
auto listStream = stream.getInnerStream();
|
2013-07-02 08:06:03 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
auto listStructID = listStream.getNextChunk();
|
|
|
|
if (listStructID != CHUNK_STRUCT) {
|
|
|
|
throw DFFLoaderException("Geometry List missing struct chunk");
|
|
|
|
}
|
2013-07-02 08:06:03 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
char *headerPtr = listStream.getCursor();
|
2013-07-02 08:06:03 +02:00
|
|
|
|
2017-12-21 21:37:58 +01:00
|
|
|
unsigned int numGeometries = bit_cast<std::uint32_t>(*headerPtr);
|
2016-09-09 22:13:21 +02:00
|
|
|
headerPtr += sizeof(std::uint32_t);
|
2013-09-11 20:23:31 +02:00
|
|
|
|
2017-01-04 22:08:21 +01:00
|
|
|
std::vector<GeometryPtr> geometrylist;
|
|
|
|
geometrylist.reserve(numGeometries);
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
for (auto chunkID = listStream.getNextChunk(); chunkID != 0;
|
|
|
|
chunkID = listStream.getNextChunk()) {
|
|
|
|
switch (chunkID) {
|
2017-01-04 22:08:21 +01:00
|
|
|
case CHUNK_GEOMETRY: {
|
|
|
|
geometrylist.push_back(readGeometry(listStream));
|
|
|
|
} break;
|
2016-09-09 22:13:21 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-01-04 22:08:21 +01:00
|
|
|
|
|
|
|
return geometrylist;
|
2014-08-04 23:21:01 +02:00
|
|
|
}
|
|
|
|
|
2017-01-04 22:08:21 +01:00
|
|
|
GeometryPtr LoaderDFF::readGeometry(const RWBStream &stream) {
|
2016-09-09 22:13:21 +02:00
|
|
|
auto geomStream = stream.getInnerStream();
|
|
|
|
|
|
|
|
auto geomStructID = geomStream.getNextChunk();
|
|
|
|
if (geomStructID != CHUNK_STRUCT) {
|
|
|
|
throw DFFLoaderException("Geometry missing struct chunk");
|
|
|
|
}
|
|
|
|
|
2017-01-04 22:08:21 +01:00
|
|
|
auto geom = std::make_shared<Geometry>();
|
2016-09-09 22:13:21 +02:00
|
|
|
|
|
|
|
char *headerPtr = geomStream.getCursor();
|
|
|
|
|
2017-12-21 21:37:58 +01:00
|
|
|
geom->flags = bit_cast<std::uint16_t>(*headerPtr);
|
2016-09-09 22:13:21 +02:00
|
|
|
headerPtr += sizeof(std::uint16_t);
|
|
|
|
|
2017-12-21 21:37:58 +01:00
|
|
|
/*unsigned short numUVs = bit_cast<std::uint8_t>(*headerPtr);*/
|
2016-09-09 22:13:21 +02:00
|
|
|
headerPtr += sizeof(std::uint8_t);
|
2017-12-21 21:37:58 +01:00
|
|
|
/*unsigned short moreFlags = bit_cast<std::uint8_t>(*headerPtr);*/
|
2016-09-09 22:13:21 +02:00
|
|
|
headerPtr += sizeof(std::uint8_t);
|
|
|
|
|
2017-12-21 21:37:58 +01:00
|
|
|
unsigned int numTris = bit_cast<std::uint32_t>(*headerPtr);
|
2016-09-09 22:13:21 +02:00
|
|
|
headerPtr += sizeof(std::uint32_t);
|
2017-12-21 21:37:58 +01:00
|
|
|
unsigned int numVerts = bit_cast<std::uint32_t>(*headerPtr);
|
2016-09-09 22:13:21 +02:00
|
|
|
headerPtr += sizeof(std::uint32_t);
|
2017-12-21 21:37:58 +01:00
|
|
|
|
|
|
|
/*unsigned int numFrames = bit_cast<std::uint32_t>(*headerPtr);*/
|
2016-09-09 22:13:21 +02:00
|
|
|
headerPtr += sizeof(std::uint32_t);
|
|
|
|
|
2017-01-03 17:21:21 +01:00
|
|
|
std::vector<GeometryVertex> verts;
|
2016-09-09 22:13:21 +02:00
|
|
|
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) {
|
2017-12-21 21:37:58 +01:00
|
|
|
verts[v].colour = bit_cast<glm::u8vec4>(*headerPtr);
|
2016-09-09 22:13:21 +02:00
|
|
|
headerPtr += sizeof(glm::u8vec4);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (size_t v = 0; v < numVerts; ++v) {
|
|
|
|
verts[v].colour = {255, 255, 255, 255};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((geom->flags & 4) == 4 || (geom->flags & 128) == 128) {
|
|
|
|
for (size_t v = 0; v < numVerts; ++v) {
|
2017-12-21 21:37:58 +01:00
|
|
|
verts[v].texcoord = bit_cast<glm::vec2>(*headerPtr);
|
2016-09-09 22:13:21 +02:00
|
|
|
headerPtr += sizeof(glm::vec2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab indicies data to generate normals (if applicable).
|
2017-12-21 21:37:58 +01:00
|
|
|
auto triangles = std::make_unique<RW::BSGeometryTriangle[]>(numTris);
|
|
|
|
memcpy(triangles.get(), headerPtr, sizeof(RW::BSGeometryTriangle) * numTris);
|
2016-09-09 22:13:21 +02:00
|
|
|
headerPtr += sizeof(RW::BSGeometryTriangle) * numTris;
|
|
|
|
|
2017-12-21 21:37:58 +01:00
|
|
|
geom->geometryBounds = bit_cast<RW::BSGeometryBounds>(*headerPtr);
|
2016-09-09 22:13:21 +02:00
|
|
|
geom->geometryBounds.radius = std::abs(geom->geometryBounds.radius);
|
|
|
|
headerPtr += sizeof(RW::BSGeometryBounds);
|
|
|
|
|
|
|
|
for (size_t v = 0; v < numVerts; ++v) {
|
2017-12-21 21:37:58 +01:00
|
|
|
verts[v].position = bit_cast<glm::vec3>(*headerPtr);
|
2016-09-09 22:13:21 +02:00
|
|
|
headerPtr += sizeof(glm::vec3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((geom->flags & 16) == 16) {
|
|
|
|
for (size_t v = 0; v < numVerts; ++v) {
|
2017-12-21 21:37:58 +01:00
|
|
|
verts[v].normal = bit_cast<glm::vec3>(*headerPtr);
|
2016-09-09 22:13:21 +02:00
|
|
|
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 = verts[triangle.first];
|
|
|
|
auto &B = verts[triangle.second];
|
|
|
|
auto &C = 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:
|
2017-01-04 22:08:21 +01:00
|
|
|
readMaterialList(geom, geomStream);
|
2016-09-09 22:13:21 +02:00
|
|
|
break;
|
|
|
|
case CHUNK_EXTENSION:
|
2017-01-04 22:08:21 +01:00
|
|
|
readGeometryExtension(geom, geomStream);
|
2016-09-09 22:13:21 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 22:08:21 +01:00
|
|
|
geom->dbuff.setFaceType(geom->facetype == Geometry::Triangles
|
|
|
|
? GL_TRIANGLES
|
|
|
|
: GL_TRIANGLE_STRIP);
|
2016-09-09 22:13:21 +02:00
|
|
|
geom->gbuff.uploadVertices(verts);
|
|
|
|
geom->dbuff.addGeometry(&geom->gbuff);
|
|
|
|
|
|
|
|
glGenBuffers(1, &geom->EBO);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, geom->EBO);
|
|
|
|
|
|
|
|
size_t icount = std::accumulate(
|
2018-08-29 22:46:00 +02:00
|
|
|
geom->subgeom.begin(), geom->subgeom.end(), size_t{0u},
|
2017-01-03 17:21:21 +01:00
|
|
|
[](size_t a, const SubGeometry &b) { return a + b.numIndices; });
|
2018-02-18 01:22:03 +01:00
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(uint32_t) * icount, nullptr,
|
2016-09-09 22:13:21 +02:00
|
|
|
GL_STATIC_DRAW);
|
|
|
|
for (auto &sg : geom->subgeom) {
|
|
|
|
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, sg.start * sizeof(uint32_t),
|
|
|
|
sizeof(uint32_t) * sg.numIndices, sg.indices.data());
|
|
|
|
}
|
2017-01-04 22:08:21 +01:00
|
|
|
|
|
|
|
return geom;
|
2014-08-04 23:21:01 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 19:13:11 +02:00
|
|
|
void LoaderDFF::readMaterialList(const GeometryPtr &geom, const RWBStream &stream) {
|
2016-09-09 22:13:21 +02:00
|
|
|
auto listStream = stream.getInnerStream();
|
|
|
|
|
|
|
|
auto listStructID = listStream.getNextChunk();
|
|
|
|
if (listStructID != CHUNK_STRUCT) {
|
|
|
|
throw DFFLoaderException("MaterialList missing struct chunk");
|
|
|
|
}
|
|
|
|
|
2017-12-21 21:37:58 +01:00
|
|
|
unsigned int numMaterials = bit_cast<std::uint32_t>(*listStream.getCursor());
|
2016-09-09 22:13:21 +02:00
|
|
|
|
2017-01-04 22:08:21 +01:00
|
|
|
geom->materials.reserve(numMaterials);
|
2016-09-09 22:13:21 +02:00
|
|
|
|
|
|
|
RWBStream::ChunkID chunkID;
|
|
|
|
while ((chunkID = listStream.getNextChunk())) {
|
|
|
|
switch (chunkID) {
|
|
|
|
case CHUNK_MATERIAL:
|
2017-01-04 22:08:21 +01:00
|
|
|
readMaterial(geom, listStream);
|
2016-09-09 22:13:21 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-08-04 23:21:01 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 19:13:11 +02:00
|
|
|
void LoaderDFF::readMaterial(const GeometryPtr &geom, const RWBStream &stream) {
|
2016-09-09 22:13:21 +02:00
|
|
|
auto materialStream = stream.getInnerStream();
|
|
|
|
|
|
|
|
auto matStructID = materialStream.getNextChunk();
|
|
|
|
if (matStructID != CHUNK_STRUCT) {
|
|
|
|
throw DFFLoaderException("Material missing struct chunk");
|
|
|
|
}
|
|
|
|
|
|
|
|
char *matData = materialStream.getCursor();
|
|
|
|
|
2017-01-03 17:21:21 +01:00
|
|
|
Geometry::Material material;
|
2016-09-09 22:13:21 +02:00
|
|
|
|
|
|
|
// Unkown
|
|
|
|
matData += sizeof(std::uint32_t);
|
2017-12-21 21:37:58 +01:00
|
|
|
material.colour = bit_cast<glm::u8vec4>(*matData);
|
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
matData += sizeof(std::uint32_t);
|
|
|
|
// Unkown
|
|
|
|
matData += sizeof(std::uint32_t);
|
2017-12-21 21:37:58 +01:00
|
|
|
/*bool usesTexture = bit_cast<std::uint32_t>(*matData);*/
|
2016-09-09 22:13:21 +02:00
|
|
|
matData += sizeof(std::uint32_t);
|
|
|
|
|
2017-12-21 21:37:58 +01:00
|
|
|
material.ambientIntensity = bit_cast<float>(*matData);
|
2016-09-09 22:13:21 +02:00
|
|
|
matData += sizeof(float);
|
2017-12-21 21:37:58 +01:00
|
|
|
|
|
|
|
/*float specular = bit_cast<float>(*matData);*/
|
2016-09-09 22:13:21 +02:00
|
|
|
matData += sizeof(float);
|
2017-12-21 21:37:58 +01:00
|
|
|
|
|
|
|
material.diffuseIntensity = bit_cast<float>(*matData);
|
2016-09-09 22:13:21 +02:00
|
|
|
matData += sizeof(float);
|
|
|
|
material.flags = 0;
|
|
|
|
|
|
|
|
RWBStream::ChunkID chunkID;
|
|
|
|
while ((chunkID = materialStream.getNextChunk())) {
|
|
|
|
switch (chunkID) {
|
|
|
|
case CHUNK_TEXTURE:
|
2017-01-04 22:08:21 +01:00
|
|
|
readTexture(material, materialStream);
|
2016-09-09 22:13:21 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-01-04 22:08:21 +01:00
|
|
|
|
|
|
|
geom->materials.push_back(material);
|
2014-08-04 23:21:01 +02:00
|
|
|
}
|
2013-07-02 08:06:03 +02:00
|
|
|
|
2017-01-04 22:08:21 +01:00
|
|
|
void LoaderDFF::readTexture(Geometry::Material &material,
|
|
|
|
const RWBStream &stream) {
|
2016-09-09 22:13:21 +02:00
|
|
|
auto texStream = stream.getInnerStream();
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
auto texStructID = texStream.getNextChunk();
|
|
|
|
if (texStructID != CHUNK_STRUCT) {
|
|
|
|
throw DFFLoaderException("Texture missing struct chunk");
|
|
|
|
}
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
// There's some data in the Texture's struct, but we don't know what it is.
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
/// @todo improve how these strings are read.
|
|
|
|
std::string name, alpha;
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
texStream.getNextChunk();
|
|
|
|
name = texStream.getCursor();
|
|
|
|
texStream.getNextChunk();
|
|
|
|
alpha = texStream.getCursor();
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
|
|
|
|
std::transform(alpha.begin(), alpha.end(), alpha.begin(), ::tolower);
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2019-01-18 03:46:59 +01:00
|
|
|
auto textureInstPtr = textureLookup ? textureLookup(name, alpha) : nullptr;
|
|
|
|
material.textures.emplace_back(std::move(name), std::move(alpha), textureInstPtr);
|
2013-07-02 08:06:03 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 19:13:11 +02:00
|
|
|
void LoaderDFF::readGeometryExtension(const GeometryPtr &geom,
|
2017-01-04 22:08:21 +01:00
|
|
|
const RWBStream &stream) {
|
2016-09-09 22:13:21 +02:00
|
|
|
auto extStream = stream.getInnerStream();
|
|
|
|
|
|
|
|
RWBStream::ChunkID chunkID;
|
|
|
|
while ((chunkID = extStream.getNextChunk())) {
|
|
|
|
switch (chunkID) {
|
|
|
|
case CHUNK_BINMESHPLG:
|
2017-01-04 22:08:21 +01:00
|
|
|
readBinMeshPLG(geom, extStream);
|
2016-09-09 22:13:21 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-08-04 23:21:01 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 19:13:11 +02:00
|
|
|
void LoaderDFF::readBinMeshPLG(const GeometryPtr &geom, const RWBStream &stream) {
|
2016-09-09 22:13:21 +02:00
|
|
|
auto data = stream.getCursor();
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2017-12-21 21:37:58 +01:00
|
|
|
geom->facetype = static_cast<Geometry::FaceType>(bit_cast<std::uint32_t>(*data));
|
2016-09-09 22:13:21 +02:00
|
|
|
data += sizeof(std::uint32_t);
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2017-12-21 21:37:58 +01:00
|
|
|
unsigned int numSplits = bit_cast<std::uint32_t>(*data);
|
2016-09-09 22:13:21 +02:00
|
|
|
data += sizeof(std::uint32_t);
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
// Number of triangles.
|
|
|
|
data += sizeof(std::uint32_t);
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2017-01-04 22:08:21 +01:00
|
|
|
geom->subgeom.reserve(numSplits);
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
size_t start = 0;
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
for (size_t s = 0; s < numSplits; ++s) {
|
2017-01-03 17:21:21 +01:00
|
|
|
SubGeometry sg;
|
2017-12-21 21:37:58 +01:00
|
|
|
sg.numIndices = bit_cast<std::uint32_t>(*data);
|
2016-09-09 22:13:21 +02:00
|
|
|
data += sizeof(std::uint32_t);
|
2017-12-21 21:37:58 +01:00
|
|
|
sg.material = bit_cast<std::uint32_t>(*data);
|
2016-09-09 22:13:21 +02:00
|
|
|
data += sizeof(std::uint32_t);
|
|
|
|
sg.start = start;
|
|
|
|
start += sg.numIndices;
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
sg.indices.resize(sg.numIndices);
|
|
|
|
std::memcpy(sg.indices.data(), data,
|
|
|
|
sizeof(std::uint32_t) * sg.numIndices);
|
|
|
|
data += sizeof(std::uint32_t) * sg.numIndices;
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2018-01-09 18:42:42 +01:00
|
|
|
geom->subgeom.push_back(std::move(sg));
|
2016-09-09 22:13:21 +02:00
|
|
|
}
|
2014-08-04 23:21:01 +02:00
|
|
|
}
|
|
|
|
|
2017-01-04 22:08:21 +01:00
|
|
|
AtomicPtr LoaderDFF::readAtomic(FrameList &framelist,
|
|
|
|
GeometryList &geometrylist,
|
|
|
|
const RWBStream &stream) {
|
2016-09-09 22:13:21 +02:00
|
|
|
auto atomicStream = stream.getInnerStream();
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
auto atomicStructID = atomicStream.getNextChunk();
|
|
|
|
if (atomicStructID != CHUNK_STRUCT) {
|
|
|
|
throw DFFLoaderException("Atomic missing struct chunk");
|
|
|
|
}
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
auto data = atomicStream.getCursor();
|
2017-12-21 21:37:58 +01:00
|
|
|
std::uint32_t frame = bit_cast<std::uint32_t>(*data);
|
2016-09-09 22:13:21 +02:00
|
|
|
data += sizeof(std::uint32_t);
|
2017-12-21 21:37:58 +01:00
|
|
|
|
|
|
|
std::uint32_t geometry = bit_cast<std::uint32_t>(*data);
|
2017-01-04 22:24:35 +01:00
|
|
|
data += sizeof(std::uint32_t);
|
2017-12-21 21:37:58 +01:00
|
|
|
|
|
|
|
std::uint32_t flags = bit_cast<std::uint32_t>(*data);
|
2017-01-04 22:08:21 +01:00
|
|
|
|
|
|
|
// Verify the atomic's particulars
|
|
|
|
RW_CHECK(frame < framelist.size(), "atomic frame " << frame
|
|
|
|
<< " out of bounds");
|
|
|
|
RW_CHECK(geometry < geometrylist.size(),
|
|
|
|
"atomic geometry " << geometry << " out of bounds");
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2017-01-04 22:08:21 +01:00
|
|
|
auto atomic = std::make_shared<Atomic>();
|
|
|
|
if (geometry < geometrylist.size()) {
|
|
|
|
atomic->setGeometry(geometrylist[geometry]);
|
|
|
|
}
|
|
|
|
if (frame < framelist.size()) {
|
|
|
|
atomic->setFrame(framelist[frame]);
|
|
|
|
}
|
2017-01-04 22:24:35 +01:00
|
|
|
atomic->setFlags(flags);
|
2017-01-04 22:08:21 +01:00
|
|
|
|
|
|
|
return atomic;
|
2013-07-02 08:06:03 +02:00
|
|
|
}
|
|
|
|
|
2018-07-06 22:05:43 +02:00
|
|
|
ClumpPtr LoaderDFF::loadFromMemory(const FileContentsInfo& file) {
|
rwlib: Use ClumpPtr instead of Clump*
Should fix these memory leaks:
==22737== 14,598,040 (131,472 direct, 14,466,568 indirect) bytes in 2,739 blocks are definitely lost in loss record 3,124 of 3,126
==22737== at 0x4C2F1CA: operator new(unsigned long) (vg_replace_malloc.c:334)
==22737== by 0x90FE4B: LoaderDFF::loadFromMemory(std::shared_ptr<FileContentsInfo>) (LoaderDFF.cpp:443)
==22737== by 0x7BCC86: GameData::loadModel(unsigned short) (GameData.cpp:474)
==22737== by 0x7DF7BC: GameWorld::createInstance(unsigned short, glm::tvec3<float, (glm::precision)0> const&, glm::tquat<float, (glm::precision)0> const&) (GameWorld.cpp:144)
==22737== by 0x7DF44C: GameWorld::placeItems(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (GameWorld.cpp:120)
==22737== by 0x758D38: RWGame::newGame() (RWGame.cpp:116)
==22737== by 0x786389: LoadingState::enter() (LoadingState.cpp:9)
==22737== by 0x75DC59: void StateManager::enter<LoadingState, RWGame*, RWGame::RWGame(Logger&, int, char**)::{lambda()#1}>(RWGame*&&, RWGame::RWGame(Logger&, int, char**)::{lambda()#1}&&) (StateManager.hpp:40)
==22737== by 0x758484: RWGame::RWGame(Logger&, int, char**) (RWGame.cpp:81)
==22737== by 0x747815: main (main.cpp:13)
2017-09-13 00:47:22 +02:00
|
|
|
auto model = std::make_shared<Clump>();
|
2016-09-09 22:13:21 +02:00
|
|
|
|
2018-08-05 18:28:59 +02:00
|
|
|
RWBStream rootStream(file.data.get(), file.length);
|
2016-09-09 22:13:21 +02:00
|
|
|
|
|
|
|
auto rootID = rootStream.getNextChunk();
|
|
|
|
if (rootID != CHUNK_CLUMP) {
|
|
|
|
throw DFFLoaderException("Invalid root section ID " +
|
|
|
|
std::to_string(rootID));
|
|
|
|
}
|
|
|
|
|
|
|
|
RWBStream modelStream = rootStream.getInnerStream();
|
|
|
|
auto rootStructID = modelStream.getNextChunk();
|
|
|
|
if (rootStructID != CHUNK_STRUCT) {
|
|
|
|
throw DFFLoaderException("Clump missing struct chunk");
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is only one value in the struct section.
|
2017-12-21 21:37:58 +01:00
|
|
|
std::uint32_t numAtomics = bit_cast<std::uint32_t>(*rootStream.getCursor());
|
2017-01-04 22:08:21 +01:00
|
|
|
RW_UNUSED(numAtomics);
|
|
|
|
|
|
|
|
GeometryList geometrylist;
|
|
|
|
FrameList framelist;
|
2016-09-09 22:13:21 +02:00
|
|
|
|
|
|
|
// Process everything inside the clump stream.
|
|
|
|
RWBStream::ChunkID chunkID;
|
|
|
|
while ((chunkID = modelStream.getNextChunk())) {
|
|
|
|
switch (chunkID) {
|
|
|
|
case CHUNK_FRAMELIST:
|
2017-01-04 22:08:21 +01:00
|
|
|
framelist = readFrameList(modelStream);
|
2016-09-09 22:13:21 +02:00
|
|
|
break;
|
|
|
|
case CHUNK_GEOMETRYLIST:
|
2017-01-04 22:08:21 +01:00
|
|
|
geometrylist = readGeometryList(modelStream);
|
2016-09-09 22:13:21 +02:00
|
|
|
break;
|
2017-01-04 22:08:21 +01:00
|
|
|
case CHUNK_ATOMIC: {
|
|
|
|
auto atomic = readAtomic(framelist, geometrylist, modelStream);
|
|
|
|
RW_CHECK(atomic, "Failed to read atomic");
|
|
|
|
if (!atomic) {
|
|
|
|
// Abort reading the rest of the clump
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
model->addAtomic(atomic);
|
|
|
|
} break;
|
2016-09-09 22:13:21 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 22:08:21 +01:00
|
|
|
if (!framelist.empty()) {
|
|
|
|
model->setFrame(framelist[0]);
|
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
// Ensure the model has cached metrics
|
|
|
|
model->recalculateMetrics();
|
|
|
|
|
|
|
|
return model;
|
2013-07-02 08:06:03 +02:00
|
|
|
}
|