MaterialSystem.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. /** @file MaterialSystem.cpp
  34. * @brief Implementation of the material system of the library
  35. */
  36. #include "Hash.h"
  37. #include "fast_atof.h"
  38. #include "ParsingUtils.h"
  39. #include "MaterialSystem.h"
  40. #include <assimp/types.h>
  41. #include <assimp/material.h>
  42. #include <assimp/DefaultLogger.hpp>
  43. #include "Macros.h"
  44. using namespace Assimp;
  45. // ------------------------------------------------------------------------------------------------
  46. // Get a specific property from a material
  47. aiReturn aiGetMaterialProperty(const aiMaterial* pMat,
  48. const char* pKey,
  49. unsigned int type,
  50. unsigned int index,
  51. const aiMaterialProperty** pPropOut)
  52. {
  53. ai_assert (pMat != NULL);
  54. ai_assert (pKey != NULL);
  55. ai_assert (pPropOut != NULL);
  56. /* Just search for a property with exactly this name ..
  57. * could be improved by hashing, but it's possibly
  58. * no worth the effort (we're bound to C structures,
  59. * thus std::map or derivates are not applicable. */
  60. for ( unsigned int i = 0; i < pMat->mNumProperties; ++i ) {
  61. aiMaterialProperty* prop = pMat->mProperties[i];
  62. if (prop /* just for safety ... */
  63. && 0 == strcmp( prop->mKey.data, pKey )
  64. && (UINT_MAX == type || prop->mSemantic == type) /* UINT_MAX is a wildcard, but this is undocumented :-) */
  65. && (UINT_MAX == index || prop->mIndex == index))
  66. {
  67. *pPropOut = pMat->mProperties[i];
  68. return AI_SUCCESS;
  69. }
  70. }
  71. *pPropOut = NULL;
  72. return AI_FAILURE;
  73. }
  74. // ------------------------------------------------------------------------------------------------
  75. // Get an array of floating-point values from the material.
  76. aiReturn aiGetMaterialFloatArray(const aiMaterial* pMat,
  77. const char* pKey,
  78. unsigned int type,
  79. unsigned int index,
  80. ai_real* pOut,
  81. unsigned int* pMax)
  82. {
  83. ai_assert (pOut != NULL);
  84. ai_assert (pMat != NULL);
  85. const aiMaterialProperty* prop;
  86. aiGetMaterialProperty(pMat,pKey,type,index, (const aiMaterialProperty**) &prop);
  87. if (!prop) {
  88. return AI_FAILURE;
  89. }
  90. // data is given in floats, convert to ai_real
  91. unsigned int iWrite = 0;
  92. if( aiPTI_Float == prop->mType || aiPTI_Buffer == prop->mType) {
  93. iWrite = prop->mDataLength / sizeof(float);
  94. if (pMax) {
  95. iWrite = std::min(*pMax,iWrite); ;
  96. }
  97. for (unsigned int a = 0; a < iWrite;++a) {
  98. pOut[a] = static_cast<ai_real> ( reinterpret_cast<float*>(prop->mData)[a] );
  99. }
  100. if (pMax) {
  101. *pMax = iWrite;
  102. }
  103. }
  104. // data is given in doubles, convert to float
  105. else if( aiPTI_Double == prop->mType) {
  106. iWrite = prop->mDataLength / sizeof(double);
  107. if (pMax) {
  108. iWrite = std::min(*pMax,iWrite); ;
  109. }
  110. for (unsigned int a = 0; a < iWrite;++a) {
  111. pOut[a] = static_cast<ai_real> ( reinterpret_cast<double*>(prop->mData)[a] );
  112. }
  113. if (pMax) {
  114. *pMax = iWrite;
  115. }
  116. }
  117. // data is given in ints, convert to float
  118. else if( aiPTI_Integer == prop->mType) {
  119. iWrite = prop->mDataLength / sizeof(int32_t);
  120. if (pMax) {
  121. iWrite = std::min(*pMax,iWrite); ;
  122. }
  123. for (unsigned int a = 0; a < iWrite;++a) {
  124. pOut[a] = static_cast<ai_real> ( reinterpret_cast<int32_t*>(prop->mData)[a] );
  125. }
  126. if (pMax) {
  127. *pMax = iWrite;
  128. }
  129. }
  130. // a string ... read floats separated by spaces
  131. else {
  132. if (pMax) {
  133. iWrite = *pMax;
  134. }
  135. // strings are zero-terminated with a 32 bit length prefix, so this is safe
  136. const char *cur = prop->mData + 4;
  137. ai_assert( prop->mDataLength >= 5 );
  138. ai_assert( !prop->mData[ prop->mDataLength - 1 ] );
  139. for ( unsigned int a = 0; ;++a) {
  140. cur = fast_atoreal_move<ai_real>(cur,pOut[a]);
  141. if ( a==iWrite-1 ) {
  142. break;
  143. }
  144. if ( !IsSpace(*cur) ) {
  145. DefaultLogger::get()->error("Material property" + std::string(pKey) +
  146. " is a string; failed to parse a float array out of it.");
  147. return AI_FAILURE;
  148. }
  149. }
  150. if (pMax) {
  151. *pMax = iWrite;
  152. }
  153. }
  154. return AI_SUCCESS;
  155. }
  156. // ------------------------------------------------------------------------------------------------
  157. // Get an array if integers from the material
  158. aiReturn aiGetMaterialIntegerArray(const aiMaterial* pMat,
  159. const char* pKey,
  160. unsigned int type,
  161. unsigned int index,
  162. int* pOut,
  163. unsigned int* pMax)
  164. {
  165. ai_assert (pOut != NULL);
  166. ai_assert (pMat != NULL);
  167. const aiMaterialProperty* prop;
  168. aiGetMaterialProperty(pMat,pKey,type,index,(const aiMaterialProperty**) &prop);
  169. if (!prop) {
  170. return AI_FAILURE;
  171. }
  172. // data is given in ints, simply copy it
  173. unsigned int iWrite = 0;
  174. if( aiPTI_Integer == prop->mType || aiPTI_Buffer == prop->mType) {
  175. iWrite = prop->mDataLength / sizeof(int32_t);
  176. if (pMax) {
  177. iWrite = std::min(*pMax,iWrite); ;
  178. }
  179. for (unsigned int a = 0; a < iWrite;++a) {
  180. pOut[a] = static_cast<int>(reinterpret_cast<int32_t*>(prop->mData)[a]);
  181. }
  182. if (pMax) {
  183. *pMax = iWrite;
  184. }
  185. }
  186. // data is given in floats convert to int
  187. else if( aiPTI_Float == prop->mType) {
  188. iWrite = prop->mDataLength / sizeof(float);
  189. if (pMax) {
  190. iWrite = std::min(*pMax,iWrite); ;
  191. }
  192. for (unsigned int a = 0; a < iWrite;++a) {
  193. pOut[a] = static_cast<int>(reinterpret_cast<float*>(prop->mData)[a]);
  194. }
  195. if (pMax) {
  196. *pMax = iWrite;
  197. }
  198. }
  199. // it is a string ... no way to read something out of this
  200. else {
  201. if (pMax) {
  202. iWrite = *pMax;
  203. }
  204. // strings are zero-terminated with a 32 bit length prefix, so this is safe
  205. const char *cur = prop->mData+4;
  206. ai_assert( prop->mDataLength >= 5 );
  207. ai_assert( !prop->mData[ prop->mDataLength - 1 ] );
  208. for (unsigned int a = 0; ;++a) {
  209. pOut[a] = strtol10(cur,&cur);
  210. if(a==iWrite-1) {
  211. break;
  212. }
  213. if(!IsSpace(*cur)) {
  214. DefaultLogger::get()->error("Material property" + std::string(pKey) +
  215. " is a string; failed to parse an integer array out of it.");
  216. return AI_FAILURE;
  217. }
  218. }
  219. if (pMax) {
  220. *pMax = iWrite;
  221. }
  222. }
  223. return AI_SUCCESS;
  224. }
  225. // ------------------------------------------------------------------------------------------------
  226. // Get a color (3 or 4 floats) from the material
  227. aiReturn aiGetMaterialColor(const aiMaterial* pMat,
  228. const char* pKey,
  229. unsigned int type,
  230. unsigned int index,
  231. aiColor4D* pOut)
  232. {
  233. unsigned int iMax = 4;
  234. const aiReturn eRet = aiGetMaterialFloatArray(pMat,pKey,type,index,(ai_real*)pOut,&iMax);
  235. // if no alpha channel is defined: set it to 1.0
  236. if (3 == iMax) {
  237. pOut->a = 1.0;
  238. }
  239. return eRet;
  240. }
  241. // ------------------------------------------------------------------------------------------------
  242. // Get a aiUVTransform (4 floats) from the material
  243. aiReturn aiGetMaterialUVTransform(const aiMaterial* pMat,
  244. const char* pKey,
  245. unsigned int type,
  246. unsigned int index,
  247. aiUVTransform* pOut)
  248. {
  249. unsigned int iMax = 4;
  250. return aiGetMaterialFloatArray(pMat,pKey,type,index,(ai_real*)pOut,&iMax);
  251. }
  252. // ------------------------------------------------------------------------------------------------
  253. // Get a string from the material
  254. aiReturn aiGetMaterialString(const aiMaterial* pMat,
  255. const char* pKey,
  256. unsigned int type,
  257. unsigned int index,
  258. aiString* pOut)
  259. {
  260. ai_assert (pOut != NULL);
  261. const aiMaterialProperty* prop;
  262. aiGetMaterialProperty(pMat,pKey,type,index,(const aiMaterialProperty**)&prop);
  263. if (!prop) {
  264. return AI_FAILURE;
  265. }
  266. if( aiPTI_String == prop->mType) {
  267. ai_assert(prop->mDataLength>=5);
  268. // The string is stored as 32 but length prefix followed by zero-terminated UTF8 data
  269. pOut->length = static_cast<unsigned int>(*reinterpret_cast<uint32_t*>(prop->mData));
  270. ai_assert( pOut->length+1+4==prop->mDataLength );
  271. ai_assert( !prop->mData[ prop->mDataLength - 1 ] );
  272. memcpy(pOut->data,prop->mData+4,pOut->length+1);
  273. }
  274. else {
  275. // TODO - implement lexical cast as well
  276. DefaultLogger::get()->error("Material property" + std::string(pKey) +
  277. " was found, but is no string" );
  278. return AI_FAILURE;
  279. }
  280. return AI_SUCCESS;
  281. }
  282. // ------------------------------------------------------------------------------------------------
  283. // Get the number of textures on a particular texture stack
  284. ASSIMP_API unsigned int aiGetMaterialTextureCount(const C_STRUCT aiMaterial* pMat,
  285. C_ENUM aiTextureType type)
  286. {
  287. ai_assert (pMat != NULL);
  288. // Textures are always stored with ascending indices (ValidateDS provides a check, so we don't need to do it again)
  289. unsigned int max = 0;
  290. for (unsigned int i = 0; i < pMat->mNumProperties;++i) {
  291. aiMaterialProperty* prop = pMat->mProperties[i];
  292. if ( prop /* just a sanity check ... */
  293. && 0 == strcmp( prop->mKey.data, _AI_MATKEY_TEXTURE_BASE )
  294. && prop->mSemantic == type) {
  295. max = std::max(max,prop->mIndex+1);
  296. }
  297. }
  298. return max;
  299. }
  300. // ------------------------------------------------------------------------------------------------
  301. aiReturn aiGetMaterialTexture(const C_STRUCT aiMaterial* mat,
  302. aiTextureType type,
  303. unsigned int index,
  304. C_STRUCT aiString* path,
  305. aiTextureMapping* _mapping /*= NULL*/,
  306. unsigned int* uvindex /*= NULL*/,
  307. ai_real* blend /*= NULL*/,
  308. aiTextureOp* op /*= NULL*/,
  309. aiTextureMapMode* mapmode /*= NULL*/,
  310. unsigned int* flags /*= NULL*/
  311. )
  312. {
  313. ai_assert(NULL != mat && NULL != path);
  314. // Get the path to the texture
  315. if (AI_SUCCESS != aiGetMaterialString(mat,AI_MATKEY_TEXTURE(type,index),path)) {
  316. return AI_FAILURE;
  317. }
  318. // Determine mapping type
  319. aiTextureMapping mapping = aiTextureMapping_UV;
  320. aiGetMaterialInteger(mat,AI_MATKEY_MAPPING(type,index),(int*)&mapping);
  321. if (_mapping)
  322. *_mapping = mapping;
  323. // Get UV index
  324. if (aiTextureMapping_UV == mapping && uvindex) {
  325. aiGetMaterialInteger(mat,AI_MATKEY_UVWSRC(type,index),(int*)uvindex);
  326. }
  327. // Get blend factor
  328. if (blend) {
  329. aiGetMaterialFloat(mat,AI_MATKEY_TEXBLEND(type,index),blend);
  330. }
  331. // Get texture operation
  332. if (op){
  333. aiGetMaterialInteger(mat,AI_MATKEY_TEXOP(type,index),(int*)op);
  334. }
  335. // Get texture mapping modes
  336. if (mapmode) {
  337. aiGetMaterialInteger(mat,AI_MATKEY_MAPPINGMODE_U(type,index),(int*)&mapmode[0]);
  338. aiGetMaterialInteger(mat,AI_MATKEY_MAPPINGMODE_V(type,index),(int*)&mapmode[1]);
  339. }
  340. // Get texture flags
  341. if (flags){
  342. aiGetMaterialInteger(mat,AI_MATKEY_TEXFLAGS(type,index),(int*)flags);
  343. }
  344. return AI_SUCCESS;
  345. }
  346. static const unsigned int DefaultNumAllocated = 5;
  347. // ------------------------------------------------------------------------------------------------
  348. // Construction. Actually the one and only way to get an aiMaterial instance
  349. aiMaterial::aiMaterial()
  350. : mProperties( NULL )
  351. , mNumProperties( 0 )
  352. , mNumAllocated( DefaultNumAllocated ) {
  353. // Allocate 5 entries by default
  354. mProperties = new aiMaterialProperty*[ DefaultNumAllocated ];
  355. }
  356. // ------------------------------------------------------------------------------------------------
  357. aiMaterial::~aiMaterial()
  358. {
  359. Clear();
  360. delete[] mProperties;
  361. }
  362. // ------------------------------------------------------------------------------------------------
  363. void aiMaterial::Clear()
  364. {
  365. for (unsigned int i = 0; i < mNumProperties;++i) {
  366. // delete this entry
  367. delete mProperties[i];
  368. AI_DEBUG_INVALIDATE_PTR(mProperties[i]);
  369. }
  370. mNumProperties = 0;
  371. // The array remains allocated, we just invalidated its contents
  372. }
  373. // ------------------------------------------------------------------------------------------------
  374. aiReturn aiMaterial::RemoveProperty (const char* pKey,unsigned int type,
  375. unsigned int index
  376. )
  377. {
  378. ai_assert(NULL != pKey);
  379. for (unsigned int i = 0; i < mNumProperties;++i) {
  380. aiMaterialProperty* prop = mProperties[i];
  381. if (prop && !strcmp( prop->mKey.data, pKey ) &&
  382. prop->mSemantic == type && prop->mIndex == index)
  383. {
  384. // Delete this entry
  385. delete mProperties[i];
  386. // collapse the array behind --.
  387. --mNumProperties;
  388. for (unsigned int a = i; a < mNumProperties;++a) {
  389. mProperties[a] = mProperties[a+1];
  390. }
  391. return AI_SUCCESS;
  392. }
  393. }
  394. return AI_FAILURE;
  395. }
  396. // ------------------------------------------------------------------------------------------------
  397. aiReturn aiMaterial::AddBinaryProperty (const void* pInput,
  398. unsigned int pSizeInBytes,
  399. const char* pKey,
  400. unsigned int type,
  401. unsigned int index,
  402. aiPropertyTypeInfo pType
  403. )
  404. {
  405. ai_assert (pInput != NULL);
  406. ai_assert (pKey != NULL);
  407. ai_assert (0 != pSizeInBytes);
  408. if ( 0 == pSizeInBytes ) {
  409. }
  410. // first search the list whether there is already an entry with this key
  411. unsigned int iOutIndex = UINT_MAX;
  412. for (unsigned int i = 0; i < mNumProperties;++i) {
  413. aiMaterialProperty* prop = mProperties[i];
  414. if (prop /* just for safety */ && !strcmp( prop->mKey.data, pKey ) &&
  415. prop->mSemantic == type && prop->mIndex == index){
  416. delete mProperties[i];
  417. iOutIndex = i;
  418. }
  419. }
  420. // Allocate a new material property
  421. aiMaterialProperty* pcNew = new aiMaterialProperty();
  422. // .. and fill it
  423. pcNew->mType = pType;
  424. pcNew->mSemantic = type;
  425. pcNew->mIndex = index;
  426. pcNew->mDataLength = pSizeInBytes;
  427. pcNew->mData = new char[pSizeInBytes];
  428. memcpy (pcNew->mData,pInput,pSizeInBytes);
  429. pcNew->mKey.length = ::strlen(pKey);
  430. ai_assert ( MAXLEN > pcNew->mKey.length);
  431. strcpy( pcNew->mKey.data, pKey );
  432. if (UINT_MAX != iOutIndex) {
  433. mProperties[iOutIndex] = pcNew;
  434. return AI_SUCCESS;
  435. }
  436. // resize the array ... double the storage allocated
  437. if (mNumProperties == mNumAllocated) {
  438. const unsigned int iOld = mNumAllocated;
  439. mNumAllocated *= 2;
  440. aiMaterialProperty** ppTemp;
  441. try {
  442. ppTemp = new aiMaterialProperty*[mNumAllocated];
  443. } catch (std::bad_alloc&) {
  444. delete pcNew;
  445. return AI_OUTOFMEMORY;
  446. }
  447. // just copy all items over; then replace the old array
  448. memcpy (ppTemp,mProperties,iOld * sizeof(void*));
  449. delete[] mProperties;
  450. mProperties = ppTemp;
  451. }
  452. // push back ...
  453. mProperties[mNumProperties++] = pcNew;
  454. return AI_SUCCESS;
  455. }
  456. // ------------------------------------------------------------------------------------------------
  457. aiReturn aiMaterial::AddProperty (const aiString* pInput,
  458. const char* pKey,
  459. unsigned int type,
  460. unsigned int index)
  461. {
  462. // We don't want to add the whole buffer .. write a 32 bit length
  463. // prefix followed by the zero-terminated UTF8 string.
  464. // (HACK) I don't want to break the ABI now, but we definitely
  465. // ought to change aiString::mLength to uint32_t one day.
  466. if (sizeof(size_t) == 8) {
  467. aiString copy = *pInput;
  468. uint32_t* s = reinterpret_cast<uint32_t*>(&copy.length);
  469. s[1] = static_cast<uint32_t>(pInput->length);
  470. return AddBinaryProperty(s+1,
  471. static_cast<unsigned int>(pInput->length+1+4),
  472. pKey,
  473. type,
  474. index,
  475. aiPTI_String);
  476. }
  477. ai_assert(sizeof(size_t)==4);
  478. return AddBinaryProperty(pInput,
  479. static_cast<unsigned int>(pInput->length+1+4),
  480. pKey,
  481. type,
  482. index,
  483. aiPTI_String);
  484. }
  485. // ------------------------------------------------------------------------------------------------
  486. uint32_t Assimp::ComputeMaterialHash(const aiMaterial* mat, bool includeMatName /*= false*/)
  487. {
  488. uint32_t hash = 1503; // magic start value, chosen to be my birthday :-)
  489. for ( unsigned int i = 0; i < mat->mNumProperties; ++i ) {
  490. aiMaterialProperty* prop;
  491. // Exclude all properties whose first character is '?' from the hash
  492. // See doc for aiMaterialProperty.
  493. if ((prop = mat->mProperties[i]) && (includeMatName || prop->mKey.data[0] != '?')) {
  494. hash = SuperFastHash(prop->mKey.data,(unsigned int)prop->mKey.length,hash);
  495. hash = SuperFastHash(prop->mData,prop->mDataLength,hash);
  496. // Combine the semantic and the index with the hash
  497. hash = SuperFastHash((const char*)&prop->mSemantic,sizeof(unsigned int),hash);
  498. hash = SuperFastHash((const char*)&prop->mIndex,sizeof(unsigned int),hash);
  499. }
  500. }
  501. return hash;
  502. }
  503. // ------------------------------------------------------------------------------------------------
  504. void aiMaterial::CopyPropertyList(aiMaterial* pcDest,
  505. const aiMaterial* pcSrc
  506. )
  507. {
  508. ai_assert(NULL != pcDest);
  509. ai_assert(NULL != pcSrc);
  510. unsigned int iOldNum = pcDest->mNumProperties;
  511. pcDest->mNumAllocated += pcSrc->mNumAllocated;
  512. pcDest->mNumProperties += pcSrc->mNumProperties;
  513. aiMaterialProperty** pcOld = pcDest->mProperties;
  514. pcDest->mProperties = new aiMaterialProperty*[pcDest->mNumAllocated];
  515. if (iOldNum && pcOld) {
  516. for (unsigned int i = 0; i < iOldNum;++i) {
  517. pcDest->mProperties[i] = pcOld[i];
  518. }
  519. }
  520. if ( pcOld ) {
  521. delete[] pcOld;
  522. }
  523. for (unsigned int i = iOldNum; i< pcDest->mNumProperties;++i) {
  524. aiMaterialProperty* propSrc = pcSrc->mProperties[i];
  525. // search whether we have already a property with this name -> if yes, overwrite it
  526. aiMaterialProperty* prop;
  527. for ( unsigned int q = 0; q < iOldNum; ++q ) {
  528. prop = pcDest->mProperties[q];
  529. if (prop /* just for safety */ && prop->mKey == propSrc->mKey && prop->mSemantic == propSrc->mSemantic
  530. && prop->mIndex == propSrc->mIndex) {
  531. delete prop;
  532. // collapse the whole array ...
  533. memmove(&pcDest->mProperties[q],&pcDest->mProperties[q+1],i-q);
  534. i--;
  535. pcDest->mNumProperties--;
  536. }
  537. }
  538. // Allocate the output property and copy the source property
  539. prop = pcDest->mProperties[i] = new aiMaterialProperty();
  540. prop->mKey = propSrc->mKey;
  541. prop->mDataLength = propSrc->mDataLength;
  542. prop->mType = propSrc->mType;
  543. prop->mSemantic = propSrc->mSemantic;
  544. prop->mIndex = propSrc->mIndex;
  545. prop->mData = new char[propSrc->mDataLength];
  546. memcpy(prop->mData,propSrc->mData,prop->mDataLength);
  547. }
  548. }