SceneCombiner.cpp 50 KB

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