SceneCombiner.cpp 45 KB

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