SceneCombiner.cpp 50 KB

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