SceneCombiner.cpp 48 KB

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