MaterialSystem.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. Free 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 "assimp.h"
  34. #include "aiMaterial.h"
  35. #include "assimp.hpp"
  36. #include "MaterialSystem.h"
  37. #include "../include/aiAssert.h"
  38. using namespace Assimp;
  39. // ------------------------------------------------------------------------------------------------
  40. aiReturn aiGetMaterialProperty(const aiMaterial* pMat,
  41. const char* pKey,
  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. if (NULL != pMat->mProperties[i])
  50. {
  51. if (0 == ASSIMP_stricmp( pMat->mProperties[i]->mKey->data, pKey ))
  52. {
  53. *pPropOut = pMat->mProperties[i];
  54. return AI_SUCCESS;
  55. }
  56. }
  57. }
  58. *pPropOut = NULL;
  59. return AI_FAILURE;
  60. }
  61. // ------------------------------------------------------------------------------------------------
  62. aiReturn aiGetMaterialFloatArray(const aiMaterial* pMat,
  63. const char* pKey,
  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. if (NULL != pMat->mProperties[i])
  73. {
  74. if (0 == ASSIMP_stricmp( pMat->mProperties[i]->mKey->data, pKey ))
  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]->
  81. mDataLength / sizeof(float);
  82. if (NULL != pMax)
  83. iWrite = *pMax < iWrite ? *pMax : iWrite;
  84. memcpy (pOut, pMat->mProperties[i]->mData, iWrite * sizeof (float));
  85. if (NULL != pMax)
  86. *pMax = iWrite;
  87. }
  88. // data is given in ints, convert to float
  89. else if( aiPTI_Integer == pMat->mProperties[i]->mType)
  90. {
  91. unsigned int iWrite = pMat->mProperties[i]->
  92. mDataLength / sizeof(int);
  93. if (NULL != pMax)
  94. iWrite = *pMax < iWrite ? *pMax : iWrite;
  95. for (unsigned int a = 0; a < iWrite;++a)
  96. {
  97. pOut[a] = (float) ((int*)pMat->mProperties[i]->mData)[a];
  98. }
  99. if (NULL != pMax)
  100. *pMax = iWrite;
  101. }
  102. // it is a string ... no way to read something out of this
  103. else
  104. {
  105. if (NULL != pMax)
  106. *pMax = 0;
  107. return AI_FAILURE;
  108. }
  109. return AI_SUCCESS;
  110. }
  111. }
  112. }
  113. return AI_FAILURE;
  114. }
  115. // ------------------------------------------------------------------------------------------------
  116. aiReturn aiGetMaterialIntegerArray(const aiMaterial* pMat,
  117. const char* pKey,
  118. int* pOut,
  119. unsigned int* pMax)
  120. {
  121. ai_assert (pMat != NULL);
  122. ai_assert (pKey != NULL);
  123. ai_assert (pOut != NULL);
  124. for (unsigned int i = 0; i < pMat->mNumProperties;++i)
  125. {
  126. if (NULL != pMat->mProperties[i])
  127. {
  128. if (0 == ASSIMP_stricmp( pMat->mProperties[i]->mKey->data, pKey ))
  129. {
  130. // data is given in ints, simply copy it
  131. if( aiPTI_Integer == pMat->mProperties[i]->mType ||
  132. aiPTI_Buffer == pMat->mProperties[i]->mType)
  133. {
  134. unsigned int iWrite = pMat->mProperties[i]->
  135. mDataLength / sizeof(int);
  136. if (NULL != pMax)
  137. iWrite = *pMax < iWrite ? *pMax : iWrite;
  138. memcpy (pOut, pMat->mProperties[i]->mData, iWrite * sizeof (int));
  139. if (NULL != pMax)
  140. *pMax = iWrite;
  141. }
  142. // data is given in floats convert to int (lossy!)
  143. else if( aiPTI_Float == pMat->mProperties[i]->mType)
  144. {
  145. unsigned int iWrite = pMat->mProperties[i]->
  146. mDataLength / sizeof(float);
  147. if (NULL != pMax)
  148. iWrite = *pMax < iWrite ? *pMax : iWrite;
  149. for (unsigned int a = 0; a < iWrite;++a)
  150. {
  151. pOut[a] = (int) ((float*)pMat->mProperties[i]->mData)[a];
  152. }
  153. if (NULL != pMax)
  154. *pMax = iWrite;
  155. }
  156. // it is a string ... no way to read something out of this
  157. else
  158. {
  159. if (NULL != pMax)
  160. *pMax = 0;
  161. return AI_FAILURE;
  162. }
  163. return AI_SUCCESS;
  164. }
  165. }
  166. }
  167. return AI_FAILURE;
  168. }
  169. // ------------------------------------------------------------------------------------------------
  170. aiReturn aiGetMaterialColor(const aiMaterial* pMat,
  171. const char* pKey,
  172. aiColor4D* pOut)
  173. {
  174. unsigned int iMax = 4;
  175. aiReturn eRet = aiGetMaterialFloatArray(pMat,pKey,(float*)pOut,&iMax);
  176. // if no alpha channel is provided set it to 1.0 by default
  177. if (3 == iMax)pOut->a = 1.0f;
  178. return eRet;
  179. }
  180. // ------------------------------------------------------------------------------------------------
  181. aiReturn aiGetMaterialString(const aiMaterial* pMat,
  182. const char* pKey,
  183. aiString* pOut)
  184. {
  185. ai_assert (pMat != NULL);
  186. ai_assert (pKey != NULL);
  187. ai_assert (pOut != NULL);
  188. for (unsigned int i = 0; i < pMat->mNumProperties;++i)
  189. {
  190. if (NULL != pMat->mProperties[i])
  191. {
  192. if (0 == ASSIMP_stricmp( pMat->mProperties[i]->mKey->data, pKey ))
  193. {
  194. if( aiPTI_String == pMat->mProperties[i]->mType)
  195. {
  196. memcpy (pOut, pMat->mProperties[i]->mData,
  197. sizeof(aiString));
  198. }
  199. // wrong type
  200. else return AI_FAILURE;
  201. return AI_SUCCESS;
  202. }
  203. }
  204. }
  205. return AI_FAILURE;
  206. }
  207. // ------------------------------------------------------------------------------------------------
  208. aiReturn MaterialHelper::RemoveProperty (const char* pKey)
  209. {
  210. ai_assert(NULL != pKey);
  211. for (unsigned int i = 0; i < this->mNumProperties;++i)
  212. {
  213. if (NULL != this->mProperties[i])
  214. {
  215. if (0 == ASSIMP_stricmp( this->mProperties[i]->mKey->data, pKey ))
  216. {
  217. // delete this entry
  218. delete[] this->mProperties[i]->mData;
  219. delete this->mProperties[i];
  220. // collapse the array behind --.
  221. --this->mNumProperties;
  222. for (unsigned int a = i; a < this->mNumProperties;++a)
  223. {
  224. this->mProperties[a] = this->mProperties[a+1];
  225. }
  226. return AI_SUCCESS;
  227. }
  228. }
  229. }
  230. return AI_FAILURE;
  231. }
  232. // ------------------------------------------------------------------------------------------------
  233. aiReturn MaterialHelper::AddBinaryProperty (const void* pInput,
  234. const unsigned int pSizeInBytes,
  235. const char* pKey,
  236. aiPropertyTypeInfo pType)
  237. {
  238. ai_assert (pInput != NULL);
  239. ai_assert (pKey != NULL);
  240. ai_assert (0 != pSizeInBytes);
  241. // first search the list whether there is already an entry
  242. // with this name.
  243. unsigned int iOutIndex = 0xFFFFFFFF;
  244. for (unsigned int i = 0; i < this->mNumProperties;++i)
  245. {
  246. if (NULL != this->mProperties[i])
  247. {
  248. if (0 == ASSIMP_stricmp( this->mProperties[i]->mKey->data, pKey ))
  249. {
  250. // delete this entry
  251. delete[] this->mProperties[i]->mData;
  252. delete this->mProperties[i];
  253. iOutIndex = i;
  254. }
  255. }
  256. }
  257. aiMaterialProperty* pcNew = new aiMaterialProperty();
  258. // fill this
  259. pcNew->mKey = new aiString();
  260. pcNew->mType = pType;
  261. pcNew->mDataLength = pSizeInBytes;
  262. pcNew->mData = new char[pSizeInBytes];
  263. memcpy (pcNew->mData,pInput,pSizeInBytes);
  264. pcNew->mKey->length = strlen(pKey);
  265. ai_assert ( MAXLEN > pcNew->mKey->length);
  266. strcpy( pcNew->mKey->data, pKey );
  267. if (0xFFFFFFFF != iOutIndex)
  268. {
  269. this->mProperties[iOutIndex] = pcNew;
  270. return AI_SUCCESS;
  271. }
  272. // resize the array ... allocate
  273. // storage for 5 other properties
  274. if (this->mNumProperties == this->mNumAllocated)
  275. {
  276. unsigned int iOld = this->mNumAllocated;
  277. this->mNumAllocated += 5;
  278. aiMaterialProperty** ppTemp = new aiMaterialProperty*[this->mNumAllocated];
  279. if (NULL == ppTemp)return AI_OUTOFMEMORY;
  280. memcpy (ppTemp,this->mProperties,iOld * sizeof(void*));
  281. delete[] this->mProperties;
  282. this->mProperties = ppTemp;
  283. }
  284. // push back ...
  285. this->mProperties[this->mNumProperties++] = pcNew;
  286. return AI_SUCCESS;
  287. }
  288. // ------------------------------------------------------------------------------------------------
  289. aiReturn MaterialHelper::AddProperty (const aiString* pInput,
  290. const char* pKey)
  291. {
  292. return this->AddBinaryProperty(pInput,
  293. sizeof(aiString),pKey,aiPTI_String);
  294. }
  295. // ------------------------------------------------------------------------------------------------
  296. void MaterialHelper::CopyPropertyList(MaterialHelper* pcDest,
  297. const MaterialHelper* pcSrc)
  298. {
  299. ai_assert(NULL != pcDest);
  300. ai_assert(NULL != pcSrc);
  301. unsigned int iOldNum = pcDest->mNumProperties;
  302. pcDest->mNumAllocated += pcSrc->mNumAllocated;
  303. pcDest->mNumProperties += pcSrc->mNumProperties;
  304. aiMaterialProperty** pcOld = pcDest->mProperties;
  305. pcDest->mProperties = new aiMaterialProperty*[pcDest->mNumAllocated];
  306. if (pcOld)
  307. {
  308. for (unsigned int i = 0; i < iOldNum;++i)
  309. pcDest->mProperties[i] = pcOld[i];
  310. delete[] pcDest->mProperties;
  311. }
  312. for (unsigned int i = iOldNum; i< pcDest->mNumProperties;++i)
  313. {
  314. pcDest->mProperties[i]->mKey = new aiString(*pcSrc->mProperties[i]->mKey);
  315. pcDest->mProperties[i]->mDataLength = pcSrc->mProperties[i]->mDataLength;
  316. pcDest->mProperties[i]->mType = pcSrc->mProperties[i]->mType;
  317. pcDest->mProperties[i]->mData = new char[pcDest->mProperties[i]->mDataLength];
  318. memcpy(pcDest->mProperties[i]->mData,pcSrc->mProperties[i]->mData,
  319. pcDest->mProperties[i]->mDataLength);
  320. }
  321. return;
  322. }
  323. // ------------------------------------------------------------------------------------------------
  324. aiReturn aiGetMaterialTexture(const aiMaterial* pcMat,
  325. unsigned int iIndex,
  326. unsigned int iTexType,
  327. aiString* szOut,
  328. unsigned int* piUVIndex,
  329. float* pfBlendFactor,
  330. aiTextureOp* peTextureOp)
  331. {
  332. ai_assert(NULL != pcMat);
  333. ai_assert(NULL != szOut);
  334. const char* szPathBase;
  335. const char* szUVBase;
  336. const char* szBlendBase;
  337. const char* szOpBase;
  338. switch (iTexType)
  339. {
  340. case AI_TEXTYPE_DIFFUSE:
  341. szPathBase = AI_MATKEY_TEXTURE_DIFFUSE_;
  342. szUVBase = AI_MATKEY_UVWSRC_DIFFUSE_;
  343. szBlendBase = AI_MATKEY_TEXBLEND_DIFFUSE_;
  344. szOpBase = AI_MATKEY_TEXOP_DIFFUSE_;
  345. break;
  346. case AI_TEXTYPE_SPECULAR:
  347. szPathBase = AI_MATKEY_TEXTURE_SPECULAR_;
  348. szUVBase = AI_MATKEY_UVWSRC_SPECULAR_;
  349. szBlendBase = AI_MATKEY_TEXBLEND_SPECULAR_;
  350. szOpBase = AI_MATKEY_TEXOP_SPECULAR_;
  351. break;
  352. case AI_TEXTYPE_AMBIENT:
  353. szPathBase = AI_MATKEY_TEXTURE_AMBIENT_;
  354. szUVBase = AI_MATKEY_UVWSRC_AMBIENT_;
  355. szBlendBase = AI_MATKEY_TEXBLEND_AMBIENT_;
  356. szOpBase = AI_MATKEY_TEXOP_AMBIENT_;
  357. break;
  358. case AI_TEXTYPE_EMISSIVE:
  359. szPathBase = AI_MATKEY_TEXTURE_EMISSIVE_;
  360. szUVBase = AI_MATKEY_UVWSRC_EMISSIVE_;
  361. szBlendBase = AI_MATKEY_TEXBLEND_EMISSIVE_;
  362. szOpBase = AI_MATKEY_TEXOP_EMISSIVE_;
  363. break;
  364. case AI_TEXTYPE_HEIGHT:
  365. szPathBase = AI_MATKEY_TEXTURE_HEIGHT_;
  366. szUVBase = AI_MATKEY_UVWSRC_HEIGHT_;
  367. szBlendBase = AI_MATKEY_TEXBLEND_HEIGHT_;
  368. szOpBase = AI_MATKEY_TEXOP_HEIGHT_;
  369. break;
  370. case AI_TEXTYPE_NORMALS:
  371. szPathBase = AI_MATKEY_TEXTURE_NORMALS_;
  372. szUVBase = AI_MATKEY_UVWSRC_NORMALS_;
  373. szBlendBase = AI_MATKEY_TEXBLEND_NORMALS_;
  374. szOpBase = AI_MATKEY_TEXOP_NORMALS_;
  375. break;
  376. case AI_TEXTYPE_SHININESS:
  377. szPathBase = AI_MATKEY_TEXTURE_SHININESS_;
  378. szUVBase = AI_MATKEY_UVWSRC_SHININESS_;
  379. szBlendBase = AI_MATKEY_TEXBLEND_SHININESS_;
  380. szOpBase = AI_MATKEY_TEXOP_SHININESS_;
  381. break;
  382. case AI_TEXTYPE_OPACITY:
  383. szPathBase = AI_MATKEY_TEXTURE_OPACITY_;
  384. szUVBase = AI_MATKEY_UVWSRC_OPACITY_;
  385. szBlendBase = AI_MATKEY_TEXBLEND_OPACITY_;
  386. szOpBase = AI_MATKEY_TEXOP_OPACITY_;
  387. break;
  388. default: return AI_FAILURE;
  389. };
  390. char szKey[256];
  391. // get the path to the texture
  392. sprintf(szKey,"%s[%i]",szPathBase,iIndex);
  393. if (AI_SUCCESS != aiGetMaterialString(pcMat,szKey,szOut))
  394. {
  395. return AI_FAILURE;
  396. }
  397. // get the UV index of the texture
  398. if (piUVIndex)
  399. {
  400. int iUV;
  401. sprintf(szKey,"%s[%i]",szUVBase,iIndex);
  402. if (AI_SUCCESS != aiGetMaterialInteger(pcMat,szKey,&iUV))
  403. iUV = 0;
  404. *piUVIndex = iUV;
  405. }
  406. // get the blend factor of the texture
  407. if (pfBlendFactor)
  408. {
  409. float fBlend;
  410. sprintf(szKey,"%s[%i]",szBlendBase,iIndex);
  411. if (AI_SUCCESS != aiGetMaterialFloat(pcMat,szKey,&fBlend))
  412. fBlend = 1.0f;
  413. *pfBlendFactor = fBlend;
  414. }
  415. // get the texture operation of the texture
  416. if (peTextureOp)
  417. {
  418. aiTextureOp op;
  419. sprintf(szKey,"%s[%i]",szOpBase,iIndex);
  420. if (AI_SUCCESS != aiGetMaterialInteger(pcMat,szKey,(int*)&op))
  421. op = aiTextureOp_Multiply;
  422. *peTextureOp = op;
  423. }
  424. return AI_SUCCESS;
  425. }