2
0

MaterialSystem.cpp 16 KB

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