MaterialSystem.cpp 18 KB

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