2015-05-19 03:48:29 +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
|
|
|
|
2015-05-19 03:48:29 +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
|
2015-05-19 03:48:29 +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
|
2015-05-19 03:48:29 +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
|
2015-05-19 03:48:29 +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
|
2015-05-19 03:48:29 +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
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TODO: refactor entire file to get rid of the "flat-copy" first approach
|
|
|
|
// to copying structures. This easily breaks in the most unintuitive way
|
|
|
|
// possible as new fields are added to assimp structures.
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2016-11-22 20:06:14 +00:00
|
|
|
/**
|
|
|
|
* @file Implements Assimp::SceneCombiner. This is a smart utility
|
|
|
|
* class that combines multiple scenes, meshes, ... into one. Currently
|
|
|
|
* these utilities are used by the IRR and LWS loaders and the
|
|
|
|
* OptimizeGraph step.
|
|
|
|
*/
|
2015-05-19 03:48:29 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
#include "ScenePrivate.h"
|
|
|
|
#include "time.h"
|
|
|
|
#include <assimp/Hash.h>
|
2017-06-21 13:24:06 +00:00
|
|
|
#include <assimp/SceneCombiner.h>
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/StringUtils.h>
|
|
|
|
#include <assimp/fast_atof.h>
|
2020-06-23 19:05:42 +00:00
|
|
|
#include <assimp/mesh.h>
|
2018-03-20 13:09:07 +00:00
|
|
|
#include <assimp/metadata.h>
|
2016-06-06 20:04:29 +00:00
|
|
|
#include <assimp/scene.h>
|
2015-05-19 03:48:29 +00:00
|
|
|
#include <stdio.h>
|
2020-06-23 19:05:42 +00:00
|
|
|
#include <assimp/DefaultLogger.hpp>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2016-11-22 20:06:14 +00:00
|
|
|
namespace Assimp {
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-15 19:34:49 +00:00
|
|
|
#if (__GNUC__ >= 8 && __GNUC_MINOR__ >= 0)
|
2020-06-23 19:05:42 +00:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wclass-memaccess"
|
2020-06-15 19:34:49 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Add a prefix to a string
|
2020-06-23 19:05:42 +00:00
|
|
|
inline void PrefixString(aiString &string, const char *prefix, unsigned int len) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// If the string is already prefixed, we won't prefix it a second time
|
|
|
|
if (string.length >= 1 && string.data[0] == '$')
|
|
|
|
return;
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (len + string.length >= MAXLEN - 1) {
|
2020-06-15 19:34:49 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG("Can't add an unique prefix because the string is too long");
|
2015-05-19 03:57:13 +00:00
|
|
|
ai_assert(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the prefix
|
2020-06-23 19:05:42 +00:00
|
|
|
::memmove(string.data + len, string.data, string.length + 1);
|
|
|
|
::memcpy(string.data, prefix, len);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// And update the string's length
|
|
|
|
string.length += len;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Add node identifiers to a hashing set
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::AddNodeHashes(aiNode *node, std::set<unsigned int> &hashes) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Add node name to hashing set if it is non-empty - empty nodes are allowed
|
|
|
|
// and they can't have any anims assigned so its absolutely safe to duplicate them.
|
|
|
|
if (node->mName.length) {
|
2020-06-23 19:05:42 +00:00
|
|
|
hashes.insert(SuperFastHash(node->mName.data, static_cast<uint32_t>(node->mName.length)));
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process all children recursively
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0; i < node->mNumChildren; ++i) {
|
|
|
|
AddNodeHashes(node->mChildren[i], hashes);
|
2020-06-15 19:34:49 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Add a name prefix to all nodes in a hierarchy
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::AddNodePrefixes(aiNode *node, const char *prefix, unsigned int len) {
|
|
|
|
ai_assert(nullptr != prefix);
|
|
|
|
|
|
|
|
PrefixString(node->mName, prefix, len);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Process all children recursively
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0; i < node->mNumChildren; ++i) {
|
|
|
|
AddNodePrefixes(node->mChildren[i], prefix, len);
|
2017-11-19 18:05:51 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Search for matching names
|
2020-06-23 19:05:42 +00:00
|
|
|
bool SceneCombiner::FindNameMatch(const aiString &name, std::vector<SceneHelper> &input, unsigned int cur) {
|
2016-11-20 03:07:24 +00:00
|
|
|
const unsigned int hash = SuperFastHash(name.data, static_cast<uint32_t>(name.length));
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Check whether we find a positive match in one of the given sets
|
|
|
|
for (unsigned int i = 0; i < input.size(); ++i) {
|
|
|
|
if (cur != i && input[i].hashes.find(hash) != input[i].hashes.end()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Add a name prefix to all nodes in a hierarchy if a hash match is found
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::AddNodePrefixesChecked(aiNode *node, const char *prefix, unsigned int len,
|
|
|
|
std::vector<SceneHelper> &input, unsigned int cur) {
|
|
|
|
ai_assert(nullptr != prefix);
|
|
|
|
|
2016-11-20 03:07:24 +00:00
|
|
|
const unsigned int hash = SuperFastHash(node->mName.data, static_cast<uint32_t>(node->mName.length));
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Check whether we find a positive match in one of the given sets
|
|
|
|
for (unsigned int i = 0; i < input.size(); ++i) {
|
|
|
|
if (cur != i && input[i].hashes.find(hash) != input[i].hashes.end()) {
|
2020-06-23 19:05:42 +00:00
|
|
|
PrefixString(node->mName, prefix, len);
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Process all children recursively
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0; i < node->mNumChildren; ++i) {
|
|
|
|
AddNodePrefixesChecked(node->mChildren[i], prefix, len, input, cur);
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Add an offset to all mesh indices in a node graph
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::OffsetNodeMeshIndices(aiNode *node, unsigned int offset) {
|
|
|
|
for (unsigned int i = 0; i < node->mNumMeshes; ++i)
|
2015-05-19 03:57:13 +00:00
|
|
|
node->mMeshes[i] += offset;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0; i < node->mNumChildren; ++i) {
|
|
|
|
OffsetNodeMeshIndices(node->mChildren[i], offset);
|
2017-11-19 18:05:51 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Merges two scenes. Currently only used by the LWS loader.
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::MergeScenes(aiScene **_dest, std::vector<aiScene *> &src, unsigned int flags) {
|
|
|
|
if (nullptr == _dest) {
|
2017-11-19 16:41:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// if _dest points to nullptr allocate a new scene. Otherwise clear the old and reuse it
|
2017-11-19 18:05:51 +00:00
|
|
|
if (src.empty()) {
|
|
|
|
if (*_dest) {
|
2015-05-19 03:57:13 +00:00
|
|
|
(*_dest)->~aiScene();
|
2020-06-23 19:05:42 +00:00
|
|
|
SceneCombiner::CopySceneFlat(_dest, src[0]);
|
|
|
|
} else
|
|
|
|
*_dest = src[0];
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-12-14 11:16:29 +00:00
|
|
|
if (*_dest) {
|
2020-06-23 19:05:42 +00:00
|
|
|
(*_dest)->~aiScene();
|
2020-12-14 11:16:29 +00:00
|
|
|
new (*_dest) aiScene();
|
|
|
|
} else
|
2020-06-23 19:05:42 +00:00
|
|
|
*_dest = new aiScene();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Create a dummy scene to serve as master for the others
|
2020-06-23 19:05:42 +00:00
|
|
|
aiScene *master = new aiScene();
|
2015-05-19 03:57:13 +00:00
|
|
|
master->mRootNode = new aiNode();
|
|
|
|
master->mRootNode->mName.Set("<MergeRoot>");
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
std::vector<AttachmentInfo> srcList(src.size());
|
|
|
|
for (unsigned int i = 0; i < srcList.size(); ++i) {
|
|
|
|
srcList[i] = AttachmentInfo(src[i], master->mRootNode);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 'master' will be deleted afterwards
|
2020-06-23 19:05:42 +00:00
|
|
|
MergeScenes(_dest, master, srcList, flags);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::AttachToGraph(aiNode *attach, std::vector<NodeAttachmentInfo> &srcList) {
|
2015-05-19 03:57:13 +00:00
|
|
|
unsigned int cnt;
|
2020-06-23 19:05:42 +00:00
|
|
|
for (cnt = 0; cnt < attach->mNumChildren; ++cnt) {
|
|
|
|
AttachToGraph(attach->mChildren[cnt], srcList);
|
2016-11-22 20:06:14 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
cnt = 0;
|
|
|
|
for (std::vector<NodeAttachmentInfo>::iterator it = srcList.begin();
|
2020-06-23 19:05:42 +00:00
|
|
|
it != srcList.end(); ++it) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if ((*it).attachToNode == attach && !(*it).resolved)
|
|
|
|
++cnt;
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (cnt) {
|
|
|
|
aiNode **n = new aiNode *[cnt + attach->mNumChildren];
|
|
|
|
if (attach->mNumChildren) {
|
|
|
|
::memcpy(n, attach->mChildren, sizeof(void *) * attach->mNumChildren);
|
2015-05-19 03:57:13 +00:00
|
|
|
delete[] attach->mChildren;
|
|
|
|
}
|
|
|
|
attach->mChildren = n;
|
|
|
|
|
|
|
|
n += attach->mNumChildren;
|
|
|
|
attach->mNumChildren += cnt;
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0; i < srcList.size(); ++i) {
|
|
|
|
NodeAttachmentInfo &att = srcList[i];
|
|
|
|
if (att.attachToNode == attach && !att.resolved) {
|
2015-05-19 03:57:13 +00:00
|
|
|
*n = att.node;
|
|
|
|
(**n).mParent = attach;
|
|
|
|
++n;
|
|
|
|
|
|
|
|
// mark this attachment as resolved
|
|
|
|
att.resolved = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::AttachToGraph(aiScene *master, std::vector<NodeAttachmentInfo> &src) {
|
|
|
|
ai_assert(nullptr != master);
|
|
|
|
|
|
|
|
AttachToGraph(master->mRootNode, src);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::MergeScenes(aiScene **_dest, aiScene *master, std::vector<AttachmentInfo> &srcList, unsigned int flags) {
|
|
|
|
if (nullptr == _dest) {
|
2017-11-19 16:41:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// if _dest points to nullptr allocate a new scene. Otherwise clear the old and reuse it
|
|
|
|
if (srcList.empty()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (*_dest) {
|
2020-06-23 19:05:42 +00:00
|
|
|
SceneCombiner::CopySceneFlat(_dest, master);
|
|
|
|
} else
|
|
|
|
*_dest = master;
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (*_dest) {
|
|
|
|
(*_dest)->~aiScene();
|
|
|
|
new (*_dest) aiScene();
|
2020-06-23 19:05:42 +00:00
|
|
|
} else
|
|
|
|
*_dest = new aiScene();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiScene *dest = *_dest;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
std::vector<SceneHelper> src(srcList.size() + 1);
|
2015-05-19 03:57:13 +00:00
|
|
|
src[0].scene = master;
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0; i < srcList.size(); ++i) {
|
|
|
|
src[i + 1] = SceneHelper(srcList[i].scene);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// this helper array specifies which scenes are duplicates of others
|
2020-06-23 19:05:42 +00:00
|
|
|
std::vector<unsigned int> duplicates(src.size(), UINT_MAX);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// this helper array is used as lookup table several times
|
|
|
|
std::vector<unsigned int> offset(src.size());
|
|
|
|
|
|
|
|
// Find duplicate scenes
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0; i < src.size(); ++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (duplicates[i] != i && duplicates[i] != UINT_MAX) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
duplicates[i] = i;
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int a = i + 1; a < src.size(); ++a) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (src[i].scene == src[a].scene) {
|
|
|
|
duplicates[a] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate unique names for all named stuff?
|
2020-06-23 19:05:42 +00:00
|
|
|
if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES) {
|
2015-05-19 03:48:29 +00:00
|
|
|
#if 0
|
2015-05-19 03:57:13 +00:00
|
|
|
// Construct a proper random number generator
|
|
|
|
boost::mt19937 rng( );
|
|
|
|
boost::uniform_int<> dist(1u,1 << 24u);
|
|
|
|
boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rndGen(rng, dist);
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 1; i < src.size(); ++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
//if (i != duplicates[i])
|
|
|
|
//{
|
|
|
|
// // duplicate scenes share the same UID
|
|
|
|
// ::strcpy( src[i].id, src[duplicates[i]].id );
|
|
|
|
// src[i].idlen = src[duplicates[i]].idlen;
|
|
|
|
|
|
|
|
// continue;
|
|
|
|
//}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
src[i].idlen = ai_snprintf(src[i].id, 32, "$%.6X$_", i);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {
|
|
|
|
|
|
|
|
// Compute hashes for all identifiers in this scene and store them
|
|
|
|
// in a sorted table (for convenience I'm using std::set). We hash
|
|
|
|
// just the node and animation channel names, all identifiers except
|
|
|
|
// the material names should be caught by doing this.
|
2020-06-23 19:05:42 +00:00
|
|
|
AddNodeHashes(src[i]->mRootNode, src[i].hashes);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int a = 0; a < src[i]->mNumAnimations; ++a) {
|
|
|
|
aiAnimation *anim = src[i]->mAnimations[a];
|
|
|
|
src[i].hashes.insert(SuperFastHash(anim->mName.data, static_cast<uint32_t>(anim->mName.length)));
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int cnt;
|
|
|
|
|
|
|
|
// First find out how large the respective output arrays must be
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int n = 0; n < src.size(); ++n) {
|
|
|
|
SceneHelper *cur = &src[n];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (n == duplicates[n] || flags & AI_INT_MERGE_SCENE_DUPLICATES_DEEP_CPY) {
|
|
|
|
dest->mNumTextures += (*cur)->mNumTextures;
|
|
|
|
dest->mNumMaterials += (*cur)->mNumMaterials;
|
|
|
|
dest->mNumMeshes += (*cur)->mNumMeshes;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
dest->mNumLights += (*cur)->mNumLights;
|
|
|
|
dest->mNumCameras += (*cur)->mNumCameras;
|
2015-05-19 03:57:13 +00:00
|
|
|
dest->mNumAnimations += (*cur)->mNumAnimations;
|
|
|
|
|
|
|
|
// Combine the flags of all scenes
|
|
|
|
// We need to process them flag-by-flag here to get correct results
|
|
|
|
// dest->mFlags ; //|= (*cur)->mFlags;
|
|
|
|
if ((*cur)->mFlags & AI_SCENE_FLAGS_NON_VERBOSE_FORMAT) {
|
|
|
|
dest->mFlags |= AI_SCENE_FLAGS_NON_VERBOSE_FORMAT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate the output texture list + an offset table for all texture indices
|
2020-06-23 19:05:42 +00:00
|
|
|
if (dest->mNumTextures) {
|
|
|
|
aiTexture **pip = dest->mTextures = new aiTexture *[dest->mNumMaterials];
|
2015-05-19 03:57:13 +00:00
|
|
|
cnt = 0;
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int n = 0; n < src.size(); ++n) {
|
|
|
|
SceneHelper *cur = &src[n];
|
|
|
|
for (unsigned int i = 0; i < (*cur)->mNumTextures; ++i) {
|
|
|
|
if (n != duplicates[n]) {
|
|
|
|
if (flags & AI_INT_MERGE_SCENE_DUPLICATES_DEEP_CPY)
|
|
|
|
Copy(pip, (*cur)->mTextures[i]);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
else
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
*pip = (*cur)->mTextures[i];
|
2015-05-19 03:57:13 +00:00
|
|
|
++pip;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset[n] = cnt;
|
|
|
|
cnt = (unsigned int)(pip - dest->mTextures);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate the output material list + an offset table for all material indices
|
2020-06-23 19:05:42 +00:00
|
|
|
if (dest->mNumMaterials) {
|
|
|
|
aiMaterial **pip = dest->mMaterials = new aiMaterial *[dest->mNumMaterials];
|
2015-05-19 03:57:13 +00:00
|
|
|
cnt = 0;
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int n = 0; n < src.size(); ++n) {
|
|
|
|
SceneHelper *cur = &src[n];
|
|
|
|
for (unsigned int i = 0; i < (*cur)->mNumMaterials; ++i) {
|
|
|
|
if (n != duplicates[n]) {
|
|
|
|
if (flags & AI_INT_MERGE_SCENE_DUPLICATES_DEEP_CPY)
|
|
|
|
Copy(pip, (*cur)->mMaterials[i]);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
else
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
*pip = (*cur)->mMaterials[i];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if ((*cur)->mNumTextures != dest->mNumTextures) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// We need to update all texture indices of the mesh. So we need to search for
|
|
|
|
// a material property called '$tex.file'
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int a = 0; a < (*pip)->mNumProperties; ++a) {
|
|
|
|
aiMaterialProperty *prop = (*pip)->mProperties[a];
|
|
|
|
if (!strncmp(prop->mKey.data, "$tex.file", 9)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Check whether this texture is an embedded texture.
|
|
|
|
// In this case the property looks like this: *<n>,
|
|
|
|
// where n is the index of the texture.
|
2020-06-23 19:05:42 +00:00
|
|
|
aiString &s = *((aiString *)prop->mData);
|
|
|
|
if ('*' == s.data[0]) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Offset the index and write it back ..
|
|
|
|
const unsigned int idx = strtoul10(&s.data[1]) + offset[n];
|
2020-06-23 19:05:42 +00:00
|
|
|
ASSIMP_itoa10(&s.data[1], sizeof(s.data) - 1, idx);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to generate new, unique material names?
|
2020-06-23 19:05:42 +00:00
|
|
|
else if (!::strcmp(prop->mKey.data, "$mat.name") && flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_MATNAMES) {
|
|
|
|
aiString *pcSrc = (aiString *)prop->mData;
|
2015-05-19 03:57:13 +00:00
|
|
|
PrefixString(*pcSrc, (*cur).id, (*cur).idlen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++pip;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset[n] = cnt;
|
|
|
|
cnt = (unsigned int)(pip - dest->mMaterials);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate the output mesh list + again an offset table for all mesh indices
|
2020-06-23 19:05:42 +00:00
|
|
|
if (dest->mNumMeshes) {
|
|
|
|
aiMesh **pip = dest->mMeshes = new aiMesh *[dest->mNumMeshes];
|
2015-05-19 03:57:13 +00:00
|
|
|
cnt = 0;
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int n = 0; n < src.size(); ++n) {
|
|
|
|
SceneHelper *cur = &src[n];
|
|
|
|
for (unsigned int i = 0; i < (*cur)->mNumMeshes; ++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (n != duplicates[n]) {
|
2020-06-23 19:05:42 +00:00
|
|
|
if (flags & AI_INT_MERGE_SCENE_DUPLICATES_DEEP_CPY)
|
2015-05-19 03:57:13 +00:00
|
|
|
Copy(pip, (*cur)->mMeshes[i]);
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
else
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
*pip = (*cur)->mMeshes[i];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// update the material index of the mesh
|
2020-06-23 19:05:42 +00:00
|
|
|
(*pip)->mMaterialIndex += offset[n];
|
2015-05-19 03:57:13 +00:00
|
|
|
++pip;
|
|
|
|
}
|
|
|
|
|
|
|
|
// reuse the offset array - store now the mesh offset in it
|
|
|
|
offset[n] = cnt;
|
|
|
|
cnt = (unsigned int)(pip - dest->mMeshes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
std::vector<NodeAttachmentInfo> nodes;
|
2015-05-19 03:57:13 +00:00
|
|
|
nodes.reserve(srcList.size());
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Now generate the output node graph. We need to make those
|
|
|
|
// names in the graph that are referenced by anims or lights
|
|
|
|
// or cameras unique. So we add a prefix to them ... $<rand>_
|
|
|
|
// We could also use a counter, but using a random value allows us to
|
|
|
|
// use just one prefix if we are joining multiple scene hierarchies recursively.
|
|
|
|
// Chances are quite good we don't collide, so we try that ...
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Allocate space for light sources, cameras and animations
|
2020-06-23 19:05:42 +00:00
|
|
|
aiLight **ppLights = dest->mLights = (dest->mNumLights ? new aiLight *[dest->mNumLights] : nullptr);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiCamera **ppCameras = dest->mCameras = (dest->mNumCameras ? new aiCamera *[dest->mNumCameras] : nullptr);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiAnimation **ppAnims = dest->mAnimations = (dest->mNumAnimations ? new aiAnimation *[dest->mNumAnimations] : nullptr);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (int n = static_cast<int>(src.size() - 1); n >= 0; --n) /* !!! important !!! */
|
2015-05-19 03:57:13 +00:00
|
|
|
{
|
2020-06-23 19:05:42 +00:00
|
|
|
SceneHelper *cur = &src[n];
|
|
|
|
aiNode *node;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// To offset or not to offset, this is the question
|
2020-06-23 19:05:42 +00:00
|
|
|
if (n != (int)duplicates[n]) {
|
2015-12-31 15:00:19 +00:00
|
|
|
// Get full scene-graph copy
|
2020-06-23 19:05:42 +00:00
|
|
|
Copy(&node, (*cur)->mRootNode);
|
|
|
|
OffsetNodeMeshIndices(node, offset[duplicates[n]]);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
if (flags & AI_INT_MERGE_SCENE_DUPLICATES_DEEP_CPY) {
|
|
|
|
// (note:) they are already 'offseted' by offset[duplicates[n]]
|
2020-06-23 19:05:42 +00:00
|
|
|
OffsetNodeMeshIndices(node, offset[n] - offset[duplicates[n]]);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
} else // if (n == duplicates[n])
|
2015-05-19 03:57:13 +00:00
|
|
|
{
|
|
|
|
node = (*cur)->mRootNode;
|
2020-06-23 19:05:42 +00:00
|
|
|
OffsetNodeMeshIndices(node, offset[n]);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
if (n) // src[0] is the master node
|
2020-06-23 19:05:42 +00:00
|
|
|
nodes.push_back(NodeAttachmentInfo(node, srcList[n - 1].attachToNode, n));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// add name prefixes?
|
|
|
|
if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES) {
|
|
|
|
|
|
|
|
// or the whole scenegraph
|
|
|
|
if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {
|
2020-06-23 19:05:42 +00:00
|
|
|
AddNodePrefixesChecked(node, (*cur).id, (*cur).idlen, src, n);
|
|
|
|
} else
|
|
|
|
AddNodePrefixes(node, (*cur).id, (*cur).idlen);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// meshes
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0; i < (*cur)->mNumMeshes; ++i) {
|
|
|
|
aiMesh *mesh = (*cur)->mMeshes[i];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// rename all bones
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int a = 0; a < mesh->mNumBones; ++a) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!FindNameMatch(mesh->mBones[a]->mName, src, n))
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
PrefixString(mesh->mBones[a]->mName, (*cur).id, (*cur).idlen);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
// Copy light sources
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0; i < (*cur)->mNumLights; ++i, ++ppLights) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (n != (int)duplicates[n]) // duplicate scene?
|
|
|
|
{
|
|
|
|
Copy(ppLights, (*cur)->mLights[i]);
|
2020-06-23 19:05:42 +00:00
|
|
|
} else
|
|
|
|
*ppLights = (*cur)->mLights[i];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Add name prefixes?
|
|
|
|
if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES) {
|
|
|
|
if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!FindNameMatch((*ppLights)->mName, src, n))
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
PrefixString((*ppLights)->mName, (*cur).id, (*cur).idlen);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
// Copy cameras
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0; i < (*cur)->mNumCameras; ++i, ++ppCameras) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (n != (int)duplicates[n]) // duplicate scene?
|
|
|
|
{
|
|
|
|
Copy(ppCameras, (*cur)->mCameras[i]);
|
2020-06-23 19:05:42 +00:00
|
|
|
} else
|
|
|
|
*ppCameras = (*cur)->mCameras[i];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Add name prefixes?
|
|
|
|
if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES) {
|
|
|
|
if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!FindNameMatch((*ppCameras)->mName, src, n))
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
PrefixString((*ppCameras)->mName, (*cur).id, (*cur).idlen);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
// Copy animations
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0; i < (*cur)->mNumAnimations; ++i, ++ppAnims) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (n != (int)duplicates[n]) // duplicate scene?
|
|
|
|
{
|
|
|
|
Copy(ppAnims, (*cur)->mAnimations[i]);
|
2020-06-23 19:05:42 +00:00
|
|
|
} else
|
|
|
|
*ppAnims = (*cur)->mAnimations[i];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Add name prefixes?
|
|
|
|
if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES) {
|
|
|
|
if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!FindNameMatch((*ppAnims)->mName, src, n))
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
PrefixString((*ppAnims)->mName, (*cur).id, (*cur).idlen);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// don't forget to update all node animation channels
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int a = 0; a < (*ppAnims)->mNumChannels; ++a) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!FindNameMatch((*ppAnims)->mChannels[a]->mNodeName, src, n))
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
PrefixString((*ppAnims)->mChannels[a]->mNodeName, (*cur).id, (*cur).idlen);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now build the output graph
|
2020-06-23 19:05:42 +00:00
|
|
|
AttachToGraph(master, nodes);
|
2015-05-19 03:57:13 +00:00
|
|
|
dest->mRootNode = master->mRootNode;
|
|
|
|
|
|
|
|
// Check whether we succeeded at building the output graph
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::vector<NodeAttachmentInfo>::iterator it = nodes.begin();
|
|
|
|
it != nodes.end(); ++it) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!(*it).resolved) {
|
|
|
|
if (flags & AI_INT_MERGE_SCENE_RESOLVE_CROSS_ATTACHMENTS) {
|
|
|
|
// search for this attachment point in all other imported scenes, too.
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int n = 0; n < src.size(); ++n) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (n != (*it).src_idx) {
|
2020-06-23 19:05:42 +00:00
|
|
|
AttachToGraph(src[n].scene, nodes);
|
2015-05-19 03:57:13 +00:00
|
|
|
if ((*it).resolved)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(*it).resolved) {
|
2020-06-23 19:05:42 +00:00
|
|
|
ASSIMP_LOG_ERROR_F("SceneCombiner: Failed to resolve attachment ", (*it).node->mName.data,
|
|
|
|
" ", (*it).attachToNode->mName.data);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now delete all input scenes. Make sure duplicate scenes aren't
|
|
|
|
// deleted more than one time
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int n = 0; n < src.size(); ++n) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (n != duplicates[n]) // duplicate scene?
|
|
|
|
continue;
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiScene *deleteMe = src[n].scene;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// We need to delete the arrays before the destructor is called -
|
|
|
|
// we are reusing the array members
|
2020-06-23 19:05:42 +00:00
|
|
|
delete[] deleteMe->mMeshes;
|
|
|
|
deleteMe->mMeshes = nullptr;
|
|
|
|
delete[] deleteMe->mCameras;
|
|
|
|
deleteMe->mCameras = nullptr;
|
|
|
|
delete[] deleteMe->mLights;
|
|
|
|
deleteMe->mLights = nullptr;
|
|
|
|
delete[] deleteMe->mMaterials;
|
|
|
|
deleteMe->mMaterials = nullptr;
|
|
|
|
delete[] deleteMe->mAnimations;
|
|
|
|
deleteMe->mAnimations = nullptr;
|
|
|
|
|
|
|
|
deleteMe->mRootNode = nullptr;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Now we can safely delete the scene
|
|
|
|
delete deleteMe;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check flags
|
|
|
|
if (!dest->mNumMeshes || !dest->mNumMaterials) {
|
|
|
|
dest->mFlags |= AI_SCENE_FLAGS_INCOMPLETE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're finished
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Build a list of unique bones
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::BuildUniqueBoneList(std::list<BoneWithHash> &asBones,
|
|
|
|
std::vector<aiMesh *>::const_iterator it,
|
|
|
|
std::vector<aiMesh *>::const_iterator end) {
|
2015-05-19 03:57:13 +00:00
|
|
|
unsigned int iOffset = 0;
|
2020-06-23 19:05:42 +00:00
|
|
|
for (; it != end; ++it) {
|
|
|
|
for (unsigned int l = 0; l < (*it)->mNumBones; ++l) {
|
|
|
|
aiBone *p = (*it)->mBones[l];
|
|
|
|
uint32_t itml = SuperFastHash(p->mName.data, (unsigned int)p->mName.length);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
std::list<BoneWithHash>::iterator it2 = asBones.begin();
|
2015-05-19 03:57:13 +00:00
|
|
|
std::list<BoneWithHash>::iterator end2 = asBones.end();
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (; it2 != end2; ++it2) {
|
|
|
|
if ((*it2).first == itml) {
|
|
|
|
(*it2).pSrcBones.push_back(BoneSrcIndex(p, iOffset));
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
if (end2 == it2) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// need to begin a new bone entry
|
|
|
|
asBones.push_back(BoneWithHash());
|
2020-06-23 19:05:42 +00:00
|
|
|
BoneWithHash &btz = asBones.back();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// setup members
|
|
|
|
btz.first = itml;
|
|
|
|
btz.second = &p->mName;
|
2020-06-23 19:05:42 +00:00
|
|
|
btz.pSrcBones.push_back(BoneSrcIndex(p, iOffset));
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
iOffset += (*it)->mNumVertices;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Merge a list of bones
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::MergeBones(aiMesh *out, std::vector<aiMesh *>::const_iterator it,
|
|
|
|
std::vector<aiMesh *>::const_iterator end) {
|
|
|
|
if (nullptr == out || out->mNumBones == 0) {
|
2017-11-19 16:41:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// find we need to build an unique list of all bones.
|
|
|
|
// we work with hashes to make the comparisons MUCH faster,
|
|
|
|
// at least if we have many bones.
|
|
|
|
std::list<BoneWithHash> asBones;
|
2020-06-23 19:05:42 +00:00
|
|
|
BuildUniqueBoneList(asBones, it, end);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// now create the output bones
|
|
|
|
out->mNumBones = 0;
|
2020-06-23 19:05:42 +00:00
|
|
|
out->mBones = new aiBone *[asBones.size()];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::list<BoneWithHash>::const_iterator boneIt = asBones.begin(), boneEnd = asBones.end(); boneIt != boneEnd; ++boneIt) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Allocate a bone and setup it's name
|
2020-06-23 19:05:42 +00:00
|
|
|
aiBone *pc = out->mBones[out->mNumBones++] = new aiBone();
|
|
|
|
pc->mName = aiString(*(boneIt->second));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
std::vector<BoneSrcIndex>::const_iterator wend = boneIt->pSrcBones.end();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Loop through all bones to be joined for this bone
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::vector<BoneSrcIndex>::const_iterator wmit = boneIt->pSrcBones.begin(); wmit != wend; ++wmit) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pc->mNumWeights += (*wmit).first->mNumWeights;
|
|
|
|
|
|
|
|
// NOTE: different offset matrices for bones with equal names
|
|
|
|
// are - at the moment - not handled correctly.
|
2018-07-18 18:29:05 +00:00
|
|
|
if (wmit != boneIt->pSrcBones.begin() && pc->mOffsetMatrix != wmit->first->mOffsetMatrix) {
|
2018-04-19 15:21:21 +00:00
|
|
|
ASSIMP_LOG_WARN("Bones with equal names but different offset matrices can't be joined at the moment");
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-07-18 18:29:05 +00:00
|
|
|
pc->mOffsetMatrix = wmit->first->mOffsetMatrix;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate the vertex weight array
|
2020-06-23 19:05:42 +00:00
|
|
|
aiVertexWeight *avw = pc->mWeights = new aiVertexWeight[pc->mNumWeights];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// And copy the final weights - adjust the vertex IDs by the
|
2016-11-06 18:36:37 +00:00
|
|
|
// face index offset of the corresponding mesh.
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::vector<BoneSrcIndex>::const_iterator wmit = (*boneIt).pSrcBones.begin(); wmit != (*boneIt).pSrcBones.end(); ++wmit) {
|
2018-12-28 10:17:27 +00:00
|
|
|
if (wmit == wend) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiBone *pip = (*wmit).first;
|
|
|
|
for (unsigned int mp = 0; mp < pip->mNumWeights; ++mp, ++avw) {
|
|
|
|
const aiVertexWeight &vfi = pip->mWeights[mp];
|
2015-05-19 03:57:13 +00:00
|
|
|
avw->mWeight = vfi.mWeight;
|
|
|
|
avw->mVertexId = vfi.mVertexId + (*wmit).second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Merge a list of meshes
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::MergeMeshes(aiMesh **_out, unsigned int /*flags*/,
|
|
|
|
std::vector<aiMesh *>::const_iterator begin,
|
|
|
|
std::vector<aiMesh *>::const_iterator end) {
|
|
|
|
if (nullptr == _out) {
|
2017-11-19 16:41:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (begin == end) {
|
|
|
|
*_out = nullptr; // no meshes ...
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate the output mesh
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMesh *out = *_out = new aiMesh();
|
2015-05-19 03:57:13 +00:00
|
|
|
out->mMaterialIndex = (*begin)->mMaterialIndex;
|
|
|
|
|
2017-09-07 18:30:17 +00:00
|
|
|
std::string name;
|
2015-05-19 03:57:13 +00:00
|
|
|
// Find out how much output storage we'll need
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
|
|
|
|
const char *meshName((*it)->mName.C_Str());
|
|
|
|
name += std::string(meshName);
|
|
|
|
if (it != end - 1) {
|
2017-09-07 18:30:17 +00:00
|
|
|
name += ".";
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
out->mNumVertices += (*it)->mNumVertices;
|
|
|
|
out->mNumFaces += (*it)->mNumFaces;
|
|
|
|
out->mNumBones += (*it)->mNumBones;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// combine primitive type flags
|
|
|
|
out->mPrimitiveTypes |= (*it)->mPrimitiveTypes;
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
out->mName.Set(name.c_str());
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
if (out->mNumVertices) {
|
2020-06-23 19:05:42 +00:00
|
|
|
aiVector3D *pv2;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// copy vertex positions
|
2020-06-23 19:05:42 +00:00
|
|
|
if ((**begin).HasPositions()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
pv2 = out->mVertices = new aiVector3D[out->mNumVertices];
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
|
|
|
|
if ((*it)->mVertices) {
|
|
|
|
::memcpy(pv2, (*it)->mVertices, (*it)->mNumVertices * sizeof(aiVector3D));
|
|
|
|
} else
|
|
|
|
ASSIMP_LOG_WARN("JoinMeshes: Positions expected but input mesh contains no positions");
|
2015-05-19 03:57:13 +00:00
|
|
|
pv2 += (*it)->mNumVertices;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// copy normals
|
|
|
|
if ((**begin).HasNormals()) {
|
|
|
|
|
|
|
|
pv2 = out->mNormals = new aiVector3D[out->mNumVertices];
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
|
|
|
|
if ((*it)->mNormals) {
|
|
|
|
::memcpy(pv2, (*it)->mNormals, (*it)->mNumVertices * sizeof(aiVector3D));
|
2018-03-20 17:43:54 +00:00
|
|
|
} else {
|
2020-06-23 19:05:42 +00:00
|
|
|
ASSIMP_LOG_WARN("JoinMeshes: Normals expected but input mesh contains no normals");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
pv2 += (*it)->mNumVertices;
|
|
|
|
}
|
|
|
|
}
|
2017-09-07 18:30:17 +00:00
|
|
|
// copy tangents and bi-tangents
|
2020-06-23 19:05:42 +00:00
|
|
|
if ((**begin).HasTangentsAndBitangents()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
pv2 = out->mTangents = new aiVector3D[out->mNumVertices];
|
2020-06-23 19:05:42 +00:00
|
|
|
aiVector3D *pv2b = out->mBitangents = new aiVector3D[out->mNumVertices];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
|
|
|
|
if ((*it)->mTangents) {
|
|
|
|
::memcpy(pv2, (*it)->mTangents, (*it)->mNumVertices * sizeof(aiVector3D));
|
|
|
|
::memcpy(pv2b, (*it)->mBitangents, (*it)->mNumVertices * sizeof(aiVector3D));
|
2018-03-20 17:43:54 +00:00
|
|
|
} else {
|
2020-06-23 19:05:42 +00:00
|
|
|
ASSIMP_LOG_WARN("JoinMeshes: Tangents expected but input mesh contains no tangents");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
pv2 += (*it)->mNumVertices;
|
2015-05-19 03:57:13 +00:00
|
|
|
pv2b += (*it)->mNumVertices;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// copy texture coordinates
|
|
|
|
unsigned int n = 0;
|
2018-03-20 17:43:54 +00:00
|
|
|
while ((**begin).HasTextureCoords(n)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
out->mNumUVComponents[n] = (*begin)->mNumUVComponents[n];
|
|
|
|
|
|
|
|
pv2 = out->mTextureCoords[n] = new aiVector3D[out->mNumVertices];
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
|
|
|
|
if ((*it)->mTextureCoords[n]) {
|
|
|
|
::memcpy(pv2, (*it)->mTextureCoords[n], (*it)->mNumVertices * sizeof(aiVector3D));
|
2018-03-20 17:43:54 +00:00
|
|
|
} else {
|
2020-06-23 19:05:42 +00:00
|
|
|
ASSIMP_LOG_WARN("JoinMeshes: UVs expected but input mesh contains no UVs");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
pv2 += (*it)->mNumVertices;
|
|
|
|
}
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
// copy vertex colors
|
|
|
|
n = 0;
|
2020-06-23 19:05:42 +00:00
|
|
|
while ((**begin).HasVertexColors(n)) {
|
2018-07-18 18:29:05 +00:00
|
|
|
aiColor4D *pVec2 = out->mColors[n] = new aiColor4D[out->mNumVertices];
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
|
|
|
|
if ((*it)->mColors[n]) {
|
|
|
|
::memcpy(pVec2, (*it)->mColors[n], (*it)->mNumVertices * sizeof(aiColor4D));
|
2018-03-20 17:43:54 +00:00
|
|
|
} else {
|
2020-06-23 19:05:42 +00:00
|
|
|
ASSIMP_LOG_WARN("JoinMeshes: VCs expected but input mesh contains no VCs");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2018-07-18 18:29:05 +00:00
|
|
|
pVec2 += (*it)->mNumVertices;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out->mNumFaces) // just for safety
|
|
|
|
{
|
|
|
|
// copy faces
|
|
|
|
out->mFaces = new aiFace[out->mNumFaces];
|
2020-06-23 19:05:42 +00:00
|
|
|
aiFace *pf2 = out->mFaces;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
unsigned int ofs = 0;
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
|
|
|
|
for (unsigned int m = 0; m < (*it)->mNumFaces; ++m, ++pf2) {
|
|
|
|
aiFace &face = (*it)->mFaces[m];
|
2015-05-19 03:57:13 +00:00
|
|
|
pf2->mNumIndices = face.mNumIndices;
|
|
|
|
pf2->mIndices = face.mIndices;
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (ofs) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// add the offset to the vertex
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int q = 0; q < face.mNumIndices; ++q) {
|
2015-05-19 03:57:13 +00:00
|
|
|
face.mIndices[q] += ofs;
|
2020-06-23 19:05:42 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
face.mIndices = nullptr;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
ofs += (*it)->mNumVertices;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// bones - as this is quite lengthy, I moved the code to a separate function
|
|
|
|
if (out->mNumBones)
|
2020-06-23 19:05:42 +00:00
|
|
|
MergeBones(out, begin, end);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// delete all source meshes
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it)
|
2015-05-19 03:57:13 +00:00
|
|
|
delete *it;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::MergeMaterials(aiMaterial **dest,
|
|
|
|
std::vector<aiMaterial *>::const_iterator begin,
|
|
|
|
std::vector<aiMaterial *>::const_iterator end) {
|
|
|
|
if (nullptr == dest) {
|
2017-11-19 16:41:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (begin == end) {
|
|
|
|
*dest = nullptr; // no materials ...
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate the output material
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMaterial *out = *dest = new aiMaterial();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Get the maximal number of properties
|
|
|
|
unsigned int size = 0;
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::vector<aiMaterial *>::const_iterator it = begin; it != end; ++it) {
|
2015-05-19 03:57:13 +00:00
|
|
|
size += (*it)->mNumProperties;
|
|
|
|
}
|
|
|
|
|
|
|
|
out->Clear();
|
|
|
|
delete[] out->mProperties;
|
|
|
|
|
|
|
|
out->mNumAllocated = size;
|
|
|
|
out->mNumProperties = 0;
|
2020-06-23 19:05:42 +00:00
|
|
|
out->mProperties = new aiMaterialProperty *[out->mNumAllocated];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::vector<aiMaterial *>::const_iterator it = begin; it != end; ++it) {
|
|
|
|
for (unsigned int i = 0; i < (*it)->mNumProperties; ++i) {
|
|
|
|
aiMaterialProperty *sprop = (*it)->mProperties[i];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Test if we already have a matching property
|
2020-06-23 19:05:42 +00:00
|
|
|
const aiMaterialProperty *prop_exist;
|
|
|
|
if (aiGetMaterialProperty(out, sprop->mKey.C_Str(), sprop->mSemantic, sprop->mIndex, &prop_exist) != AI_SUCCESS) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// If not, we add it to the new material
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMaterialProperty *prop = out->mProperties[out->mNumProperties] = new aiMaterialProperty();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
prop->mDataLength = sprop->mDataLength;
|
|
|
|
prop->mData = new char[prop->mDataLength];
|
|
|
|
::memcpy(prop->mData, sprop->mData, prop->mDataLength);
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
prop->mIndex = sprop->mIndex;
|
2015-05-19 03:57:13 +00:00
|
|
|
prop->mSemantic = sprop->mSemantic;
|
2020-06-23 19:05:42 +00:00
|
|
|
prop->mKey = sprop->mKey;
|
|
|
|
prop->mType = sprop->mType;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
out->mNumProperties++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <typename Type>
|
2020-06-23 19:05:42 +00:00
|
|
|
inline void CopyPtrArray(Type **&dest, const Type *const *src, ai_uint num) {
|
2017-11-19 16:41:31 +00:00
|
|
|
if (!num) {
|
2020-06-23 19:05:42 +00:00
|
|
|
dest = nullptr;
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
dest = new Type *[num];
|
|
|
|
for (ai_uint i = 0; i < num; ++i) {
|
|
|
|
SceneCombiner::Copy(&dest[i], src[i]);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template <typename Type>
|
2020-06-23 19:05:42 +00:00
|
|
|
inline void GetArrayCopy(Type *&dest, ai_uint num) {
|
|
|
|
if (!dest) {
|
2017-11-19 16:41:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
Type *old = dest;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
dest = new Type[num];
|
|
|
|
::memcpy(dest, old, sizeof(Type) * num);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::CopySceneFlat(aiScene **_dest, const aiScene *src) {
|
|
|
|
if (nullptr == _dest || nullptr == src) {
|
2017-11-19 16:41:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// reuse the old scene or allocate a new?
|
|
|
|
if (*_dest) {
|
|
|
|
(*_dest)->~aiScene();
|
|
|
|
new (*_dest) aiScene();
|
2017-11-19 16:41:31 +00:00
|
|
|
} else {
|
|
|
|
*_dest = new aiScene();
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-04-26 17:14:30 +00:00
|
|
|
CopyScene(_dest, src, false);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::CopyScene(aiScene **_dest, const aiScene *src, bool allocate) {
|
|
|
|
if (nullptr == _dest || nullptr == src) {
|
2017-11-19 16:41:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
if (allocate) {
|
|
|
|
*_dest = new aiScene();
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
aiScene *dest = *_dest;
|
2018-03-20 13:09:07 +00:00
|
|
|
ai_assert(nullptr != dest);
|
|
|
|
|
|
|
|
// copy metadata
|
2020-06-23 19:05:42 +00:00
|
|
|
if (nullptr != src->mMetaData) {
|
|
|
|
dest->mMetaData = new aiMetadata(*src->mMetaData);
|
2018-03-20 13:09:07 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// copy animations
|
|
|
|
dest->mNumAnimations = src->mNumAnimations;
|
2020-06-23 19:05:42 +00:00
|
|
|
CopyPtrArray(dest->mAnimations, src->mAnimations,
|
|
|
|
dest->mNumAnimations);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// copy textures
|
|
|
|
dest->mNumTextures = src->mNumTextures;
|
2020-06-23 19:05:42 +00:00
|
|
|
CopyPtrArray(dest->mTextures, src->mTextures,
|
|
|
|
dest->mNumTextures);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// copy materials
|
|
|
|
dest->mNumMaterials = src->mNumMaterials;
|
2020-06-23 19:05:42 +00:00
|
|
|
CopyPtrArray(dest->mMaterials, src->mMaterials,
|
|
|
|
dest->mNumMaterials);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// copy lights
|
|
|
|
dest->mNumLights = src->mNumLights;
|
2020-06-23 19:05:42 +00:00
|
|
|
CopyPtrArray(dest->mLights, src->mLights,
|
|
|
|
dest->mNumLights);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// copy cameras
|
|
|
|
dest->mNumCameras = src->mNumCameras;
|
2020-06-23 19:05:42 +00:00
|
|
|
CopyPtrArray(dest->mCameras, src->mCameras,
|
|
|
|
dest->mNumCameras);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// copy meshes
|
|
|
|
dest->mNumMeshes = src->mNumMeshes;
|
2020-06-23 19:05:42 +00:00
|
|
|
CopyPtrArray(dest->mMeshes, src->mMeshes,
|
|
|
|
dest->mNumMeshes);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// now - copy the root node of the scene (deep copy, too)
|
2020-06-23 19:05:42 +00:00
|
|
|
Copy(&dest->mRootNode, src->mRootNode);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// and keep the flags ...
|
|
|
|
dest->mFlags = src->mFlags;
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// source private data might be nullptr if the scene is user-allocated (i.e. for use with the export API)
|
|
|
|
if (dest->mPrivate != nullptr) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ScenePriv(dest)->mPPStepsApplied = ScenePriv(src) ? ScenePriv(src)->mPPStepsApplied : 0;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::Copy(aiMesh **_dest, const aiMesh *src) {
|
|
|
|
if (nullptr == _dest || nullptr == src) {
|
2017-11-19 16:41:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMesh *dest = *_dest = new aiMesh();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// get a flat copy
|
2020-04-28 05:39:13 +00:00
|
|
|
*dest = *src;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// and reallocate all arrays
|
2020-06-23 19:05:42 +00:00
|
|
|
GetArrayCopy(dest->mVertices, dest->mNumVertices);
|
|
|
|
GetArrayCopy(dest->mNormals, dest->mNumVertices);
|
|
|
|
GetArrayCopy(dest->mTangents, dest->mNumVertices);
|
|
|
|
GetArrayCopy(dest->mBitangents, dest->mNumVertices);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
unsigned int n = 0;
|
2020-06-15 19:34:49 +00:00
|
|
|
while (dest->HasTextureCoords(n)) {
|
2020-06-23 19:05:42 +00:00
|
|
|
GetArrayCopy(dest->mTextureCoords[n++], dest->mNumVertices);
|
2020-06-15 19:34:49 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
n = 0;
|
2020-06-15 19:34:49 +00:00
|
|
|
while (dest->HasVertexColors(n)) {
|
2020-06-23 19:05:42 +00:00
|
|
|
GetArrayCopy(dest->mColors[n++], dest->mNumVertices);
|
2020-06-15 19:34:49 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// make a deep copy of all bones
|
2020-06-23 19:05:42 +00:00
|
|
|
CopyPtrArray(dest->mBones, dest->mBones, dest->mNumBones);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// make a deep copy of all faces
|
2020-06-23 19:05:42 +00:00
|
|
|
GetArrayCopy(dest->mFaces, dest->mNumFaces);
|
|
|
|
for (unsigned int i = 0; i < dest->mNumFaces; ++i) {
|
|
|
|
aiFace &f = dest->mFaces[i];
|
|
|
|
GetArrayCopy(f.mIndices, f.mNumIndices);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2019-08-28 17:42:49 +00:00
|
|
|
|
|
|
|
// make a deep copy of all blend shapes
|
|
|
|
CopyPtrArray(dest->mAnimMeshes, dest->mAnimMeshes, dest->mNumAnimMeshes);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::Copy(aiAnimMesh **_dest, const aiAnimMesh *src) {
|
2019-08-28 17:42:49 +00:00
|
|
|
if (nullptr == _dest || nullptr == src) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiAnimMesh *dest = *_dest = new aiAnimMesh();
|
2019-08-28 17:42:49 +00:00
|
|
|
|
|
|
|
// get a flat copy
|
2020-04-28 05:39:13 +00:00
|
|
|
*dest = *src;
|
2019-08-28 17:42:49 +00:00
|
|
|
|
|
|
|
// and reallocate all arrays
|
|
|
|
GetArrayCopy(dest->mVertices, dest->mNumVertices);
|
|
|
|
GetArrayCopy(dest->mNormals, dest->mNumVertices);
|
|
|
|
GetArrayCopy(dest->mTangents, dest->mNumVertices);
|
|
|
|
GetArrayCopy(dest->mBitangents, dest->mNumVertices);
|
|
|
|
|
|
|
|
unsigned int n = 0;
|
|
|
|
while (dest->HasTextureCoords(n))
|
|
|
|
GetArrayCopy(dest->mTextureCoords[n++], dest->mNumVertices);
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
while (dest->HasVertexColors(n))
|
|
|
|
GetArrayCopy(dest->mColors[n++], dest->mNumVertices);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::Copy(aiMaterial **_dest, const aiMaterial *src) {
|
|
|
|
if (nullptr == _dest || nullptr == src) {
|
2017-11-19 16:41:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMaterial *dest = (aiMaterial *)(*_dest = new aiMaterial());
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
dest->Clear();
|
|
|
|
delete[] dest->mProperties;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
dest->mNumAllocated = src->mNumAllocated;
|
|
|
|
dest->mNumProperties = src->mNumProperties;
|
|
|
|
dest->mProperties = new aiMaterialProperty *[dest->mNumAllocated];
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int i = 0; i < dest->mNumProperties; ++i) {
|
|
|
|
aiMaterialProperty *prop = dest->mProperties[i] = new aiMaterialProperty();
|
|
|
|
aiMaterialProperty *sprop = src->mProperties[i];
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
prop->mDataLength = sprop->mDataLength;
|
|
|
|
prop->mData = new char[prop->mDataLength];
|
2020-06-23 19:05:42 +00:00
|
|
|
::memcpy(prop->mData, sprop->mData, prop->mDataLength);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
prop->mIndex = sprop->mIndex;
|
2015-05-19 03:57:13 +00:00
|
|
|
prop->mSemantic = sprop->mSemantic;
|
2020-06-23 19:05:42 +00:00
|
|
|
prop->mKey = sprop->mKey;
|
|
|
|
prop->mType = sprop->mType;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::Copy(aiTexture **_dest, const aiTexture *src) {
|
|
|
|
if (nullptr == _dest || nullptr == src) {
|
2017-11-19 16:41:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiTexture *dest = *_dest = new aiTexture();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// get a flat copy
|
2020-04-28 05:39:13 +00:00
|
|
|
*dest = *src;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// and reallocate all arrays. We must do it manually here
|
2020-06-23 19:05:42 +00:00
|
|
|
const char *old = (const char *)dest->pcData;
|
|
|
|
if (old) {
|
2015-05-19 03:57:13 +00:00
|
|
|
unsigned int cpy;
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!dest->mHeight)
|
|
|
|
cpy = dest->mWidth;
|
|
|
|
else
|
|
|
|
cpy = dest->mHeight * dest->mWidth * sizeof(aiTexel);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!cpy) {
|
|
|
|
dest->pcData = nullptr;
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// the cast is legal, the aiTexel c'tor does nothing important
|
2020-06-23 19:05:42 +00:00
|
|
|
dest->pcData = (aiTexel *)new char[cpy];
|
2015-05-19 03:57:13 +00:00
|
|
|
::memcpy(dest->pcData, old, cpy);
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::Copy(aiAnimation **_dest, const aiAnimation *src) {
|
|
|
|
if (nullptr == _dest || nullptr == src) {
|
2017-11-19 16:41:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-07-15 02:53:49 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiAnimation *dest = *_dest = new aiAnimation();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// get a flat copy
|
2020-04-28 05:39:13 +00:00
|
|
|
*dest = *src;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// and reallocate all arrays
|
2020-06-23 19:05:42 +00:00
|
|
|
CopyPtrArray(dest->mChannels, src->mChannels, dest->mNumChannels);
|
|
|
|
CopyPtrArray(dest->mMorphMeshChannels, src->mMorphMeshChannels, dest->mNumMorphMeshChannels);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::Copy(aiNodeAnim **_dest, const aiNodeAnim *src) {
|
|
|
|
if (nullptr == _dest || nullptr == src) {
|
2017-11-19 16:41:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiNodeAnim *dest = *_dest = new aiNodeAnim();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// get a flat copy
|
2020-04-28 05:39:13 +00:00
|
|
|
*dest = *src;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// and reallocate all arrays
|
2020-06-23 19:05:42 +00:00
|
|
|
GetArrayCopy(dest->mPositionKeys, dest->mNumPositionKeys);
|
|
|
|
GetArrayCopy(dest->mScalingKeys, dest->mNumScalingKeys);
|
|
|
|
GetArrayCopy(dest->mRotationKeys, dest->mNumRotationKeys);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::Copy(aiMeshMorphAnim **_dest, const aiMeshMorphAnim *src) {
|
|
|
|
if (nullptr == _dest || nullptr == src) {
|
2019-09-25 08:57:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMeshMorphAnim *dest = *_dest = new aiMeshMorphAnim();
|
2019-09-25 08:57:53 +00:00
|
|
|
|
|
|
|
// get a flat copy
|
2020-04-28 05:39:13 +00:00
|
|
|
*dest = *src;
|
2019-09-25 08:57:53 +00:00
|
|
|
|
|
|
|
// and reallocate all arrays
|
2020-06-23 19:05:42 +00:00
|
|
|
GetArrayCopy(dest->mKeys, dest->mNumKeys);
|
|
|
|
for (ai_uint i = 0; i < dest->mNumKeys; ++i) {
|
2019-09-25 08:57:53 +00:00
|
|
|
dest->mKeys[i].mValues = new unsigned int[dest->mKeys[i].mNumValuesAndWeights];
|
|
|
|
dest->mKeys[i].mWeights = new double[dest->mKeys[i].mNumValuesAndWeights];
|
|
|
|
::memcpy(dest->mKeys[i].mValues, src->mKeys[i].mValues, dest->mKeys[i].mNumValuesAndWeights * sizeof(unsigned int));
|
|
|
|
::memcpy(dest->mKeys[i].mWeights, src->mKeys[i].mWeights, dest->mKeys[i].mNumValuesAndWeights * sizeof(double));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::Copy(aiCamera **_dest, const aiCamera *src) {
|
|
|
|
if (nullptr == _dest || nullptr == src) {
|
2017-11-19 16:41:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiCamera *dest = *_dest = new aiCamera();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// get a flat copy, that's already OK
|
2020-04-28 05:39:13 +00:00
|
|
|
*dest = *src;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::Copy(aiLight **_dest, const aiLight *src) {
|
|
|
|
if (nullptr == _dest || nullptr == src) {
|
2017-11-19 16:41:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiLight *dest = *_dest = new aiLight();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// get a flat copy, that's already OK
|
2020-04-28 05:39:13 +00:00
|
|
|
*dest = *src;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::Copy(aiBone **_dest, const aiBone *src) {
|
|
|
|
if (nullptr == _dest || nullptr == src) {
|
2017-11-19 16:41:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiBone *dest = *_dest = new aiBone();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// get a flat copy
|
2020-04-28 05:39:13 +00:00
|
|
|
*dest = *src;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::Copy(aiNode **_dest, const aiNode *src) {
|
|
|
|
ai_assert(nullptr != _dest);
|
|
|
|
ai_assert(nullptr != src);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiNode *dest = *_dest = new aiNode();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// get a flat copy
|
2020-04-28 05:39:13 +00:00
|
|
|
*dest = *src;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
if (src->mMetaData) {
|
|
|
|
Copy(&dest->mMetaData, src->mMetaData);
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// and reallocate all arrays
|
2020-06-23 19:05:42 +00:00
|
|
|
GetArrayCopy(dest->mMeshes, dest->mNumMeshes);
|
|
|
|
CopyPtrArray(dest->mChildren, src->mChildren, dest->mNumChildren);
|
2016-04-18 09:28:21 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// need to set the mParent fields to the created aiNode.
|
|
|
|
for (unsigned int i = 0; i < dest->mNumChildren; i++) {
|
|
|
|
dest->mChildren[i]->mParent = dest;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void SceneCombiner::Copy(aiMetadata **_dest, const aiMetadata *src) {
|
|
|
|
if (nullptr == _dest || nullptr == src) {
|
2017-11-19 16:41:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (0 == src->mNumProperties) {
|
2017-11-19 18:05:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMetadata *dest = *_dest = aiMetadata::Alloc(src->mNumProperties);
|
2015-05-19 03:57:13 +00:00
|
|
|
std::copy(src->mKeys, src->mKeys + src->mNumProperties, dest->mKeys);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < src->mNumProperties; ++i) {
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMetadataEntry &in = src->mValues[i];
|
|
|
|
aiMetadataEntry &out = dest->mValues[i];
|
2015-05-19 03:57:13 +00:00
|
|
|
out.mType = in.mType;
|
|
|
|
switch (dest->mValues[i].mType) {
|
2020-06-23 19:05:42 +00:00
|
|
|
case AI_BOOL:
|
|
|
|
out.mData = new bool(*static_cast<bool *>(in.mData));
|
|
|
|
break;
|
|
|
|
case AI_INT32:
|
|
|
|
out.mData = new int32_t(*static_cast<int32_t *>(in.mData));
|
|
|
|
break;
|
|
|
|
case AI_UINT64:
|
|
|
|
out.mData = new uint64_t(*static_cast<uint64_t *>(in.mData));
|
|
|
|
break;
|
|
|
|
case AI_FLOAT:
|
|
|
|
out.mData = new float(*static_cast<float *>(in.mData));
|
|
|
|
break;
|
|
|
|
case AI_DOUBLE:
|
|
|
|
out.mData = new double(*static_cast<double *>(in.mData));
|
|
|
|
break;
|
|
|
|
case AI_AISTRING:
|
|
|
|
out.mData = new aiString(*static_cast<aiString *>(in.mData));
|
|
|
|
break;
|
|
|
|
case AI_AIVECTOR3D:
|
|
|
|
out.mData = new aiVector3D(*static_cast<aiVector3D *>(in.mData));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ai_assert(false);
|
|
|
|
break;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 19:34:49 +00:00
|
|
|
#if (__GNUC__ >= 8 && __GNUC_MINOR__ >= 0)
|
2020-06-23 19:05:42 +00:00
|
|
|
#pragma GCC diagnostic pop
|
2020-06-15 19:34:49 +00:00
|
|
|
#endif
|
2017-11-19 16:41:31 +00:00
|
|
|
|
|
|
|
} // Namespace Assimp
|