SceneCombiner.cpp 45 KB

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