MaterialSystem.cpp 16 KB

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