2
0

SceneCombiner.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. // TODO: refactor entire file to get rid of the "flat-copy" first approach
  34. // to copying structures. This easily breaks in the most unintuitive way
  35. // possible as new fields are added to assimp structures.
  36. // ----------------------------------------------------------------------------
  37. /**
  38. * @file Implements Assimp::SceneCombiner. This is a smart utility
  39. * class that combines multiple scenes, meshes, ... into one. Currently
  40. * these utilities are used by the IRR and LWS loaders and the
  41. * OptimizeGraph step.
  42. */
  43. // ----------------------------------------------------------------------------
  44. #include "ScenePrivate.h"
  45. #include <assimp/Hash.h>
  46. #include <assimp/SceneCombiner.h>
  47. #include <assimp/StringUtils.h>
  48. #include <assimp/fast_atof.h>
  49. #include <assimp/mesh.h>
  50. #include <assimp/metadata.h>
  51. #include <assimp/scene.h>
  52. #include <assimp/DefaultLogger.hpp>
  53. #include <unordered_set>
  54. #include <ctime>
  55. #include <cstdio>
  56. namespace Assimp {
  57. #if (__GNUC__ >= 8 && __GNUC_MINOR__ >= 0)
  58. #pragma GCC diagnostic push
  59. #pragma GCC diagnostic ignored "-Wclass-memaccess"
  60. #endif
  61. // ------------------------------------------------------------------------------------------------
  62. // Add a prefix to a string
  63. inline void PrefixString(aiString &string, const char *prefix, unsigned int len) {
  64. // If the string is already prefixed, we won't prefix it a second time
  65. if (string.length >= 1 && string.data[0] == '$')
  66. return;
  67. if (len + string.length >= AI_MAXLEN - 1) {
  68. ASSIMP_LOG_VERBOSE_DEBUG("Can't add an unique prefix because the string is too long");
  69. ai_assert(false);
  70. return;
  71. }
  72. // Add the prefix
  73. ::memmove(string.data + len, string.data, string.length + 1);
  74. ::memcpy(string.data, prefix, len);
  75. // And update the string's length
  76. string.length += len;
  77. }
  78. // ------------------------------------------------------------------------------------------------
  79. // Add node identifiers to a hashing set
  80. void SceneCombiner::AddNodeHashes(aiNode *node, std::set<unsigned int> &hashes) {
  81. if (node == nullptr) {
  82. ASSIMP_LOG_ERROR("Pointer to aiNode is nullptr.");
  83. return;
  84. }
  85. // Add node name to hashing set if it is non-empty - empty nodes are allowed
  86. // and they can't have any anims assigned so its absolutely safe to duplicate them.
  87. if (node->mName.length) {
  88. hashes.insert(SuperFastHash(node->mName.data, static_cast<uint32_t>(node->mName.length)));
  89. }
  90. // Process all children recursively
  91. for (unsigned int i = 0; i < node->mNumChildren; ++i) {
  92. AddNodeHashes(node->mChildren[i], hashes);
  93. }
  94. }
  95. // ------------------------------------------------------------------------------------------------
  96. // Add a name prefix to all nodes in a hierarchy
  97. void SceneCombiner::AddNodePrefixes(aiNode *node, const char *prefix, unsigned int len) {
  98. ai_assert(nullptr != prefix);
  99. PrefixString(node->mName, prefix, len);
  100. // Process all children recursively
  101. for (unsigned int i = 0; i < node->mNumChildren; ++i) {
  102. AddNodePrefixes(node->mChildren[i], prefix, len);
  103. }
  104. }
  105. // ------------------------------------------------------------------------------------------------
  106. // Search for matching names
  107. bool SceneCombiner::FindNameMatch(const aiString &name, std::vector<SceneHelper> &input, unsigned int cur) {
  108. const unsigned int hash = SuperFastHash(name.data, static_cast<uint32_t>(name.length));
  109. // Check whether we find a positive match in one of the given sets
  110. for (unsigned int i = 0; i < input.size(); ++i) {
  111. if (cur != i && input[i].hashes.find(hash) != input[i].hashes.end()) {
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117. // ------------------------------------------------------------------------------------------------
  118. // Add a name prefix to all nodes in a hierarchy if a hash match is found
  119. void SceneCombiner::AddNodePrefixesChecked(aiNode *node, const char *prefix, unsigned int len,
  120. std::vector<SceneHelper> &input, unsigned int cur) {
  121. ai_assert(nullptr != prefix);
  122. const unsigned int hash = SuperFastHash(node->mName.data, static_cast<uint32_t>(node->mName.length));
  123. // Check whether we find a positive match in one of the given sets
  124. for (unsigned int i = 0; i < input.size(); ++i) {
  125. if (cur != i && input[i].hashes.find(hash) != input[i].hashes.end()) {
  126. PrefixString(node->mName, prefix, len);
  127. break;
  128. }
  129. }
  130. // Process all children recursively
  131. for (unsigned int i = 0; i < node->mNumChildren; ++i) {
  132. AddNodePrefixesChecked(node->mChildren[i], prefix, len, input, cur);
  133. }
  134. }
  135. // ------------------------------------------------------------------------------------------------
  136. // Add an offset to all mesh indices in a node graph
  137. void SceneCombiner::OffsetNodeMeshIndices(aiNode *node, unsigned int offset) {
  138. for (unsigned int i = 0; i < node->mNumMeshes; ++i)
  139. node->mMeshes[i] += offset;
  140. for (unsigned int i = 0; i < node->mNumChildren; ++i) {
  141. OffsetNodeMeshIndices(node->mChildren[i], offset);
  142. }
  143. }
  144. // ------------------------------------------------------------------------------------------------
  145. // Merges two scenes. Currently only used by the LWS loader.
  146. void SceneCombiner::MergeScenes(aiScene **_dest, std::vector<aiScene *> &src, unsigned int flags) {
  147. if (nullptr == _dest) {
  148. return;
  149. }
  150. // if _dest points to nullptr allocate a new scene. Otherwise clear the old and reuse it
  151. if (src.empty()) {
  152. if (*_dest) {
  153. (*_dest)->~aiScene();
  154. SceneCombiner::CopySceneFlat(_dest, src[0]);
  155. } else
  156. *_dest = src[0];
  157. return;
  158. }
  159. if (*_dest) {
  160. (*_dest)->~aiScene();
  161. new (*_dest) aiScene();
  162. } else
  163. *_dest = new aiScene();
  164. // Create a dummy scene to serve as master for the others
  165. aiScene *master = new aiScene();
  166. master->mRootNode = new aiNode();
  167. master->mRootNode->mName.Set("<MergeRoot>");
  168. std::vector<AttachmentInfo> srcList(src.size());
  169. for (unsigned int i = 0; i < srcList.size(); ++i) {
  170. srcList[i] = AttachmentInfo(src[i], master->mRootNode);
  171. }
  172. // 'master' will be deleted afterwards
  173. MergeScenes(_dest, master, srcList, flags);
  174. }
  175. // ------------------------------------------------------------------------------------------------
  176. void SceneCombiner::AttachToGraph(aiNode *attach, std::vector<NodeAttachmentInfo> &srcList) {
  177. unsigned int cnt;
  178. for (cnt = 0; cnt < attach->mNumChildren; ++cnt) {
  179. AttachToGraph(attach->mChildren[cnt], srcList);
  180. }
  181. cnt = 0;
  182. for (std::vector<NodeAttachmentInfo>::iterator it = srcList.begin();
  183. it != srcList.end(); ++it) {
  184. if ((*it).attachToNode == attach && !(*it).resolved)
  185. ++cnt;
  186. }
  187. if (cnt) {
  188. aiNode **n = new aiNode *[cnt + attach->mNumChildren];
  189. if (attach->mNumChildren) {
  190. ::memcpy(n, attach->mChildren, sizeof(void *) * attach->mNumChildren);
  191. delete[] attach->mChildren;
  192. }
  193. attach->mChildren = n;
  194. n += attach->mNumChildren;
  195. attach->mNumChildren += cnt;
  196. for (unsigned int i = 0; i < srcList.size(); ++i) {
  197. NodeAttachmentInfo &att = srcList[i];
  198. if (att.attachToNode == attach && !att.resolved) {
  199. *n = att.node;
  200. (**n).mParent = attach;
  201. ++n;
  202. // mark this attachment as resolved
  203. att.resolved = true;
  204. }
  205. }
  206. }
  207. }
  208. // ------------------------------------------------------------------------------------------------
  209. void SceneCombiner::AttachToGraph(aiScene *master, std::vector<NodeAttachmentInfo> &src) {
  210. ai_assert(nullptr != master);
  211. AttachToGraph(master->mRootNode, src);
  212. }
  213. // ------------------------------------------------------------------------------------------------
  214. void SceneCombiner::MergeScenes(aiScene **_dest, aiScene *master, std::vector<AttachmentInfo> &srcList, unsigned int flags) {
  215. if (nullptr == _dest) {
  216. std::unordered_set<aiScene *> uniqueScenes;
  217. uniqueScenes.insert(master);
  218. for (const auto &item : srcList) {
  219. uniqueScenes.insert(item.scene);
  220. }
  221. for (const auto &item : uniqueScenes) {
  222. delete item;
  223. }
  224. return;
  225. }
  226. // if _dest points to nullptr allocate a new scene. Otherwise clear the old and reuse it
  227. if (srcList.empty()) {
  228. if (*_dest) {
  229. SceneCombiner::CopySceneFlat(_dest, master);
  230. delete master;
  231. } else
  232. *_dest = master;
  233. return;
  234. }
  235. if (*_dest) {
  236. (*_dest)->~aiScene();
  237. new (*_dest) aiScene();
  238. } else
  239. *_dest = new aiScene();
  240. aiScene *dest = *_dest;
  241. std::vector<SceneHelper> src(srcList.size() + 1);
  242. src[0].scene = master;
  243. for (unsigned int i = 0; i < srcList.size(); ++i) {
  244. src[i + 1] = SceneHelper(srcList[i].scene);
  245. }
  246. // this helper array specifies which scenes are duplicates of others
  247. std::vector<unsigned int> duplicates(src.size(), UINT_MAX);
  248. // this helper array is used as lookup table several times
  249. std::vector<unsigned int> offset(src.size());
  250. // Find duplicate scenes
  251. for (unsigned int i = 0; i < src.size(); ++i) {
  252. if (duplicates[i] != i && duplicates[i] != UINT_MAX) {
  253. continue;
  254. }
  255. duplicates[i] = i;
  256. for (unsigned int a = i + 1; a < src.size(); ++a) {
  257. if (src[i].scene == src[a].scene) {
  258. duplicates[a] = i;
  259. }
  260. }
  261. }
  262. // Generate unique names for all named stuff?
  263. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES) {
  264. #if 0
  265. // Construct a proper random number generator
  266. boost::mt19937 rng( );
  267. boost::uniform_int<> dist(1u,1 << 24u);
  268. boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rndGen(rng, dist);
  269. #endif
  270. for (unsigned int i = 1; i < src.size(); ++i) {
  271. src[i].idlen = ai_snprintf(src[i].id, 32, "$%.6X$_", i);
  272. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {
  273. // Compute hashes for all identifiers in this scene and store them
  274. // in a sorted table (for convenience I'm using std::set). We hash
  275. // just the node and animation channel names, all identifiers except
  276. // the material names should be caught by doing this.
  277. AddNodeHashes(src[i]->mRootNode, src[i].hashes);
  278. for (unsigned int a = 0; a < src[i]->mNumAnimations; ++a) {
  279. aiAnimation *anim = src[i]->mAnimations[a];
  280. src[i].hashes.insert(SuperFastHash(anim->mName.data, static_cast<uint32_t>(anim->mName.length)));
  281. }
  282. }
  283. }
  284. }
  285. unsigned int cnt;
  286. // First find out how large the respective output arrays must be
  287. for (unsigned int n = 0; n < src.size(); ++n) {
  288. SceneHelper *cur = &src[n];
  289. if (n == duplicates[n] || flags & AI_INT_MERGE_SCENE_DUPLICATES_DEEP_CPY) {
  290. dest->mNumTextures += (*cur)->mNumTextures;
  291. dest->mNumMaterials += (*cur)->mNumMaterials;
  292. dest->mNumMeshes += (*cur)->mNumMeshes;
  293. }
  294. dest->mNumLights += (*cur)->mNumLights;
  295. dest->mNumCameras += (*cur)->mNumCameras;
  296. dest->mNumAnimations += (*cur)->mNumAnimations;
  297. // Combine the flags of all scenes
  298. // We need to process them flag-by-flag here to get correct results
  299. // dest->mFlags ; //|= (*cur)->mFlags;
  300. if ((*cur)->mFlags & AI_SCENE_FLAGS_NON_VERBOSE_FORMAT) {
  301. dest->mFlags |= AI_SCENE_FLAGS_NON_VERBOSE_FORMAT;
  302. }
  303. }
  304. // generate the output texture list + an offset table for all texture indices
  305. if (dest->mNumTextures) {
  306. aiTexture **pip = dest->mTextures = new aiTexture *[dest->mNumTextures];
  307. cnt = 0;
  308. for (unsigned int n = 0; n < src.size(); ++n) {
  309. SceneHelper *cur = &src[n];
  310. for (unsigned int i = 0; i < (*cur)->mNumTextures; ++i) {
  311. if (n != duplicates[n]) {
  312. if (flags & AI_INT_MERGE_SCENE_DUPLICATES_DEEP_CPY)
  313. Copy(pip, (*cur)->mTextures[i]);
  314. else
  315. continue;
  316. } else
  317. *pip = (*cur)->mTextures[i];
  318. ++pip;
  319. }
  320. offset[n] = cnt;
  321. cnt = (unsigned int)(pip - dest->mTextures);
  322. }
  323. }
  324. // generate the output material list + an offset table for all material indices
  325. if (dest->mNumMaterials) {
  326. aiMaterial **pip = dest->mMaterials = new aiMaterial *[dest->mNumMaterials];
  327. cnt = 0;
  328. for (unsigned int n = 0; n < src.size(); ++n) {
  329. SceneHelper *cur = &src[n];
  330. for (unsigned int i = 0; i < (*cur)->mNumMaterials; ++i) {
  331. if (n != duplicates[n]) {
  332. if (flags & AI_INT_MERGE_SCENE_DUPLICATES_DEEP_CPY)
  333. Copy(pip, (*cur)->mMaterials[i]);
  334. else
  335. continue;
  336. } else
  337. *pip = (*cur)->mMaterials[i];
  338. if ((*cur)->mNumTextures != dest->mNumTextures) {
  339. // We need to update all texture indices of the mesh. So we need to search for
  340. // a material property called '$tex.file'
  341. for (unsigned int a = 0; a < (*pip)->mNumProperties; ++a) {
  342. aiMaterialProperty *prop = (*pip)->mProperties[a];
  343. if (!strncmp(prop->mKey.data, "$tex.file", 9)) {
  344. // Check whether this texture is an embedded texture.
  345. // In this case the property looks like this: *<n>,
  346. // where n is the index of the texture.
  347. // Copy here because we overwrite the string data in-place and the buffer inside of aiString
  348. // will be a lie if we just reinterpret from prop->mData. The size of mData is not guaranteed to be
  349. // AI_MAXLEN in size.
  350. aiString s(*(aiString *)prop->mData);
  351. if ('*' == s.data[0]) {
  352. // Offset the index and write it back ..
  353. const unsigned int idx = strtoul10(&s.data[1]) + offset[n];
  354. const unsigned int oldLen = s.length;
  355. s.length = 1 + ASSIMP_itoa10(&s.data[1], sizeof(s.data) - 1, idx);
  356. // The string changed in size so we need to reallocate the buffer for the property.
  357. if (oldLen < s.length) {
  358. prop->mDataLength += s.length - oldLen;
  359. delete[] prop->mData;
  360. prop->mData = new char[prop->mDataLength];
  361. }
  362. memcpy(prop->mData, static_cast<void*>(&s), prop->mDataLength);
  363. }
  364. }
  365. // Need to generate new, unique material names?
  366. else if (!::strcmp(prop->mKey.data, "$mat.name") && flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_MATNAMES) {
  367. aiString *pcSrc = (aiString *)prop->mData;
  368. PrefixString(*pcSrc, (*cur).id, (*cur).idlen);
  369. }
  370. }
  371. }
  372. ++pip;
  373. }
  374. offset[n] = cnt;
  375. cnt = (unsigned int)(pip - dest->mMaterials);
  376. }
  377. }
  378. // generate the output mesh list + again an offset table for all mesh indices
  379. if (dest->mNumMeshes) {
  380. aiMesh **pip = dest->mMeshes = new aiMesh *[dest->mNumMeshes];
  381. cnt = 0;
  382. for (unsigned int n = 0; n < src.size(); ++n) {
  383. SceneHelper *cur = &src[n];
  384. for (unsigned int i = 0; i < (*cur)->mNumMeshes; ++i) {
  385. if (n != duplicates[n]) {
  386. if (flags & AI_INT_MERGE_SCENE_DUPLICATES_DEEP_CPY)
  387. Copy(pip, (*cur)->mMeshes[i]);
  388. else
  389. continue;
  390. } else
  391. *pip = (*cur)->mMeshes[i];
  392. // update the material index of the mesh
  393. (*pip)->mMaterialIndex += offset[n];
  394. ++pip;
  395. }
  396. // reuse the offset array - store now the mesh offset in it
  397. offset[n] = cnt;
  398. cnt = (unsigned int)(pip - dest->mMeshes);
  399. }
  400. }
  401. std::vector<NodeAttachmentInfo> nodes;
  402. nodes.reserve(srcList.size());
  403. // ----------------------------------------------------------------------------
  404. // Now generate the output node graph. We need to make those
  405. // names in the graph that are referenced by anims or lights
  406. // or cameras unique. So we add a prefix to them ... $<rand>_
  407. // We could also use a counter, but using a random value allows us to
  408. // use just one prefix if we are joining multiple scene hierarchies recursively.
  409. // Chances are quite good we don't collide, so we try that ...
  410. // ----------------------------------------------------------------------------
  411. // Allocate space for light sources, cameras and animations
  412. aiLight **ppLights = dest->mLights = (dest->mNumLights ? new aiLight *[dest->mNumLights] : nullptr);
  413. aiCamera **ppCameras = dest->mCameras = (dest->mNumCameras ? new aiCamera *[dest->mNumCameras] : nullptr);
  414. aiAnimation **ppAnims = dest->mAnimations = (dest->mNumAnimations ? new aiAnimation *[dest->mNumAnimations] : nullptr);
  415. for (int n = static_cast<int>(src.size() - 1); n >= 0; --n) /* !!! important !!! */
  416. {
  417. SceneHelper *cur = &src[n];
  418. aiNode *node;
  419. // To offset or not to offset, this is the question
  420. if (n != (int)duplicates[n]) {
  421. // Get full scene-graph copy
  422. Copy(&node, (*cur)->mRootNode);
  423. OffsetNodeMeshIndices(node, offset[duplicates[n]]);
  424. if (flags & AI_INT_MERGE_SCENE_DUPLICATES_DEEP_CPY) {
  425. // (note:) they are already 'offseted' by offset[duplicates[n]]
  426. OffsetNodeMeshIndices(node, offset[n] - offset[duplicates[n]]);
  427. }
  428. } else // if (n == duplicates[n])
  429. {
  430. node = (*cur)->mRootNode;
  431. OffsetNodeMeshIndices(node, offset[n]);
  432. }
  433. if (n) // src[0] is the master node
  434. nodes.emplace_back(node, srcList[n - 1].attachToNode, n);
  435. // add name prefixes?
  436. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES) {
  437. // or the whole scenegraph
  438. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {
  439. AddNodePrefixesChecked(node, (*cur).id, (*cur).idlen, src, n);
  440. } else
  441. AddNodePrefixes(node, (*cur).id, (*cur).idlen);
  442. // meshes
  443. for (unsigned int i = 0; i < (*cur)->mNumMeshes; ++i) {
  444. aiMesh *mesh = (*cur)->mMeshes[i];
  445. // rename all bones
  446. for (unsigned int a = 0; a < mesh->mNumBones; ++a) {
  447. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {
  448. if (!FindNameMatch(mesh->mBones[a]->mName, src, n))
  449. continue;
  450. }
  451. PrefixString(mesh->mBones[a]->mName, (*cur).id, (*cur).idlen);
  452. }
  453. }
  454. }
  455. // --------------------------------------------------------------------
  456. // Copy light sources
  457. for (unsigned int i = 0; i < (*cur)->mNumLights; ++i, ++ppLights) {
  458. if (n != (int)duplicates[n]) // duplicate scene?
  459. {
  460. Copy(ppLights, (*cur)->mLights[i]);
  461. } else
  462. *ppLights = (*cur)->mLights[i];
  463. // Add name prefixes?
  464. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES) {
  465. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {
  466. if (!FindNameMatch((*ppLights)->mName, src, n))
  467. continue;
  468. }
  469. PrefixString((*ppLights)->mName, (*cur).id, (*cur).idlen);
  470. }
  471. }
  472. // --------------------------------------------------------------------
  473. // Copy cameras
  474. for (unsigned int i = 0; i < (*cur)->mNumCameras; ++i, ++ppCameras) {
  475. if (n != (int)duplicates[n]) // duplicate scene?
  476. {
  477. Copy(ppCameras, (*cur)->mCameras[i]);
  478. } else
  479. *ppCameras = (*cur)->mCameras[i];
  480. // Add name prefixes?
  481. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES) {
  482. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {
  483. if (!FindNameMatch((*ppCameras)->mName, src, n))
  484. continue;
  485. }
  486. PrefixString((*ppCameras)->mName, (*cur).id, (*cur).idlen);
  487. }
  488. }
  489. // --------------------------------------------------------------------
  490. // Copy animations
  491. for (unsigned int i = 0; i < (*cur)->mNumAnimations; ++i, ++ppAnims) {
  492. if (n != (int)duplicates[n]) // duplicate scene?
  493. {
  494. Copy(ppAnims, (*cur)->mAnimations[i]);
  495. } else
  496. *ppAnims = (*cur)->mAnimations[i];
  497. // Add name prefixes?
  498. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES) {
  499. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {
  500. if (!FindNameMatch((*ppAnims)->mName, src, n))
  501. continue;
  502. }
  503. PrefixString((*ppAnims)->mName, (*cur).id, (*cur).idlen);
  504. // don't forget to update all node animation channels
  505. for (unsigned int a = 0; a < (*ppAnims)->mNumChannels; ++a) {
  506. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {
  507. if (!FindNameMatch((*ppAnims)->mChannels[a]->mNodeName, src, n))
  508. continue;
  509. }
  510. PrefixString((*ppAnims)->mChannels[a]->mNodeName, (*cur).id, (*cur).idlen);
  511. }
  512. }
  513. }
  514. }
  515. // Now build the output graph
  516. AttachToGraph(master, nodes);
  517. dest->mRootNode = master->mRootNode;
  518. // Check whether we succeeded at building the output graph
  519. for (std::vector<NodeAttachmentInfo>::iterator it = nodes.begin();
  520. it != nodes.end(); ++it) {
  521. if (!(*it).resolved) {
  522. if (flags & AI_INT_MERGE_SCENE_RESOLVE_CROSS_ATTACHMENTS) {
  523. // search for this attachment point in all other imported scenes, too.
  524. for (unsigned int n = 0; n < src.size(); ++n) {
  525. if (n != (*it).src_idx) {
  526. AttachToGraph(src[n].scene, nodes);
  527. if ((*it).resolved)
  528. break;
  529. }
  530. }
  531. }
  532. if (!(*it).resolved) {
  533. ASSIMP_LOG_ERROR("SceneCombiner: Failed to resolve attachment ", (*it).node->mName.data,
  534. " ", (*it).attachToNode->mName.data);
  535. }
  536. }
  537. }
  538. // now delete all input scenes. Make sure duplicate scenes aren't
  539. // deleted more than one time
  540. for (unsigned int n = 0; n < src.size(); ++n) {
  541. if (n != duplicates[n]) // duplicate scene?
  542. continue;
  543. aiScene *deleteMe = src[n].scene;
  544. // We need to delete the arrays before the destructor is called -
  545. // we are reusing the array members
  546. delete[] deleteMe->mMeshes;
  547. deleteMe->mMeshes = nullptr;
  548. delete[] deleteMe->mCameras;
  549. deleteMe->mCameras = nullptr;
  550. delete[] deleteMe->mLights;
  551. deleteMe->mLights = nullptr;
  552. delete[] deleteMe->mMaterials;
  553. deleteMe->mMaterials = nullptr;
  554. delete[] deleteMe->mAnimations;
  555. deleteMe->mAnimations = nullptr;
  556. delete[] deleteMe->mTextures;
  557. deleteMe->mTextures = nullptr;
  558. deleteMe->mRootNode = nullptr;
  559. // Now we can safely delete the scene
  560. delete deleteMe;
  561. }
  562. // Check flags
  563. if (!dest->mNumMeshes || !dest->mNumMaterials) {
  564. dest->mFlags |= AI_SCENE_FLAGS_INCOMPLETE;
  565. }
  566. // We're finished
  567. }
  568. // ------------------------------------------------------------------------------------------------
  569. // Build a list of unique bones
  570. void SceneCombiner::BuildUniqueBoneList(std::list<BoneWithHash> &asBones,
  571. std::vector<aiMesh *>::const_iterator it,
  572. std::vector<aiMesh *>::const_iterator end) {
  573. unsigned int iOffset = 0;
  574. for (; it != end; ++it) {
  575. for (unsigned int l = 0; l < (*it)->mNumBones; ++l) {
  576. aiBone *p = (*it)->mBones[l];
  577. uint32_t itml = SuperFastHash(p->mName.data, (unsigned int)p->mName.length);
  578. std::list<BoneWithHash>::iterator it2 = asBones.begin();
  579. std::list<BoneWithHash>::iterator end2 = asBones.end();
  580. for (; it2 != end2; ++it2) {
  581. if ((*it2).first == itml) {
  582. (*it2).pSrcBones.emplace_back(p, iOffset);
  583. break;
  584. }
  585. }
  586. if (end2 == it2) {
  587. // need to begin a new bone entry
  588. asBones.emplace_back();
  589. BoneWithHash &btz = asBones.back();
  590. // setup members
  591. btz.first = itml;
  592. btz.second = &p->mName;
  593. btz.pSrcBones.emplace_back(p, iOffset);
  594. }
  595. }
  596. iOffset += (*it)->mNumVertices;
  597. }
  598. }
  599. // ------------------------------------------------------------------------------------------------
  600. // Merge a list of bones
  601. void SceneCombiner::MergeBones(aiMesh *out, std::vector<aiMesh *>::const_iterator it,
  602. std::vector<aiMesh *>::const_iterator end) {
  603. if (nullptr == out || out->mNumBones == 0) {
  604. return;
  605. }
  606. // find we need to build an unique list of all bones.
  607. // we work with hashes to make the comparisons MUCH faster,
  608. // at least if we have many bones.
  609. std::list<BoneWithHash> asBones;
  610. BuildUniqueBoneList(asBones, it, end);
  611. // now create the output bones
  612. out->mNumBones = 0;
  613. out->mBones = new aiBone *[asBones.size()];
  614. for (std::list<BoneWithHash>::const_iterator boneIt = asBones.begin(), boneEnd = asBones.end(); boneIt != boneEnd; ++boneIt) {
  615. // Allocate a bone and setup it's name
  616. aiBone *pc = out->mBones[out->mNumBones++] = new aiBone();
  617. pc->mName = aiString(*(boneIt->second));
  618. std::vector<BoneSrcIndex>::const_iterator wend = boneIt->pSrcBones.end();
  619. // Loop through all bones to be joined for this bone
  620. for (std::vector<BoneSrcIndex>::const_iterator wmit = boneIt->pSrcBones.begin(); wmit != wend; ++wmit) {
  621. pc->mNumWeights += (*wmit).first->mNumWeights;
  622. // NOTE: different offset matrices for bones with equal names
  623. // are - at the moment - not handled correctly.
  624. if (wmit != boneIt->pSrcBones.begin() && pc->mOffsetMatrix != wmit->first->mOffsetMatrix) {
  625. ASSIMP_LOG_WARN("Bones with equal names but different offset matrices can't be joined at the moment");
  626. continue;
  627. }
  628. pc->mOffsetMatrix = wmit->first->mOffsetMatrix;
  629. }
  630. // Allocate the vertex weight array
  631. aiVertexWeight *avw = pc->mWeights = new aiVertexWeight[pc->mNumWeights];
  632. // And copy the final weights - adjust the vertex IDs by the
  633. // face index offset of the corresponding mesh.
  634. for (std::vector<BoneSrcIndex>::const_iterator wmit = (*boneIt).pSrcBones.begin(); wmit != (*boneIt).pSrcBones.end(); ++wmit) {
  635. if (wmit == wend) {
  636. break;
  637. }
  638. aiBone *pip = (*wmit).first;
  639. for (unsigned int mp = 0; mp < pip->mNumWeights; ++mp, ++avw) {
  640. const aiVertexWeight &vfi = pip->mWeights[mp];
  641. avw->mWeight = vfi.mWeight;
  642. avw->mVertexId = vfi.mVertexId + (*wmit).second;
  643. }
  644. }
  645. }
  646. }
  647. // ------------------------------------------------------------------------------------------------
  648. // Merge a list of meshes
  649. void SceneCombiner::MergeMeshes(aiMesh **_out, unsigned int /*flags*/,
  650. std::vector<aiMesh *>::const_iterator begin,
  651. std::vector<aiMesh *>::const_iterator end) {
  652. if (nullptr == _out) {
  653. return;
  654. }
  655. if (begin == end) {
  656. *_out = nullptr; // no meshes ...
  657. return;
  658. }
  659. // Allocate the output mesh
  660. aiMesh *out = *_out = new aiMesh();
  661. out->mMaterialIndex = (*begin)->mMaterialIndex;
  662. std::string name;
  663. // Find out how much output storage we'll need
  664. for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
  665. const char *meshName((*it)->mName.C_Str());
  666. name += std::string(meshName);
  667. if (it != end - 1) {
  668. name += ".";
  669. }
  670. out->mNumVertices += (*it)->mNumVertices;
  671. out->mNumFaces += (*it)->mNumFaces;
  672. out->mNumBones += (*it)->mNumBones;
  673. // combine primitive type flags
  674. out->mPrimitiveTypes |= (*it)->mPrimitiveTypes;
  675. }
  676. out->mName.Set(name.c_str());
  677. if (out->mNumVertices) {
  678. aiVector3D *pv2;
  679. // copy vertex positions
  680. if ((**begin).HasPositions()) {
  681. pv2 = out->mVertices = new aiVector3D[out->mNumVertices];
  682. for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
  683. if ((*it)->mVertices) {
  684. ::memcpy(pv2, (*it)->mVertices, (*it)->mNumVertices * sizeof(aiVector3D));
  685. } else
  686. ASSIMP_LOG_WARN("JoinMeshes: Positions expected but input mesh contains no positions");
  687. pv2 += (*it)->mNumVertices;
  688. }
  689. }
  690. // copy normals
  691. if ((**begin).HasNormals()) {
  692. pv2 = out->mNormals = new aiVector3D[out->mNumVertices];
  693. for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
  694. if ((*it)->mNormals) {
  695. ::memcpy(pv2, (*it)->mNormals, (*it)->mNumVertices * sizeof(aiVector3D));
  696. } else {
  697. ASSIMP_LOG_WARN("JoinMeshes: Normals expected but input mesh contains no normals");
  698. }
  699. pv2 += (*it)->mNumVertices;
  700. }
  701. }
  702. // copy tangents and bi-tangents
  703. if ((**begin).HasTangentsAndBitangents()) {
  704. pv2 = out->mTangents = new aiVector3D[out->mNumVertices];
  705. aiVector3D *pv2b = out->mBitangents = new aiVector3D[out->mNumVertices];
  706. for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
  707. if ((*it)->mTangents) {
  708. ::memcpy(pv2, (*it)->mTangents, (*it)->mNumVertices * sizeof(aiVector3D));
  709. ::memcpy(pv2b, (*it)->mBitangents, (*it)->mNumVertices * sizeof(aiVector3D));
  710. } else {
  711. ASSIMP_LOG_WARN("JoinMeshes: Tangents expected but input mesh contains no tangents");
  712. }
  713. pv2 += (*it)->mNumVertices;
  714. pv2b += (*it)->mNumVertices;
  715. }
  716. }
  717. // copy texture coordinates
  718. unsigned int n = 0;
  719. while ((**begin).HasTextureCoords(n)) {
  720. out->mNumUVComponents[n] = (*begin)->mNumUVComponents[n];
  721. pv2 = out->mTextureCoords[n] = new aiVector3D[out->mNumVertices];
  722. for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
  723. if ((*it)->mTextureCoords[n]) {
  724. ::memcpy(pv2, (*it)->mTextureCoords[n], (*it)->mNumVertices * sizeof(aiVector3D));
  725. } else {
  726. ASSIMP_LOG_WARN("JoinMeshes: UVs expected but input mesh contains no UVs");
  727. }
  728. pv2 += (*it)->mNumVertices;
  729. }
  730. ++n;
  731. }
  732. // copy vertex colors
  733. n = 0;
  734. while ((**begin).HasVertexColors(n)) {
  735. aiColor4D *pVec2 = out->mColors[n] = new aiColor4D[out->mNumVertices];
  736. for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
  737. if ((*it)->mColors[n]) {
  738. ::memcpy(pVec2, (*it)->mColors[n], (*it)->mNumVertices * sizeof(aiColor4D));
  739. } else {
  740. ASSIMP_LOG_WARN("JoinMeshes: VCs expected but input mesh contains no VCs");
  741. }
  742. pVec2 += (*it)->mNumVertices;
  743. }
  744. ++n;
  745. }
  746. }
  747. if (out->mNumFaces) // just for safety
  748. {
  749. // copy faces
  750. out->mFaces = new aiFace[out->mNumFaces];
  751. aiFace *pf2 = out->mFaces;
  752. unsigned int ofs = 0;
  753. for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
  754. for (unsigned int m = 0; m < (*it)->mNumFaces; ++m, ++pf2) {
  755. aiFace &face = (*it)->mFaces[m];
  756. pf2->mNumIndices = face.mNumIndices;
  757. pf2->mIndices = face.mIndices;
  758. if (ofs) {
  759. // add the offset to the vertex
  760. for (unsigned int q = 0; q < face.mNumIndices; ++q) {
  761. face.mIndices[q] += ofs;
  762. }
  763. }
  764. face.mIndices = nullptr;
  765. }
  766. ofs += (*it)->mNumVertices;
  767. }
  768. }
  769. // bones - as this is quite lengthy, I moved the code to a separate function
  770. if (out->mNumBones)
  771. MergeBones(out, begin, end);
  772. // delete all source meshes
  773. for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it)
  774. delete *it;
  775. }
  776. // ------------------------------------------------------------------------------------------------
  777. void SceneCombiner::MergeMaterials(aiMaterial **dest,
  778. std::vector<aiMaterial *>::const_iterator begin,
  779. std::vector<aiMaterial *>::const_iterator end) {
  780. if (nullptr == dest) {
  781. return;
  782. }
  783. if (begin == end) {
  784. *dest = nullptr; // no materials ...
  785. return;
  786. }
  787. // Allocate the output material
  788. aiMaterial *out = *dest = new aiMaterial();
  789. // Get the maximal number of properties
  790. unsigned int size = 0;
  791. for (std::vector<aiMaterial *>::const_iterator it = begin; it != end; ++it) {
  792. size += (*it)->mNumProperties;
  793. }
  794. out->Clear();
  795. delete[] out->mProperties;
  796. out->mNumAllocated = size;
  797. out->mNumProperties = 0;
  798. out->mProperties = new aiMaterialProperty *[out->mNumAllocated];
  799. for (std::vector<aiMaterial *>::const_iterator it = begin; it != end; ++it) {
  800. for (unsigned int i = 0; i < (*it)->mNumProperties; ++i) {
  801. aiMaterialProperty *sprop = (*it)->mProperties[i];
  802. // Test if we already have a matching property
  803. const aiMaterialProperty *prop_exist;
  804. if (aiGetMaterialProperty(out, sprop->mKey.C_Str(), sprop->mSemantic, sprop->mIndex, &prop_exist) != AI_SUCCESS) {
  805. // If not, we add it to the new material
  806. aiMaterialProperty *prop = out->mProperties[out->mNumProperties] = new aiMaterialProperty();
  807. prop->mDataLength = sprop->mDataLength;
  808. prop->mData = new char[prop->mDataLength];
  809. ::memcpy(prop->mData, sprop->mData, prop->mDataLength);
  810. prop->mIndex = sprop->mIndex;
  811. prop->mSemantic = sprop->mSemantic;
  812. prop->mKey = sprop->mKey;
  813. prop->mType = sprop->mType;
  814. out->mNumProperties++;
  815. }
  816. }
  817. }
  818. }
  819. // ------------------------------------------------------------------------------------------------
  820. template <typename Type>
  821. inline void CopyPtrArray(Type **&dest, const Type *const *src, ai_uint num) {
  822. if (!num) {
  823. dest = nullptr;
  824. return;
  825. }
  826. dest = new Type *[num];
  827. for (ai_uint i = 0; i < num; ++i) {
  828. SceneCombiner::Copy(&dest[i], src[i]);
  829. }
  830. }
  831. // ------------------------------------------------------------------------------------------------
  832. template <typename Type>
  833. inline void GetArrayCopy(Type *&dest, ai_uint num) {
  834. if (!dest) {
  835. return;
  836. }
  837. Type *old = dest;
  838. dest = new Type[num];
  839. std::copy(old, old+num, dest);
  840. }
  841. // ------------------------------------------------------------------------------------------------
  842. void SceneCombiner::CopySceneFlat(aiScene **_dest, const aiScene *src) {
  843. if (nullptr == _dest || nullptr == src) {
  844. return;
  845. }
  846. // reuse the old scene or allocate a new?
  847. if (*_dest) {
  848. (*_dest)->~aiScene();
  849. new (*_dest) aiScene();
  850. } else {
  851. *_dest = new aiScene();
  852. }
  853. CopyScene(_dest, src, false);
  854. }
  855. // ------------------------------------------------------------------------------------------------
  856. void SceneCombiner::CopyScene(aiScene **_dest, const aiScene *src, bool allocate) {
  857. if (nullptr == _dest || nullptr == src) {
  858. return;
  859. }
  860. if (allocate) {
  861. *_dest = new aiScene();
  862. }
  863. aiScene *dest = *_dest;
  864. ai_assert(nullptr != dest);
  865. // copy metadata
  866. if (nullptr != src->mMetaData) {
  867. dest->mMetaData = new aiMetadata(*src->mMetaData);
  868. }
  869. // copy animations
  870. dest->mNumAnimations = src->mNumAnimations;
  871. CopyPtrArray(dest->mAnimations, src->mAnimations,
  872. dest->mNumAnimations);
  873. // copy textures
  874. dest->mNumTextures = src->mNumTextures;
  875. CopyPtrArray(dest->mTextures, src->mTextures,
  876. dest->mNumTextures);
  877. // copy materials
  878. dest->mNumMaterials = src->mNumMaterials;
  879. CopyPtrArray(dest->mMaterials, src->mMaterials,
  880. dest->mNumMaterials);
  881. // copy lights
  882. dest->mNumLights = src->mNumLights;
  883. CopyPtrArray(dest->mLights, src->mLights,
  884. dest->mNumLights);
  885. // copy cameras
  886. dest->mNumCameras = src->mNumCameras;
  887. CopyPtrArray(dest->mCameras, src->mCameras,
  888. dest->mNumCameras);
  889. // copy meshes
  890. dest->mNumMeshes = src->mNumMeshes;
  891. CopyPtrArray(dest->mMeshes, src->mMeshes,
  892. dest->mNumMeshes);
  893. // now - copy the root node of the scene (deep copy, too)
  894. Copy(&dest->mRootNode, src->mRootNode);
  895. // and keep the flags ...
  896. dest->mFlags = src->mFlags;
  897. // source private data might be nullptr if the scene is user-allocated (i.e. for use with the export API)
  898. if (src->mPrivate != nullptr) {
  899. ScenePriv(dest)->mPPStepsApplied = ScenePriv(src) ? ScenePriv(src)->mPPStepsApplied : 0;
  900. }
  901. }
  902. // ------------------------------------------------------------------------------------------------
  903. void SceneCombiner::Copy(aiMesh **_dest, const aiMesh *src) {
  904. if (nullptr == _dest || nullptr == src) {
  905. return;
  906. }
  907. aiMesh *dest = *_dest = new aiMesh();
  908. // get a flat copy
  909. *dest = *src;
  910. // and reallocate all arrays
  911. GetArrayCopy(dest->mVertices, dest->mNumVertices);
  912. GetArrayCopy(dest->mNormals, dest->mNumVertices);
  913. GetArrayCopy(dest->mTangents, dest->mNumVertices);
  914. GetArrayCopy(dest->mBitangents, dest->mNumVertices);
  915. unsigned int n = 0;
  916. while (dest->HasTextureCoords(n)) {
  917. GetArrayCopy(dest->mTextureCoords[n++], dest->mNumVertices);
  918. }
  919. n = 0;
  920. while (dest->HasVertexColors(n)) {
  921. GetArrayCopy(dest->mColors[n++], dest->mNumVertices);
  922. }
  923. // make a deep copy of all bones
  924. CopyPtrArray(dest->mBones, dest->mBones, dest->mNumBones);
  925. // make a deep copy of all faces
  926. GetArrayCopy(dest->mFaces, dest->mNumFaces);
  927. // make a deep copy of all blend shapes
  928. CopyPtrArray(dest->mAnimMeshes, dest->mAnimMeshes, dest->mNumAnimMeshes);
  929. // make a deep copy of all texture coordinate names
  930. if (src->mTextureCoordsNames != nullptr) {
  931. dest->mTextureCoordsNames = new aiString *[AI_MAX_NUMBER_OF_TEXTURECOORDS] {};
  932. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
  933. Copy(&dest->mTextureCoordsNames[i], src->mTextureCoordsNames[i]);
  934. }
  935. }
  936. }
  937. // ------------------------------------------------------------------------------------------------
  938. void SceneCombiner::Copy(aiAnimMesh **_dest, const aiAnimMesh *src) {
  939. if (nullptr == _dest || nullptr == src) {
  940. return;
  941. }
  942. aiAnimMesh *dest = *_dest = new aiAnimMesh();
  943. // get a flat copy
  944. *dest = *src;
  945. // and reallocate all arrays
  946. GetArrayCopy(dest->mVertices, dest->mNumVertices);
  947. GetArrayCopy(dest->mNormals, dest->mNumVertices);
  948. GetArrayCopy(dest->mTangents, dest->mNumVertices);
  949. GetArrayCopy(dest->mBitangents, dest->mNumVertices);
  950. unsigned int n = 0;
  951. while (dest->HasTextureCoords(n))
  952. GetArrayCopy(dest->mTextureCoords[n++], dest->mNumVertices);
  953. n = 0;
  954. while (dest->HasVertexColors(n))
  955. GetArrayCopy(dest->mColors[n++], dest->mNumVertices);
  956. }
  957. // ------------------------------------------------------------------------------------------------
  958. void SceneCombiner::Copy(aiMaterial **_dest, const aiMaterial *src) {
  959. if (nullptr == _dest || nullptr == src) {
  960. return;
  961. }
  962. aiMaterial *dest = (aiMaterial *)(*_dest = new aiMaterial());
  963. dest->Clear();
  964. delete[] dest->mProperties;
  965. dest->mNumAllocated = src->mNumAllocated;
  966. dest->mNumProperties = src->mNumProperties;
  967. dest->mProperties = new aiMaterialProperty *[dest->mNumAllocated];
  968. for (unsigned int i = 0; i < dest->mNumProperties; ++i) {
  969. aiMaterialProperty *prop = dest->mProperties[i] = new aiMaterialProperty();
  970. aiMaterialProperty *sprop = src->mProperties[i];
  971. prop->mDataLength = sprop->mDataLength;
  972. prop->mData = new char[prop->mDataLength];
  973. ::memcpy(prop->mData, sprop->mData, prop->mDataLength);
  974. prop->mIndex = sprop->mIndex;
  975. prop->mSemantic = sprop->mSemantic;
  976. prop->mKey = sprop->mKey;
  977. prop->mType = sprop->mType;
  978. }
  979. }
  980. // ------------------------------------------------------------------------------------------------
  981. void SceneCombiner::Copy(aiTexture **_dest, const aiTexture *src) {
  982. if (nullptr == _dest || nullptr == src) {
  983. return;
  984. }
  985. aiTexture *dest = *_dest = new aiTexture();
  986. // get a flat copy
  987. *dest = *src;
  988. // and reallocate all arrays. We must do it manually here
  989. const char *old = (const char *)dest->pcData;
  990. if (old) {
  991. unsigned int cpy;
  992. if (!dest->mHeight)
  993. cpy = dest->mWidth;
  994. else
  995. cpy = dest->mHeight * dest->mWidth * sizeof(aiTexel);
  996. if (!cpy) {
  997. dest->pcData = nullptr;
  998. return;
  999. }
  1000. // the cast is legal, the aiTexel c'tor does nothing important
  1001. dest->pcData = (aiTexel *)new char[cpy];
  1002. ::memcpy(dest->pcData, old, cpy);
  1003. }
  1004. }
  1005. // ------------------------------------------------------------------------------------------------
  1006. void SceneCombiner::Copy(aiAnimation **_dest, const aiAnimation *src) {
  1007. if (nullptr == _dest || nullptr == src) {
  1008. return;
  1009. }
  1010. aiAnimation *dest = *_dest = new aiAnimation();
  1011. // get a flat copy
  1012. *dest = *src;
  1013. // and reallocate all arrays
  1014. CopyPtrArray(dest->mChannels, src->mChannels, dest->mNumChannels);
  1015. CopyPtrArray(dest->mMorphMeshChannels, src->mMorphMeshChannels, dest->mNumMorphMeshChannels);
  1016. }
  1017. // ------------------------------------------------------------------------------------------------
  1018. void SceneCombiner::Copy(aiNodeAnim **_dest, const aiNodeAnim *src) {
  1019. if (nullptr == _dest || nullptr == src) {
  1020. return;
  1021. }
  1022. aiNodeAnim *dest = *_dest = new aiNodeAnim();
  1023. // get a flat copy
  1024. *dest = *src;
  1025. // and reallocate all arrays
  1026. GetArrayCopy(dest->mPositionKeys, dest->mNumPositionKeys);
  1027. GetArrayCopy(dest->mScalingKeys, dest->mNumScalingKeys);
  1028. GetArrayCopy(dest->mRotationKeys, dest->mNumRotationKeys);
  1029. }
  1030. void SceneCombiner::Copy(aiMeshMorphAnim **_dest, const aiMeshMorphAnim *src) {
  1031. if (nullptr == _dest || nullptr == src) {
  1032. return;
  1033. }
  1034. aiMeshMorphAnim *dest = *_dest = new aiMeshMorphAnim();
  1035. // get a flat copy
  1036. *dest = *src;
  1037. // and reallocate all arrays
  1038. GetArrayCopy(dest->mKeys, dest->mNumKeys);
  1039. for (ai_uint i = 0; i < dest->mNumKeys; ++i) {
  1040. dest->mKeys[i].mValues = new unsigned int[dest->mKeys[i].mNumValuesAndWeights];
  1041. dest->mKeys[i].mWeights = new double[dest->mKeys[i].mNumValuesAndWeights];
  1042. ::memcpy(dest->mKeys[i].mValues, src->mKeys[i].mValues, dest->mKeys[i].mNumValuesAndWeights * sizeof(unsigned int));
  1043. ::memcpy(dest->mKeys[i].mWeights, src->mKeys[i].mWeights, dest->mKeys[i].mNumValuesAndWeights * sizeof(double));
  1044. }
  1045. }
  1046. // ------------------------------------------------------------------------------------------------
  1047. void SceneCombiner::Copy(aiCamera **_dest, const aiCamera *src) {
  1048. if (nullptr == _dest || nullptr == src) {
  1049. return;
  1050. }
  1051. aiCamera *dest = *_dest = new aiCamera();
  1052. // get a flat copy, that's already OK
  1053. *dest = *src;
  1054. }
  1055. // ------------------------------------------------------------------------------------------------
  1056. void SceneCombiner::Copy(aiLight **_dest, const aiLight *src) {
  1057. if (nullptr == _dest || nullptr == src) {
  1058. return;
  1059. }
  1060. aiLight *dest = *_dest = new aiLight();
  1061. // get a flat copy, that's already OK
  1062. *dest = *src;
  1063. }
  1064. // ------------------------------------------------------------------------------------------------
  1065. void SceneCombiner::Copy(aiBone **_dest, const aiBone *src) {
  1066. if (nullptr == _dest || nullptr == src) {
  1067. return;
  1068. }
  1069. aiBone *dest = *_dest = new aiBone();
  1070. // get a flat copy
  1071. *dest = *src;
  1072. }
  1073. // ------------------------------------------------------------------------------------------------
  1074. void SceneCombiner::Copy(aiNode **_dest, const aiNode *src) {
  1075. ai_assert(nullptr != _dest);
  1076. ai_assert(nullptr != src);
  1077. aiNode *dest = *_dest = new aiNode();
  1078. // get a flat copy
  1079. *dest = *src;
  1080. if (src->mMetaData) {
  1081. Copy(&dest->mMetaData, src->mMetaData);
  1082. }
  1083. // and reallocate all arrays
  1084. GetArrayCopy(dest->mMeshes, dest->mNumMeshes);
  1085. CopyPtrArray(dest->mChildren, src->mChildren, dest->mNumChildren);
  1086. // need to set the mParent fields to the created aiNode.
  1087. for (unsigned int i = 0; i < dest->mNumChildren; i++) {
  1088. dest->mChildren[i]->mParent = dest;
  1089. }
  1090. }
  1091. // ------------------------------------------------------------------------------------------------
  1092. void SceneCombiner::Copy(aiMetadata **_dest, const aiMetadata *src) {
  1093. if (nullptr == _dest || nullptr == src) {
  1094. return;
  1095. }
  1096. if (0 == src->mNumProperties) {
  1097. return;
  1098. }
  1099. aiMetadata *dest = *_dest = aiMetadata::Alloc(src->mNumProperties);
  1100. std::copy(src->mKeys, src->mKeys + src->mNumProperties, dest->mKeys);
  1101. for (unsigned int i = 0; i < src->mNumProperties; ++i) {
  1102. aiMetadataEntry &in = src->mValues[i];
  1103. aiMetadataEntry &out = dest->mValues[i];
  1104. out.mType = in.mType;
  1105. switch (dest->mValues[i].mType) {
  1106. case AI_BOOL:
  1107. out.mData = new bool(*static_cast<bool *>(in.mData));
  1108. break;
  1109. case AI_INT32:
  1110. out.mData = new int32_t(*static_cast<int32_t *>(in.mData));
  1111. break;
  1112. case AI_UINT64:
  1113. out.mData = new uint64_t(*static_cast<uint64_t *>(in.mData));
  1114. break;
  1115. case AI_FLOAT:
  1116. out.mData = new float(*static_cast<float *>(in.mData));
  1117. break;
  1118. case AI_DOUBLE:
  1119. out.mData = new double(*static_cast<double *>(in.mData));
  1120. break;
  1121. case AI_AISTRING:
  1122. out.mData = new aiString(*static_cast<aiString *>(in.mData));
  1123. break;
  1124. case AI_AIVECTOR3D:
  1125. out.mData = new aiVector3D(*static_cast<aiVector3D *>(in.mData));
  1126. break;
  1127. case AI_AIMETADATA:
  1128. out.mData = new aiMetadata(*static_cast<aiMetadata *>(in.mData));
  1129. break;
  1130. default:
  1131. ai_assert(false);
  1132. break;
  1133. }
  1134. }
  1135. }
  1136. // ------------------------------------------------------------------------------------------------
  1137. void SceneCombiner::Copy(aiString **_dest, const aiString *src) {
  1138. if (nullptr == _dest || nullptr == src) {
  1139. return;
  1140. }
  1141. aiString *dest = *_dest = new aiString();
  1142. // get a flat copy
  1143. *dest = *src;
  1144. }
  1145. #if (__GNUC__ >= 8 && __GNUC_MINOR__ >= 0)
  1146. #pragma GCC diagnostic pop
  1147. #endif
  1148. } // Namespace Assimp