2014-05-18 08:57:44 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2021-02-28 11:17:54 +00:00
|
|
|
Copyright (c) 2006-2021, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2017-05-09 17:57:36 +00:00
|
|
|
|
2014-05-18 08:57:44 +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
|
2014-05-18 08:57:44 +00:00
|
|
|
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.
|
|
|
|
|
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-05-18 08:57:44 +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-05-18 08:57:44 +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-05-18 08:57:44 +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-05-18 08:57:44 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "OgreBinarySerializer.h"
|
2014-05-21 01:00:11 +00:00
|
|
|
#include "OgreParsingUtils.h"
|
2020-03-08 20:24:01 +00:00
|
|
|
#include "OgreXmlSerializer.h"
|
2014-05-21 01:00:11 +00:00
|
|
|
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/TinyFormatter.h>
|
2016-06-06 20:04:29 +00:00
|
|
|
#include <assimp/DefaultLogger.hpp>
|
2015-04-15 23:00:17 +00:00
|
|
|
|
2014-05-18 08:57:44 +00:00
|
|
|
#ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
|
|
|
|
|
2014-05-18 09:30:16 +00:00
|
|
|
// Define as 1 to get verbose logging.
|
|
|
|
#define OGRE_BINARY_SERIALIZER_DEBUG 0
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
namespace Assimp {
|
|
|
|
namespace Ogre {
|
2014-05-18 08:57:44 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
const std::string MESH_VERSION_1_8 = "[MeshSerializer_v1.8]";
|
|
|
|
const std::string SKELETON_VERSION_1_8 = "[Serializer_v1.80]";
|
|
|
|
const std::string SKELETON_VERSION_1_1 = "[Serializer_v1.10]";
|
2014-05-21 01:00:11 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
const unsigned short HEADER_CHUNK_ID = 0x1000;
|
2014-05-21 01:00:11 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
const long MSTREAM_OVERHEAD_SIZE = sizeof(uint16_t) + sizeof(uint32_t);
|
|
|
|
const long MSTREAM_BONE_SIZE_WITHOUT_SCALE = MSTREAM_OVERHEAD_SIZE + sizeof(unsigned short) + (sizeof(float) * 7);
|
|
|
|
const long MSTREAM_KEYFRAME_SIZE_WITHOUT_SCALE = MSTREAM_OVERHEAD_SIZE + (sizeof(float) * 8);
|
2014-05-18 08:57:44 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <>
|
|
|
|
inline bool OgreBinarySerializer::Read<bool>() {
|
2015-05-19 03:57:13 +00:00
|
|
|
return (m_reader->GetU1() > 0);
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <>
|
|
|
|
inline char OgreBinarySerializer::Read<char>() {
|
2015-05-19 03:57:13 +00:00
|
|
|
return static_cast<char>(m_reader->GetU1());
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <>
|
|
|
|
inline uint8_t OgreBinarySerializer::Read<uint8_t>() {
|
2015-05-19 03:57:13 +00:00
|
|
|
return m_reader->GetU1();
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <>
|
|
|
|
inline uint16_t OgreBinarySerializer::Read<uint16_t>() {
|
2015-05-19 03:57:13 +00:00
|
|
|
return m_reader->GetU2();
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <>
|
|
|
|
inline uint32_t OgreBinarySerializer::Read<uint32_t>() {
|
2015-05-19 03:57:13 +00:00
|
|
|
return m_reader->GetU4();
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <>
|
|
|
|
inline float OgreBinarySerializer::Read<float>() {
|
2015-05-19 03:57:13 +00:00
|
|
|
return m_reader->GetF4();
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadBytes(char *dest, size_t numBytes) {
|
|
|
|
ReadBytes(static_cast<void *>(dest), numBytes);
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadBytes(uint8_t *dest, size_t numBytes) {
|
|
|
|
ReadBytes(static_cast<void *>(dest), numBytes);
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadBytes(void *dest, size_t numBytes) {
|
2015-05-19 03:57:13 +00:00
|
|
|
m_reader->CopyAndAdvance(dest, numBytes);
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
uint8_t *OgreBinarySerializer::ReadBytes(size_t numBytes) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint8_t *bytes = new uint8_t[numBytes];
|
|
|
|
ReadBytes(bytes, numBytes);
|
|
|
|
return bytes;
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadVector(aiVector3D &vec) {
|
|
|
|
m_reader->CopyAndAdvance(&vec.x, sizeof(float) * 3);
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadQuaternion(aiQuaternion &quat) {
|
2015-05-19 03:57:13 +00:00
|
|
|
float temp[4];
|
2020-03-08 20:24:01 +00:00
|
|
|
m_reader->CopyAndAdvance(temp, sizeof(float) * 4);
|
2015-05-19 03:57:13 +00:00
|
|
|
quat.x = temp[0];
|
|
|
|
quat.y = temp[1];
|
|
|
|
quat.z = temp[2];
|
|
|
|
quat.w = temp[3];
|
2014-05-21 01:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
bool OgreBinarySerializer::AtEnd() const {
|
2015-05-19 03:57:13 +00:00
|
|
|
return (m_reader->GetRemainingSize() == 0);
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
std::string OgreBinarySerializer::ReadString(size_t len) {
|
2015-05-19 03:57:13 +00:00
|
|
|
std::string str;
|
|
|
|
str.resize(len);
|
|
|
|
ReadBytes(&str[0], len);
|
|
|
|
return str;
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
std::string OgreBinarySerializer::ReadLine() {
|
2015-05-19 03:57:13 +00:00
|
|
|
std::string str;
|
2020-03-08 20:24:01 +00:00
|
|
|
while (!AtEnd()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
char c = Read<char>();
|
|
|
|
if (c == '\n')
|
|
|
|
break;
|
|
|
|
str += c;
|
|
|
|
}
|
|
|
|
return str;
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
uint16_t OgreBinarySerializer::ReadHeader(bool readLen) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t id = Read<uint16_t>();
|
|
|
|
if (readLen)
|
|
|
|
m_currentLen = Read<uint32_t>();
|
2014-05-18 09:30:16 +00:00
|
|
|
|
|
|
|
#if (OGRE_BINARY_SERIALIZER_DEBUG == 1)
|
2020-03-08 20:24:01 +00:00
|
|
|
if (id != HEADER_CHUNK_ID) {
|
2021-05-13 11:05:31 +00:00
|
|
|
ASSIMP_LOG_DEBUG((assetMode == AM_Mesh ? MeshHeaderToString(static_cast<MeshChunkId>(id)) : SkeletonHeaderToString(static_cast<SkeletonChunkId>(id))));
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2014-05-18 09:30:16 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
return id;
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::RollbackHeader() {
|
2015-05-19 03:57:13 +00:00
|
|
|
m_reader->IncPtr(-MSTREAM_OVERHEAD_SIZE);
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::SkipBytes(size_t numBytes) {
|
2014-05-18 09:30:16 +00:00
|
|
|
#if (OGRE_BINARY_SERIALIZER_DEBUG == 1)
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG("Skipping ", numBytes, " bytes");
|
2014-05-18 09:30:16 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
m_reader->IncPtr(numBytes);
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2014-05-21 01:00:11 +00:00
|
|
|
// Mesh
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
Mesh *OgreBinarySerializer::ImportMesh(MemoryStreamReader *stream) {
|
2015-05-19 03:57:13 +00:00
|
|
|
OgreBinarySerializer serializer(stream, OgreBinarySerializer::AM_Mesh);
|
|
|
|
|
|
|
|
uint16_t id = serializer.ReadHeader(false);
|
|
|
|
if (id != HEADER_CHUNK_ID) {
|
2021-04-19 17:33:41 +00:00
|
|
|
throw DeadlyImportError("Invalid Ogre Mesh file header.");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @todo Check what we can actually support.
|
|
|
|
std::string version = serializer.ReadLine();
|
2020-03-08 20:24:01 +00:00
|
|
|
if (version != MESH_VERSION_1_8) {
|
2021-04-19 17:33:41 +00:00
|
|
|
throw DeadlyImportError("Mesh version ", version, " not supported by this importer. Run OgreMeshUpgrader tool on the file and try again.",
|
2020-08-18 15:32:34 +00:00
|
|
|
" Supported versions: ", MESH_VERSION_1_8);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Mesh *mesh = new Mesh();
|
2020-03-08 20:24:01 +00:00
|
|
|
while (!serializer.AtEnd()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
id = serializer.ReadHeader();
|
2020-03-08 20:24:01 +00:00
|
|
|
switch (id) {
|
|
|
|
case M_MESH: {
|
2015-05-19 03:57:13 +00:00
|
|
|
serializer.ReadMesh(mesh);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mesh;
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadMesh(Mesh *mesh) {
|
2015-05-19 03:57:13 +00:00
|
|
|
mesh->hasSkeletalAnimations = Read<bool>();
|
|
|
|
|
2020-05-15 15:59:42 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG("Reading Mesh");
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG(" - Skeletal animations: ", mesh->hasSkeletalAnimations ? "true" : "false");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!AtEnd()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t id = ReadHeader();
|
|
|
|
while (!AtEnd() &&
|
2020-03-08 20:24:01 +00:00
|
|
|
(id == M_GEOMETRY ||
|
|
|
|
id == M_SUBMESH ||
|
|
|
|
id == M_MESH_SKELETON_LINK ||
|
|
|
|
id == M_MESH_BONE_ASSIGNMENT ||
|
|
|
|
id == M_MESH_LOD ||
|
|
|
|
id == M_MESH_BOUNDS ||
|
|
|
|
id == M_SUBMESH_NAME_TABLE ||
|
|
|
|
id == M_EDGE_LISTS ||
|
|
|
|
id == M_POSES ||
|
|
|
|
id == M_ANIMATIONS ||
|
|
|
|
id == M_TABLE_EXTREMES)) {
|
|
|
|
switch (id) {
|
|
|
|
case M_GEOMETRY: {
|
2015-05-19 03:57:13 +00:00
|
|
|
mesh->sharedVertexData = new VertexData();
|
|
|
|
ReadGeometry(mesh->sharedVertexData);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
case M_SUBMESH: {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadSubMesh(mesh);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
case M_MESH_SKELETON_LINK: {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadMeshSkeletonLink(mesh);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
case M_MESH_BONE_ASSIGNMENT: {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadBoneAssignment(mesh->sharedVertexData);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
case M_MESH_LOD: {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadMeshLodInfo(mesh);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
case M_MESH_BOUNDS: {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadMeshBounds(mesh);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
case M_SUBMESH_NAME_TABLE: {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadSubMeshNames(mesh);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
case M_EDGE_LISTS: {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadEdgeList(mesh);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
case M_POSES: {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadPoses(mesh);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
case M_ANIMATIONS: {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadAnimations(mesh);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
case M_TABLE_EXTREMES: {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadMeshExtremes(mesh);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AtEnd())
|
|
|
|
id = ReadHeader();
|
|
|
|
}
|
|
|
|
if (!AtEnd())
|
|
|
|
RollbackHeader();
|
|
|
|
}
|
|
|
|
|
|
|
|
NormalizeBoneWeights(mesh->sharedVertexData);
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadMeshLodInfo(Mesh *mesh) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Assimp does not acknowledge LOD levels as far as I can see it. This info is just skipped.
|
|
|
|
// @todo Put this stuff to scene/mesh custom properties. If manual mesh the app can use the information.
|
|
|
|
ReadLine(); // strategy name
|
|
|
|
uint16_t numLods = Read<uint16_t>();
|
|
|
|
bool manual = Read<bool>();
|
|
|
|
|
|
|
|
/// @note Main mesh is considered as LOD 0, start from index 1.
|
2020-03-08 20:24:01 +00:00
|
|
|
for (size_t i = 1; i < numLods; ++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t id = ReadHeader();
|
|
|
|
if (id != M_MESH_LOD_USAGE) {
|
|
|
|
throw DeadlyImportError("M_MESH_LOD does not contain a M_MESH_LOD_USAGE for each LOD level");
|
|
|
|
}
|
|
|
|
|
|
|
|
m_reader->IncPtr(sizeof(float)); // user value
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (manual) {
|
2015-05-19 03:57:13 +00:00
|
|
|
id = ReadHeader();
|
|
|
|
if (id != M_MESH_LOD_MANUAL) {
|
|
|
|
throw DeadlyImportError("Manual M_MESH_LOD_USAGE does not contain M_MESH_LOD_MANUAL");
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadLine(); // manual mesh name (ref to another mesh)
|
2020-03-08 20:24:01 +00:00
|
|
|
} else {
|
|
|
|
for (size_t si = 0, silen = mesh->NumSubMeshes(); si < silen; ++si) {
|
2015-05-19 03:57:13 +00:00
|
|
|
id = ReadHeader();
|
|
|
|
if (id != M_MESH_LOD_GENERATED) {
|
|
|
|
throw DeadlyImportError("Generated M_MESH_LOD_USAGE does not contain M_MESH_LOD_GENERATED");
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t indexCount = Read<uint32_t>();
|
|
|
|
bool is32bit = Read<bool>();
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (indexCount > 0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint32_t len = indexCount * (is32bit ? sizeof(uint32_t) : sizeof(uint16_t));
|
|
|
|
m_reader->IncPtr(len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadMeshSkeletonLink(Mesh *mesh) {
|
2015-05-19 03:57:13 +00:00
|
|
|
mesh->skeletonRef = ReadLine();
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadMeshBounds(Mesh * /*mesh*/) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Skip bounds, not compatible with Assimp.
|
|
|
|
// 2x float vec3 + 1x float sphere radius
|
|
|
|
SkipBytes(sizeof(float) * 7);
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadMeshExtremes(Mesh * /*mesh*/) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Skip extremes, not compatible with Assimp.
|
|
|
|
size_t numBytes = m_currentLen - MSTREAM_OVERHEAD_SIZE;
|
|
|
|
SkipBytes(numBytes);
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadBoneAssignment(VertexData *dest) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!dest) {
|
|
|
|
throw DeadlyImportError("Cannot read bone assignments, vertex data is null.");
|
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
VertexBoneAssignment ba;
|
|
|
|
ba.vertexIndex = Read<uint32_t>();
|
|
|
|
ba.boneIndex = Read<uint16_t>();
|
|
|
|
ba.weight = Read<float>();
|
2014-05-18 08:57:44 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
dest->boneAssignments.push_back(ba);
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadSubMesh(Mesh *mesh) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t id = 0;
|
|
|
|
|
|
|
|
SubMesh *submesh = new SubMesh();
|
|
|
|
submesh->materialRef = ReadLine();
|
|
|
|
submesh->usesSharedVertexData = Read<bool>();
|
|
|
|
|
|
|
|
submesh->indexData->count = Read<uint32_t>();
|
|
|
|
submesh->indexData->faceCount = static_cast<uint32_t>(submesh->indexData->count / 3);
|
|
|
|
submesh->indexData->is32bit = Read<bool>();
|
|
|
|
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG("Reading SubMesh ", mesh->subMeshes.size());
|
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG(" - Material: '", submesh->materialRef, "'");
|
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG(" - Uses shared geometry: ", submesh->usesSharedVertexData ? "true" : "false");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Index buffer
|
2020-03-08 20:24:01 +00:00
|
|
|
if (submesh->indexData->count > 0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint32_t numBytes = submesh->indexData->count * (submesh->indexData->is32bit ? sizeof(uint32_t) : sizeof(uint16_t));
|
|
|
|
uint8_t *indexBuffer = ReadBytes(numBytes);
|
|
|
|
submesh->indexData->buffer = MemoryStreamPtr(new Assimp::MemoryIOStream(indexBuffer, numBytes, true));
|
|
|
|
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG(" - ", submesh->indexData->faceCount,
|
2020-03-08 20:24:01 +00:00
|
|
|
" faces from ", submesh->indexData->count, (submesh->indexData->is32bit ? " 32bit" : " 16bit"),
|
|
|
|
" indexes of ", numBytes, " bytes");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Vertex buffer if not referencing the shared geometry
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!submesh->usesSharedVertexData) {
|
2015-05-19 03:57:13 +00:00
|
|
|
id = ReadHeader();
|
|
|
|
if (id != M_GEOMETRY) {
|
|
|
|
throw DeadlyImportError("M_SUBMESH does not contain M_GEOMETRY, but shader geometry is set to false");
|
|
|
|
}
|
|
|
|
|
|
|
|
submesh->vertexData = new VertexData();
|
|
|
|
ReadGeometry(submesh->vertexData);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bone assignment, submesh operation and texture aliases
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!AtEnd()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
id = ReadHeader();
|
|
|
|
while (!AtEnd() &&
|
2020-03-08 20:24:01 +00:00
|
|
|
(id == M_SUBMESH_OPERATION ||
|
|
|
|
id == M_SUBMESH_BONE_ASSIGNMENT ||
|
|
|
|
id == M_SUBMESH_TEXTURE_ALIAS)) {
|
|
|
|
switch (id) {
|
|
|
|
case M_SUBMESH_OPERATION: {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadSubMeshOperation(submesh);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
case M_SUBMESH_BONE_ASSIGNMENT: {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadBoneAssignment(submesh->vertexData);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
case M_SUBMESH_TEXTURE_ALIAS: {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadSubMeshTextureAlias(submesh);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AtEnd())
|
|
|
|
id = ReadHeader();
|
|
|
|
}
|
|
|
|
if (!AtEnd())
|
|
|
|
RollbackHeader();
|
|
|
|
}
|
|
|
|
|
|
|
|
NormalizeBoneWeights(submesh->vertexData);
|
|
|
|
|
2016-11-20 01:49:33 +00:00
|
|
|
submesh->index = static_cast<unsigned int>(mesh->subMeshes.size());
|
2015-05-19 03:57:13 +00:00
|
|
|
mesh->subMeshes.push_back(submesh);
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::NormalizeBoneWeights(VertexData *vertexData) const {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!vertexData || vertexData->boneAssignments.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::set<uint32_t> influencedVertices;
|
2020-03-08 20:24:01 +00:00
|
|
|
for (VertexBoneAssignmentList::const_iterator baIter = vertexData->boneAssignments.begin(), baEnd = vertexData->boneAssignments.end(); baIter != baEnd; ++baIter) {
|
2015-05-19 03:57:13 +00:00
|
|
|
influencedVertices.insert(baIter->vertexIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Normalize bone weights.
|
2016-12-08 02:31:51 +00:00
|
|
|
Some exporters won't care if the sum of all bone weights
|
2015-05-19 03:57:13 +00:00
|
|
|
for a single vertex equals 1 or not, so validate here. */
|
|
|
|
const float epsilon = 0.05f;
|
2020-03-08 20:24:01 +00:00
|
|
|
for (const uint32_t vertexIndex : influencedVertices) {
|
2015-05-19 03:57:13 +00:00
|
|
|
float sum = 0.0f;
|
2020-03-08 20:24:01 +00:00
|
|
|
for (VertexBoneAssignmentList::const_iterator baIter = vertexData->boneAssignments.begin(), baEnd = vertexData->boneAssignments.end(); baIter != baEnd; ++baIter) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (baIter->vertexIndex == vertexIndex)
|
|
|
|
sum += baIter->weight;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if ((sum < (1.0f - epsilon)) || (sum > (1.0f + epsilon))) {
|
|
|
|
for (auto &boneAssign : vertexData->boneAssignments) {
|
2016-05-21 22:44:37 +00:00
|
|
|
if (boneAssign.vertexIndex == vertexIndex)
|
|
|
|
boneAssign.weight /= sum;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-20 01:52:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadSubMeshOperation(SubMesh *submesh) {
|
2015-05-19 03:57:13 +00:00
|
|
|
submesh->operationType = static_cast<SubMesh::OperationType>(Read<uint16_t>());
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadSubMeshTextureAlias(SubMesh *submesh) {
|
2015-05-19 03:57:13 +00:00
|
|
|
submesh->textureAliasName = ReadLine();
|
|
|
|
submesh->textureAliasRef = ReadLine();
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadSubMeshNames(Mesh *mesh) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t id = 0;
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!AtEnd()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
id = ReadHeader();
|
2020-03-08 20:24:01 +00:00
|
|
|
while (!AtEnd() && id == M_SUBMESH_NAME_TABLE_ELEMENT) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t submeshIndex = Read<uint16_t>();
|
|
|
|
SubMesh *submesh = mesh->GetSubMesh(submeshIndex);
|
|
|
|
if (!submesh) {
|
2020-08-18 15:32:34 +00:00
|
|
|
throw DeadlyImportError("Ogre Mesh does not include submesh ", submeshIndex, " referenced in M_SUBMESH_NAME_TABLE_ELEMENT. Invalid mesh file.");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
submesh->name = ReadLine();
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG(" - SubMesh ", submesh->index, " name '", submesh->name, "'");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
if (!AtEnd())
|
|
|
|
id = ReadHeader();
|
|
|
|
}
|
|
|
|
if (!AtEnd())
|
|
|
|
RollbackHeader();
|
|
|
|
}
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadGeometry(VertexData *dest) {
|
2015-05-19 03:57:13 +00:00
|
|
|
dest->count = Read<uint32_t>();
|
|
|
|
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG(" - Reading geometry of ", dest->count, " vertices");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!AtEnd()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t id = ReadHeader();
|
|
|
|
while (!AtEnd() &&
|
2020-03-08 20:24:01 +00:00
|
|
|
(id == M_GEOMETRY_VERTEX_DECLARATION ||
|
|
|
|
id == M_GEOMETRY_VERTEX_BUFFER)) {
|
|
|
|
switch (id) {
|
|
|
|
case M_GEOMETRY_VERTEX_DECLARATION: {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadGeometryVertexDeclaration(dest);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
case M_GEOMETRY_VERTEX_BUFFER: {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadGeometryVertexBuffer(dest);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AtEnd())
|
|
|
|
id = ReadHeader();
|
|
|
|
}
|
|
|
|
if (!AtEnd())
|
|
|
|
RollbackHeader();
|
|
|
|
}
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadGeometryVertexDeclaration(VertexData *dest) {
|
|
|
|
if (!AtEnd()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t id = ReadHeader();
|
2020-03-08 20:24:01 +00:00
|
|
|
while (!AtEnd() && id == M_GEOMETRY_VERTEX_ELEMENT) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadGeometryVertexElement(dest);
|
|
|
|
|
|
|
|
if (!AtEnd())
|
|
|
|
id = ReadHeader();
|
|
|
|
}
|
|
|
|
if (!AtEnd())
|
|
|
|
RollbackHeader();
|
|
|
|
}
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadGeometryVertexElement(VertexData *dest) {
|
2015-05-19 03:57:13 +00:00
|
|
|
VertexElement element;
|
|
|
|
element.source = Read<uint16_t>();
|
|
|
|
element.type = static_cast<VertexElement::Type>(Read<uint16_t>());
|
|
|
|
element.semantic = static_cast<VertexElement::Semantic>(Read<uint16_t>());
|
|
|
|
element.offset = Read<uint16_t>();
|
|
|
|
element.index = Read<uint16_t>();
|
2014-05-18 08:57:44 +00:00
|
|
|
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG(" - Vertex element ", element.SemanticToString(), " of type ",
|
2020-03-08 20:24:01 +00:00
|
|
|
element.TypeToString(), " index=", element.index, " source=", element.source);
|
2014-05-18 08:57:44 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
dest->vertexElements.push_back(element);
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadGeometryVertexBuffer(VertexData *dest) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t bindIndex = Read<uint16_t>();
|
|
|
|
uint16_t vertexSize = Read<uint16_t>();
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t id = ReadHeader();
|
|
|
|
if (id != M_GEOMETRY_VERTEX_BUFFER_DATA)
|
|
|
|
throw DeadlyImportError("M_GEOMETRY_VERTEX_BUFFER_DATA not found in M_GEOMETRY_VERTEX_BUFFER");
|
2014-05-18 08:57:44 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
if (dest->VertexSize(bindIndex) != vertexSize)
|
|
|
|
throw DeadlyImportError("Vertex buffer size does not agree with vertex declaration in M_GEOMETRY_VERTEX_BUFFER");
|
2014-05-18 08:57:44 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
size_t numBytes = dest->count * vertexSize;
|
|
|
|
uint8_t *vertexBuffer = ReadBytes(numBytes);
|
|
|
|
dest->vertexBindings[bindIndex] = MemoryStreamPtr(new Assimp::MemoryIOStream(vertexBuffer, numBytes, true));
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG(" - Read vertex buffer for source ", bindIndex, " of ", numBytes, " bytes");
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadEdgeList(Mesh * /*mesh*/) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Assimp does not acknowledge LOD levels as far as I can see it. This info is just skipped.
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!AtEnd()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t id = ReadHeader();
|
2020-03-08 20:24:01 +00:00
|
|
|
while (!AtEnd() && id == M_EDGE_LIST_LOD) {
|
2015-05-19 03:57:13 +00:00
|
|
|
m_reader->IncPtr(sizeof(uint16_t)); // lod index
|
|
|
|
bool manual = Read<bool>();
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!manual) {
|
2015-05-19 03:57:13 +00:00
|
|
|
m_reader->IncPtr(sizeof(uint8_t));
|
|
|
|
uint32_t numTriangles = Read<uint32_t>();
|
|
|
|
uint32_t numEdgeGroups = Read<uint32_t>();
|
|
|
|
|
|
|
|
size_t skipBytes = (sizeof(uint32_t) * 8 + sizeof(float) * 4) * numTriangles;
|
|
|
|
m_reader->IncPtr(skipBytes);
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
for (size_t i = 0; i < numEdgeGroups; ++i) {
|
2020-03-01 12:15:45 +00:00
|
|
|
uint16_t curId = ReadHeader();
|
|
|
|
if (curId != M_EDGE_GROUP)
|
2015-05-19 03:57:13 +00:00
|
|
|
throw DeadlyImportError("M_EDGE_GROUP not found in M_EDGE_LIST_LOD");
|
|
|
|
|
|
|
|
m_reader->IncPtr(sizeof(uint32_t) * 3);
|
|
|
|
uint32_t numEdges = Read<uint32_t>();
|
2020-03-08 20:24:01 +00:00
|
|
|
for (size_t j = 0; j < numEdges; ++j) {
|
2015-05-19 03:57:13 +00:00
|
|
|
m_reader->IncPtr(sizeof(uint32_t) * 6 + sizeof(uint8_t));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AtEnd())
|
|
|
|
id = ReadHeader();
|
|
|
|
}
|
|
|
|
if (!AtEnd())
|
|
|
|
RollbackHeader();
|
|
|
|
}
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadPoses(Mesh *mesh) {
|
|
|
|
if (!AtEnd()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t id = ReadHeader();
|
2020-03-08 20:24:01 +00:00
|
|
|
while (!AtEnd() && id == M_POSE) {
|
2015-05-19 03:57:13 +00:00
|
|
|
Pose *pose = new Pose();
|
|
|
|
pose->name = ReadLine();
|
|
|
|
pose->target = Read<uint16_t>();
|
|
|
|
pose->hasNormals = Read<bool>();
|
|
|
|
|
|
|
|
ReadPoseVertices(pose);
|
|
|
|
|
|
|
|
mesh->poses.push_back(pose);
|
|
|
|
|
|
|
|
if (!AtEnd())
|
|
|
|
id = ReadHeader();
|
|
|
|
}
|
|
|
|
if (!AtEnd())
|
|
|
|
RollbackHeader();
|
|
|
|
}
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadPoseVertices(Pose *pose) {
|
|
|
|
if (!AtEnd()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t id = ReadHeader();
|
2020-03-08 20:24:01 +00:00
|
|
|
while (!AtEnd() && id == M_POSE_VERTEX) {
|
2015-05-19 03:57:13 +00:00
|
|
|
Pose::Vertex v;
|
|
|
|
v.index = Read<uint32_t>();
|
|
|
|
ReadVector(v.offset);
|
|
|
|
if (pose->hasNormals)
|
|
|
|
ReadVector(v.normal);
|
|
|
|
|
|
|
|
pose->vertices[v.index] = v;
|
|
|
|
|
|
|
|
if (!AtEnd())
|
|
|
|
id = ReadHeader();
|
|
|
|
}
|
|
|
|
if (!AtEnd())
|
|
|
|
RollbackHeader();
|
|
|
|
}
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadAnimations(Mesh *mesh) {
|
|
|
|
if (!AtEnd()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t id = ReadHeader();
|
2020-03-08 20:24:01 +00:00
|
|
|
while (!AtEnd() && id == M_ANIMATION) {
|
2015-05-19 03:57:13 +00:00
|
|
|
Animation *anim = new Animation(mesh);
|
|
|
|
anim->name = ReadLine();
|
|
|
|
anim->length = Read<float>();
|
|
|
|
|
|
|
|
ReadAnimation(anim);
|
|
|
|
|
|
|
|
mesh->animations.push_back(anim);
|
|
|
|
|
|
|
|
if (!AtEnd())
|
|
|
|
id = ReadHeader();
|
|
|
|
}
|
|
|
|
if (!AtEnd())
|
|
|
|
RollbackHeader();
|
|
|
|
}
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadAnimation(Animation *anim) {
|
|
|
|
if (!AtEnd()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t id = ReadHeader();
|
2020-03-08 20:24:01 +00:00
|
|
|
if (id == M_ANIMATION_BASEINFO) {
|
2015-05-19 03:57:13 +00:00
|
|
|
anim->baseName = ReadLine();
|
|
|
|
anim->baseTime = Read<float>();
|
|
|
|
|
|
|
|
// Advance to first track
|
|
|
|
id = ReadHeader();
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
while (!AtEnd() && id == M_ANIMATION_TRACK) {
|
2015-05-19 03:57:13 +00:00
|
|
|
VertexAnimationTrack track;
|
|
|
|
track.type = static_cast<VertexAnimationTrack::Type>(Read<uint16_t>());
|
|
|
|
track.target = Read<uint16_t>();
|
|
|
|
|
|
|
|
ReadAnimationKeyFrames(anim, &track);
|
|
|
|
|
|
|
|
anim->tracks.push_back(track);
|
|
|
|
|
|
|
|
if (!AtEnd())
|
|
|
|
id = ReadHeader();
|
|
|
|
}
|
|
|
|
if (!AtEnd())
|
|
|
|
RollbackHeader();
|
|
|
|
}
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadAnimationKeyFrames(Animation *anim, VertexAnimationTrack *track) {
|
|
|
|
if (!AtEnd()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t id = ReadHeader();
|
|
|
|
while (!AtEnd() &&
|
2020-03-08 20:24:01 +00:00
|
|
|
(id == M_ANIMATION_MORPH_KEYFRAME ||
|
|
|
|
id == M_ANIMATION_POSE_KEYFRAME)) {
|
|
|
|
if (id == M_ANIMATION_MORPH_KEYFRAME) {
|
2015-05-19 03:57:13 +00:00
|
|
|
MorphKeyFrame kf;
|
|
|
|
kf.timePos = Read<float>();
|
|
|
|
bool hasNormals = Read<bool>();
|
|
|
|
|
|
|
|
size_t vertexCount = anim->AssociatedVertexData(track)->count;
|
|
|
|
size_t vertexSize = sizeof(float) * (hasNormals ? 6 : 3);
|
|
|
|
size_t numBytes = vertexCount * vertexSize;
|
|
|
|
|
|
|
|
uint8_t *morphBuffer = ReadBytes(numBytes);
|
|
|
|
kf.buffer = MemoryStreamPtr(new Assimp::MemoryIOStream(morphBuffer, numBytes, true));
|
|
|
|
|
|
|
|
track->morphKeyFrames.push_back(kf);
|
2020-03-08 20:24:01 +00:00
|
|
|
} else if (id == M_ANIMATION_POSE_KEYFRAME) {
|
2015-05-19 03:57:13 +00:00
|
|
|
PoseKeyFrame kf;
|
|
|
|
kf.timePos = Read<float>();
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!AtEnd()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
id = ReadHeader();
|
2020-03-08 20:24:01 +00:00
|
|
|
while (!AtEnd() && id == M_ANIMATION_POSE_REF) {
|
2015-05-19 03:57:13 +00:00
|
|
|
PoseRef pr;
|
|
|
|
pr.index = Read<uint16_t>();
|
|
|
|
pr.influence = Read<float>();
|
|
|
|
kf.references.push_back(pr);
|
|
|
|
|
|
|
|
if (!AtEnd())
|
|
|
|
id = ReadHeader();
|
|
|
|
}
|
|
|
|
if (!AtEnd())
|
|
|
|
RollbackHeader();
|
|
|
|
}
|
|
|
|
|
|
|
|
track->poseKeyFrames.push_back(kf);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AtEnd())
|
|
|
|
id = ReadHeader();
|
|
|
|
}
|
|
|
|
if (!AtEnd())
|
|
|
|
RollbackHeader();
|
|
|
|
}
|
2014-05-18 08:57:44 +00:00
|
|
|
}
|
|
|
|
|
2014-05-21 01:00:11 +00:00
|
|
|
// Skeleton
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
bool OgreBinarySerializer::ImportSkeleton(Assimp::IOSystem *pIOHandler, Mesh *mesh) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!mesh || mesh->skeletonRef.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Highly unusual to see in read world cases but support
|
|
|
|
// binary mesh referencing a XML skeleton file.
|
2020-03-08 20:24:01 +00:00
|
|
|
if (EndsWith(mesh->skeletonRef, ".skeleton.xml", false)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
OgreXmlSerializer::ImportSkeleton(pIOHandler, mesh);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryStreamReaderPtr reader = OpenReader(pIOHandler, mesh->skeletonRef);
|
2019-06-07 07:49:24 +00:00
|
|
|
if (!reader)
|
2020-03-08 20:24:01 +00:00
|
|
|
return false;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
Skeleton *skeleton = new Skeleton();
|
|
|
|
OgreBinarySerializer serializer(reader.get(), OgreBinarySerializer::AM_Skeleton);
|
|
|
|
serializer.ReadSkeleton(skeleton);
|
|
|
|
mesh->skeleton = skeleton;
|
|
|
|
return true;
|
2014-05-21 01:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
bool OgreBinarySerializer::ImportSkeleton(Assimp::IOSystem *pIOHandler, MeshXml *mesh) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!mesh || mesh->skeletonRef.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MemoryStreamReaderPtr reader = OpenReader(pIOHandler, mesh->skeletonRef);
|
|
|
|
if (!reader.get())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Skeleton *skeleton = new Skeleton();
|
|
|
|
OgreBinarySerializer serializer(reader.get(), OgreBinarySerializer::AM_Skeleton);
|
|
|
|
serializer.ReadSkeleton(skeleton);
|
|
|
|
mesh->skeleton = skeleton;
|
|
|
|
return true;
|
2014-05-21 01:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
MemoryStreamReaderPtr OgreBinarySerializer::OpenReader(Assimp::IOSystem *pIOHandler, const std::string &filename) {
|
|
|
|
if (!EndsWith(filename, ".skeleton", false)) {
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_ERROR("Imported Mesh is referencing to unsupported '", filename, "' skeleton file.");
|
2015-05-19 03:57:13 +00:00
|
|
|
return MemoryStreamReaderPtr();
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!pIOHandler->Exists(filename)) {
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_ERROR("Failed to find skeleton file '", filename, "' that is referenced by imported Mesh.");
|
2015-05-19 03:57:13 +00:00
|
|
|
return MemoryStreamReaderPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStream *f = pIOHandler->Open(filename, "rb");
|
|
|
|
if (!f) {
|
2020-08-18 16:35:08 +00:00
|
|
|
throw DeadlyImportError("Failed to open skeleton file ", filename);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return MemoryStreamReaderPtr(new MemoryStreamReader(f));
|
2014-05-21 01:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadSkeleton(Skeleton *skeleton) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t id = ReadHeader(false);
|
|
|
|
if (id != HEADER_CHUNK_ID) {
|
2021-04-19 17:33:41 +00:00
|
|
|
throw DeadlyImportError("Invalid Ogre Skeleton file header.");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This deserialization supports both versions of the skeleton spec
|
|
|
|
std::string version = ReadLine();
|
2020-03-08 20:24:01 +00:00
|
|
|
if (version != SKELETON_VERSION_1_8 && version != SKELETON_VERSION_1_1) {
|
2021-04-19 17:33:41 +00:00
|
|
|
throw DeadlyImportError("Skeleton version ", version, " not supported by this importer.",
|
2020-08-18 15:32:34 +00:00
|
|
|
" Supported versions: ", SKELETON_VERSION_1_8, " and ", SKELETON_VERSION_1_1);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 10:45:00 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG("Reading Skeleton");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
bool firstBone = true;
|
|
|
|
bool firstAnim = true;
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
while (!AtEnd()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
id = ReadHeader();
|
2020-03-08 20:24:01 +00:00
|
|
|
switch (id) {
|
|
|
|
case SKELETON_BLENDMODE: {
|
2015-05-19 03:57:13 +00:00
|
|
|
skeleton->blendMode = static_cast<Skeleton::BlendMode>(Read<uint16_t>());
|
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
case SKELETON_BONE: {
|
|
|
|
if (firstBone) {
|
2020-05-18 10:45:00 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG(" - Bones");
|
2015-05-19 03:57:13 +00:00
|
|
|
firstBone = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadBone(skeleton);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
case SKELETON_BONE_PARENT: {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadBoneParent(skeleton);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
case SKELETON_ANIMATION: {
|
|
|
|
if (firstAnim) {
|
2020-05-18 10:45:00 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG(" - Animations");
|
2015-05-19 03:57:13 +00:00
|
|
|
firstAnim = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadSkeletonAnimation(skeleton);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
case SKELETON_ANIMATION_LINK: {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadSkeletonAnimationLink(skeleton);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate bone matrices for root bones. Recursively calculates their children.
|
2020-03-08 20:24:01 +00:00
|
|
|
for (size_t i = 0, len = skeleton->bones.size(); i < len; ++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
Bone *bone = skeleton->bones[i];
|
|
|
|
if (!bone->IsParented())
|
|
|
|
bone->CalculateWorldMatrixAndDefaultPose(skeleton);
|
|
|
|
}
|
2014-05-21 01:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadBone(Skeleton *skeleton) {
|
2015-05-19 03:57:13 +00:00
|
|
|
Bone *bone = new Bone();
|
|
|
|
bone->name = ReadLine();
|
|
|
|
bone->id = Read<uint16_t>();
|
2014-05-21 01:00:11 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Pos and rot
|
|
|
|
ReadVector(bone->position);
|
|
|
|
ReadQuaternion(bone->rotation);
|
2014-05-21 01:00:11 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Scale (optional)
|
|
|
|
if (m_currentLen > MSTREAM_BONE_SIZE_WITHOUT_SCALE)
|
|
|
|
ReadVector(bone->scale);
|
2014-05-21 01:00:11 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Bone indexes need to start from 0 and be contiguous
|
|
|
|
if (bone->id != skeleton->bones.size()) {
|
2020-08-18 15:32:34 +00:00
|
|
|
throw DeadlyImportError("Ogre Skeleton bone indexes not contiguous. Error at bone index ", bone->id);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2014-05-21 01:00:11 +00:00
|
|
|
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG(" ", bone->id, " ", bone->name);
|
2014-05-21 01:00:11 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
skeleton->bones.push_back(bone);
|
2014-05-21 01:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadBoneParent(Skeleton *skeleton) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t childId = Read<uint16_t>();
|
|
|
|
uint16_t parentId = Read<uint16_t>();
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
Bone *child = skeleton->BoneById(childId);
|
|
|
|
Bone *parent = skeleton->BoneById(parentId);
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
if (child && parent)
|
|
|
|
parent->AddChild(child);
|
|
|
|
else
|
2020-08-18 15:32:34 +00:00
|
|
|
throw DeadlyImportError("Failed to find bones for parenting: Child id ", childId, " for parent id ", parentId);
|
2014-05-21 01:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadSkeletonAnimation(Skeleton *skeleton) {
|
2015-05-19 03:57:13 +00:00
|
|
|
Animation *anim = new Animation(skeleton);
|
|
|
|
anim->name = ReadLine();
|
|
|
|
anim->length = Read<float>();
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!AtEnd()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t id = ReadHeader();
|
2020-03-08 20:24:01 +00:00
|
|
|
if (id == SKELETON_ANIMATION_BASEINFO) {
|
2015-05-19 03:57:13 +00:00
|
|
|
anim->baseName = ReadLine();
|
|
|
|
anim->baseTime = Read<float>();
|
|
|
|
|
|
|
|
// Advance to first track
|
|
|
|
id = ReadHeader();
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
while (!AtEnd() && id == SKELETON_ANIMATION_TRACK) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadSkeletonAnimationTrack(skeleton, anim);
|
|
|
|
|
|
|
|
if (!AtEnd())
|
|
|
|
id = ReadHeader();
|
|
|
|
}
|
|
|
|
if (!AtEnd())
|
|
|
|
RollbackHeader();
|
|
|
|
}
|
|
|
|
|
|
|
|
skeleton->animations.push_back(anim);
|
|
|
|
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG(" ", anim->name, " (", anim->length, " sec, ", anim->tracks.size(), " tracks)");
|
2014-05-21 01:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadSkeletonAnimationTrack(Skeleton * /*skeleton*/, Animation *dest) {
|
2015-05-19 03:57:13 +00:00
|
|
|
uint16_t boneId = Read<uint16_t>();
|
|
|
|
Bone *bone = dest->parentSkeleton->BoneById(boneId);
|
|
|
|
if (!bone) {
|
2020-08-18 15:32:34 +00:00
|
|
|
throw DeadlyImportError("Cannot read animation track, target bone ", boneId, " not in target Skeleton");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VertexAnimationTrack track;
|
|
|
|
track.type = VertexAnimationTrack::VAT_TRANSFORM;
|
|
|
|
track.boneName = bone->name;
|
|
|
|
|
|
|
|
uint16_t id = ReadHeader();
|
2020-03-08 20:24:01 +00:00
|
|
|
while (!AtEnd() && id == SKELETON_ANIMATION_TRACK_KEYFRAME) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ReadSkeletonAnimationKeyFrame(&track);
|
|
|
|
|
|
|
|
if (!AtEnd())
|
|
|
|
id = ReadHeader();
|
|
|
|
}
|
|
|
|
if (!AtEnd())
|
|
|
|
RollbackHeader();
|
|
|
|
|
|
|
|
dest->tracks.push_back(track);
|
2014-05-21 01:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadSkeletonAnimationKeyFrame(VertexAnimationTrack *dest) {
|
2015-05-19 03:57:13 +00:00
|
|
|
TransformKeyFrame keyframe;
|
|
|
|
keyframe.timePos = Read<float>();
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Rot and pos
|
|
|
|
ReadQuaternion(keyframe.rotation);
|
|
|
|
ReadVector(keyframe.position);
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Scale (optional)
|
|
|
|
if (m_currentLen > MSTREAM_KEYFRAME_SIZE_WITHOUT_SCALE)
|
|
|
|
ReadVector(keyframe.scale);
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
dest->transformKeyFrames.push_back(keyframe);
|
2014-05-21 01:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void OgreBinarySerializer::ReadSkeletonAnimationLink(Skeleton * /*skeleton*/) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Skip bounds, not compatible with Assimp.
|
|
|
|
ReadLine(); // skeleton name
|
|
|
|
SkipBytes(sizeof(float) * 3); // scale
|
2014-05-21 01:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
} // namespace Ogre
|
|
|
|
} // namespace Assimp
|
2014-05-18 08:57:44 +00:00
|
|
|
|
|
|
|
#endif // ASSIMP_BUILD_NO_OGRE_IMPORTER
|