SceneCombiner.cpp 48 KB

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