2
0

SceneCombiner.cpp 48 KB

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