assimp/code/AssetLib/Assbin/AssbinLoader.cpp

735 lines
26 KiB
C++
Raw Permalink Normal View History

2014-08-05 22:39:54 +00:00
/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
2022-01-10 20:13:43 +00:00
Copyright (c) 2006-2022, assimp team
2018-01-28 18:42:05 +00:00
2014-08-05 22:39:54 +00:00
All rights reserved.
2015-05-19 03:52:10 +00:00
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the following
2014-08-05 22:39:54 +00:00
conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.
2015-05-19 03:52:10 +00:00
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2014-08-05 22:39:54 +00:00
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2015-05-19 03:52:10 +00:00
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2014-08-05 22:39:54 +00:00
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2015-05-19 03:52:10 +00:00
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2014-08-05 22:39:54 +00:00
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2015-05-19 03:52:10 +00:00
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2014-08-05 22:39:54 +00:00
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------
*/
/** @file AssbinLoader.cpp
* @brief Implementation of the .assbin importer class
*
* see assbin_chunks.h
*/
#ifndef ASSIMP_BUILD_NO_ASSBIN_IMPORTER
// internal headers
#include "AssetLib/Assbin/AssbinLoader.h"
#include "Common/assbin_chunks.h"
#include <assimp/MemoryIOWrapper.h>
2016-06-06 20:04:29 +00:00
#include <assimp/anim.h>
2017-02-22 16:20:26 +00:00
#include <assimp/importerdesc.h>
2020-03-14 10:16:44 +00:00
#include <assimp/mesh.h>
#include <assimp/scene.h>
#include <memory>
2015-04-15 23:00:17 +00:00
#ifdef ASSIMP_BUILD_NO_OWN_ZLIB
2020-03-14 10:16:44 +00:00
#include <zlib.h>
#else
2020-03-14 10:16:44 +00:00
#include <contrib/zlib/zlib.h>
#endif
2014-08-05 22:39:54 +00:00
using namespace Assimp;
2014-08-05 22:52:05 +00:00
static const aiImporterDesc desc = {
"Assimp Binary Importer",
2015-05-19 03:57:13 +00:00
"Gargaj / Conspiracy",
"",
"",
aiImporterFlags_SupportBinaryFlavour | aiImporterFlags_SupportCompressedFlavour,
0,
0,
0,
0,
"assbin"
2014-08-05 22:52:05 +00:00
};
// -----------------------------------------------------------------------------------
2020-03-14 10:16:44 +00:00
const aiImporterDesc *AssbinImporter::GetInfo() const {
2015-05-19 03:57:13 +00:00
return &desc;
2014-08-05 22:39:54 +00:00
}
// -----------------------------------------------------------------------------------
2020-03-14 10:16:44 +00:00
bool AssbinImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const {
IOStream *in = pIOHandler->Open(pFile);
if (nullptr == in) {
2015-05-19 03:57:13 +00:00
return false;
}
2014-08-05 22:52:05 +00:00
2015-05-19 03:57:13 +00:00
char s[32];
2020-03-14 10:16:44 +00:00
in->Read(s, sizeof(char), 32);
2014-08-05 22:52:05 +00:00
2015-05-19 03:57:13 +00:00
pIOHandler->Close(in);
2014-08-05 22:52:05 +00:00
2020-03-14 10:16:44 +00:00
return strncmp(s, "ASSIMP.binary-dump.", 19) == 0;
2014-08-05 22:39:54 +00:00
}
// -----------------------------------------------------------------------------------
2014-08-06 11:26:26 +00:00
template <typename T>
2020-03-14 10:16:44 +00:00
T Read(IOStream *stream) {
2015-05-19 03:57:13 +00:00
T t;
2020-03-14 10:16:44 +00:00
size_t res = stream->Read(&t, sizeof(T), 1);
2020-03-15 09:17:54 +00:00
if (res != 1) {
throw DeadlyImportError("Unexpected EOF");
2020-03-15 09:17:54 +00:00
}
2015-05-19 03:57:13 +00:00
return t;
2014-08-06 11:26:26 +00:00
}
// -----------------------------------------------------------------------------------
template <>
2020-03-14 10:16:44 +00:00
aiVector3D Read<aiVector3D>(IOStream *stream) {
2015-05-19 03:57:13 +00:00
aiVector3D v;
2020-03-14 10:16:44 +00:00
v.x = Read<ai_real>(stream);
v.y = Read<ai_real>(stream);
v.z = Read<ai_real>(stream);
2015-05-19 03:57:13 +00:00
return v;
}
// -----------------------------------------------------------------------------------
template <>
2020-03-14 10:16:44 +00:00
aiColor4D Read<aiColor4D>(IOStream *stream) {
2015-05-19 03:57:13 +00:00
aiColor4D c;
2020-03-14 10:16:44 +00:00
c.r = Read<ai_real>(stream);
c.g = Read<ai_real>(stream);
c.b = Read<ai_real>(stream);
c.a = Read<ai_real>(stream);
2015-05-19 03:57:13 +00:00
return c;
}
// -----------------------------------------------------------------------------------
template <>
2020-03-14 10:16:44 +00:00
aiQuaternion Read<aiQuaternion>(IOStream *stream) {
2015-05-19 03:57:13 +00:00
aiQuaternion v;
2020-03-14 10:16:44 +00:00
v.w = Read<ai_real>(stream);
v.x = Read<ai_real>(stream);
v.y = Read<ai_real>(stream);
v.z = Read<ai_real>(stream);
2015-05-19 03:57:13 +00:00
return v;
}
// -----------------------------------------------------------------------------------
2014-08-06 11:26:26 +00:00
template <>
2020-03-14 10:16:44 +00:00
aiString Read<aiString>(IOStream *stream) {
2015-05-19 03:57:13 +00:00
aiString s;
2020-03-14 10:16:44 +00:00
stream->Read(&s.length, 4, 1);
if (s.length) {
stream->Read(s.data, s.length, 1);
}
2015-05-19 03:57:13 +00:00
s.data[s.length] = 0;
2020-03-14 10:16:44 +00:00
2015-05-19 03:57:13 +00:00
return s;
2014-08-06 11:26:26 +00:00
}
// -----------------------------------------------------------------------------------
template <>
2020-03-14 10:16:44 +00:00
aiVertexWeight Read<aiVertexWeight>(IOStream *stream) {
2015-05-19 03:57:13 +00:00
aiVertexWeight w;
w.mVertexId = Read<unsigned int>(stream);
2020-03-14 10:16:44 +00:00
w.mWeight = Read<ai_real>(stream);
2015-05-19 03:57:13 +00:00
return w;
}
// -----------------------------------------------------------------------------------
2014-08-06 11:26:26 +00:00
template <>
2020-03-14 10:16:44 +00:00
aiMatrix4x4 Read<aiMatrix4x4>(IOStream *stream) {
2015-05-19 03:57:13 +00:00
aiMatrix4x4 m;
2020-03-14 10:16:44 +00:00
for (unsigned int i = 0; i < 4; ++i) {
for (unsigned int i2 = 0; i2 < 4; ++i2) {
m[i][i2] = Read<ai_real>(stream);
2015-05-19 03:57:13 +00:00
}
}
return m;
2014-08-06 11:26:26 +00:00
}
// -----------------------------------------------------------------------------------
template <>
2020-03-14 10:16:44 +00:00
aiVectorKey Read<aiVectorKey>(IOStream *stream) {
2015-05-19 03:57:13 +00:00
aiVectorKey v;
v.mTime = Read<double>(stream);
v.mValue = Read<aiVector3D>(stream);
return v;
}
// -----------------------------------------------------------------------------------
template <>
2020-03-14 10:16:44 +00:00
aiQuatKey Read<aiQuatKey>(IOStream *stream) {
2015-05-19 03:57:13 +00:00
aiQuatKey v;
v.mTime = Read<double>(stream);
v.mValue = Read<aiQuaternion>(stream);
return v;
}
// -----------------------------------------------------------------------------------
template <typename T>
2020-03-14 10:16:44 +00:00
void ReadArray(IOStream *stream, T *out, unsigned int size) {
ai_assert(nullptr != stream);
ai_assert(nullptr != out);
2020-03-14 10:16:44 +00:00
for (unsigned int i = 0; i < size; i++) {
out[i] = Read<T>(stream);
}
}
// -----------------------------------------------------------------------------------
template <typename T>
2020-03-14 10:16:44 +00:00
void ReadBounds(IOStream *stream, T * /*p*/, unsigned int n) {
2015-05-19 03:57:13 +00:00
// not sure what to do here, the data isn't really useful.
2020-03-14 10:16:44 +00:00
stream->Seek(sizeof(T) * n, aiOrigin_CUR);
2014-08-06 12:06:08 +00:00
}
// -----------------------------------------------------------------------------------
2020-03-14 10:16:44 +00:00
void AssbinImporter::ReadBinaryNode(IOStream *stream, aiNode **onode, aiNode *parent) {
if (Read<uint32_t>(stream) != ASSBIN_CHUNK_AINODE)
throw DeadlyImportError("Magic chunk identifiers are wrong!");
2020-03-14 10:16:44 +00:00
/*uint32_t size =*/Read<uint32_t>(stream);
2015-05-19 03:57:13 +00:00
std::unique_ptr<aiNode> node(new aiNode());
2015-05-19 03:57:13 +00:00
node->mName = Read<aiString>(stream);
node->mTransformation = Read<aiMatrix4x4>(stream);
unsigned numChildren = Read<unsigned int>(stream);
unsigned numMeshes = Read<unsigned int>(stream);
2020-03-14 10:16:44 +00:00
unsigned int nb_metadata = Read<unsigned int>(stream);
2020-03-14 10:16:44 +00:00
if (parent) {
node->mParent = parent;
}
2015-05-19 03:57:13 +00:00
2020-03-14 10:16:44 +00:00
if (numMeshes) {
node->mMeshes = new unsigned int[numMeshes];
for (unsigned int i = 0; i < numMeshes; ++i) {
node->mMeshes[i] = Read<unsigned int>(stream);
node->mNumMeshes++;
2015-05-19 03:57:13 +00:00
}
}
if (numChildren) {
2020-03-14 10:16:44 +00:00
node->mChildren = new aiNode *[numChildren];
for (unsigned int i = 0; i < numChildren; ++i) {
2020-03-14 10:16:44 +00:00
ReadBinaryNode(stream, &node->mChildren[i], node.get());
node->mNumChildren++;
2015-05-19 03:57:13 +00:00
}
}
2014-08-06 11:26:26 +00:00
2020-03-14 10:16:44 +00:00
if (nb_metadata > 0) {
node->mMetaData = aiMetadata::Alloc(nb_metadata);
2017-07-15 23:02:33 +00:00
for (unsigned int i = 0; i < nb_metadata; ++i) {
node->mMetaData->mKeys[i] = Read<aiString>(stream);
2020-03-14 10:16:44 +00:00
node->mMetaData->mValues[i].mType = (aiMetadataType)Read<uint16_t>(stream);
void *data = nullptr;
2017-07-15 23:02:33 +00:00
switch (node->mMetaData->mValues[i].mType) {
case AI_BOOL:
data = new bool(Read<bool>(stream));
break;
case AI_INT32:
data = new int32_t(Read<int32_t>(stream));
break;
case AI_UINT64:
data = new uint64_t(Read<uint64_t>(stream));
break;
case AI_FLOAT:
data = new ai_real(Read<ai_real>(stream));
break;
case AI_DOUBLE:
data = new double(Read<double>(stream));
break;
case AI_AISTRING:
data = new aiString(Read<aiString>(stream));
break;
case AI_AIVECTOR3D:
data = new aiVector3D(Read<aiVector3D>(stream));
break;
2017-07-15 23:02:33 +00:00
#ifndef SWIG
case FORCE_32BIT:
2017-07-15 23:02:33 +00:00
#endif // SWIG
default:
break;
2017-07-15 23:02:33 +00:00
}
2020-03-14 10:16:44 +00:00
node->mMetaData->mValues[i].mData = data;
}
}
*onode = node.release();
2014-08-06 11:26:26 +00:00
}
2014-08-06 12:06:08 +00:00
// -----------------------------------------------------------------------------------
2020-03-14 10:16:44 +00:00
void AssbinImporter::ReadBinaryBone(IOStream *stream, aiBone *b) {
if (Read<uint32_t>(stream) != ASSBIN_CHUNK_AIBONE)
throw DeadlyImportError("Magic chunk identifiers are wrong!");
2020-03-14 10:16:44 +00:00
/*uint32_t size =*/Read<uint32_t>(stream);
2015-05-19 03:57:13 +00:00
b->mName = Read<aiString>(stream);
b->mNumWeights = Read<unsigned int>(stream);
b->mOffsetMatrix = Read<aiMatrix4x4>(stream);
// for the moment we write dumb min/max values for the bones, too.
// maybe I'll add a better, hash-like solution later
if (shortened) {
2020-03-14 10:16:44 +00:00
ReadBounds(stream, b->mWeights, b->mNumWeights);
} else {
// else write as usual
2015-05-19 03:57:13 +00:00
b->mWeights = new aiVertexWeight[b->mNumWeights];
2020-03-14 10:16:44 +00:00
ReadArray<aiVertexWeight>(stream, b->mWeights, b->mNumWeights);
2015-05-19 03:57:13 +00:00
}
2014-08-06 12:06:08 +00:00
}
// -----------------------------------------------------------------------------------
static bool fitsIntoUI16(unsigned int mNumVertices) {
2020-03-14 10:16:44 +00:00
return (mNumVertices < (1u << 16));
}
2020-03-15 09:17:54 +00:00
// -----------------------------------------------------------------------------------
2020-03-14 10:16:44 +00:00
void AssbinImporter::ReadBinaryMesh(IOStream *stream, aiMesh *mesh) {
if (Read<uint32_t>(stream) != ASSBIN_CHUNK_AIMESH)
throw DeadlyImportError("Magic chunk identifiers are wrong!");
2020-03-14 10:16:44 +00:00
/*uint32_t size =*/Read<uint32_t>(stream);
2015-05-19 03:57:13 +00:00
mesh->mPrimitiveTypes = Read<unsigned int>(stream);
mesh->mNumVertices = Read<unsigned int>(stream);
mesh->mNumFaces = Read<unsigned int>(stream);
mesh->mNumBones = Read<unsigned int>(stream);
mesh->mMaterialIndex = Read<unsigned int>(stream);
// first of all, write bits for all existent vertex components
unsigned int c = Read<unsigned int>(stream);
if (c & ASSBIN_MESH_HAS_POSITIONS) {
2015-05-19 03:57:13 +00:00
if (shortened) {
2020-03-14 10:16:44 +00:00
ReadBounds(stream, mesh->mVertices, mesh->mNumVertices);
} else {
// else write as usual
2015-05-19 03:57:13 +00:00
mesh->mVertices = new aiVector3D[mesh->mNumVertices];
2020-03-14 10:16:44 +00:00
ReadArray<aiVector3D>(stream, mesh->mVertices, mesh->mNumVertices);
2015-05-19 03:57:13 +00:00
}
}
if (c & ASSBIN_MESH_HAS_NORMALS) {
2015-05-19 03:57:13 +00:00
if (shortened) {
2020-03-14 10:16:44 +00:00
ReadBounds(stream, mesh->mNormals, mesh->mNumVertices);
} else {
// else write as usual
2015-05-19 03:57:13 +00:00
mesh->mNormals = new aiVector3D[mesh->mNumVertices];
2020-03-14 10:16:44 +00:00
ReadArray<aiVector3D>(stream, mesh->mNormals, mesh->mNumVertices);
2015-05-19 03:57:13 +00:00
}
}
if (c & ASSBIN_MESH_HAS_TANGENTS_AND_BITANGENTS) {
2015-05-19 03:57:13 +00:00
if (shortened) {
2020-03-14 10:16:44 +00:00
ReadBounds(stream, mesh->mTangents, mesh->mNumVertices);
ReadBounds(stream, mesh->mBitangents, mesh->mNumVertices);
} else {
// else write as usual
2015-05-19 03:57:13 +00:00
mesh->mTangents = new aiVector3D[mesh->mNumVertices];
2020-03-14 10:16:44 +00:00
ReadArray<aiVector3D>(stream, mesh->mTangents, mesh->mNumVertices);
2015-05-19 03:57:13 +00:00
mesh->mBitangents = new aiVector3D[mesh->mNumVertices];
2020-03-14 10:16:44 +00:00
ReadArray<aiVector3D>(stream, mesh->mBitangents, mesh->mNumVertices);
2015-05-19 03:57:13 +00:00
}
}
2020-03-14 10:16:44 +00:00
for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_COLOR_SETS; ++n) {
if (!(c & ASSBIN_MESH_HAS_COLOR(n))) {
2015-05-19 03:57:13 +00:00
break;
}
2015-05-19 03:57:13 +00:00
if (shortened) {
2020-03-14 10:16:44 +00:00
ReadBounds(stream, mesh->mColors[n], mesh->mNumVertices);
} else {
// else write as usual
2015-05-19 03:57:13 +00:00
mesh->mColors[n] = new aiColor4D[mesh->mNumVertices];
2020-03-14 10:16:44 +00:00
ReadArray<aiColor4D>(stream, mesh->mColors[n], mesh->mNumVertices);
2015-05-19 03:57:13 +00:00
}
}
2020-03-14 10:16:44 +00:00
for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++n) {
if (!(c & ASSBIN_MESH_HAS_TEXCOORD(n))) {
2015-05-19 03:57:13 +00:00
break;
}
2015-05-19 03:57:13 +00:00
// write number of UV components
mesh->mNumUVComponents[n] = Read<unsigned int>(stream);
if (shortened) {
2020-03-14 10:16:44 +00:00
ReadBounds(stream, mesh->mTextureCoords[n], mesh->mNumVertices);
} else {
// else write as usual
2015-05-19 03:57:13 +00:00
mesh->mTextureCoords[n] = new aiVector3D[mesh->mNumVertices];
2020-03-14 10:16:44 +00:00
ReadArray<aiVector3D>(stream, mesh->mTextureCoords[n], mesh->mNumVertices);
2015-05-19 03:57:13 +00:00
}
}
// write faces. There are no floating-point calculations involved
// in these, so we can write a simple hash over the face data
// to the dump file. We generate a single 32 Bit hash for 512 faces
// using Assimp's standard hashing function.
if (shortened) {
Read<unsigned int>(stream);
2020-03-14 10:16:44 +00:00
} else {
// else write as usual
2015-05-19 03:57:13 +00:00
// if there are less than 2^16 vertices, we can simply use 16 bit integers ...
mesh->mFaces = new aiFace[mesh->mNumFaces];
2020-03-14 10:16:44 +00:00
for (unsigned int i = 0; i < mesh->mNumFaces; ++i) {
aiFace &f = mesh->mFaces[i];
2015-05-19 03:57:13 +00:00
2016-03-19 00:02:39 +00:00
static_assert(AI_MAX_FACE_INDICES <= 0xffff, "AI_MAX_FACE_INDICES <= 0xffff");
2015-05-19 03:57:13 +00:00
f.mNumIndices = Read<uint16_t>(stream);
f.mIndices = new unsigned int[f.mNumIndices];
2020-03-14 10:16:44 +00:00
for (unsigned int a = 0; a < f.mNumIndices; ++a) {
2021-10-05 08:59:43 +00:00
// Check if unsigned short ( 16 bit ) are big enough for the indices
2020-03-14 10:16:44 +00:00
if (fitsIntoUI16(mesh->mNumVertices)) {
2015-05-19 03:57:13 +00:00
f.mIndices[a] = Read<uint16_t>(stream);
} else {
2015-05-19 03:57:13 +00:00
f.mIndices[a] = Read<unsigned int>(stream);
}
}
}
}
// write bones
if (mesh->mNumBones) {
2020-03-14 10:16:44 +00:00
mesh->mBones = new C_STRUCT aiBone *[mesh->mNumBones];
for (unsigned int a = 0; a < mesh->mNumBones; ++a) {
2015-05-19 03:57:13 +00:00
mesh->mBones[a] = new aiBone();
2020-03-14 10:16:44 +00:00
ReadBinaryBone(stream, mesh->mBones[a]);
2015-05-19 03:57:13 +00:00
}
}
2014-08-06 12:06:08 +00:00
}
// -----------------------------------------------------------------------------------
2020-03-14 10:16:44 +00:00
void AssbinImporter::ReadBinaryMaterialProperty(IOStream *stream, aiMaterialProperty *prop) {
if (Read<uint32_t>(stream) != ASSBIN_CHUNK_AIMATERIALPROPERTY)
throw DeadlyImportError("Magic chunk identifiers are wrong!");
2020-03-14 10:16:44 +00:00
/*uint32_t size =*/Read<uint32_t>(stream);
2015-05-19 03:57:13 +00:00
prop->mKey = Read<aiString>(stream);
prop->mSemantic = Read<unsigned int>(stream);
prop->mIndex = Read<unsigned int>(stream);
prop->mDataLength = Read<unsigned int>(stream);
prop->mType = (aiPropertyTypeInfo)Read<unsigned int>(stream);
2020-03-14 10:16:44 +00:00
prop->mData = new char[prop->mDataLength];
stream->Read(prop->mData, 1, prop->mDataLength);
2014-08-06 12:21:53 +00:00
}
// -----------------------------------------------------------------------------------
2020-03-14 10:16:44 +00:00
void AssbinImporter::ReadBinaryMaterial(IOStream *stream, aiMaterial *mat) {
if (Read<uint32_t>(stream) != ASSBIN_CHUNK_AIMATERIAL)
throw DeadlyImportError("Magic chunk identifiers are wrong!");
2020-03-14 10:16:44 +00:00
/*uint32_t size =*/Read<uint32_t>(stream);
2015-05-19 03:57:13 +00:00
mat->mNumAllocated = mat->mNumProperties = Read<unsigned int>(stream);
2020-03-14 10:16:44 +00:00
if (mat->mNumProperties) {
if (mat->mProperties) {
2015-05-19 03:57:13 +00:00
delete[] mat->mProperties;
}
2020-03-14 10:16:44 +00:00
mat->mProperties = new aiMaterialProperty *[mat->mNumProperties];
for (unsigned int i = 0; i < mat->mNumProperties; ++i) {
2015-05-19 03:57:13 +00:00
mat->mProperties[i] = new aiMaterialProperty();
2020-03-14 10:16:44 +00:00
ReadBinaryMaterialProperty(stream, mat->mProperties[i]);
2015-05-19 03:57:13 +00:00
}
}
2014-08-06 12:21:53 +00:00
}
2014-08-06 12:44:06 +00:00
// -----------------------------------------------------------------------------------
2020-03-14 10:16:44 +00:00
void AssbinImporter::ReadBinaryNodeAnim(IOStream *stream, aiNodeAnim *nd) {
if (Read<uint32_t>(stream) != ASSBIN_CHUNK_AINODEANIM)
throw DeadlyImportError("Magic chunk identifiers are wrong!");
2020-03-14 10:16:44 +00:00
/*uint32_t size =*/Read<uint32_t>(stream);
2015-05-19 03:57:13 +00:00
nd->mNodeName = Read<aiString>(stream);
nd->mNumPositionKeys = Read<unsigned int>(stream);
nd->mNumRotationKeys = Read<unsigned int>(stream);
nd->mNumScalingKeys = Read<unsigned int>(stream);
nd->mPreState = (aiAnimBehaviour)Read<unsigned int>(stream);
nd->mPostState = (aiAnimBehaviour)Read<unsigned int>(stream);
if (nd->mNumPositionKeys) {
if (shortened) {
2020-03-14 10:16:44 +00:00
ReadBounds(stream, nd->mPositionKeys, nd->mNumPositionKeys);
2015-05-19 03:57:13 +00:00
} // else write as usual
else {
nd->mPositionKeys = new aiVectorKey[nd->mNumPositionKeys];
2020-03-14 10:16:44 +00:00
ReadArray<aiVectorKey>(stream, nd->mPositionKeys, nd->mNumPositionKeys);
2015-05-19 03:57:13 +00:00
}
}
if (nd->mNumRotationKeys) {
if (shortened) {
2020-03-14 10:16:44 +00:00
ReadBounds(stream, nd->mRotationKeys, nd->mNumRotationKeys);
2015-05-19 03:57:13 +00:00
2020-03-14 10:16:44 +00:00
} else {
// else write as usual
2015-05-19 03:57:13 +00:00
nd->mRotationKeys = new aiQuatKey[nd->mNumRotationKeys];
2020-03-14 10:16:44 +00:00
ReadArray<aiQuatKey>(stream, nd->mRotationKeys, nd->mNumRotationKeys);
2015-05-19 03:57:13 +00:00
}
}
if (nd->mNumScalingKeys) {
if (shortened) {
2020-03-14 10:16:44 +00:00
ReadBounds(stream, nd->mScalingKeys, nd->mNumScalingKeys);
2015-05-19 03:57:13 +00:00
2020-03-14 10:16:44 +00:00
} else {
// else write as usual
2015-05-19 03:57:13 +00:00
nd->mScalingKeys = new aiVectorKey[nd->mNumScalingKeys];
2020-03-14 10:16:44 +00:00
ReadArray<aiVectorKey>(stream, nd->mScalingKeys, nd->mNumScalingKeys);
2015-05-19 03:57:13 +00:00
}
}
2014-08-06 12:44:06 +00:00
}
// -----------------------------------------------------------------------------------
2020-03-14 10:16:44 +00:00
void AssbinImporter::ReadBinaryAnim(IOStream *stream, aiAnimation *anim) {
if (Read<uint32_t>(stream) != ASSBIN_CHUNK_AIANIMATION)
throw DeadlyImportError("Magic chunk identifiers are wrong!");
2020-03-14 10:16:44 +00:00
/*uint32_t size =*/Read<uint32_t>(stream);
2015-05-19 03:57:13 +00:00
2020-03-14 10:16:44 +00:00
anim->mName = Read<aiString>(stream);
anim->mDuration = Read<double>(stream);
anim->mTicksPerSecond = Read<double>(stream);
2015-05-19 03:57:13 +00:00
anim->mNumChannels = Read<unsigned int>(stream);
if (anim->mNumChannels) {
2020-03-14 10:16:44 +00:00
anim->mChannels = new aiNodeAnim *[anim->mNumChannels];
for (unsigned int a = 0; a < anim->mNumChannels; ++a) {
2015-05-19 03:57:13 +00:00
anim->mChannels[a] = new aiNodeAnim();
2020-03-14 10:16:44 +00:00
ReadBinaryNodeAnim(stream, anim->mChannels[a]);
2015-05-19 03:57:13 +00:00
}
}
2014-08-06 12:44:06 +00:00
}
// -----------------------------------------------------------------------------------
2020-03-14 10:16:44 +00:00
void AssbinImporter::ReadBinaryTexture(IOStream *stream, aiTexture *tex) {
if (Read<uint32_t>(stream) != ASSBIN_CHUNK_AITEXTURE)
throw DeadlyImportError("Magic chunk identifiers are wrong!");
2020-03-14 10:16:44 +00:00
/*uint32_t size =*/Read<uint32_t>(stream);
2015-05-19 03:57:13 +00:00
tex->mWidth = Read<unsigned int>(stream);
tex->mHeight = Read<unsigned int>(stream);
2020-03-14 10:16:44 +00:00
stream->Read(tex->achFormatHint, sizeof(char), HINTMAXTEXTURELEN - 1);
2015-05-19 03:57:13 +00:00
2020-03-14 10:16:44 +00:00
if (!shortened) {
2015-05-19 03:57:13 +00:00
if (!tex->mHeight) {
2020-03-14 10:16:44 +00:00
tex->pcData = new aiTexel[tex->mWidth];
stream->Read(tex->pcData, 1, tex->mWidth);
} else {
2020-03-14 10:16:44 +00:00
tex->pcData = new aiTexel[tex->mWidth * tex->mHeight];
stream->Read(tex->pcData, 1, tex->mWidth * tex->mHeight * 4);
2015-05-19 03:57:13 +00:00
}
}
2014-08-06 12:44:06 +00:00
}
2014-08-06 13:01:04 +00:00
// -----------------------------------------------------------------------------------
2020-03-14 10:16:44 +00:00
void AssbinImporter::ReadBinaryLight(IOStream *stream, aiLight *l) {
if (Read<uint32_t>(stream) != ASSBIN_CHUNK_AILIGHT)
throw DeadlyImportError("Magic chunk identifiers are wrong!");
2020-03-14 10:16:44 +00:00
/*uint32_t size =*/Read<uint32_t>(stream);
2014-08-06 13:01:04 +00:00
2015-05-19 03:57:13 +00:00
l->mName = Read<aiString>(stream);
l->mType = (aiLightSourceType)Read<unsigned int>(stream);
2014-08-06 13:01:04 +00:00
l->mPosition = Read<aiVector3D>(stream);
l->mDirection = Read<aiVector3D>(stream);
l->mUp = Read<aiVector3D>(stream);
2015-05-19 03:57:13 +00:00
if (l->mType != aiLightSource_DIRECTIONAL) {
l->mAttenuationConstant = Read<float>(stream);
l->mAttenuationLinear = Read<float>(stream);
l->mAttenuationQuadratic = Read<float>(stream);
}
2014-08-06 13:01:04 +00:00
2015-05-19 03:57:13 +00:00
l->mColorDiffuse = Read<aiColor3D>(stream);
l->mColorSpecular = Read<aiColor3D>(stream);
l->mColorAmbient = Read<aiColor3D>(stream);
2014-08-06 13:01:04 +00:00
2015-05-19 03:57:13 +00:00
if (l->mType == aiLightSource_SPOT) {
l->mAngleInnerCone = Read<float>(stream);
l->mAngleOuterCone = Read<float>(stream);
}
2014-08-06 13:01:04 +00:00
}
// -----------------------------------------------------------------------------------
2020-03-14 10:16:44 +00:00
void AssbinImporter::ReadBinaryCamera(IOStream *stream, aiCamera *cam) {
if (Read<uint32_t>(stream) != ASSBIN_CHUNK_AICAMERA)
throw DeadlyImportError("Magic chunk identifiers are wrong!");
2020-03-14 10:16:44 +00:00
/*uint32_t size =*/Read<uint32_t>(stream);
2015-05-19 03:57:13 +00:00
cam->mName = Read<aiString>(stream);
cam->mPosition = Read<aiVector3D>(stream);
cam->mLookAt = Read<aiVector3D>(stream);
cam->mUp = Read<aiVector3D>(stream);
cam->mHorizontalFOV = Read<float>(stream);
cam->mClipPlaneNear = Read<float>(stream);
cam->mClipPlaneFar = Read<float>(stream);
cam->mAspect = Read<float>(stream);
2014-08-06 13:01:04 +00:00
}
// -----------------------------------------------------------------------------------
2020-03-14 10:16:44 +00:00
void AssbinImporter::ReadBinaryScene(IOStream *stream, aiScene *scene) {
if (Read<uint32_t>(stream) != ASSBIN_CHUNK_AISCENE)
throw DeadlyImportError("Magic chunk identifiers are wrong!");
2020-03-14 10:16:44 +00:00
/*uint32_t size =*/Read<uint32_t>(stream);
2015-05-19 03:57:13 +00:00
2020-03-14 10:16:44 +00:00
scene->mFlags = Read<unsigned int>(stream);
scene->mNumMeshes = Read<unsigned int>(stream);
scene->mNumMaterials = Read<unsigned int>(stream);
2015-05-19 03:57:13 +00:00
scene->mNumAnimations = Read<unsigned int>(stream);
2020-03-14 10:16:44 +00:00
scene->mNumTextures = Read<unsigned int>(stream);
scene->mNumLights = Read<unsigned int>(stream);
scene->mNumCameras = Read<unsigned int>(stream);
2015-05-19 03:57:13 +00:00
// Read node graph
//scene->mRootNode = new aiNode[1];
ReadBinaryNode(stream, &scene->mRootNode, (aiNode *)nullptr);
2015-05-19 03:57:13 +00:00
// Read all meshes
if (scene->mNumMeshes) {
2020-03-14 10:16:44 +00:00
scene->mMeshes = new aiMesh *[scene->mNumMeshes];
memset(scene->mMeshes, 0, scene->mNumMeshes * sizeof(aiMesh *));
for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
2015-05-19 03:57:13 +00:00
scene->mMeshes[i] = new aiMesh();
2020-03-14 10:16:44 +00:00
ReadBinaryMesh(stream, scene->mMeshes[i]);
2015-05-19 03:57:13 +00:00
}
}
// Read materials
if (scene->mNumMaterials) {
2020-03-14 10:16:44 +00:00
scene->mMaterials = new aiMaterial *[scene->mNumMaterials];
memset(scene->mMaterials, 0, scene->mNumMaterials * sizeof(aiMaterial *));
for (unsigned int i = 0; i < scene->mNumMaterials; ++i) {
2015-05-19 03:57:13 +00:00
scene->mMaterials[i] = new aiMaterial();
2020-03-14 10:16:44 +00:00
ReadBinaryMaterial(stream, scene->mMaterials[i]);
2015-05-19 03:57:13 +00:00
}
}
// Read all animations
if (scene->mNumAnimations) {
2020-03-14 10:16:44 +00:00
scene->mAnimations = new aiAnimation *[scene->mNumAnimations];
memset(scene->mAnimations, 0, scene->mNumAnimations * sizeof(aiAnimation *));
for (unsigned int i = 0; i < scene->mNumAnimations; ++i) {
2015-05-19 03:57:13 +00:00
scene->mAnimations[i] = new aiAnimation();
2020-03-14 10:16:44 +00:00
ReadBinaryAnim(stream, scene->mAnimations[i]);
2015-05-19 03:57:13 +00:00
}
}
// Read all textures
if (scene->mNumTextures) {
2020-03-14 10:16:44 +00:00
scene->mTextures = new aiTexture *[scene->mNumTextures];
memset(scene->mTextures, 0, scene->mNumTextures * sizeof(aiTexture *));
for (unsigned int i = 0; i < scene->mNumTextures; ++i) {
2015-05-19 03:57:13 +00:00
scene->mTextures[i] = new aiTexture();
2020-03-14 10:16:44 +00:00
ReadBinaryTexture(stream, scene->mTextures[i]);
2015-05-19 03:57:13 +00:00
}
}
// Read lights
if (scene->mNumLights) {
2020-03-14 10:16:44 +00:00
scene->mLights = new aiLight *[scene->mNumLights];
memset(scene->mLights, 0, scene->mNumLights * sizeof(aiLight *));
for (unsigned int i = 0; i < scene->mNumLights; ++i) {
2015-05-19 03:57:13 +00:00
scene->mLights[i] = new aiLight();
2020-03-14 10:16:44 +00:00
ReadBinaryLight(stream, scene->mLights[i]);
2015-05-19 03:57:13 +00:00
}
}
// Read cameras
if (scene->mNumCameras) {
2020-03-14 10:16:44 +00:00
scene->mCameras = new aiCamera *[scene->mNumCameras];
memset(scene->mCameras, 0, scene->mNumCameras * sizeof(aiCamera *));
for (unsigned int i = 0; i < scene->mNumCameras; ++i) {
2015-05-19 03:57:13 +00:00
scene->mCameras[i] = new aiCamera();
2020-03-14 10:16:44 +00:00
ReadBinaryCamera(stream, scene->mCameras[i]);
2015-05-19 03:57:13 +00:00
}
}
2014-08-06 11:26:26 +00:00
}
// -----------------------------------------------------------------------------------
2020-03-14 10:16:44 +00:00
void AssbinImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
IOStream *stream = pIOHandler->Open(pFile, "rb");
if (nullptr == stream) {
throw DeadlyImportError("ASSBIN: Could not open ", pFile);
}
2014-08-06 11:26:26 +00:00
// signature
2020-03-14 10:16:44 +00:00
stream->Seek(44, aiOrigin_CUR);
2014-08-06 11:26:26 +00:00
unsigned int versionMajor = Read<unsigned int>(stream);
unsigned int versionMinor = Read<unsigned int>(stream);
if (versionMinor != ASSBIN_VERSION_MINOR || versionMajor != ASSBIN_VERSION_MAJOR) {
2020-03-14 10:16:44 +00:00
throw DeadlyImportError("Invalid version, data format not compatible!");
}
2020-03-14 10:16:44 +00:00
/*unsigned int versionRevision =*/Read<unsigned int>(stream);
/*unsigned int compileFlags =*/Read<unsigned int>(stream);
2014-08-06 11:26:26 +00:00
2015-05-19 03:57:13 +00:00
shortened = Read<uint16_t>(stream) > 0;
compressed = Read<uint16_t>(stream) > 0;
2014-08-06 11:26:26 +00:00
2015-05-19 03:57:13 +00:00
if (shortened)
2020-03-14 10:16:44 +00:00
throw DeadlyImportError("Shortened binaries are not supported!");
2020-03-14 10:16:44 +00:00
stream->Seek(256, aiOrigin_CUR); // original filename
stream->Seek(128, aiOrigin_CUR); // options
stream->Seek(64, aiOrigin_CUR); // padding
2014-08-06 13:25:39 +00:00
if (compressed) {
2015-05-19 03:57:13 +00:00
uLongf uncompressedSize = Read<uint32_t>(stream);
uLongf compressedSize = static_cast<uLongf>(stream->FileSize() - stream->Tell());
2020-03-14 10:16:44 +00:00
unsigned char *compressedData = new unsigned char[compressedSize];
size_t len = stream->Read(compressedData, 1, compressedSize);
ai_assert(len == compressedSize);
2020-03-14 10:16:44 +00:00
unsigned char *uncompressedData = new unsigned char[uncompressedSize];
2020-03-14 10:16:44 +00:00
int res = uncompress(uncompressedData, &uncompressedSize, compressedData, (uLong)len);
if (res != Z_OK) {
delete[] uncompressedData;
delete[] compressedData;
pIOHandler->Close(stream);
throw DeadlyImportError("Zlib decompression failed.");
}
2020-03-14 10:16:44 +00:00
MemoryIOStream io(uncompressedData, uncompressedSize);
2020-03-14 10:16:44 +00:00
ReadBinaryScene(&io, pScene);
2015-05-19 03:57:13 +00:00
delete[] uncompressedData;
delete[] compressedData;
} else {
2020-03-14 10:16:44 +00:00
ReadBinaryScene(stream, pScene);
2015-05-19 03:57:13 +00:00
}
2015-05-19 03:52:10 +00:00
2015-05-19 03:57:13 +00:00
pIOHandler->Close(stream);
2014-08-05 22:39:54 +00:00
}
#endif // !! ASSIMP_BUILD_NO_ASSBIN_IMPORTER