2
0

SceneCombiner.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2022, 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->mNumTextures];
  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. // Copy here because we overwrite the string data in-place and the buffer inside of aiString
  341. // will be a lie if we just reinterpret from prop->mData. The size of mData is not guaranteed to be
  342. // MAXLEN in size.
  343. aiString s(*(aiString *)prop->mData);
  344. if ('*' == s.data[0]) {
  345. // Offset the index and write it back ..
  346. const unsigned int idx = strtoul10(&s.data[1]) + offset[n];
  347. const unsigned int oldLen = s.length;
  348. s.length = 1 + ASSIMP_itoa10(&s.data[1], sizeof(s.data) - 1, idx);
  349. // The string changed in size so we need to reallocate the buffer for the property.
  350. if (oldLen < s.length) {
  351. prop->mDataLength += s.length - oldLen;
  352. delete[] prop->mData;
  353. prop->mData = new char[prop->mDataLength];
  354. }
  355. memcpy(prop->mData, static_cast<void*>(&s), prop->mDataLength);
  356. }
  357. }
  358. // Need to generate new, unique material names?
  359. else if (!::strcmp(prop->mKey.data, "$mat.name") && flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_MATNAMES) {
  360. aiString *pcSrc = (aiString *)prop->mData;
  361. PrefixString(*pcSrc, (*cur).id, (*cur).idlen);
  362. }
  363. }
  364. }
  365. ++pip;
  366. }
  367. offset[n] = cnt;
  368. cnt = (unsigned int)(pip - dest->mMaterials);
  369. }
  370. }
  371. // generate the output mesh list + again an offset table for all mesh indices
  372. if (dest->mNumMeshes) {
  373. aiMesh **pip = dest->mMeshes = new aiMesh *[dest->mNumMeshes];
  374. cnt = 0;
  375. for (unsigned int n = 0; n < src.size(); ++n) {
  376. SceneHelper *cur = &src[n];
  377. for (unsigned int i = 0; i < (*cur)->mNumMeshes; ++i) {
  378. if (n != duplicates[n]) {
  379. if (flags & AI_INT_MERGE_SCENE_DUPLICATES_DEEP_CPY)
  380. Copy(pip, (*cur)->mMeshes[i]);
  381. else
  382. continue;
  383. } else
  384. *pip = (*cur)->mMeshes[i];
  385. // update the material index of the mesh
  386. (*pip)->mMaterialIndex += offset[n];
  387. ++pip;
  388. }
  389. // reuse the offset array - store now the mesh offset in it
  390. offset[n] = cnt;
  391. cnt = (unsigned int)(pip - dest->mMeshes);
  392. }
  393. }
  394. std::vector<NodeAttachmentInfo> nodes;
  395. nodes.reserve(srcList.size());
  396. // ----------------------------------------------------------------------------
  397. // Now generate the output node graph. We need to make those
  398. // names in the graph that are referenced by anims or lights
  399. // or cameras unique. So we add a prefix to them ... $<rand>_
  400. // We could also use a counter, but using a random value allows us to
  401. // use just one prefix if we are joining multiple scene hierarchies recursively.
  402. // Chances are quite good we don't collide, so we try that ...
  403. // ----------------------------------------------------------------------------
  404. // Allocate space for light sources, cameras and animations
  405. aiLight **ppLights = dest->mLights = (dest->mNumLights ? new aiLight *[dest->mNumLights] : nullptr);
  406. aiCamera **ppCameras = dest->mCameras = (dest->mNumCameras ? new aiCamera *[dest->mNumCameras] : nullptr);
  407. aiAnimation **ppAnims = dest->mAnimations = (dest->mNumAnimations ? new aiAnimation *[dest->mNumAnimations] : nullptr);
  408. for (int n = static_cast<int>(src.size() - 1); n >= 0; --n) /* !!! important !!! */
  409. {
  410. SceneHelper *cur = &src[n];
  411. aiNode *node;
  412. // To offset or not to offset, this is the question
  413. if (n != (int)duplicates[n]) {
  414. // Get full scene-graph copy
  415. Copy(&node, (*cur)->mRootNode);
  416. OffsetNodeMeshIndices(node, offset[duplicates[n]]);
  417. if (flags & AI_INT_MERGE_SCENE_DUPLICATES_DEEP_CPY) {
  418. // (note:) they are already 'offseted' by offset[duplicates[n]]
  419. OffsetNodeMeshIndices(node, offset[n] - offset[duplicates[n]]);
  420. }
  421. } else // if (n == duplicates[n])
  422. {
  423. node = (*cur)->mRootNode;
  424. OffsetNodeMeshIndices(node, offset[n]);
  425. }
  426. if (n) // src[0] is the master node
  427. nodes.push_back(NodeAttachmentInfo(node, srcList[n - 1].attachToNode, n));
  428. // add name prefixes?
  429. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES) {
  430. // or the whole scenegraph
  431. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {
  432. AddNodePrefixesChecked(node, (*cur).id, (*cur).idlen, src, n);
  433. } else
  434. AddNodePrefixes(node, (*cur).id, (*cur).idlen);
  435. // meshes
  436. for (unsigned int i = 0; i < (*cur)->mNumMeshes; ++i) {
  437. aiMesh *mesh = (*cur)->mMeshes[i];
  438. // rename all bones
  439. for (unsigned int a = 0; a < mesh->mNumBones; ++a) {
  440. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {
  441. if (!FindNameMatch(mesh->mBones[a]->mName, src, n))
  442. continue;
  443. }
  444. PrefixString(mesh->mBones[a]->mName, (*cur).id, (*cur).idlen);
  445. }
  446. }
  447. }
  448. // --------------------------------------------------------------------
  449. // Copy light sources
  450. for (unsigned int i = 0; i < (*cur)->mNumLights; ++i, ++ppLights) {
  451. if (n != (int)duplicates[n]) // duplicate scene?
  452. {
  453. Copy(ppLights, (*cur)->mLights[i]);
  454. } else
  455. *ppLights = (*cur)->mLights[i];
  456. // Add name prefixes?
  457. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES) {
  458. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {
  459. if (!FindNameMatch((*ppLights)->mName, src, n))
  460. continue;
  461. }
  462. PrefixString((*ppLights)->mName, (*cur).id, (*cur).idlen);
  463. }
  464. }
  465. // --------------------------------------------------------------------
  466. // Copy cameras
  467. for (unsigned int i = 0; i < (*cur)->mNumCameras; ++i, ++ppCameras) {
  468. if (n != (int)duplicates[n]) // duplicate scene?
  469. {
  470. Copy(ppCameras, (*cur)->mCameras[i]);
  471. } else
  472. *ppCameras = (*cur)->mCameras[i];
  473. // Add name prefixes?
  474. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES) {
  475. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {
  476. if (!FindNameMatch((*ppCameras)->mName, src, n))
  477. continue;
  478. }
  479. PrefixString((*ppCameras)->mName, (*cur).id, (*cur).idlen);
  480. }
  481. }
  482. // --------------------------------------------------------------------
  483. // Copy animations
  484. for (unsigned int i = 0; i < (*cur)->mNumAnimations; ++i, ++ppAnims) {
  485. if (n != (int)duplicates[n]) // duplicate scene?
  486. {
  487. Copy(ppAnims, (*cur)->mAnimations[i]);
  488. } else
  489. *ppAnims = (*cur)->mAnimations[i];
  490. // Add name prefixes?
  491. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES) {
  492. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {
  493. if (!FindNameMatch((*ppAnims)->mName, src, n))
  494. continue;
  495. }
  496. PrefixString((*ppAnims)->mName, (*cur).id, (*cur).idlen);
  497. // don't forget to update all node animation channels
  498. for (unsigned int a = 0; a < (*ppAnims)->mNumChannels; ++a) {
  499. if (flags & AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES_IF_NECESSARY) {
  500. if (!FindNameMatch((*ppAnims)->mChannels[a]->mNodeName, src, n))
  501. continue;
  502. }
  503. PrefixString((*ppAnims)->mChannels[a]->mNodeName, (*cur).id, (*cur).idlen);
  504. }
  505. }
  506. }
  507. }
  508. // Now build the output graph
  509. AttachToGraph(master, nodes);
  510. dest->mRootNode = master->mRootNode;
  511. // Check whether we succeeded at building the output graph
  512. for (std::vector<NodeAttachmentInfo>::iterator it = nodes.begin();
  513. it != nodes.end(); ++it) {
  514. if (!(*it).resolved) {
  515. if (flags & AI_INT_MERGE_SCENE_RESOLVE_CROSS_ATTACHMENTS) {
  516. // search for this attachment point in all other imported scenes, too.
  517. for (unsigned int n = 0; n < src.size(); ++n) {
  518. if (n != (*it).src_idx) {
  519. AttachToGraph(src[n].scene, nodes);
  520. if ((*it).resolved)
  521. break;
  522. }
  523. }
  524. }
  525. if (!(*it).resolved) {
  526. ASSIMP_LOG_ERROR("SceneCombiner: Failed to resolve attachment ", (*it).node->mName.data,
  527. " ", (*it).attachToNode->mName.data);
  528. }
  529. }
  530. }
  531. // now delete all input scenes. Make sure duplicate scenes aren't
  532. // deleted more than one time
  533. for (unsigned int n = 0; n < src.size(); ++n) {
  534. if (n != duplicates[n]) // duplicate scene?
  535. continue;
  536. aiScene *deleteMe = src[n].scene;
  537. // We need to delete the arrays before the destructor is called -
  538. // we are reusing the array members
  539. delete[] deleteMe->mMeshes;
  540. deleteMe->mMeshes = nullptr;
  541. delete[] deleteMe->mCameras;
  542. deleteMe->mCameras = nullptr;
  543. delete[] deleteMe->mLights;
  544. deleteMe->mLights = nullptr;
  545. delete[] deleteMe->mMaterials;
  546. deleteMe->mMaterials = nullptr;
  547. delete[] deleteMe->mAnimations;
  548. deleteMe->mAnimations = nullptr;
  549. delete[] deleteMe->mTextures;
  550. deleteMe->mTextures = nullptr;
  551. deleteMe->mRootNode = nullptr;
  552. // Now we can safely delete the scene
  553. delete deleteMe;
  554. }
  555. // Check flags
  556. if (!dest->mNumMeshes || !dest->mNumMaterials) {
  557. dest->mFlags |= AI_SCENE_FLAGS_INCOMPLETE;
  558. }
  559. // We're finished
  560. }
  561. // ------------------------------------------------------------------------------------------------
  562. // Build a list of unique bones
  563. void SceneCombiner::BuildUniqueBoneList(std::list<BoneWithHash> &asBones,
  564. std::vector<aiMesh *>::const_iterator it,
  565. std::vector<aiMesh *>::const_iterator end) {
  566. unsigned int iOffset = 0;
  567. for (; it != end; ++it) {
  568. for (unsigned int l = 0; l < (*it)->mNumBones; ++l) {
  569. aiBone *p = (*it)->mBones[l];
  570. uint32_t itml = SuperFastHash(p->mName.data, (unsigned int)p->mName.length);
  571. std::list<BoneWithHash>::iterator it2 = asBones.begin();
  572. std::list<BoneWithHash>::iterator end2 = asBones.end();
  573. for (; it2 != end2; ++it2) {
  574. if ((*it2).first == itml) {
  575. (*it2).pSrcBones.push_back(BoneSrcIndex(p, iOffset));
  576. break;
  577. }
  578. }
  579. if (end2 == it2) {
  580. // need to begin a new bone entry
  581. asBones.push_back(BoneWithHash());
  582. BoneWithHash &btz = asBones.back();
  583. // setup members
  584. btz.first = itml;
  585. btz.second = &p->mName;
  586. btz.pSrcBones.push_back(BoneSrcIndex(p, iOffset));
  587. }
  588. }
  589. iOffset += (*it)->mNumVertices;
  590. }
  591. }
  592. // ------------------------------------------------------------------------------------------------
  593. // Merge a list of bones
  594. void SceneCombiner::MergeBones(aiMesh *out, std::vector<aiMesh *>::const_iterator it,
  595. std::vector<aiMesh *>::const_iterator end) {
  596. if (nullptr == out || out->mNumBones == 0) {
  597. return;
  598. }
  599. // find we need to build an unique list of all bones.
  600. // we work with hashes to make the comparisons MUCH faster,
  601. // at least if we have many bones.
  602. std::list<BoneWithHash> asBones;
  603. BuildUniqueBoneList(asBones, it, end);
  604. // now create the output bones
  605. out->mNumBones = 0;
  606. out->mBones = new aiBone *[asBones.size()];
  607. for (std::list<BoneWithHash>::const_iterator boneIt = asBones.begin(), boneEnd = asBones.end(); boneIt != boneEnd; ++boneIt) {
  608. // Allocate a bone and setup it's name
  609. aiBone *pc = out->mBones[out->mNumBones++] = new aiBone();
  610. pc->mName = aiString(*(boneIt->second));
  611. std::vector<BoneSrcIndex>::const_iterator wend = boneIt->pSrcBones.end();
  612. // Loop through all bones to be joined for this bone
  613. for (std::vector<BoneSrcIndex>::const_iterator wmit = boneIt->pSrcBones.begin(); wmit != wend; ++wmit) {
  614. pc->mNumWeights += (*wmit).first->mNumWeights;
  615. // NOTE: different offset matrices for bones with equal names
  616. // are - at the moment - not handled correctly.
  617. if (wmit != boneIt->pSrcBones.begin() && pc->mOffsetMatrix != wmit->first->mOffsetMatrix) {
  618. ASSIMP_LOG_WARN("Bones with equal names but different offset matrices can't be joined at the moment");
  619. continue;
  620. }
  621. pc->mOffsetMatrix = wmit->first->mOffsetMatrix;
  622. }
  623. // Allocate the vertex weight array
  624. aiVertexWeight *avw = pc->mWeights = new aiVertexWeight[pc->mNumWeights];
  625. // And copy the final weights - adjust the vertex IDs by the
  626. // face index offset of the corresponding mesh.
  627. for (std::vector<BoneSrcIndex>::const_iterator wmit = (*boneIt).pSrcBones.begin(); wmit != (*boneIt).pSrcBones.end(); ++wmit) {
  628. if (wmit == wend) {
  629. break;
  630. }
  631. aiBone *pip = (*wmit).first;
  632. for (unsigned int mp = 0; mp < pip->mNumWeights; ++mp, ++avw) {
  633. const aiVertexWeight &vfi = pip->mWeights[mp];
  634. avw->mWeight = vfi.mWeight;
  635. avw->mVertexId = vfi.mVertexId + (*wmit).second;
  636. }
  637. }
  638. }
  639. }
  640. // ------------------------------------------------------------------------------------------------
  641. // Merge a list of meshes
  642. void SceneCombiner::MergeMeshes(aiMesh **_out, unsigned int /*flags*/,
  643. std::vector<aiMesh *>::const_iterator begin,
  644. std::vector<aiMesh *>::const_iterator end) {
  645. if (nullptr == _out) {
  646. return;
  647. }
  648. if (begin == end) {
  649. *_out = nullptr; // no meshes ...
  650. return;
  651. }
  652. // Allocate the output mesh
  653. aiMesh *out = *_out = new aiMesh();
  654. out->mMaterialIndex = (*begin)->mMaterialIndex;
  655. std::string name;
  656. // Find out how much output storage we'll need
  657. for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
  658. const char *meshName((*it)->mName.C_Str());
  659. name += std::string(meshName);
  660. if (it != end - 1) {
  661. name += ".";
  662. }
  663. out->mNumVertices += (*it)->mNumVertices;
  664. out->mNumFaces += (*it)->mNumFaces;
  665. out->mNumBones += (*it)->mNumBones;
  666. // combine primitive type flags
  667. out->mPrimitiveTypes |= (*it)->mPrimitiveTypes;
  668. }
  669. out->mName.Set(name.c_str());
  670. if (out->mNumVertices) {
  671. aiVector3D *pv2;
  672. // copy vertex positions
  673. if ((**begin).HasPositions()) {
  674. pv2 = out->mVertices = new aiVector3D[out->mNumVertices];
  675. for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
  676. if ((*it)->mVertices) {
  677. ::memcpy(pv2, (*it)->mVertices, (*it)->mNumVertices * sizeof(aiVector3D));
  678. } else
  679. ASSIMP_LOG_WARN("JoinMeshes: Positions expected but input mesh contains no positions");
  680. pv2 += (*it)->mNumVertices;
  681. }
  682. }
  683. // copy normals
  684. if ((**begin).HasNormals()) {
  685. pv2 = out->mNormals = new aiVector3D[out->mNumVertices];
  686. for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
  687. if ((*it)->mNormals) {
  688. ::memcpy(pv2, (*it)->mNormals, (*it)->mNumVertices * sizeof(aiVector3D));
  689. } else {
  690. ASSIMP_LOG_WARN("JoinMeshes: Normals expected but input mesh contains no normals");
  691. }
  692. pv2 += (*it)->mNumVertices;
  693. }
  694. }
  695. // copy tangents and bi-tangents
  696. if ((**begin).HasTangentsAndBitangents()) {
  697. pv2 = out->mTangents = new aiVector3D[out->mNumVertices];
  698. aiVector3D *pv2b = out->mBitangents = new aiVector3D[out->mNumVertices];
  699. for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
  700. if ((*it)->mTangents) {
  701. ::memcpy(pv2, (*it)->mTangents, (*it)->mNumVertices * sizeof(aiVector3D));
  702. ::memcpy(pv2b, (*it)->mBitangents, (*it)->mNumVertices * sizeof(aiVector3D));
  703. } else {
  704. ASSIMP_LOG_WARN("JoinMeshes: Tangents expected but input mesh contains no tangents");
  705. }
  706. pv2 += (*it)->mNumVertices;
  707. pv2b += (*it)->mNumVertices;
  708. }
  709. }
  710. // copy texture coordinates
  711. unsigned int n = 0;
  712. while ((**begin).HasTextureCoords(n)) {
  713. out->mNumUVComponents[n] = (*begin)->mNumUVComponents[n];
  714. pv2 = out->mTextureCoords[n] = new aiVector3D[out->mNumVertices];
  715. for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
  716. if ((*it)->mTextureCoords[n]) {
  717. ::memcpy(pv2, (*it)->mTextureCoords[n], (*it)->mNumVertices * sizeof(aiVector3D));
  718. } else {
  719. ASSIMP_LOG_WARN("JoinMeshes: UVs expected but input mesh contains no UVs");
  720. }
  721. pv2 += (*it)->mNumVertices;
  722. }
  723. ++n;
  724. }
  725. // copy vertex colors
  726. n = 0;
  727. while ((**begin).HasVertexColors(n)) {
  728. aiColor4D *pVec2 = out->mColors[n] = new aiColor4D[out->mNumVertices];
  729. for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
  730. if ((*it)->mColors[n]) {
  731. ::memcpy(pVec2, (*it)->mColors[n], (*it)->mNumVertices * sizeof(aiColor4D));
  732. } else {
  733. ASSIMP_LOG_WARN("JoinMeshes: VCs expected but input mesh contains no VCs");
  734. }
  735. pVec2 += (*it)->mNumVertices;
  736. }
  737. ++n;
  738. }
  739. }
  740. if (out->mNumFaces) // just for safety
  741. {
  742. // copy faces
  743. out->mFaces = new aiFace[out->mNumFaces];
  744. aiFace *pf2 = out->mFaces;
  745. unsigned int ofs = 0;
  746. for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it) {
  747. for (unsigned int m = 0; m < (*it)->mNumFaces; ++m, ++pf2) {
  748. aiFace &face = (*it)->mFaces[m];
  749. pf2->mNumIndices = face.mNumIndices;
  750. pf2->mIndices = face.mIndices;
  751. if (ofs) {
  752. // add the offset to the vertex
  753. for (unsigned int q = 0; q < face.mNumIndices; ++q) {
  754. face.mIndices[q] += ofs;
  755. }
  756. }
  757. face.mIndices = nullptr;
  758. }
  759. ofs += (*it)->mNumVertices;
  760. }
  761. }
  762. // bones - as this is quite lengthy, I moved the code to a separate function
  763. if (out->mNumBones)
  764. MergeBones(out, begin, end);
  765. // delete all source meshes
  766. for (std::vector<aiMesh *>::const_iterator it = begin; it != end; ++it)
  767. delete *it;
  768. }
  769. // ------------------------------------------------------------------------------------------------
  770. void SceneCombiner::MergeMaterials(aiMaterial **dest,
  771. std::vector<aiMaterial *>::const_iterator begin,
  772. std::vector<aiMaterial *>::const_iterator end) {
  773. if (nullptr == dest) {
  774. return;
  775. }
  776. if (begin == end) {
  777. *dest = nullptr; // no materials ...
  778. return;
  779. }
  780. // Allocate the output material
  781. aiMaterial *out = *dest = new aiMaterial();
  782. // Get the maximal number of properties
  783. unsigned int size = 0;
  784. for (std::vector<aiMaterial *>::const_iterator it = begin; it != end; ++it) {
  785. size += (*it)->mNumProperties;
  786. }
  787. out->Clear();
  788. delete[] out->mProperties;
  789. out->mNumAllocated = size;
  790. out->mNumProperties = 0;
  791. out->mProperties = new aiMaterialProperty *[out->mNumAllocated];
  792. for (std::vector<aiMaterial *>::const_iterator it = begin; it != end; ++it) {
  793. for (unsigned int i = 0; i < (*it)->mNumProperties; ++i) {
  794. aiMaterialProperty *sprop = (*it)->mProperties[i];
  795. // Test if we already have a matching property
  796. const aiMaterialProperty *prop_exist;
  797. if (aiGetMaterialProperty(out, sprop->mKey.C_Str(), sprop->mSemantic, sprop->mIndex, &prop_exist) != AI_SUCCESS) {
  798. // If not, we add it to the new material
  799. aiMaterialProperty *prop = out->mProperties[out->mNumProperties] = new aiMaterialProperty();
  800. prop->mDataLength = sprop->mDataLength;
  801. prop->mData = new char[prop->mDataLength];
  802. ::memcpy(prop->mData, sprop->mData, prop->mDataLength);
  803. prop->mIndex = sprop->mIndex;
  804. prop->mSemantic = sprop->mSemantic;
  805. prop->mKey = sprop->mKey;
  806. prop->mType = sprop->mType;
  807. out->mNumProperties++;
  808. }
  809. }
  810. }
  811. }
  812. // ------------------------------------------------------------------------------------------------
  813. template <typename Type>
  814. inline void CopyPtrArray(Type **&dest, const Type *const *src, ai_uint num) {
  815. if (!num) {
  816. dest = nullptr;
  817. return;
  818. }
  819. dest = new Type *[num];
  820. for (ai_uint i = 0; i < num; ++i) {
  821. SceneCombiner::Copy(&dest[i], src[i]);
  822. }
  823. }
  824. // ------------------------------------------------------------------------------------------------
  825. template <typename Type>
  826. inline void GetArrayCopy(Type *&dest, ai_uint num) {
  827. if (!dest) {
  828. return;
  829. }
  830. Type *old = dest;
  831. dest = new Type[num];
  832. ::memcpy(dest, old, sizeof(Type) * num);
  833. }
  834. // ------------------------------------------------------------------------------------------------
  835. void SceneCombiner::CopySceneFlat(aiScene **_dest, const aiScene *src) {
  836. if (nullptr == _dest || nullptr == src) {
  837. return;
  838. }
  839. // reuse the old scene or allocate a new?
  840. if (*_dest) {
  841. (*_dest)->~aiScene();
  842. new (*_dest) aiScene();
  843. } else {
  844. *_dest = new aiScene();
  845. }
  846. CopyScene(_dest, src, false);
  847. }
  848. // ------------------------------------------------------------------------------------------------
  849. void SceneCombiner::CopyScene(aiScene **_dest, const aiScene *src, bool allocate) {
  850. if (nullptr == _dest || nullptr == src) {
  851. return;
  852. }
  853. if (allocate) {
  854. *_dest = new aiScene();
  855. }
  856. aiScene *dest = *_dest;
  857. ai_assert(nullptr != dest);
  858. // copy metadata
  859. if (nullptr != src->mMetaData) {
  860. dest->mMetaData = new aiMetadata(*src->mMetaData);
  861. }
  862. // copy animations
  863. dest->mNumAnimations = src->mNumAnimations;
  864. CopyPtrArray(dest->mAnimations, src->mAnimations,
  865. dest->mNumAnimations);
  866. // copy textures
  867. dest->mNumTextures = src->mNumTextures;
  868. CopyPtrArray(dest->mTextures, src->mTextures,
  869. dest->mNumTextures);
  870. // copy materials
  871. dest->mNumMaterials = src->mNumMaterials;
  872. CopyPtrArray(dest->mMaterials, src->mMaterials,
  873. dest->mNumMaterials);
  874. // copy lights
  875. dest->mNumLights = src->mNumLights;
  876. CopyPtrArray(dest->mLights, src->mLights,
  877. dest->mNumLights);
  878. // copy cameras
  879. dest->mNumCameras = src->mNumCameras;
  880. CopyPtrArray(dest->mCameras, src->mCameras,
  881. dest->mNumCameras);
  882. // copy meshes
  883. dest->mNumMeshes = src->mNumMeshes;
  884. CopyPtrArray(dest->mMeshes, src->mMeshes,
  885. dest->mNumMeshes);
  886. // now - copy the root node of the scene (deep copy, too)
  887. Copy(&dest->mRootNode, src->mRootNode);
  888. // and keep the flags ...
  889. dest->mFlags = src->mFlags;
  890. // source private data might be nullptr if the scene is user-allocated (i.e. for use with the export API)
  891. if (dest->mPrivate != nullptr) {
  892. ScenePriv(dest)->mPPStepsApplied = ScenePriv(src) ? ScenePriv(src)->mPPStepsApplied : 0;
  893. }
  894. }
  895. // ------------------------------------------------------------------------------------------------
  896. void SceneCombiner::Copy(aiMesh **_dest, const aiMesh *src) {
  897. if (nullptr == _dest || nullptr == src) {
  898. return;
  899. }
  900. aiMesh *dest = *_dest = new aiMesh();
  901. // get a flat copy
  902. *dest = *src;
  903. // and reallocate all arrays
  904. GetArrayCopy(dest->mVertices, dest->mNumVertices);
  905. GetArrayCopy(dest->mNormals, dest->mNumVertices);
  906. GetArrayCopy(dest->mTangents, dest->mNumVertices);
  907. GetArrayCopy(dest->mBitangents, dest->mNumVertices);
  908. unsigned int n = 0;
  909. while (dest->HasTextureCoords(n)) {
  910. GetArrayCopy(dest->mTextureCoords[n++], dest->mNumVertices);
  911. }
  912. n = 0;
  913. while (dest->HasVertexColors(n)) {
  914. GetArrayCopy(dest->mColors[n++], dest->mNumVertices);
  915. }
  916. // make a deep copy of all bones
  917. CopyPtrArray(dest->mBones, dest->mBones, dest->mNumBones);
  918. // make a deep copy of all faces
  919. GetArrayCopy(dest->mFaces, dest->mNumFaces);
  920. for (unsigned int i = 0; i < dest->mNumFaces; ++i) {
  921. aiFace &f = dest->mFaces[i];
  922. GetArrayCopy(f.mIndices, f.mNumIndices);
  923. }
  924. // make a deep copy of all blend shapes
  925. CopyPtrArray(dest->mAnimMeshes, dest->mAnimMeshes, dest->mNumAnimMeshes);
  926. // make a deep copy of all texture coordinate names
  927. if (src->mTextureCoordsNames != nullptr) {
  928. dest->mTextureCoordsNames = new aiString *[AI_MAX_NUMBER_OF_TEXTURECOORDS] {};
  929. for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
  930. Copy(&dest->mTextureCoordsNames[i], src->mTextureCoordsNames[i]);
  931. }
  932. }
  933. }
  934. // ------------------------------------------------------------------------------------------------
  935. void SceneCombiner::Copy(aiAnimMesh **_dest, const aiAnimMesh *src) {
  936. if (nullptr == _dest || nullptr == src) {
  937. return;
  938. }
  939. aiAnimMesh *dest = *_dest = new aiAnimMesh();
  940. // get a flat copy
  941. *dest = *src;
  942. // and reallocate all arrays
  943. GetArrayCopy(dest->mVertices, dest->mNumVertices);
  944. GetArrayCopy(dest->mNormals, dest->mNumVertices);
  945. GetArrayCopy(dest->mTangents, dest->mNumVertices);
  946. GetArrayCopy(dest->mBitangents, dest->mNumVertices);
  947. unsigned int n = 0;
  948. while (dest->HasTextureCoords(n))
  949. GetArrayCopy(dest->mTextureCoords[n++], dest->mNumVertices);
  950. n = 0;
  951. while (dest->HasVertexColors(n))
  952. GetArrayCopy(dest->mColors[n++], dest->mNumVertices);
  953. }
  954. // ------------------------------------------------------------------------------------------------
  955. void SceneCombiner::Copy(aiMaterial **_dest, const aiMaterial *src) {
  956. if (nullptr == _dest || nullptr == src) {
  957. return;
  958. }
  959. aiMaterial *dest = (aiMaterial *)(*_dest = new aiMaterial());
  960. dest->Clear();
  961. delete[] dest->mProperties;
  962. dest->mNumAllocated = src->mNumAllocated;
  963. dest->mNumProperties = src->mNumProperties;
  964. dest->mProperties = new aiMaterialProperty *[dest->mNumAllocated];
  965. for (unsigned int i = 0; i < dest->mNumProperties; ++i) {
  966. aiMaterialProperty *prop = dest->mProperties[i] = new aiMaterialProperty();
  967. aiMaterialProperty *sprop = src->mProperties[i];
  968. prop->mDataLength = sprop->mDataLength;
  969. prop->mData = new char[prop->mDataLength];
  970. ::memcpy(prop->mData, sprop->mData, prop->mDataLength);
  971. prop->mIndex = sprop->mIndex;
  972. prop->mSemantic = sprop->mSemantic;
  973. prop->mKey = sprop->mKey;
  974. prop->mType = sprop->mType;
  975. }
  976. }
  977. // ------------------------------------------------------------------------------------------------
  978. void SceneCombiner::Copy(aiTexture **_dest, const aiTexture *src) {
  979. if (nullptr == _dest || nullptr == src) {
  980. return;
  981. }
  982. aiTexture *dest = *_dest = new aiTexture();
  983. // get a flat copy
  984. *dest = *src;
  985. // and reallocate all arrays. We must do it manually here
  986. const char *old = (const char *)dest->pcData;
  987. if (old) {
  988. unsigned int cpy;
  989. if (!dest->mHeight)
  990. cpy = dest->mWidth;
  991. else
  992. cpy = dest->mHeight * dest->mWidth * sizeof(aiTexel);
  993. if (!cpy) {
  994. dest->pcData = nullptr;
  995. return;
  996. }
  997. // the cast is legal, the aiTexel c'tor does nothing important
  998. dest->pcData = (aiTexel *)new char[cpy];
  999. ::memcpy(dest->pcData, old, cpy);
  1000. }
  1001. }
  1002. // ------------------------------------------------------------------------------------------------
  1003. void SceneCombiner::Copy(aiAnimation **_dest, const aiAnimation *src) {
  1004. if (nullptr == _dest || nullptr == src) {
  1005. return;
  1006. }
  1007. aiAnimation *dest = *_dest = new aiAnimation();
  1008. // get a flat copy
  1009. *dest = *src;
  1010. // and reallocate all arrays
  1011. CopyPtrArray(dest->mChannels, src->mChannels, dest->mNumChannels);
  1012. CopyPtrArray(dest->mMorphMeshChannels, src->mMorphMeshChannels, dest->mNumMorphMeshChannels);
  1013. }
  1014. // ------------------------------------------------------------------------------------------------
  1015. void SceneCombiner::Copy(aiNodeAnim **_dest, const aiNodeAnim *src) {
  1016. if (nullptr == _dest || nullptr == src) {
  1017. return;
  1018. }
  1019. aiNodeAnim *dest = *_dest = new aiNodeAnim();
  1020. // get a flat copy
  1021. *dest = *src;
  1022. // and reallocate all arrays
  1023. GetArrayCopy(dest->mPositionKeys, dest->mNumPositionKeys);
  1024. GetArrayCopy(dest->mScalingKeys, dest->mNumScalingKeys);
  1025. GetArrayCopy(dest->mRotationKeys, dest->mNumRotationKeys);
  1026. }
  1027. void SceneCombiner::Copy(aiMeshMorphAnim **_dest, const aiMeshMorphAnim *src) {
  1028. if (nullptr == _dest || nullptr == src) {
  1029. return;
  1030. }
  1031. aiMeshMorphAnim *dest = *_dest = new aiMeshMorphAnim();
  1032. // get a flat copy
  1033. *dest = *src;
  1034. // and reallocate all arrays
  1035. GetArrayCopy(dest->mKeys, dest->mNumKeys);
  1036. for (ai_uint i = 0; i < dest->mNumKeys; ++i) {
  1037. dest->mKeys[i].mValues = new unsigned int[dest->mKeys[i].mNumValuesAndWeights];
  1038. dest->mKeys[i].mWeights = new double[dest->mKeys[i].mNumValuesAndWeights];
  1039. ::memcpy(dest->mKeys[i].mValues, src->mKeys[i].mValues, dest->mKeys[i].mNumValuesAndWeights * sizeof(unsigned int));
  1040. ::memcpy(dest->mKeys[i].mWeights, src->mKeys[i].mWeights, dest->mKeys[i].mNumValuesAndWeights * sizeof(double));
  1041. }
  1042. }
  1043. // ------------------------------------------------------------------------------------------------
  1044. void SceneCombiner::Copy(aiCamera **_dest, const aiCamera *src) {
  1045. if (nullptr == _dest || nullptr == src) {
  1046. return;
  1047. }
  1048. aiCamera *dest = *_dest = new aiCamera();
  1049. // get a flat copy, that's already OK
  1050. *dest = *src;
  1051. }
  1052. // ------------------------------------------------------------------------------------------------
  1053. void SceneCombiner::Copy(aiLight **_dest, const aiLight *src) {
  1054. if (nullptr == _dest || nullptr == src) {
  1055. return;
  1056. }
  1057. aiLight *dest = *_dest = new aiLight();
  1058. // get a flat copy, that's already OK
  1059. *dest = *src;
  1060. }
  1061. // ------------------------------------------------------------------------------------------------
  1062. void SceneCombiner::Copy(aiBone **_dest, const aiBone *src) {
  1063. if (nullptr == _dest || nullptr == src) {
  1064. return;
  1065. }
  1066. aiBone *dest = *_dest = new aiBone();
  1067. // get a flat copy
  1068. *dest = *src;
  1069. }
  1070. // ------------------------------------------------------------------------------------------------
  1071. void SceneCombiner::Copy(aiNode **_dest, const aiNode *src) {
  1072. ai_assert(nullptr != _dest);
  1073. ai_assert(nullptr != src);
  1074. aiNode *dest = *_dest = new aiNode();
  1075. // get a flat copy
  1076. *dest = *src;
  1077. if (src->mMetaData) {
  1078. Copy(&dest->mMetaData, src->mMetaData);
  1079. }
  1080. // and reallocate all arrays
  1081. GetArrayCopy(dest->mMeshes, dest->mNumMeshes);
  1082. CopyPtrArray(dest->mChildren, src->mChildren, dest->mNumChildren);
  1083. // need to set the mParent fields to the created aiNode.
  1084. for (unsigned int i = 0; i < dest->mNumChildren; i++) {
  1085. dest->mChildren[i]->mParent = dest;
  1086. }
  1087. }
  1088. // ------------------------------------------------------------------------------------------------
  1089. void SceneCombiner::Copy(aiMetadata **_dest, const aiMetadata *src) {
  1090. if (nullptr == _dest || nullptr == src) {
  1091. return;
  1092. }
  1093. if (0 == src->mNumProperties) {
  1094. return;
  1095. }
  1096. aiMetadata *dest = *_dest = aiMetadata::Alloc(src->mNumProperties);
  1097. std::copy(src->mKeys, src->mKeys + src->mNumProperties, dest->mKeys);
  1098. for (unsigned int i = 0; i < src->mNumProperties; ++i) {
  1099. aiMetadataEntry &in = src->mValues[i];
  1100. aiMetadataEntry &out = dest->mValues[i];
  1101. out.mType = in.mType;
  1102. switch (dest->mValues[i].mType) {
  1103. case AI_BOOL:
  1104. out.mData = new bool(*static_cast<bool *>(in.mData));
  1105. break;
  1106. case AI_INT32:
  1107. out.mData = new int32_t(*static_cast<int32_t *>(in.mData));
  1108. break;
  1109. case AI_UINT64:
  1110. out.mData = new uint64_t(*static_cast<uint64_t *>(in.mData));
  1111. break;
  1112. case AI_FLOAT:
  1113. out.mData = new float(*static_cast<float *>(in.mData));
  1114. break;
  1115. case AI_DOUBLE:
  1116. out.mData = new double(*static_cast<double *>(in.mData));
  1117. break;
  1118. case AI_AISTRING:
  1119. out.mData = new aiString(*static_cast<aiString *>(in.mData));
  1120. break;
  1121. case AI_AIVECTOR3D:
  1122. out.mData = new aiVector3D(*static_cast<aiVector3D *>(in.mData));
  1123. break;
  1124. default:
  1125. ai_assert(false);
  1126. break;
  1127. }
  1128. }
  1129. }
  1130. // ------------------------------------------------------------------------------------------------
  1131. void SceneCombiner::Copy(aiString **_dest, const aiString *src) {
  1132. if (nullptr == _dest || nullptr == src) {
  1133. return;
  1134. }
  1135. aiString *dest = *_dest = new aiString();
  1136. // get a flat copy
  1137. *dest = *src;
  1138. }
  1139. #if (__GNUC__ >= 8 && __GNUC_MINOR__ >= 0)
  1140. #pragma GCC diagnostic pop
  1141. #endif
  1142. } // Namespace Assimp