MaterialSystem.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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. #include "AssimpPCH.h"
  34. #include "Hash.h"
  35. using namespace Assimp;
  36. // ------------------------------------------------------------------------------------------------
  37. aiReturn aiGetMaterialProperty(const aiMaterial* pMat,
  38. const char* pKey,
  39. unsigned int type,
  40. unsigned int index,
  41. const aiMaterialProperty** pPropOut)
  42. {
  43. ai_assert (pMat != NULL);
  44. ai_assert (pKey != NULL);
  45. ai_assert (pPropOut != NULL);
  46. for (unsigned int i = 0; i < pMat->mNumProperties;++i)
  47. {
  48. aiMaterialProperty* prop = pMat->mProperties[i];
  49. if (prop && !::strcmp( prop->mKey.data, pKey ) &&
  50. prop->mSemantic == type && prop->mIndex == index)
  51. {
  52. *pPropOut = pMat->mProperties[i];
  53. return AI_SUCCESS;
  54. }
  55. }
  56. *pPropOut = NULL;
  57. return AI_FAILURE;
  58. }
  59. // ------------------------------------------------------------------------------------------------
  60. aiReturn aiGetMaterialFloatArray(const aiMaterial* pMat,
  61. const char* pKey,
  62. unsigned int type,
  63. unsigned int index,
  64. float* pOut,
  65. unsigned int* pMax)
  66. {
  67. ai_assert (pMat != NULL);
  68. ai_assert (pKey != NULL);
  69. ai_assert (pOut != NULL);
  70. for (unsigned int i = 0; i < pMat->mNumProperties;++i)
  71. {
  72. aiMaterialProperty* prop = pMat->mProperties[i];
  73. if (prop && !::strcmp( prop->mKey.data, pKey ) &&
  74. prop->mSemantic == type && prop->mIndex == index)
  75. {
  76. // data is given in floats, simply copy it
  77. if( aiPTI_Float == pMat->mProperties[i]->mType ||
  78. aiPTI_Buffer == pMat->mProperties[i]->mType)
  79. {
  80. unsigned int iWrite = pMat->mProperties[i]->mDataLength / sizeof(float);
  81. if (pMax)iWrite = *pMax < iWrite ? *pMax : iWrite;
  82. ::memcpy (pOut, pMat->mProperties[i]->mData, iWrite * sizeof (float));
  83. if (pMax)*pMax = iWrite;
  84. }
  85. // data is given in ints, convert to float
  86. else if( aiPTI_Integer == pMat->mProperties[i]->mType)
  87. {
  88. unsigned int iWrite = pMat->mProperties[i]->mDataLength / sizeof(int);
  89. if (pMax)iWrite = *pMax < iWrite ? *pMax : iWrite;
  90. for (unsigned int a = 0; a < iWrite;++a)
  91. {
  92. pOut[a] = (float) ((int*)pMat->mProperties[i]->mData)[a];
  93. }
  94. if (pMax)*pMax = iWrite;
  95. }
  96. // it is a string ... no way to read something out of this
  97. else
  98. {
  99. if (pMax)*pMax = 0;
  100. return AI_FAILURE;
  101. }
  102. return AI_SUCCESS;
  103. }
  104. }
  105. return AI_FAILURE;
  106. }
  107. // ------------------------------------------------------------------------------------------------
  108. aiReturn aiGetMaterialIntegerArray(const aiMaterial* pMat,
  109. const char* pKey,
  110. unsigned int type,
  111. unsigned int index,
  112. int* pOut,
  113. unsigned int* pMax)
  114. {
  115. ai_assert (pMat != NULL);
  116. ai_assert (pKey != NULL);
  117. ai_assert (pOut != NULL);
  118. for (unsigned int i = 0; i < pMat->mNumProperties;++i)
  119. {
  120. aiMaterialProperty* prop = pMat->mProperties[i];
  121. if (prop && !::strcmp( prop->mKey.data, pKey ) &&
  122. prop->mSemantic == type && prop->mIndex == index)
  123. {
  124. // data is given in ints, simply copy it
  125. if( aiPTI_Integer == pMat->mProperties[i]->mType ||
  126. aiPTI_Buffer == pMat->mProperties[i]->mType)
  127. {
  128. unsigned int iWrite = pMat->mProperties[i]->mDataLength / sizeof(int);
  129. if (pMax)iWrite = *pMax < iWrite ? *pMax : iWrite;
  130. ::memcpy (pOut, pMat->mProperties[i]->mData, iWrite * sizeof (int));
  131. if (pMax)*pMax = iWrite;
  132. }
  133. // data is given in floats convert to int (lossy!)
  134. else if( aiPTI_Float == pMat->mProperties[i]->mType)
  135. {
  136. unsigned int iWrite = pMat->mProperties[i]->mDataLength / sizeof(float);
  137. if (pMax)iWrite = *pMax < iWrite ? *pMax : iWrite;
  138. for (unsigned int a = 0; a < iWrite;++a)
  139. {
  140. pOut[a] = (int) ((float*)pMat->mProperties[i]->mData)[a];
  141. }
  142. if (pMax)*pMax = iWrite;
  143. }
  144. // it is a string ... no way to read something out of this
  145. else
  146. {
  147. if (pMax)*pMax = 0;
  148. return AI_FAILURE;
  149. }
  150. return AI_SUCCESS;
  151. }
  152. }
  153. return AI_FAILURE;
  154. }
  155. // ------------------------------------------------------------------------------------------------
  156. aiReturn aiGetMaterialColor(const aiMaterial* pMat,
  157. const char* pKey,
  158. unsigned int type,
  159. unsigned int index,
  160. aiColor4D* pOut)
  161. {
  162. unsigned int iMax = 4;
  163. aiReturn eRet = aiGetMaterialFloatArray(pMat,pKey,type,index,(float*)pOut,&iMax);
  164. // if no alpha channel is provided set it to 1.0 by default
  165. if (3 == iMax)pOut->a = 1.0f;
  166. return eRet;
  167. }
  168. // ------------------------------------------------------------------------------------------------
  169. aiReturn aiGetMaterialString(const aiMaterial* pMat,
  170. const char* pKey,
  171. unsigned int type,
  172. unsigned int index,
  173. aiString* pOut)
  174. {
  175. ai_assert (pMat != NULL);
  176. ai_assert (pKey != NULL);
  177. ai_assert (pOut != NULL);
  178. for (unsigned int i = 0; i < pMat->mNumProperties;++i)
  179. {
  180. aiMaterialProperty* prop = pMat->mProperties[i];
  181. if (prop && !::strcmp( prop->mKey.data, pKey ) &&
  182. prop->mSemantic == type && prop->mIndex == index)
  183. {
  184. if( aiPTI_String == pMat->mProperties[i]->mType)
  185. {
  186. const aiString* pcSrc = (const aiString*)pMat->mProperties[i]->mData;
  187. ::memcpy (pOut->data, pcSrc->data, (pOut->length = pcSrc->length)+1);
  188. }
  189. // Wrong type
  190. else return AI_FAILURE;
  191. return AI_SUCCESS;
  192. }
  193. }
  194. return AI_FAILURE;
  195. }
  196. // ------------------------------------------------------------------------------------------------
  197. MaterialHelper::MaterialHelper()
  198. {
  199. // Allocate 5 entries by default
  200. mNumProperties = 0;
  201. mNumAllocated = 5;
  202. mProperties = new aiMaterialProperty*[5];
  203. }
  204. // ------------------------------------------------------------------------------------------------
  205. MaterialHelper::~MaterialHelper()
  206. {
  207. Clear();
  208. }
  209. // ------------------------------------------------------------------------------------------------
  210. void MaterialHelper::Clear()
  211. {
  212. for (unsigned int i = 0; i < mNumProperties;++i)
  213. {
  214. // delete this entry
  215. delete mProperties[i];
  216. }
  217. mNumProperties = 0;
  218. // The array remains
  219. }
  220. // ------------------------------------------------------------------------------------------------
  221. uint32_t MaterialHelper::ComputeHash()
  222. {
  223. uint32_t hash = 1503; // magic start value, choosen to be my birthday :-)
  224. for (unsigned int i = 0; i < this->mNumProperties;++i)
  225. {
  226. aiMaterialProperty* prop;
  227. // NOTE: We need to exclude the material name from the hash
  228. if ((prop = this->mProperties[i]) && ::strcmp(prop->mKey.data,"$mat.name"))
  229. {
  230. hash = SuperFastHash(prop->mKey.data,(unsigned int)prop->mKey.length,hash);
  231. hash = SuperFastHash(prop->mData,prop->mDataLength,hash);
  232. // Combine the semantic and the index with the hash
  233. // We print them to a string to make sure the quality
  234. // of the hash isn't decreased.
  235. char buff[32];
  236. unsigned int len;
  237. len = itoa10(buff,prop->mSemantic);
  238. hash = SuperFastHash(buff,len-1,hash);
  239. len = itoa10(buff,prop->mIndex);
  240. hash = SuperFastHash(buff,len-1,hash);
  241. }
  242. }
  243. return hash;
  244. }
  245. // ------------------------------------------------------------------------------------------------
  246. aiReturn MaterialHelper::RemoveProperty (const char* pKey,unsigned int type,
  247. unsigned int index)
  248. {
  249. ai_assert(NULL != pKey);
  250. for (unsigned int i = 0; i < mNumProperties;++i)
  251. {
  252. aiMaterialProperty* prop = mProperties[i];
  253. if (prop && !::strcmp( prop->mKey.data, pKey ) &&
  254. prop->mSemantic == type && prop->mIndex == index)
  255. {
  256. // Delete this entry
  257. delete mProperties[i];
  258. // collapse the array behind --.
  259. --mNumProperties;
  260. for (unsigned int a = i; a < mNumProperties;++a)
  261. {
  262. mProperties[a] = mProperties[a+1];
  263. }
  264. return AI_SUCCESS;
  265. }
  266. }
  267. return AI_FAILURE;
  268. }
  269. // ------------------------------------------------------------------------------------------------
  270. aiReturn MaterialHelper::AddBinaryProperty (const void* pInput,
  271. unsigned int pSizeInBytes,
  272. const char* pKey,
  273. unsigned int type,
  274. unsigned int index,
  275. aiPropertyTypeInfo pType)
  276. {
  277. ai_assert (pInput != NULL);
  278. ai_assert (pKey != NULL);
  279. ai_assert (0 != pSizeInBytes);
  280. // first search the list whether there is already an entry
  281. // with this name.
  282. unsigned int iOutIndex = 0xFFFFFFFF;
  283. for (unsigned int i = 0; i < mNumProperties;++i)
  284. {
  285. aiMaterialProperty* prop = mProperties[i];
  286. if (prop && !::strcmp( prop->mKey.data, pKey ) &&
  287. prop->mSemantic == type && prop->mIndex == index)
  288. {
  289. // delete this entry
  290. delete this->mProperties[i];
  291. iOutIndex = i;
  292. }
  293. }
  294. // Allocate a new material property
  295. aiMaterialProperty* pcNew = new aiMaterialProperty();
  296. // Fill this
  297. pcNew->mType = pType;
  298. pcNew->mSemantic = type;
  299. pcNew->mIndex = index;
  300. pcNew->mDataLength = pSizeInBytes;
  301. pcNew->mData = new char[pSizeInBytes];
  302. ::memcpy (pcNew->mData,pInput,pSizeInBytes);
  303. pcNew->mKey.length = ::strlen(pKey);
  304. ai_assert ( MAXLEN > pcNew->mKey.length);
  305. ::strcpy( pcNew->mKey.data, pKey );
  306. if (0xFFFFFFFF != iOutIndex)
  307. {
  308. mProperties[iOutIndex] = pcNew;
  309. return AI_SUCCESS;
  310. }
  311. // resize the array ... allocate storage for 5 other properties
  312. if (mNumProperties == mNumAllocated)
  313. {
  314. unsigned int iOld = mNumAllocated;
  315. mNumAllocated += 5;
  316. aiMaterialProperty** ppTemp = new aiMaterialProperty*[mNumAllocated];
  317. if (NULL == ppTemp)return AI_OUTOFMEMORY;
  318. ::memcpy (ppTemp,mProperties,iOld * sizeof(void*));
  319. delete[] mProperties;
  320. mProperties = ppTemp;
  321. }
  322. // push back ...
  323. mProperties[mNumProperties++] = pcNew;
  324. return AI_SUCCESS;
  325. }
  326. // ------------------------------------------------------------------------------------------------
  327. aiReturn MaterialHelper::AddProperty (const aiString* pInput,
  328. const char* pKey,
  329. unsigned int type,
  330. unsigned int index)
  331. {
  332. // Fix ... don't keep the whole string buffer
  333. return this->AddBinaryProperty(pInput,(unsigned int)pInput->length+1+
  334. (unsigned int)(((uint8_t*)&pInput->data - (uint8_t*)&pInput->length)),
  335. pKey,type,index, aiPTI_String);
  336. }
  337. // ------------------------------------------------------------------------------------------------
  338. void MaterialHelper::CopyPropertyList(MaterialHelper* pcDest,
  339. const MaterialHelper* pcSrc)
  340. {
  341. ai_assert(NULL != pcDest);
  342. ai_assert(NULL != pcSrc);
  343. unsigned int iOldNum = pcDest->mNumProperties;
  344. pcDest->mNumAllocated += pcSrc->mNumAllocated;
  345. pcDest->mNumProperties += pcSrc->mNumProperties;
  346. aiMaterialProperty** pcOld = pcDest->mProperties;
  347. pcDest->mProperties = new aiMaterialProperty*[pcDest->mNumAllocated];
  348. if (iOldNum && pcOld)
  349. {
  350. for (unsigned int i = 0; i < iOldNum;++i)
  351. pcDest->mProperties[i] = pcOld[i];
  352. delete[] pcOld;
  353. }
  354. for (unsigned int i = iOldNum; i< pcDest->mNumProperties;++i)
  355. {
  356. aiMaterialProperty* propSrc = pcSrc->mProperties[i];
  357. // search whether we have already a property with this name
  358. // (if yes we overwrite the old one)
  359. aiMaterialProperty* prop;
  360. for (unsigned int q = 0; q < iOldNum;++q)
  361. {
  362. prop = pcDest->mProperties[q];
  363. if (prop && prop->mKey == propSrc->mKey &&
  364. prop->mSemantic == propSrc->mSemantic && prop->mIndex == propSrc->mIndex)
  365. {
  366. delete prop;
  367. // collapse the whole array ...
  368. ::memmove(&pcDest->mProperties[q],&pcDest->mProperties[q+1],i-q);
  369. i--;
  370. pcDest->mNumProperties--;
  371. }
  372. }
  373. // Allocate the output property and copy the source property
  374. prop = pcDest->mProperties[i] = new aiMaterialProperty();
  375. prop->mKey = propSrc->mKey;
  376. prop->mDataLength = propSrc->mDataLength;
  377. prop->mType = propSrc->mType;
  378. prop->mSemantic = propSrc->mSemantic;
  379. prop->mIndex = propSrc->mIndex;
  380. prop->mData = new char[propSrc->mDataLength];
  381. ::memcpy(prop->mData,propSrc->mData,prop->mDataLength);
  382. }
  383. return;
  384. }
  385. // ------------------------------------------------------------------------------------------------
  386. aiReturn aiGetMaterialTexture(const C_STRUCT aiMaterial* mat,
  387. aiTextureType type,
  388. unsigned int index,
  389. C_STRUCT aiString* path,
  390. aiTextureMapping* _mapping /*= NULL*/,
  391. unsigned int* uvindex /*= NULL*/,
  392. float* blend /*= NULL*/,
  393. aiTextureOp* op /*= NULL*/,
  394. aiTextureMapMode* mapmode /*= NULL*/)
  395. {
  396. ai_assert(NULL != mat && NULL != path);
  397. // Get the path to the texture
  398. if (AI_SUCCESS != aiGetMaterialString(mat,AI_MATKEY_TEXTURE(type,index),path))
  399. {
  400. return AI_FAILURE;
  401. }
  402. // Determine the mapping type of the texture
  403. aiTextureMapping mapping = aiTextureMapping_UV;
  404. aiGetMaterialInteger(mat,AI_MATKEY_MAPPING(type,index),(int*)&mapping);
  405. if (_mapping)*_mapping = mapping;
  406. // Get the UV index of the texture
  407. if (aiTextureMapping_UV == mapping && uvindex)
  408. {
  409. aiGetMaterialInteger(mat,AI_MATKEY_UVWSRC(type,index),(int*)uvindex);
  410. }
  411. // Get the blend factor of the texture
  412. if (blend)
  413. {
  414. aiGetMaterialFloat(mat,AI_MATKEY_TEXBLEND(type,index),blend);
  415. }
  416. // Get the texture operation of the texture
  417. if (op)
  418. {
  419. aiGetMaterialInteger(mat,AI_MATKEY_TEXOP(type,index),(int*)op);
  420. }
  421. // get the texture mapping modes for the texture
  422. if (mapmode)
  423. {
  424. aiGetMaterialInteger(mat,AI_MATKEY_MAPPINGMODE_U(type,index),(int*)&mapmode[0]);
  425. aiGetMaterialInteger(mat,AI_MATKEY_MAPPINGMODE_V(type,index),(int*)&mapmode[1]);
  426. aiGetMaterialInteger(mat,AI_MATKEY_MAPPINGMODE_W(type,index),(int*)&mapmode[2]);
  427. }
  428. return AI_SUCCESS;
  429. }