SceneCombiner.cpp 46 KB

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