SceneCombiner.cpp 40 KB

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