2020-01-28 14:55:05 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
|
|
|
Copyright (c) 2006-2020, assimp team
|
|
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
|
|
|
following 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.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
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
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
/** @file AssbinFileWriter.cpp
|
|
|
|
* @brief Implementation of Assbin file writer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "AssbinFileWriter.h"
|
|
|
|
|
|
|
|
#include "Common/assbin_chunks.h"
|
|
|
|
#include "PostProcessing/ProcessHelper.h"
|
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
#include <assimp/Exceptional.h>
|
2020-01-28 14:55:05 +00:00
|
|
|
#include <assimp/version.h>
|
|
|
|
#include <assimp/Exporter.hpp>
|
2020-03-15 09:17:54 +00:00
|
|
|
#include <assimp/IOStream.hpp>
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
#ifdef ASSIMP_BUILD_NO_OWN_ZLIB
|
2020-05-02 13:14:38 +00:00
|
|
|
#include <zlib.h>
|
2020-01-28 14:55:05 +00:00
|
|
|
#else
|
2020-05-02 13:14:38 +00:00
|
|
|
#include "../contrib/zlib/zlib.h"
|
2020-01-28 14:55:05 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
2020-07-15 02:19:07 +00:00
|
|
|
#if _MSC_VER
|
2020-05-02 13:14:38 +00:00
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4706)
|
2020-07-15 02:19:07 +00:00
|
|
|
#endif // _MSC_VER
|
2020-03-08 20:24:01 +00:00
|
|
|
|
2020-01-28 14:55:05 +00:00
|
|
|
namespace Assimp {
|
|
|
|
|
|
|
|
template <typename T>
|
2020-03-15 09:17:54 +00:00
|
|
|
size_t Write(IOStream *stream, const T &v) {
|
|
|
|
return stream->Write(&v, sizeof(T), 1);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
|
|
|
// Serialize an aiString
|
|
|
|
template <>
|
2020-03-15 09:17:54 +00:00
|
|
|
inline size_t Write<aiString>(IOStream *stream, const aiString &s) {
|
2020-01-28 14:55:05 +00:00
|
|
|
const size_t s2 = (uint32_t)s.length;
|
2020-03-15 09:17:54 +00:00
|
|
|
stream->Write(&s, 4, 1);
|
|
|
|
stream->Write(s.data, s2, 1);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
return s2 + 4;
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
|
|
|
// Serialize an unsigned int as uint32_t
|
|
|
|
template <>
|
2020-03-15 09:17:54 +00:00
|
|
|
inline size_t Write<unsigned int>(IOStream *stream, const unsigned int &w) {
|
2020-01-28 14:55:05 +00:00
|
|
|
const uint32_t t = (uint32_t)w;
|
|
|
|
if (w > t) {
|
|
|
|
// this shouldn't happen, integers in Assimp data structures never exceed 2^32
|
|
|
|
throw DeadlyExportError("loss of data due to 64 -> 32 bit integer conversion");
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
stream->Write(&t, 4, 1);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
|
|
|
// Serialize an unsigned int as uint16_t
|
|
|
|
template <>
|
2020-03-15 09:17:54 +00:00
|
|
|
inline size_t Write<uint16_t>(IOStream *stream, const uint16_t &w) {
|
|
|
|
static_assert(sizeof(uint16_t) == 2, "sizeof(uint16_t)==2");
|
|
|
|
stream->Write(&w, 2, 1);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
|
|
|
// Serialize a float
|
|
|
|
template <>
|
2020-03-15 09:17:54 +00:00
|
|
|
inline size_t Write<float>(IOStream *stream, const float &f) {
|
|
|
|
static_assert(sizeof(float) == 4, "sizeof(float)==4");
|
|
|
|
stream->Write(&f, 4, 1);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
|
|
|
// Serialize a double
|
|
|
|
template <>
|
2020-03-15 09:17:54 +00:00
|
|
|
inline size_t Write<double>(IOStream *stream, const double &f) {
|
|
|
|
static_assert(sizeof(double) == 8, "sizeof(double)==8");
|
|
|
|
stream->Write(&f, 8, 1);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
|
|
|
// Serialize a vec3
|
|
|
|
template <>
|
2020-03-15 09:17:54 +00:00
|
|
|
inline size_t Write<aiVector3D>(IOStream *stream, const aiVector3D &v) {
|
|
|
|
size_t t = Write<float>(stream, v.x);
|
|
|
|
t += Write<float>(stream, v.y);
|
|
|
|
t += Write<float>(stream, v.z);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
|
|
|
// Serialize a color value
|
|
|
|
template <>
|
2020-03-15 09:17:54 +00:00
|
|
|
inline size_t Write<aiColor3D>(IOStream *stream, const aiColor3D &v) {
|
|
|
|
size_t t = Write<float>(stream, v.r);
|
|
|
|
t += Write<float>(stream, v.g);
|
|
|
|
t += Write<float>(stream, v.b);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
|
|
|
// Serialize a color value
|
|
|
|
template <>
|
2020-03-15 09:17:54 +00:00
|
|
|
inline size_t Write<aiColor4D>(IOStream *stream, const aiColor4D &v) {
|
|
|
|
size_t t = Write<float>(stream, v.r);
|
|
|
|
t += Write<float>(stream, v.g);
|
|
|
|
t += Write<float>(stream, v.b);
|
|
|
|
t += Write<float>(stream, v.a);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
|
|
|
// Serialize a quaternion
|
|
|
|
template <>
|
2020-03-15 09:17:54 +00:00
|
|
|
inline size_t Write<aiQuaternion>(IOStream *stream, const aiQuaternion &v) {
|
|
|
|
size_t t = Write<float>(stream, v.w);
|
|
|
|
t += Write<float>(stream, v.x);
|
|
|
|
t += Write<float>(stream, v.y);
|
|
|
|
t += Write<float>(stream, v.z);
|
2020-01-28 14:55:05 +00:00
|
|
|
ai_assert(t == 16);
|
|
|
|
|
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
|
|
|
// Serialize a vertex weight
|
|
|
|
template <>
|
2020-03-15 09:17:54 +00:00
|
|
|
inline size_t Write<aiVertexWeight>(IOStream *stream, const aiVertexWeight &v) {
|
|
|
|
size_t t = Write<unsigned int>(stream, v.mVertexId);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
return t + Write<float>(stream, v.mWeight);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
|
|
|
// Serialize a mat4x4
|
|
|
|
template <>
|
2020-03-15 09:17:54 +00:00
|
|
|
inline size_t Write<aiMatrix4x4>(IOStream *stream, const aiMatrix4x4 &m) {
|
|
|
|
for (unsigned int i = 0; i < 4; ++i) {
|
|
|
|
for (unsigned int i2 = 0; i2 < 4; ++i2) {
|
|
|
|
Write<float>(stream, m[i][i2]);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
|
|
|
// Serialize an aiVectorKey
|
|
|
|
template <>
|
2020-03-15 09:17:54 +00:00
|
|
|
inline size_t Write<aiVectorKey>(IOStream *stream, const aiVectorKey &v) {
|
|
|
|
const size_t t = Write<double>(stream, v.mTime);
|
|
|
|
return t + Write<aiVector3D>(stream, v.mValue);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
|
|
|
// Serialize an aiQuatKey
|
|
|
|
template <>
|
2020-03-15 09:17:54 +00:00
|
|
|
inline size_t Write<aiQuatKey>(IOStream *stream, const aiQuatKey &v) {
|
|
|
|
const size_t t = Write<double>(stream, v.mTime);
|
|
|
|
return t + Write<aiQuaternion>(stream, v.mValue);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-03-15 09:17:54 +00:00
|
|
|
inline size_t WriteBounds(IOStream *stream, const T *in, unsigned int size) {
|
2020-01-28 14:55:05 +00:00
|
|
|
T minc, maxc;
|
2020-03-15 09:17:54 +00:00
|
|
|
ArrayBounds(in, size, minc, maxc);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
const size_t t = Write<T>(stream, minc);
|
|
|
|
return t + Write<T>(stream, maxc);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We use this to write out non-byte arrays so that we write using the specializations.
|
|
|
|
// This way we avoid writing out extra bytes that potentially come from struct alignment.
|
|
|
|
template <typename T>
|
2020-03-15 09:17:54 +00:00
|
|
|
inline size_t WriteArray(IOStream *stream, const T *in, unsigned int size) {
|
2020-01-28 14:55:05 +00:00
|
|
|
size_t n = 0;
|
2020-03-15 09:17:54 +00:00
|
|
|
for (unsigned int i = 0; i < size; i++)
|
|
|
|
n += Write<T>(stream, in[i]);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------
|
|
|
|
/** @class AssbinChunkWriter
|
|
|
|
* @brief Chunk writer mechanism for the .assbin file structure
|
|
|
|
*
|
|
|
|
* This is a standard in-memory IOStream (most of the code is based on BlobIOStream),
|
|
|
|
* the difference being that this takes another IOStream as a "container" in the
|
|
|
|
* constructor, and when it is destroyed, it appends the magic number, the chunk size,
|
|
|
|
* and the chunk contents to the container stream. This allows relatively easy chunk
|
|
|
|
* chunk construction, even recursively.
|
|
|
|
*/
|
2020-03-15 09:17:54 +00:00
|
|
|
class AssbinChunkWriter : public IOStream {
|
2020-01-28 14:55:05 +00:00
|
|
|
private:
|
2020-03-15 09:17:54 +00:00
|
|
|
uint8_t *buffer;
|
2020-01-28 14:55:05 +00:00
|
|
|
uint32_t magic;
|
2020-03-15 09:17:54 +00:00
|
|
|
IOStream *container;
|
2020-01-28 14:55:05 +00:00
|
|
|
size_t cur_size, cursor, initial;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// -------------------------------------------------------------------
|
2020-03-15 09:17:54 +00:00
|
|
|
void Grow(size_t need = 0) {
|
|
|
|
size_t new_size = std::max(initial, std::max(need, cur_size + (cur_size >> 1)));
|
2020-01-28 14:55:05 +00:00
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
const uint8_t *const old = buffer;
|
2020-01-28 14:55:05 +00:00
|
|
|
buffer = new uint8_t[new_size];
|
|
|
|
|
|
|
|
if (old) {
|
2020-03-15 09:17:54 +00:00
|
|
|
memcpy(buffer, old, cur_size);
|
2020-01-28 14:55:05 +00:00
|
|
|
delete[] old;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur_size = new_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2020-03-15 09:17:54 +00:00
|
|
|
AssbinChunkWriter(IOStream *container, uint32_t magic, size_t initial = 4096) :
|
2020-05-02 13:14:38 +00:00
|
|
|
buffer(nullptr),
|
2020-03-15 09:17:54 +00:00
|
|
|
magic(magic),
|
|
|
|
container(container),
|
|
|
|
cur_size(0),
|
|
|
|
cursor(0),
|
|
|
|
initial(initial) {
|
|
|
|
// empty
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
virtual ~AssbinChunkWriter() {
|
2020-01-28 14:55:05 +00:00
|
|
|
if (container) {
|
2020-03-15 09:17:54 +00:00
|
|
|
container->Write(&magic, sizeof(uint32_t), 1);
|
|
|
|
container->Write(&cursor, sizeof(uint32_t), 1);
|
|
|
|
container->Write(buffer, 1, cursor);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
if (buffer) delete[] buffer;
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
void *GetBufferPointer() { return buffer; }
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2020-03-15 09:17:54 +00:00
|
|
|
virtual size_t Read(void * /*pvBuffer*/, size_t /*pSize*/, size_t /*pCount*/) {
|
2020-01-28 14:55:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
virtual aiReturn Seek(size_t /*pOffset*/, aiOrigin /*pOrigin*/) {
|
|
|
|
return aiReturn_FAILURE;
|
|
|
|
}
|
|
|
|
virtual size_t Tell() const {
|
|
|
|
return cursor;
|
|
|
|
}
|
|
|
|
virtual void Flush() {
|
|
|
|
// not implemented
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t FileSize() const {
|
|
|
|
return cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2020-03-15 09:17:54 +00:00
|
|
|
virtual size_t Write(const void *pvBuffer, size_t pSize, size_t pCount) {
|
2020-01-28 14:55:05 +00:00
|
|
|
pSize *= pCount;
|
|
|
|
if (cursor + pSize > cur_size) {
|
|
|
|
Grow(cursor + pSize);
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
memcpy(buffer + cursor, pvBuffer, pSize);
|
2020-01-28 14:55:05 +00:00
|
|
|
cursor += pSize;
|
|
|
|
|
|
|
|
return pCount;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------
|
|
|
|
/** @class AssbinFileWriter
|
|
|
|
* @brief Assbin file writer class
|
|
|
|
*
|
|
|
|
* This class writes an .assbin file, and is responsible for the file layout.
|
|
|
|
*/
|
2020-03-15 09:17:54 +00:00
|
|
|
class AssbinFileWriter {
|
2020-01-28 14:55:05 +00:00
|
|
|
private:
|
|
|
|
bool shortened;
|
|
|
|
bool compressed;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// -----------------------------------------------------------------------------------
|
2020-03-15 09:17:54 +00:00
|
|
|
void WriteBinaryNode(IOStream *container, const aiNode *node) {
|
|
|
|
AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AINODE);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
unsigned int nb_metadata = (node->mMetaData != nullptr ? node->mMetaData->mNumProperties : 0);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
Write<aiString>(&chunk, node->mName);
|
|
|
|
Write<aiMatrix4x4>(&chunk, node->mTransformation);
|
|
|
|
Write<unsigned int>(&chunk, node->mNumChildren);
|
|
|
|
Write<unsigned int>(&chunk, node->mNumMeshes);
|
|
|
|
Write<unsigned int>(&chunk, nb_metadata);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
for (unsigned int i = 0; i < node->mNumMeshes; ++i) {
|
|
|
|
Write<unsigned int>(&chunk, node->mMeshes[i]);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
for (unsigned int i = 0; i < node->mNumChildren; ++i) {
|
|
|
|
WriteBinaryNode(&chunk, node->mChildren[i]);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < nb_metadata; ++i) {
|
2020-03-15 09:17:54 +00:00
|
|
|
const aiString &key = node->mMetaData->mKeys[i];
|
2020-01-28 14:55:05 +00:00
|
|
|
aiMetadataType type = node->mMetaData->mValues[i].mType;
|
2020-03-15 09:17:54 +00:00
|
|
|
void *value = node->mMetaData->mValues[i].mData;
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
Write<aiString>(&chunk, key);
|
2020-03-15 09:17:54 +00:00
|
|
|
Write<uint16_t>(&chunk, (uint16_t)type);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
switch (type) {
|
2020-05-02 13:14:38 +00:00
|
|
|
case AI_BOOL:
|
|
|
|
Write<bool>(&chunk, *((bool *)value));
|
|
|
|
break;
|
|
|
|
case AI_INT32:
|
|
|
|
Write<int32_t>(&chunk, *((int32_t *)value));
|
|
|
|
break;
|
|
|
|
case AI_UINT64:
|
|
|
|
Write<uint64_t>(&chunk, *((uint64_t *)value));
|
|
|
|
break;
|
|
|
|
case AI_FLOAT:
|
|
|
|
Write<float>(&chunk, *((float *)value));
|
|
|
|
break;
|
|
|
|
case AI_DOUBLE:
|
|
|
|
Write<double>(&chunk, *((double *)value));
|
|
|
|
break;
|
|
|
|
case AI_AISTRING:
|
|
|
|
Write<aiString>(&chunk, *((aiString *)value));
|
|
|
|
break;
|
|
|
|
case AI_AIVECTOR3D:
|
|
|
|
Write<aiVector3D>(&chunk, *((aiVector3D *)value));
|
|
|
|
break;
|
2020-01-28 14:55:05 +00:00
|
|
|
#ifdef SWIG
|
|
|
|
case FORCE_32BIT:
|
|
|
|
#endif // SWIG
|
2020-05-02 13:14:38 +00:00
|
|
|
default:
|
|
|
|
break;
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
2020-03-15 09:17:54 +00:00
|
|
|
void WriteBinaryTexture(IOStream *container, const aiTexture *tex) {
|
|
|
|
AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AITEXTURE);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
Write<unsigned int>(&chunk, tex->mWidth);
|
|
|
|
Write<unsigned int>(&chunk, tex->mHeight);
|
2020-01-28 14:55:05 +00:00
|
|
|
// Write the texture format, but don't include the null terminator.
|
2020-03-15 09:17:54 +00:00
|
|
|
chunk.Write(tex->achFormatHint, sizeof(char), HINTMAXTEXTURELEN - 1);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
if (!shortened) {
|
2020-01-28 14:55:05 +00:00
|
|
|
if (!tex->mHeight) {
|
2020-03-15 09:17:54 +00:00
|
|
|
chunk.Write(tex->pcData, 1, tex->mWidth);
|
|
|
|
} else {
|
|
|
|
chunk.Write(tex->pcData, 1, tex->mWidth * tex->mHeight * 4);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
2020-03-15 09:17:54 +00:00
|
|
|
void WriteBinaryBone(IOStream *container, const aiBone *b) {
|
|
|
|
AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AIBONE);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
Write<aiString>(&chunk, b->mName);
|
|
|
|
Write<unsigned int>(&chunk, b->mNumWeights);
|
|
|
|
Write<aiMatrix4x4>(&chunk, b->mOffsetMatrix);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
// 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-15 09:17:54 +00:00
|
|
|
WriteBounds(&chunk, b->mWeights, b->mNumWeights);
|
2020-01-28 14:55:05 +00:00
|
|
|
} // else write as usual
|
2020-03-15 09:17:54 +00:00
|
|
|
else
|
|
|
|
WriteArray<aiVertexWeight>(&chunk, b->mWeights, b->mNumWeights);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
2020-03-15 09:17:54 +00:00
|
|
|
void WriteBinaryMesh(IOStream *container, const aiMesh *mesh) {
|
|
|
|
AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AIMESH);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
Write<unsigned int>(&chunk, mesh->mPrimitiveTypes);
|
|
|
|
Write<unsigned int>(&chunk, mesh->mNumVertices);
|
|
|
|
Write<unsigned int>(&chunk, mesh->mNumFaces);
|
|
|
|
Write<unsigned int>(&chunk, mesh->mNumBones);
|
|
|
|
Write<unsigned int>(&chunk, mesh->mMaterialIndex);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
// first of all, write bits for all existent vertex components
|
|
|
|
unsigned int c = 0;
|
|
|
|
if (mesh->mVertices) {
|
|
|
|
c |= ASSBIN_MESH_HAS_POSITIONS;
|
|
|
|
}
|
|
|
|
if (mesh->mNormals) {
|
|
|
|
c |= ASSBIN_MESH_HAS_NORMALS;
|
|
|
|
}
|
|
|
|
if (mesh->mTangents && mesh->mBitangents) {
|
|
|
|
c |= ASSBIN_MESH_HAS_TANGENTS_AND_BITANGENTS;
|
|
|
|
}
|
2020-03-15 09:17:54 +00:00
|
|
|
for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++n) {
|
2020-01-28 14:55:05 +00:00
|
|
|
if (!mesh->mTextureCoords[n]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
c |= ASSBIN_MESH_HAS_TEXCOORD(n);
|
|
|
|
}
|
2020-03-15 09:17:54 +00:00
|
|
|
for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_COLOR_SETS; ++n) {
|
2020-01-28 14:55:05 +00:00
|
|
|
if (!mesh->mColors[n]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
c |= ASSBIN_MESH_HAS_COLOR(n);
|
|
|
|
}
|
2020-03-15 09:17:54 +00:00
|
|
|
Write<unsigned int>(&chunk, c);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
aiVector3D minVec, maxVec;
|
|
|
|
if (mesh->mVertices) {
|
|
|
|
if (shortened) {
|
2020-03-15 09:17:54 +00:00
|
|
|
WriteBounds(&chunk, mesh->mVertices, mesh->mNumVertices);
|
2020-01-28 14:55:05 +00:00
|
|
|
} // else write as usual
|
2020-03-15 09:17:54 +00:00
|
|
|
else
|
|
|
|
WriteArray<aiVector3D>(&chunk, mesh->mVertices, mesh->mNumVertices);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
if (mesh->mNormals) {
|
|
|
|
if (shortened) {
|
2020-03-15 09:17:54 +00:00
|
|
|
WriteBounds(&chunk, mesh->mNormals, mesh->mNumVertices);
|
2020-01-28 14:55:05 +00:00
|
|
|
} // else write as usual
|
2020-03-15 09:17:54 +00:00
|
|
|
else
|
|
|
|
WriteArray<aiVector3D>(&chunk, mesh->mNormals, mesh->mNumVertices);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
if (mesh->mTangents && mesh->mBitangents) {
|
|
|
|
if (shortened) {
|
2020-03-15 09:17:54 +00:00
|
|
|
WriteBounds(&chunk, mesh->mTangents, mesh->mNumVertices);
|
|
|
|
WriteBounds(&chunk, mesh->mBitangents, mesh->mNumVertices);
|
2020-01-28 14:55:05 +00:00
|
|
|
} // else write as usual
|
|
|
|
else {
|
2020-03-15 09:17:54 +00:00
|
|
|
WriteArray<aiVector3D>(&chunk, mesh->mTangents, mesh->mNumVertices);
|
|
|
|
WriteArray<aiVector3D>(&chunk, mesh->mBitangents, mesh->mNumVertices);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-15 09:17:54 +00:00
|
|
|
for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_COLOR_SETS; ++n) {
|
2020-01-28 14:55:05 +00:00
|
|
|
if (!mesh->mColors[n])
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (shortened) {
|
2020-03-15 09:17:54 +00:00
|
|
|
WriteBounds(&chunk, mesh->mColors[n], mesh->mNumVertices);
|
2020-01-28 14:55:05 +00:00
|
|
|
} // else write as usual
|
2020-03-15 09:17:54 +00:00
|
|
|
else
|
|
|
|
WriteArray<aiColor4D>(&chunk, mesh->mColors[n], mesh->mNumVertices);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
2020-03-15 09:17:54 +00:00
|
|
|
for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++n) {
|
2020-01-28 14:55:05 +00:00
|
|
|
if (!mesh->mTextureCoords[n])
|
|
|
|
break;
|
|
|
|
|
|
|
|
// write number of UV components
|
2020-03-15 09:17:54 +00:00
|
|
|
Write<unsigned int>(&chunk, mesh->mNumUVComponents[n]);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
if (shortened) {
|
2020-03-15 09:17:54 +00:00
|
|
|
WriteBounds(&chunk, mesh->mTextureCoords[n], mesh->mNumVertices);
|
2020-01-28 14:55:05 +00:00
|
|
|
} // else write as usual
|
2020-03-15 09:17:54 +00:00
|
|
|
else
|
|
|
|
WriteArray<aiVector3D>(&chunk, mesh->mTextureCoords[n], mesh->mNumVertices);
|
2020-01-28 14:55:05 +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) {
|
|
|
|
unsigned int processed = 0;
|
2020-03-15 09:17:54 +00:00
|
|
|
for (unsigned int job; (job = std::min(mesh->mNumFaces - processed, 512u)); processed += job) {
|
2020-01-28 14:55:05 +00:00
|
|
|
uint32_t hash = 0;
|
2020-03-15 09:17:54 +00:00
|
|
|
for (unsigned int a = 0; a < job; ++a) {
|
2020-01-28 14:55:05 +00:00
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
const aiFace &f = mesh->mFaces[processed + a];
|
2020-01-28 14:55:05 +00:00
|
|
|
uint32_t tmp = f.mNumIndices;
|
2020-03-15 09:17:54 +00:00
|
|
|
hash = SuperFastHash(reinterpret_cast<const char *>(&tmp), sizeof tmp, hash);
|
2020-01-28 14:55:05 +00:00
|
|
|
for (unsigned int i = 0; i < f.mNumIndices; ++i) {
|
|
|
|
static_assert(AI_MAX_VERTICES <= 0xffffffff, "AI_MAX_VERTICES <= 0xffffffff");
|
2020-03-15 09:17:54 +00:00
|
|
|
tmp = static_cast<uint32_t>(f.mIndices[i]);
|
|
|
|
hash = SuperFastHash(reinterpret_cast<const char *>(&tmp), sizeof tmp, hash);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-15 09:17:54 +00:00
|
|
|
Write<unsigned int>(&chunk, hash);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
2020-03-15 09:17:54 +00:00
|
|
|
} else // else write as usual
|
2020-01-28 14:55:05 +00:00
|
|
|
{
|
|
|
|
// if there are less than 2^16 vertices, we can simply use 16 bit integers ...
|
2020-03-15 09:17:54 +00:00
|
|
|
for (unsigned int i = 0; i < mesh->mNumFaces; ++i) {
|
|
|
|
const aiFace &f = mesh->mFaces[i];
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
static_assert(AI_MAX_FACE_INDICES <= 0xffff, "AI_MAX_FACE_INDICES <= 0xffff");
|
2020-03-15 09:17:54 +00:00
|
|
|
Write<uint16_t>(&chunk, static_cast<uint16_t>(f.mNumIndices));
|
|
|
|
|
|
|
|
for (unsigned int a = 0; a < f.mNumIndices; ++a) {
|
|
|
|
if (mesh->mNumVertices < (1u << 16)) {
|
|
|
|
Write<uint16_t>(&chunk, static_cast<uint16_t>(f.mIndices[a]));
|
|
|
|
} else {
|
|
|
|
Write<unsigned int>(&chunk, f.mIndices[a]);
|
|
|
|
}
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// write bones
|
|
|
|
if (mesh->mNumBones) {
|
2020-03-15 09:17:54 +00:00
|
|
|
for (unsigned int a = 0; a < mesh->mNumBones; ++a) {
|
|
|
|
const aiBone *b = mesh->mBones[a];
|
|
|
|
WriteBinaryBone(&chunk, b);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
2020-03-15 09:17:54 +00:00
|
|
|
void WriteBinaryMaterialProperty(IOStream *container, const aiMaterialProperty *prop) {
|
|
|
|
AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AIMATERIALPROPERTY);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
Write<aiString>(&chunk, prop->mKey);
|
|
|
|
Write<unsigned int>(&chunk, prop->mSemantic);
|
|
|
|
Write<unsigned int>(&chunk, prop->mIndex);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
Write<unsigned int>(&chunk, prop->mDataLength);
|
|
|
|
Write<unsigned int>(&chunk, (unsigned int)prop->mType);
|
|
|
|
chunk.Write(prop->mData, 1, prop->mDataLength);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
2020-03-15 09:17:54 +00:00
|
|
|
void WriteBinaryMaterial(IOStream *container, const aiMaterial *mat) {
|
|
|
|
AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AIMATERIAL);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
Write<unsigned int>(&chunk, mat->mNumProperties);
|
|
|
|
for (unsigned int i = 0; i < mat->mNumProperties; ++i) {
|
|
|
|
WriteBinaryMaterialProperty(&chunk, mat->mProperties[i]);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
2020-03-15 09:17:54 +00:00
|
|
|
void WriteBinaryNodeAnim(IOStream *container, const aiNodeAnim *nd) {
|
|
|
|
AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AINODEANIM);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
Write<aiString>(&chunk, nd->mNodeName);
|
|
|
|
Write<unsigned int>(&chunk, nd->mNumPositionKeys);
|
|
|
|
Write<unsigned int>(&chunk, nd->mNumRotationKeys);
|
|
|
|
Write<unsigned int>(&chunk, nd->mNumScalingKeys);
|
|
|
|
Write<unsigned int>(&chunk, nd->mPreState);
|
|
|
|
Write<unsigned int>(&chunk, nd->mPostState);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
if (nd->mPositionKeys) {
|
|
|
|
if (shortened) {
|
2020-03-15 09:17:54 +00:00
|
|
|
WriteBounds(&chunk, nd->mPositionKeys, nd->mNumPositionKeys);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
} // else write as usual
|
2020-03-15 09:17:54 +00:00
|
|
|
else
|
|
|
|
WriteArray<aiVectorKey>(&chunk, nd->mPositionKeys, nd->mNumPositionKeys);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
if (nd->mRotationKeys) {
|
|
|
|
if (shortened) {
|
2020-03-15 09:17:54 +00:00
|
|
|
WriteBounds(&chunk, nd->mRotationKeys, nd->mNumRotationKeys);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
} // else write as usual
|
2020-03-15 09:17:54 +00:00
|
|
|
else
|
|
|
|
WriteArray<aiQuatKey>(&chunk, nd->mRotationKeys, nd->mNumRotationKeys);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
if (nd->mScalingKeys) {
|
|
|
|
if (shortened) {
|
2020-03-15 09:17:54 +00:00
|
|
|
WriteBounds(&chunk, nd->mScalingKeys, nd->mNumScalingKeys);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
} // else write as usual
|
2020-03-15 09:17:54 +00:00
|
|
|
else
|
|
|
|
WriteArray<aiVectorKey>(&chunk, nd->mScalingKeys, nd->mNumScalingKeys);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
2020-03-15 09:17:54 +00:00
|
|
|
void WriteBinaryAnim(IOStream *container, const aiAnimation *anim) {
|
|
|
|
AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AIANIMATION);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
Write<aiString>(&chunk, anim->mName);
|
|
|
|
Write<double>(&chunk, anim->mDuration);
|
|
|
|
Write<double>(&chunk, anim->mTicksPerSecond);
|
|
|
|
Write<unsigned int>(&chunk, anim->mNumChannels);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
for (unsigned int a = 0; a < anim->mNumChannels; ++a) {
|
|
|
|
const aiNodeAnim *nd = anim->mChannels[a];
|
|
|
|
WriteBinaryNodeAnim(&chunk, nd);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
2020-03-15 09:17:54 +00:00
|
|
|
void WriteBinaryLight(IOStream *container, const aiLight *l) {
|
|
|
|
AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AILIGHT);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
Write<aiString>(&chunk, l->mName);
|
|
|
|
Write<unsigned int>(&chunk, l->mType);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
if (l->mType != aiLightSource_DIRECTIONAL) {
|
2020-03-15 09:17:54 +00:00
|
|
|
Write<float>(&chunk, l->mAttenuationConstant);
|
|
|
|
Write<float>(&chunk, l->mAttenuationLinear);
|
|
|
|
Write<float>(&chunk, l->mAttenuationQuadratic);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
Write<aiColor3D>(&chunk, l->mColorDiffuse);
|
|
|
|
Write<aiColor3D>(&chunk, l->mColorSpecular);
|
|
|
|
Write<aiColor3D>(&chunk, l->mColorAmbient);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
if (l->mType == aiLightSource_SPOT) {
|
2020-03-15 09:17:54 +00:00
|
|
|
Write<float>(&chunk, l->mAngleInnerCone);
|
|
|
|
Write<float>(&chunk, l->mAngleOuterCone);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
2020-03-15 09:17:54 +00:00
|
|
|
void WriteBinaryCamera(IOStream *container, const aiCamera *cam) {
|
|
|
|
AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AICAMERA);
|
|
|
|
|
|
|
|
Write<aiString>(&chunk, cam->mName);
|
|
|
|
Write<aiVector3D>(&chunk, cam->mPosition);
|
|
|
|
Write<aiVector3D>(&chunk, cam->mLookAt);
|
|
|
|
Write<aiVector3D>(&chunk, cam->mUp);
|
|
|
|
Write<float>(&chunk, cam->mHorizontalFOV);
|
|
|
|
Write<float>(&chunk, cam->mClipPlaneNear);
|
|
|
|
Write<float>(&chunk, cam->mClipPlaneFar);
|
|
|
|
Write<float>(&chunk, cam->mAspect);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
2020-03-15 09:17:54 +00:00
|
|
|
void WriteBinaryScene(IOStream *container, const aiScene *scene) {
|
|
|
|
AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AISCENE);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
// basic scene information
|
2020-03-15 09:17:54 +00:00
|
|
|
Write<unsigned int>(&chunk, scene->mFlags);
|
|
|
|
Write<unsigned int>(&chunk, scene->mNumMeshes);
|
|
|
|
Write<unsigned int>(&chunk, scene->mNumMaterials);
|
|
|
|
Write<unsigned int>(&chunk, scene->mNumAnimations);
|
|
|
|
Write<unsigned int>(&chunk, scene->mNumTextures);
|
|
|
|
Write<unsigned int>(&chunk, scene->mNumLights);
|
|
|
|
Write<unsigned int>(&chunk, scene->mNumCameras);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
// write node graph
|
2020-03-15 09:17:54 +00:00
|
|
|
WriteBinaryNode(&chunk, scene->mRootNode);
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
// write all meshes
|
2020-03-15 09:17:54 +00:00
|
|
|
for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
|
|
|
|
const aiMesh *mesh = scene->mMeshes[i];
|
|
|
|
WriteBinaryMesh(&chunk, mesh);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// write materials
|
2020-03-15 09:17:54 +00:00
|
|
|
for (unsigned int i = 0; i < scene->mNumMaterials; ++i) {
|
|
|
|
const aiMaterial *mat = scene->mMaterials[i];
|
|
|
|
WriteBinaryMaterial(&chunk, mat);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// write all animations
|
2020-03-15 09:17:54 +00:00
|
|
|
for (unsigned int i = 0; i < scene->mNumAnimations; ++i) {
|
|
|
|
const aiAnimation *anim = scene->mAnimations[i];
|
|
|
|
WriteBinaryAnim(&chunk, anim);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// write all textures
|
2020-03-15 09:17:54 +00:00
|
|
|
for (unsigned int i = 0; i < scene->mNumTextures; ++i) {
|
|
|
|
const aiTexture *mesh = scene->mTextures[i];
|
|
|
|
WriteBinaryTexture(&chunk, mesh);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// write lights
|
2020-03-15 09:17:54 +00:00
|
|
|
for (unsigned int i = 0; i < scene->mNumLights; ++i) {
|
|
|
|
const aiLight *l = scene->mLights[i];
|
|
|
|
WriteBinaryLight(&chunk, l);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// write cameras
|
2020-03-15 09:17:54 +00:00
|
|
|
for (unsigned int i = 0; i < scene->mNumCameras; ++i) {
|
|
|
|
const aiCamera *cam = scene->mCameras[i];
|
|
|
|
WriteBinaryCamera(&chunk, cam);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2020-03-15 09:17:54 +00:00
|
|
|
AssbinFileWriter(bool shortened, bool compressed) :
|
|
|
|
shortened(shortened), compressed(compressed) {
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------------
|
|
|
|
// Write a binary model dump
|
2020-03-15 09:17:54 +00:00
|
|
|
void WriteBinaryDump(const char *pFile, const char *cmd, IOSystem *pIOSystem, const aiScene *pScene) {
|
|
|
|
IOStream *out = pIOSystem->Open(pFile, "wb");
|
2020-01-28 14:55:05 +00:00
|
|
|
if (!out)
|
|
|
|
throw std::runtime_error("Unable to open output file " + std::string(pFile) + '\n');
|
|
|
|
|
|
|
|
auto CloseIOStream = [&]() {
|
|
|
|
if (out) {
|
|
|
|
pIOSystem->Close(out);
|
|
|
|
out = nullptr; // Ensure this is only done once.
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2020-06-23 19:05:42 +00:00
|
|
|
time_t tt = time(nullptr);
|
2020-01-28 14:55:05 +00:00
|
|
|
#if _WIN32
|
2020-03-15 09:17:54 +00:00
|
|
|
tm *p = gmtime(&tt);
|
2020-01-28 14:55:05 +00:00
|
|
|
#else
|
|
|
|
struct tm now;
|
2020-03-15 09:17:54 +00:00
|
|
|
tm *p = gmtime_r(&tt, &now);
|
2020-01-28 14:55:05 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// header
|
|
|
|
char s[64];
|
|
|
|
memset(s, 0, 64);
|
|
|
|
#if _MSC_VER >= 1400
|
|
|
|
sprintf_s(s, "ASSIMP.binary-dump.%s", asctime(p));
|
|
|
|
#else
|
|
|
|
ai_snprintf(s, 64, "ASSIMP.binary-dump.%s", asctime(p));
|
|
|
|
#endif
|
|
|
|
out->Write(s, 44, 1);
|
|
|
|
// == 44 bytes
|
|
|
|
|
|
|
|
Write<unsigned int>(out, ASSBIN_VERSION_MAJOR);
|
|
|
|
Write<unsigned int>(out, ASSBIN_VERSION_MINOR);
|
|
|
|
Write<unsigned int>(out, aiGetVersionRevision());
|
|
|
|
Write<unsigned int>(out, aiGetCompileFlags());
|
|
|
|
Write<uint16_t>(out, shortened);
|
|
|
|
Write<uint16_t>(out, compressed);
|
|
|
|
// == 20 bytes
|
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
char buff[256] = { 0 };
|
2020-01-28 19:02:09 +00:00
|
|
|
ai_snprintf(buff, 256, "%s", pFile);
|
2020-01-28 14:55:05 +00:00
|
|
|
out->Write(buff, sizeof(char), 256);
|
|
|
|
|
2020-01-28 19:02:09 +00:00
|
|
|
memset(buff, 0, sizeof(buff));
|
|
|
|
ai_snprintf(buff, 128, "%s", cmd);
|
2020-01-28 14:55:05 +00:00
|
|
|
out->Write(buff, sizeof(char), 128);
|
2020-03-15 09:17:54 +00:00
|
|
|
|
2020-01-28 14:55:05 +00:00
|
|
|
// leave 64 bytes free for future extensions
|
|
|
|
memset(buff, 0xcd, 64);
|
|
|
|
out->Write(buff, sizeof(char), 64);
|
|
|
|
// == 435 bytes
|
|
|
|
|
|
|
|
// ==== total header size: 512 bytes
|
|
|
|
ai_assert(out->Tell() == ASSBIN_HEADER_LENGTH);
|
|
|
|
|
|
|
|
// Up to here the data is uncompressed. For compressed files, the rest
|
|
|
|
// is compressed using standard DEFLATE from zlib.
|
2020-03-15 09:17:54 +00:00
|
|
|
if (compressed) {
|
2020-06-23 19:05:42 +00:00
|
|
|
AssbinChunkWriter uncompressedStream(nullptr, 0);
|
2020-01-28 14:55:05 +00:00
|
|
|
WriteBinaryScene(&uncompressedStream, pScene);
|
|
|
|
|
|
|
|
uLongf uncompressedSize = static_cast<uLongf>(uncompressedStream.Tell());
|
|
|
|
uLongf compressedSize = (uLongf)compressBound(uncompressedSize);
|
2020-03-15 09:17:54 +00:00
|
|
|
uint8_t *compressedBuffer = new uint8_t[compressedSize];
|
2020-01-28 14:55:05 +00:00
|
|
|
|
2020-03-15 09:17:54 +00:00
|
|
|
int res = compress2(compressedBuffer, &compressedSize, (const Bytef *)uncompressedStream.GetBufferPointer(), uncompressedSize, 9);
|
|
|
|
if (res != Z_OK) {
|
2020-01-28 14:55:05 +00:00
|
|
|
delete[] compressedBuffer;
|
|
|
|
throw DeadlyExportError("Compression failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
out->Write(&uncompressedSize, sizeof(uint32_t), 1);
|
|
|
|
out->Write(compressedBuffer, sizeof(char), compressedSize);
|
|
|
|
|
|
|
|
delete[] compressedBuffer;
|
2020-03-15 09:17:54 +00:00
|
|
|
} else {
|
2020-01-28 14:55:05 +00:00
|
|
|
WriteBinaryScene(out, pScene);
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseIOStream();
|
2020-03-15 09:17:54 +00:00
|
|
|
} catch (...) {
|
2020-01-28 14:55:05 +00:00
|
|
|
CloseIOStream();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void DumpSceneToAssbin(
|
2020-03-15 09:17:54 +00:00
|
|
|
const char *pFile, const char *cmd, IOSystem *pIOSystem,
|
|
|
|
const aiScene *pScene, bool shortened, bool compressed) {
|
2020-01-28 14:55:05 +00:00
|
|
|
AssbinFileWriter fileWriter(shortened, compressed);
|
2020-01-28 17:30:09 +00:00
|
|
|
fileWriter.WriteBinaryDump(pFile, cmd, pIOSystem, pScene);
|
2020-01-28 14:55:05 +00:00
|
|
|
}
|
2020-07-15 02:19:07 +00:00
|
|
|
#if _MSC_VER
|
2020-05-02 13:14:38 +00:00
|
|
|
#pragma warning(pop)
|
2020-07-15 02:19:07 +00:00
|
|
|
#endif // _MSC_VER
|
2020-01-28 14:55:05 +00:00
|
|
|
|
|
|
|
} // end of namespace Assimp
|