assimp/code/AssetLib/X/XFileImporter.cpp

681 lines
29 KiB
C++
Raw Normal View History

/*
---------------------------------------------------------------------------
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
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
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
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
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
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
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------
*/
/** @file XFileImporter.cpp
2015-05-19 03:52:10 +00:00
* @brief Implementation of the XFile importer class
*/
#ifndef ASSIMP_BUILD_NO_X_IMPORTER
#include "AssetLib/X/XFileImporter.h"
#include "AssetLib/X/XFileParser.h"
#include "PostProcessing/ConvertToLHProcess.h"
#include <assimp/TinyFormatter.h>
2016-06-06 20:04:29 +00:00
#include <assimp/IOSystem.hpp>
#include <assimp/scene.h>
#include <assimp/DefaultLogger.hpp>
2017-02-22 16:20:26 +00:00
#include <assimp/importerdesc.h>
#include <cctype>
2016-06-06 20:04:29 +00:00
#include <memory>
2023-11-03 10:47:13 +00:00
namespace Assimp {
using namespace Assimp::Formatter;
static const aiImporterDesc desc = {
2015-05-19 03:57:13 +00:00
"Direct3D XFile Importer",
"",
"",
"",
aiImporterFlags_SupportTextFlavour | aiImporterFlags_SupportBinaryFlavour | aiImporterFlags_SupportCompressedFlavour,
1,
3,
1,
5,
"x"
};
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Returns whether the class can handle the format of the given file.
2023-11-03 10:47:13 +00:00
bool XFileImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const {
static const uint32_t token[] = { AI_MAKE_MAGIC("xof ") };
2023-11-03 10:47:13 +00:00
return CheckMagicToken(pIOHandler, pFile, token, AI_COUNT_OF(token));
}
// ------------------------------------------------------------------------------------------------
// Get file extension list
2023-11-03 10:47:13 +00:00
const aiImporterDesc *XFileImporter::GetInfo() const {
2015-05-19 03:57:13 +00:00
return &desc;
}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Imports the given file into the given scene structure.
2023-11-03 10:47:13 +00:00
void XFileImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
2015-05-19 03:57:13 +00:00
// read file into memory
2023-11-03 10:47:13 +00:00
std::unique_ptr<IOStream> file(pIOHandler->Open(pFile));
if (file == nullptr) {
2023-11-03 10:47:13 +00:00
throw DeadlyImportError("Failed to open file ", pFile, ".");
2018-02-06 22:59:46 +00:00
}
2018-02-06 22:59:46 +00:00
static const size_t MinSize = 16;
2015-05-19 03:57:13 +00:00
size_t fileSize = file->FileSize();
2023-11-03 10:47:13 +00:00
if (fileSize < MinSize) {
throw DeadlyImportError("XFile is too small.");
2018-02-06 22:59:46 +00:00
}
2015-05-19 03:57:13 +00:00
// in the hope that binary files will never start with a BOM ...
2023-11-03 10:47:13 +00:00
mBuffer.resize(fileSize + 1);
file->Read(&mBuffer.front(), 1, fileSize);
2015-05-19 03:57:13 +00:00
ConvertToUTF8(mBuffer);
2015-05-19 03:57:13 +00:00
// parse the file into a temporary representation
2023-11-03 10:47:13 +00:00
XFileParser parser(mBuffer);
2015-05-19 03:57:13 +00:00
// and create the proper return structures out of it
2023-11-03 10:47:13 +00:00
CreateDataRepresentationFromImport(pScene, parser.GetImportedData());
2015-05-19 03:57:13 +00:00
// if nothing came from it, report it as error
2023-11-03 10:47:13 +00:00
if (!pScene->mRootNode) {
throw DeadlyImportError("XFile is ill-formatted - no content imported.");
2018-02-06 22:59:46 +00:00
}
}
// ------------------------------------------------------------------------------------------------
// Constructs the return data structure out of the imported data.
2023-11-03 10:47:13 +00:00
void XFileImporter::CreateDataRepresentationFromImport(aiScene *pScene, XFile::Scene *pData) {
2015-05-19 03:57:13 +00:00
// Read the global materials first so that meshes referring to them can find them later
2023-11-03 10:47:13 +00:00
ConvertMaterials(pScene, pData->mGlobalMaterials);
2015-05-19 03:57:13 +00:00
// copy nodes, extracting meshes and materials on the way
2023-11-03 10:47:13 +00:00
pScene->mRootNode = CreateNodes(pScene, nullptr, pData->mRootNode);
2015-05-19 03:57:13 +00:00
// extract animations
2023-11-03 10:47:13 +00:00
CreateAnimations(pScene, pData);
2015-05-19 03:57:13 +00:00
// read the global meshes that were stored outside of any node
2023-11-03 10:47:13 +00:00
if (!pData->mGlobalMeshes.empty()) {
2015-05-19 03:57:13 +00:00
// create a root node to hold them if there isn't any, yet
2023-11-03 10:47:13 +00:00
if (pScene->mRootNode == nullptr) {
2015-05-19 03:57:13 +00:00
pScene->mRootNode = new aiNode;
2023-11-03 10:47:13 +00:00
pScene->mRootNode->mName.Set("$dummy_node");
2015-05-19 03:57:13 +00:00
}
// convert all global meshes and store them in the root node.
// If there was one before, the global meshes now suddenly have its transformation matrix...
// Don't know what to do there, I don't want to insert another node under the present root node
// just to avoid this.
2023-11-03 10:47:13 +00:00
CreateMeshes(pScene, pScene->mRootNode, pData->mGlobalMeshes);
2015-05-19 03:57:13 +00:00
}
if (!pScene->mRootNode) {
2023-11-03 10:47:13 +00:00
throw DeadlyImportError("No root node");
2015-05-19 03:57:13 +00:00
}
// Convert everything to OpenGL space... it's the same operation as the conversion back, so we can reuse the step directly
MakeLeftHandedProcess convertProcess;
2023-11-03 10:47:13 +00:00
convertProcess.Execute(pScene);
2015-05-19 03:57:13 +00:00
FlipWindingOrderProcess flipper;
flipper.Execute(pScene);
// finally: create a dummy material if not material was imported
2023-11-03 10:47:13 +00:00
if (pScene->mNumMaterials == 0) {
2015-05-19 03:57:13 +00:00
pScene->mNumMaterials = 1;
// create the Material
2023-11-03 10:47:13 +00:00
aiMaterial *mat = new aiMaterial;
int shadeMode = (int)aiShadingMode_Gouraud;
mat->AddProperty<int>(&shadeMode, 1, AI_MATKEY_SHADING_MODEL);
2015-05-19 03:57:13 +00:00
// material colours
int specExp = 1;
2023-11-03 10:47:13 +00:00
aiColor3D clr = aiColor3D(0, 0, 0);
mat->AddProperty(&clr, 1, AI_MATKEY_COLOR_EMISSIVE);
mat->AddProperty(&clr, 1, AI_MATKEY_COLOR_SPECULAR);
2015-05-19 03:57:13 +00:00
2023-11-03 10:47:13 +00:00
clr = aiColor3D(0.5f, 0.5f, 0.5f);
mat->AddProperty(&clr, 1, AI_MATKEY_COLOR_DIFFUSE);
mat->AddProperty(&specExp, 1, AI_MATKEY_SHININESS);
2015-05-19 03:57:13 +00:00
2023-11-03 10:47:13 +00:00
pScene->mMaterials = new aiMaterial *[1];
2015-05-19 03:57:13 +00:00
pScene->mMaterials[0] = mat;
}
}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Recursively creates scene nodes from the imported hierarchy.
2023-11-03 10:47:13 +00:00
aiNode *XFileImporter::CreateNodes(aiScene *pScene, aiNode *pParent, const XFile::Node *pNode) {
if (!pNode) {
2018-02-06 22:59:46 +00:00
return nullptr;
}
2015-05-19 03:57:13 +00:00
// create node
2023-11-03 10:47:13 +00:00
aiNode *node = new aiNode;
node->mName.length = (ai_uint32)pNode->mName.length();
2015-05-19 03:57:13 +00:00
node->mParent = pParent;
2023-11-03 10:47:13 +00:00
memcpy(node->mName.data, pNode->mName.c_str(), pNode->mName.length());
2015-05-19 03:57:13 +00:00
node->mName.data[node->mName.length] = 0;
node->mTransformation = pNode->mTrafoMatrix;
// convert meshes from the source node
2023-11-03 10:47:13 +00:00
CreateMeshes(pScene, node, pNode->mMeshes);
2015-05-19 03:57:13 +00:00
2021-10-05 08:59:43 +00:00
// handle children
2023-11-03 10:47:13 +00:00
if (!pNode->mChildren.empty()) {
2015-05-19 03:57:13 +00:00
node->mNumChildren = (unsigned int)pNode->mChildren.size();
2023-11-03 10:47:13 +00:00
node->mChildren = new aiNode *[node->mNumChildren];
2015-05-19 03:57:13 +00:00
2023-11-03 10:47:13 +00:00
for (unsigned int a = 0; a < pNode->mChildren.size(); ++a) {
node->mChildren[a] = CreateNodes(pScene, node, pNode->mChildren[a]);
2018-02-06 22:59:46 +00:00
}
2015-05-19 03:57:13 +00:00
}
return node;
}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Creates the meshes for the given node.
2023-11-03 10:47:13 +00:00
void XFileImporter::CreateMeshes(aiScene *pScene, aiNode *pNode, const std::vector<XFile::Mesh *> &pMeshes) {
2017-07-07 14:34:08 +00:00
if (pMeshes.empty()) {
2015-05-19 03:57:13 +00:00
return;
2017-07-07 14:34:08 +00:00
}
2015-05-19 03:57:13 +00:00
// create a mesh for each mesh-material combination in the source node
2023-11-03 10:47:13 +00:00
std::vector<aiMesh *> meshes;
for (unsigned int a = 0; a < pMeshes.size(); ++a) {
XFile::Mesh *sourceMesh = pMeshes[a];
if (nullptr == sourceMesh) {
continue;
}
2015-05-19 03:57:13 +00:00
// first convert its materials so that we can find them with their index afterwards
2023-11-03 10:47:13 +00:00
ConvertMaterials(pScene, sourceMesh->mMaterials);
2015-05-19 03:57:13 +00:00
2023-11-03 10:47:13 +00:00
unsigned int numMaterials = std::max((unsigned int)sourceMesh->mMaterials.size(), 1u);
for (unsigned int b = 0; b < numMaterials; ++b) {
2015-05-19 03:57:13 +00:00
// collect the faces belonging to this material
std::vector<unsigned int> faces;
unsigned int numVertices = 0;
2023-11-03 10:47:13 +00:00
if (!sourceMesh->mFaceMaterials.empty()) {
2015-05-19 03:57:13 +00:00
// if there is a per-face material defined, select the faces with the corresponding material
2023-11-03 10:47:13 +00:00
for (unsigned int c = 0; c < sourceMesh->mFaceMaterials.size(); ++c) {
if (sourceMesh->mFaceMaterials[c] == b) {
faces.push_back(c);
2015-05-19 03:57:13 +00:00
numVertices += (unsigned int)sourceMesh->mPosFaces[c].mIndices.size();
}
}
2018-02-06 22:59:46 +00:00
} else {
2015-05-19 03:57:13 +00:00
// if there is no per-face material, place everything into one mesh
2023-11-03 10:47:13 +00:00
for (unsigned int c = 0; c < sourceMesh->mPosFaces.size(); ++c) {
faces.push_back(c);
2015-05-19 03:57:13 +00:00
numVertices += (unsigned int)sourceMesh->mPosFaces[c].mIndices.size();
}
}
// no faces/vertices using this material? strange...
2023-11-03 10:47:13 +00:00
if (numVertices == 0) {
2015-05-19 03:57:13 +00:00
continue;
2018-02-06 22:59:46 +00:00
}
2015-05-19 03:57:13 +00:00
// create a submesh using this material
2023-11-03 10:47:13 +00:00
aiMesh *mesh = new aiMesh;
meshes.push_back(mesh);
2015-05-19 03:57:13 +00:00
// find the material in the scene's material list. Either own material
// or referenced material, it should already have a valid index
2023-11-03 10:47:13 +00:00
if (!sourceMesh->mFaceMaterials.empty()) {
mesh->mMaterialIndex = static_cast<unsigned int>(sourceMesh->mMaterials[b].sceneIndex);
2018-02-06 22:59:46 +00:00
} else {
2015-05-19 03:57:13 +00:00
mesh->mMaterialIndex = 0;
}
// Create properly sized data arrays in the mesh. We store unique vertices per face,
// as specified
mesh->mNumVertices = numVertices;
mesh->mVertices = new aiVector3D[numVertices];
mesh->mNumFaces = (unsigned int)faces.size();
mesh->mFaces = new aiFace[mesh->mNumFaces];
// name
mesh->mName.Set(sourceMesh->mName);
// normals?
2023-11-03 10:47:13 +00:00
if (sourceMesh->mNormals.size() > 0) {
mesh->mNormals = new aiVector3D[numVertices];
2018-02-06 22:59:46 +00:00
}
2015-05-19 03:57:13 +00:00
// texture coords
2023-11-03 10:47:13 +00:00
for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++c) {
if (!sourceMesh->mTexCoords[c].empty()) {
mesh->mTextureCoords[c] = new aiVector3D[numVertices];
2018-02-06 22:59:46 +00:00
}
2015-05-19 03:57:13 +00:00
}
// vertex colors
2023-11-03 10:47:13 +00:00
for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS; ++c) {
if (!sourceMesh->mColors[c].empty()) {
mesh->mColors[c] = new aiColor4D[numVertices];
2018-02-06 22:59:46 +00:00
}
2015-05-19 03:57:13 +00:00
}
// now collect the vertex data of all data streams present in the imported mesh
2023-11-03 10:47:13 +00:00
unsigned int newIndex(0);
2015-05-19 03:57:13 +00:00
std::vector<unsigned int> orgPoints; // from which original point each new vertex stems
2023-11-03 10:47:13 +00:00
orgPoints.resize(numVertices, 0);
2015-05-19 03:57:13 +00:00
2023-11-03 10:47:13 +00:00
for (unsigned int c = 0; c < faces.size(); ++c) {
2015-05-19 03:57:13 +00:00
unsigned int f = faces[c]; // index of the source face
2023-11-03 10:47:13 +00:00
const XFile::Face &pf = sourceMesh->mPosFaces[f]; // position source face
2015-05-19 03:57:13 +00:00
// create face. either triangle or triangle fan depending on the index count
2023-11-03 10:47:13 +00:00
aiFace &df = mesh->mFaces[c]; // destination face
2015-05-19 03:57:13 +00:00
df.mNumIndices = (unsigned int)pf.mIndices.size();
2023-11-03 10:47:13 +00:00
df.mIndices = new unsigned int[df.mNumIndices];
2015-05-19 03:57:13 +00:00
// collect vertex data for indices of this face
2023-11-03 10:47:13 +00:00
for (unsigned int d = 0; d < df.mNumIndices; ++d) {
df.mIndices[d] = newIndex;
const unsigned int newIdx = pf.mIndices[d];
if (newIdx >= sourceMesh->mPositions.size()) {
2018-05-31 18:15:13 +00:00
continue;
}
2015-05-19 03:57:13 +00:00
orgPoints[newIndex] = pf.mIndices[d];
// Position
mesh->mVertices[newIndex] = sourceMesh->mPositions[pf.mIndices[d]];
// Normal, if present
2023-11-03 10:47:13 +00:00
if (mesh->HasNormals()) {
if (sourceMesh->mNormFaces[f].mIndices.size() > d) {
const size_t idx(sourceMesh->mNormFaces[f].mIndices[d]);
if (idx < sourceMesh->mNormals.size()) {
mesh->mNormals[newIndex] = sourceMesh->mNormals[idx];
}
2019-06-15 17:17:15 +00:00
}
2018-02-06 22:59:46 +00:00
}
2015-05-19 03:57:13 +00:00
// texture coord sets
2023-11-03 10:47:13 +00:00
for (unsigned int e = 0; e < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++e) {
if (mesh->HasTextureCoords(e)) {
2015-05-19 03:57:13 +00:00
aiVector2D tex = sourceMesh->mTexCoords[e][pf.mIndices[d]];
2023-11-03 10:47:13 +00:00
mesh->mTextureCoords[e][newIndex] = aiVector3D(tex.x, 1.0f - tex.y, 0.0f);
2015-05-19 03:57:13 +00:00
}
}
// vertex color sets
2023-11-03 10:47:13 +00:00
for (unsigned int e = 0; e < AI_MAX_NUMBER_OF_COLOR_SETS; ++e) {
if (mesh->HasVertexColors(e)) {
mesh->mColors[e][newIndex] = sourceMesh->mColors[e][pf.mIndices[d]];
2018-02-06 22:59:46 +00:00
}
}
2015-05-19 03:57:13 +00:00
newIndex++;
}
}
// there should be as much new vertices as we calculated before
2023-11-03 10:47:13 +00:00
ai_assert(newIndex == numVertices);
2015-05-19 03:57:13 +00:00
// convert all bones of the source mesh which influence vertices in this newly created mesh
2023-11-03 10:47:13 +00:00
const std::vector<XFile::Bone> &bones = sourceMesh->mBones;
std::vector<aiBone *> newBones;
for (unsigned int c = 0; c < bones.size(); ++c) {
const XFile::Bone &obone = bones[c];
2015-05-19 03:57:13 +00:00
// set up a vertex-linear array of the weights for quick searching if a bone influences a vertex
2023-11-03 10:47:13 +00:00
std::vector<ai_real> oldWeights(sourceMesh->mPositions.size(), 0.0);
for (unsigned int d = 0; d < obone.mWeights.size(); ++d) {
const unsigned int boneIdx = obone.mWeights[d].mVertex;
if (boneIdx < obone.mWeights.size()) {
oldWeights[obone.mWeights[d].mVertex] = obone.mWeights[d].mWeight;
}
2018-02-06 22:59:46 +00:00
}
2015-05-19 03:57:13 +00:00
// collect all vertex weights that influence a vertex in the new mesh
std::vector<aiVertexWeight> newWeights;
2023-11-03 10:47:13 +00:00
newWeights.reserve(numVertices);
for (unsigned int d = 0; d < orgPoints.size(); ++d) {
2015-05-19 03:57:13 +00:00
// does the new vertex stem from an old vertex which was influenced by this bone?
ai_real w = oldWeights[orgPoints[d]];
2023-11-03 10:47:13 +00:00
if (w > 0.0) {
newWeights.emplace_back(d, w);
2018-02-06 22:59:46 +00:00
}
2015-05-19 03:57:13 +00:00
}
// if the bone has no weights in the newly created mesh, ignore it
2023-11-03 10:47:13 +00:00
if (newWeights.empty()) {
2015-05-19 03:57:13 +00:00
continue;
2018-02-06 22:59:46 +00:00
}
2015-05-19 03:57:13 +00:00
// create
2023-11-03 10:47:13 +00:00
aiBone *nbone = new aiBone;
newBones.push_back(nbone);
2015-05-19 03:57:13 +00:00
// copy name and matrix
2023-11-03 10:47:13 +00:00
nbone->mName.Set(obone.mName);
2015-05-19 03:57:13 +00:00
nbone->mOffsetMatrix = obone.mOffsetMatrix;
nbone->mNumWeights = (unsigned int)newWeights.size();
nbone->mWeights = new aiVertexWeight[nbone->mNumWeights];
2023-11-03 10:47:13 +00:00
for (unsigned int d = 0; d < newWeights.size(); ++d) {
nbone->mWeights[d] = newWeights[d];
2018-02-06 22:59:46 +00:00
}
2015-05-19 03:57:13 +00:00
}
// store the bones in the mesh
mesh->mNumBones = (unsigned int)newBones.size();
2023-11-03 10:47:13 +00:00
if (!newBones.empty()) {
mesh->mBones = new aiBone *[mesh->mNumBones];
std::copy(newBones.begin(), newBones.end(), mesh->mBones);
2015-05-19 03:57:13 +00:00
}
}
}
// reallocate scene mesh array to be large enough
2023-11-03 10:47:13 +00:00
aiMesh **prevArray = pScene->mMeshes;
pScene->mMeshes = new aiMesh *[pScene->mNumMeshes + meshes.size()];
if (prevArray) {
memcpy(pScene->mMeshes, prevArray, pScene->mNumMeshes * sizeof(aiMesh *));
delete[] prevArray;
2015-05-19 03:57:13 +00:00
}
// allocate mesh index array in the node
pNode->mNumMeshes = (unsigned int)meshes.size();
pNode->mMeshes = new unsigned int[pNode->mNumMeshes];
// store all meshes in the mesh library of the scene and store their indices in the node
2023-11-03 10:47:13 +00:00
for (unsigned int a = 0; a < meshes.size(); a++) {
2015-05-19 03:57:13 +00:00
pScene->mMeshes[pScene->mNumMeshes] = meshes[a];
pNode->mMeshes[a] = pScene->mNumMeshes;
pScene->mNumMeshes++;
}
}
// ------------------------------------------------------------------------------------------------
// Converts the animations from the given imported data and creates them in the scene.
2023-11-03 10:47:13 +00:00
void XFileImporter::CreateAnimations(aiScene *pScene, const XFile::Scene *pData) {
std::vector<aiAnimation *> newAnims;
2015-05-19 03:57:13 +00:00
2023-11-03 10:47:13 +00:00
for (unsigned int a = 0; a < pData->mAnims.size(); ++a) {
const XFile::Animation *anim = pData->mAnims[a];
2015-05-19 03:57:13 +00:00
// some exporters mock me with empty animation tags.
2023-11-03 10:47:13 +00:00
if (anim->mAnims.empty()) {
2015-05-19 03:57:13 +00:00
continue;
2018-02-06 22:59:46 +00:00
}
2015-05-19 03:57:13 +00:00
// create a new animation to hold the data
2023-11-03 10:47:13 +00:00
aiAnimation *nanim = new aiAnimation;
newAnims.push_back(nanim);
nanim->mName.Set(anim->mName);
2015-05-19 03:57:13 +00:00
// duration will be determined by the maximum length
nanim->mDuration = 0;
nanim->mTicksPerSecond = pData->mAnimTicksPerSecond;
nanim->mNumChannels = (unsigned int)anim->mAnims.size();
2023-11-03 10:47:13 +00:00
nanim->mChannels = new aiNodeAnim *[nanim->mNumChannels];
2015-05-19 03:57:13 +00:00
2023-11-03 10:47:13 +00:00
for (unsigned int b = 0; b < anim->mAnims.size(); ++b) {
const XFile::AnimBone *bone = anim->mAnims[b];
aiNodeAnim *nbone = new aiNodeAnim;
nbone->mNodeName.Set(bone->mBoneName);
2015-05-19 03:57:13 +00:00
nanim->mChannels[b] = nbone;
2018-05-31 18:15:13 +00:00
// key-frames are given as combined transformation matrix keys
2023-11-03 10:47:13 +00:00
if (!bone->mTrafoKeys.empty()) {
2015-05-19 03:57:13 +00:00
nbone->mNumPositionKeys = (unsigned int)bone->mTrafoKeys.size();
nbone->mPositionKeys = new aiVectorKey[nbone->mNumPositionKeys];
nbone->mNumRotationKeys = (unsigned int)bone->mTrafoKeys.size();
nbone->mRotationKeys = new aiQuatKey[nbone->mNumRotationKeys];
nbone->mNumScalingKeys = (unsigned int)bone->mTrafoKeys.size();
nbone->mScalingKeys = new aiVectorKey[nbone->mNumScalingKeys];
2023-11-03 10:47:13 +00:00
for (unsigned int c = 0; c < bone->mTrafoKeys.size(); ++c) {
2015-05-19 03:57:13 +00:00
// deconstruct each matrix into separate position, rotation and scaling
double time = bone->mTrafoKeys[c].mTime;
aiMatrix4x4 trafo = bone->mTrafoKeys[c].mMatrix;
// extract position
2023-11-03 10:47:13 +00:00
aiVector3D pos(trafo.a4, trafo.b4, trafo.c4);
2015-05-19 03:57:13 +00:00
nbone->mPositionKeys[c].mTime = time;
nbone->mPositionKeys[c].mValue = pos;
// extract scaling
aiVector3D scale;
2023-11-03 10:47:13 +00:00
scale.x = aiVector3D(trafo.a1, trafo.b1, trafo.c1).Length();
scale.y = aiVector3D(trafo.a2, trafo.b2, trafo.c2).Length();
scale.z = aiVector3D(trafo.a3, trafo.b3, trafo.c3).Length();
2015-05-19 03:57:13 +00:00
nbone->mScalingKeys[c].mTime = time;
nbone->mScalingKeys[c].mValue = scale;
// reconstruct rotation matrix without scaling
aiMatrix3x3 rotmat(
2023-11-03 10:47:13 +00:00
trafo.a1 / scale.x, trafo.a2 / scale.y, trafo.a3 / scale.z,
trafo.b1 / scale.x, trafo.b2 / scale.y, trafo.b3 / scale.z,
trafo.c1 / scale.x, trafo.c2 / scale.y, trafo.c3 / scale.z);
2015-05-19 03:57:13 +00:00
// and convert it into a quaternion
nbone->mRotationKeys[c].mTime = time;
2023-11-03 10:47:13 +00:00
nbone->mRotationKeys[c].mValue = aiQuaternion(rotmat);
2015-05-19 03:57:13 +00:00
}
// longest lasting key sequence determines duration
2023-11-03 10:47:13 +00:00
nanim->mDuration = std::max(nanim->mDuration, bone->mTrafoKeys.back().mTime);
2018-02-06 22:59:46 +00:00
} else {
2015-05-19 03:57:13 +00:00
// separate key sequences for position, rotation, scaling
nbone->mNumPositionKeys = (unsigned int)bone->mPosKeys.size();
if (nbone->mNumPositionKeys != 0) {
2020-01-22 10:20:34 +00:00
nbone->mPositionKeys = new aiVectorKey[nbone->mNumPositionKeys];
2023-11-03 10:47:13 +00:00
for (unsigned int c = 0; c < nbone->mNumPositionKeys; ++c) {
2020-01-22 10:20:34 +00:00
aiVector3D pos = bone->mPosKeys[c].mValue;
2015-05-19 03:57:13 +00:00
2020-01-22 10:20:34 +00:00
nbone->mPositionKeys[c].mTime = bone->mPosKeys[c].mTime;
nbone->mPositionKeys[c].mValue = pos;
}
2015-05-19 03:57:13 +00:00
}
// rotation
nbone->mNumRotationKeys = (unsigned int)bone->mRotKeys.size();
if (nbone->mNumRotationKeys != 0) {
2020-01-22 10:20:34 +00:00
nbone->mRotationKeys = new aiQuatKey[nbone->mNumRotationKeys];
2023-11-03 10:47:13 +00:00
for (unsigned int c = 0; c < nbone->mNumRotationKeys; ++c) {
2020-01-22 10:20:34 +00:00
aiMatrix3x3 rotmat = bone->mRotKeys[c].mValue.GetMatrix();
2015-05-19 03:57:13 +00:00
2020-01-22 10:20:34 +00:00
nbone->mRotationKeys[c].mTime = bone->mRotKeys[c].mTime;
2023-11-03 10:47:13 +00:00
nbone->mRotationKeys[c].mValue = aiQuaternion(rotmat);
2020-01-22 10:20:34 +00:00
nbone->mRotationKeys[c].mValue.w *= -1.0f; // needs quat inversion
}
2015-05-19 03:57:13 +00:00
}
// scaling
nbone->mNumScalingKeys = (unsigned int)bone->mScaleKeys.size();
if (nbone->mNumScalingKeys != 0) {
2020-01-22 10:20:34 +00:00
nbone->mScalingKeys = new aiVectorKey[nbone->mNumScalingKeys];
2023-11-03 10:47:13 +00:00
for (unsigned int c = 0; c < nbone->mNumScalingKeys; c++)
2020-01-22 10:20:34 +00:00
nbone->mScalingKeys[c] = bone->mScaleKeys[c];
}
2015-05-19 03:57:13 +00:00
// longest lasting key sequence determines duration
2023-11-03 10:47:13 +00:00
if (bone->mPosKeys.size() > 0)
nanim->mDuration = std::max(nanim->mDuration, bone->mPosKeys.back().mTime);
if (bone->mRotKeys.size() > 0)
nanim->mDuration = std::max(nanim->mDuration, bone->mRotKeys.back().mTime);
if (bone->mScaleKeys.size() > 0)
nanim->mDuration = std::max(nanim->mDuration, bone->mScaleKeys.back().mTime);
2015-05-19 03:57:13 +00:00
}
}
}
// store all converted animations in the scene
2023-11-03 10:47:13 +00:00
if (newAnims.size() > 0) {
2015-05-19 03:57:13 +00:00
pScene->mNumAnimations = (unsigned int)newAnims.size();
2023-11-03 10:47:13 +00:00
pScene->mAnimations = new aiAnimation *[pScene->mNumAnimations];
for (unsigned int a = 0; a < newAnims.size(); a++)
2015-05-19 03:57:13 +00:00
pScene->mAnimations[a] = newAnims[a];
}
}
// ------------------------------------------------------------------------------------------------
// Converts all materials in the given array and stores them in the scene's material list.
2023-11-03 10:47:13 +00:00
void XFileImporter::ConvertMaterials(aiScene *pScene, std::vector<XFile::Material> &pMaterials) {
2015-05-19 03:57:13 +00:00
// count the non-referrer materials in the array
2023-11-03 10:47:13 +00:00
unsigned int numNewMaterials(0);
for (unsigned int a = 0; a < pMaterials.size(); ++a) {
if (!pMaterials[a].mIsReference) {
2018-02-06 22:59:46 +00:00
++numNewMaterials;
}
}
2015-05-19 03:57:13 +00:00
// resize the scene's material list to offer enough space for the new materials
2023-11-03 10:47:13 +00:00
if (numNewMaterials > 0) {
aiMaterial **prevMats = pScene->mMaterials;
pScene->mMaterials = new aiMaterial *[pScene->mNumMaterials + numNewMaterials];
if (nullptr != prevMats) {
::memcpy(pScene->mMaterials, prevMats, pScene->mNumMaterials * sizeof(aiMaterial *));
delete[] prevMats;
2018-02-06 22:59:46 +00:00
}
}
2015-05-19 03:57:13 +00:00
// convert all the materials given in the array
2023-11-03 10:47:13 +00:00
for (unsigned int a = 0; a < pMaterials.size(); ++a) {
XFile::Material &oldMat = pMaterials[a];
if (oldMat.mIsReference) {
2018-02-06 22:59:46 +00:00
// find the material it refers to by name, and store its index
2023-11-03 10:47:13 +00:00
for (size_t b = 0; b < pScene->mNumMaterials; ++b) {
2018-02-06 22:59:46 +00:00
aiString name;
2023-11-03 10:47:13 +00:00
pScene->mMaterials[b]->Get(AI_MATKEY_NAME, name);
if (strcmp(name.C_Str(), oldMat.mName.data()) == 0) {
oldMat.sceneIndex = b;
2018-02-06 22:59:46 +00:00
break;
}
}
2023-11-03 10:47:13 +00:00
if (oldMat.sceneIndex == SIZE_MAX) {
ASSIMP_LOG_WARN("Could not resolve global material reference \"", oldMat.mName, "\"");
2018-02-06 22:59:46 +00:00
oldMat.sceneIndex = 0;
}
2018-02-06 22:59:46 +00:00
continue;
}
2023-11-03 10:47:13 +00:00
aiMaterial *mat = new aiMaterial;
2015-05-19 03:57:13 +00:00
aiString name;
2023-11-03 10:47:13 +00:00
name.Set(oldMat.mName);
mat->AddProperty(&name, AI_MATKEY_NAME);
2018-02-06 22:59:46 +00:00
// Shading model: hard-coded to PHONG, there is no such information in an XFile
2015-05-19 03:57:13 +00:00
// FIX (aramis): If the specular exponent is 0, use gouraud shading. This is a bugfix
// for some models in the SDK (e.g. good old tiny.x)
2023-11-03 10:47:13 +00:00
int shadeMode = (int)oldMat.mSpecularExponent == 0.0f ? aiShadingMode_Gouraud : aiShadingMode_Phong;
2023-11-03 10:47:13 +00:00
mat->AddProperty<int>(&shadeMode, 1, AI_MATKEY_SHADING_MODEL);
2015-05-19 03:57:13 +00:00
// material colours
2018-02-06 22:59:46 +00:00
// Unclear: there's no ambient colour, but emissive. What to put for ambient?
// Probably nothing at all, let the user select a suitable default.
2023-11-03 10:47:13 +00:00
mat->AddProperty(&oldMat.mEmissive, 1, AI_MATKEY_COLOR_EMISSIVE);
mat->AddProperty(&oldMat.mDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
mat->AddProperty(&oldMat.mSpecular, 1, AI_MATKEY_COLOR_SPECULAR);
mat->AddProperty(&oldMat.mSpecularExponent, 1, AI_MATKEY_SHININESS);
2015-05-19 03:57:13 +00:00
// texture, if there is one
2023-11-03 10:47:13 +00:00
if (1 == oldMat.mTextures.size()) {
const XFile::TexEntry &otex = oldMat.mTextures.back();
2018-02-06 22:59:46 +00:00
if (otex.mName.length()) {
2015-05-19 03:57:13 +00:00
// if there is only one texture assume it contains the diffuse color
2023-11-03 10:47:13 +00:00
aiString tex(otex.mName);
if (otex.mIsNormalMap) {
mat->AddProperty(&tex, AI_MATKEY_TEXTURE_NORMALS(0));
2018-02-06 22:59:46 +00:00
} else {
2023-11-03 10:47:13 +00:00
mat->AddProperty(&tex, AI_MATKEY_TEXTURE_DIFFUSE(0));
2018-02-06 22:59:46 +00:00
}
2015-05-19 03:57:13 +00:00
}
2018-02-06 22:59:46 +00:00
} else {
2015-05-19 03:57:13 +00:00
// Otherwise ... try to search for typical strings in the
// texture's file name like 'bump' or 'diffuse'
2023-11-03 10:47:13 +00:00
unsigned int iHM = 0, iNM = 0, iDM = 0, iSM = 0, iAM = 0, iEM = 0;
for (unsigned int b = 0; b < oldMat.mTextures.size(); ++b) {
const XFile::TexEntry &otex = oldMat.mTextures[b];
2015-05-19 03:57:13 +00:00
std::string sz = otex.mName;
2023-11-03 10:47:13 +00:00
if (!sz.length()) {
2018-02-06 22:59:46 +00:00
continue;
}
2015-05-19 03:57:13 +00:00
// find the file name
std::string::size_type s = sz.find_last_of("\\/");
2023-11-03 10:47:13 +00:00
if (std::string::npos == s) {
2015-05-19 03:57:13 +00:00
s = 0;
2018-02-06 22:59:46 +00:00
}
2015-05-19 03:57:13 +00:00
// cut off the file extension
std::string::size_type sExt = sz.find_last_of('.');
2023-11-03 10:47:13 +00:00
if (std::string::npos != sExt) {
2015-05-19 03:57:13 +00:00
sz[sExt] = '\0';
}
2016-04-03 00:38:00 +00:00
// convert to lower case for easier comparison
2023-11-03 10:47:13 +00:00
for (unsigned int c = 0; c < sz.length(); ++c) {
sz[c] = (char)tolower((unsigned char)sz[c]);
2018-02-06 22:59:46 +00:00
}
2015-05-19 03:57:13 +00:00
// Place texture filename property under the corresponding name
2023-11-03 10:47:13 +00:00
aiString tex(oldMat.mTextures[b].mName);
2015-05-19 03:57:13 +00:00
// bump map
2018-02-06 22:59:46 +00:00
if (std::string::npos != sz.find("bump", s) || std::string::npos != sz.find("height", s)) {
2023-11-03 10:47:13 +00:00
mat->AddProperty(&tex, AI_MATKEY_TEXTURE_HEIGHT(iHM++));
} else if (otex.mIsNormalMap || std::string::npos != sz.find("normal", s) || std::string::npos != sz.find("nm", s)) {
mat->AddProperty(&tex, AI_MATKEY_TEXTURE_NORMALS(iNM++));
} else if (std::string::npos != sz.find("spec", s) || std::string::npos != sz.find("glanz", s)) {
mat->AddProperty(&tex, AI_MATKEY_TEXTURE_SPECULAR(iSM++));
} else if (std::string::npos != sz.find("ambi", s) || std::string::npos != sz.find("env", s)) {
mat->AddProperty(&tex, AI_MATKEY_TEXTURE_AMBIENT(iAM++));
} else if (std::string::npos != sz.find("emissive", s) || std::string::npos != sz.find("self", s)) {
mat->AddProperty(&tex, AI_MATKEY_TEXTURE_EMISSIVE(iEM++));
2018-02-06 22:59:46 +00:00
} else {
2015-05-19 03:57:13 +00:00
// Assume it is a diffuse texture
2023-11-03 10:47:13 +00:00
mat->AddProperty(&tex, AI_MATKEY_TEXTURE_DIFFUSE(iDM++));
2015-05-19 03:57:13 +00:00
}
}
}
pScene->mMaterials[pScene->mNumMaterials] = mat;
oldMat.sceneIndex = pScene->mNumMaterials;
pScene->mNumMaterials++;
}
}
2023-11-03 10:47:13 +00:00
} // namespace Assimp
#endif // !! ASSIMP_BUILD_NO_X_IMPORTER