SceneCombiner.cpp 46 KB

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