MaterialSystem.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include "assimp.h"
  2. #include "aiMaterial.h"
  3. #include "assimp.hpp"
  4. #include "MaterialSystem.h"
  5. #include "../include/aiAssert.h"
  6. using namespace Assimp;
  7. // ---------------------------------------------------------------------------
  8. // ---------------------------------------------------------------------------
  9. aiReturn aiGetMaterialProperty(const aiMaterial* pMat,
  10. const char* pKey,
  11. const aiMaterialProperty** pPropOut)
  12. {
  13. #if (defined DEBUG)
  14. ai_assert (pMat != NULL);
  15. ai_assert (pKey != NULL);
  16. ai_assert (pPropOut != NULL);
  17. #endif // ASSIMP_DEBUG
  18. for (unsigned int i = 0; i < pMat->mNumProperties;++i)
  19. {
  20. if (NULL != pMat->mProperties[i])
  21. {
  22. if (0 == ASSIMP_stricmp( pMat->mProperties[i]->mKey->data, pKey ))
  23. {
  24. *pPropOut = pMat->mProperties[i];
  25. return AI_SUCCESS;
  26. }
  27. }
  28. }
  29. *pPropOut = NULL;
  30. return AI_FAILURE;
  31. }
  32. // ---------------------------------------------------------------------------
  33. // ---------------------------------------------------------------------------
  34. aiReturn aiGetMaterialFloatArray(const aiMaterial* pMat,
  35. const char* pKey,
  36. float* pOut,
  37. unsigned int* pMax)
  38. {
  39. #if (defined DEBUG)
  40. ai_assert (pMat != NULL);
  41. ai_assert (pKey != NULL);
  42. ai_assert (pOut != NULL);
  43. #endif // ASSIMP_DEBUG
  44. for (unsigned int i = 0; i < pMat->mNumProperties;++i)
  45. {
  46. if (NULL != pMat->mProperties[i])
  47. {
  48. if (0 == ASSIMP_stricmp( pMat->mProperties[i]->mKey->data, pKey ))
  49. {
  50. // data is given in floats, simply copy it
  51. if( aiPTI_Float == pMat->mProperties[i]->mType ||
  52. aiPTI_Buffer == pMat->mProperties[i]->mType)
  53. {
  54. unsigned int iWrite = pMat->mProperties[i]->
  55. mDataLength / sizeof(float);
  56. if (NULL != pMax)
  57. iWrite = *pMax < iWrite ? *pMax : iWrite;
  58. memcpy (pOut, pMat->mProperties[i]->mData, iWrite * sizeof (float));
  59. if (NULL != pMax)
  60. *pMax = iWrite;
  61. }
  62. // data is given in ints, convert to float
  63. else if( aiPTI_Integer == pMat->mProperties[i]->mType)
  64. {
  65. unsigned int iWrite = pMat->mProperties[i]->
  66. mDataLength / sizeof(int);
  67. if (NULL != pMax)
  68. iWrite = *pMax < iWrite ? *pMax : iWrite;
  69. for (unsigned int a = 0; a < iWrite;++a)
  70. {
  71. pOut[a] = (float) ((int*)pMat->mProperties[i]->mData)[a];
  72. }
  73. if (NULL != pMax)
  74. *pMax = iWrite;
  75. }
  76. // it is a string ... no way to read something out of this
  77. else
  78. {
  79. if (NULL != pMax)
  80. *pMax = 0;
  81. return AI_FAILURE;
  82. }
  83. return AI_SUCCESS;
  84. }
  85. }
  86. }
  87. return AI_FAILURE;
  88. }
  89. // ---------------------------------------------------------------------------
  90. // ---------------------------------------------------------------------------
  91. aiReturn aiGetMaterialIntegerArray(const aiMaterial* pMat,
  92. const char* pKey,
  93. int* pOut,
  94. unsigned int* pMax)
  95. {
  96. #if (defined DEBUG)
  97. ai_assert (pMat != NULL);
  98. ai_assert (pKey != NULL);
  99. ai_assert (pOut != NULL);
  100. #endif // ASSIMP_DEBUG
  101. for (unsigned int i = 0; i < pMat->mNumProperties;++i)
  102. {
  103. if (NULL != pMat->mProperties[i])
  104. {
  105. if (0 == ASSIMP_stricmp( pMat->mProperties[i]->mKey->data, pKey ))
  106. {
  107. // data is given in ints, simply copy it
  108. if( aiPTI_Integer == pMat->mProperties[i]->mType ||
  109. aiPTI_Buffer == pMat->mProperties[i]->mType)
  110. {
  111. unsigned int iWrite = pMat->mProperties[i]->
  112. mDataLength / sizeof(int);
  113. if (NULL != pMax)
  114. iWrite = *pMax < iWrite ? *pMax : iWrite;
  115. memcpy (pOut, pMat->mProperties[i]->mData, iWrite * sizeof (int));
  116. if (NULL != pMax)
  117. *pMax = iWrite;
  118. }
  119. // data is given in floats convert to int (lossy!)
  120. else if( aiPTI_Float == pMat->mProperties[i]->mType)
  121. {
  122. unsigned int iWrite = pMat->mProperties[i]->
  123. mDataLength / sizeof(float);
  124. if (NULL != pMax)
  125. iWrite = *pMax < iWrite ? *pMax : iWrite;
  126. for (unsigned int a = 0; a < iWrite;++a)
  127. {
  128. pOut[a] = (int) ((float*)pMat->mProperties[i]->mData)[a];
  129. }
  130. if (NULL != pMax)
  131. *pMax = iWrite;
  132. }
  133. // it is a string ... no way to read something out of this
  134. else
  135. {
  136. if (NULL != pMax)
  137. *pMax = 0;
  138. return AI_FAILURE;
  139. }
  140. return AI_SUCCESS;
  141. }
  142. }
  143. }
  144. return AI_FAILURE;
  145. }
  146. // ---------------------------------------------------------------------------
  147. // ---------------------------------------------------------------------------
  148. aiReturn aiGetMaterialColor(const aiMaterial* pMat,
  149. const char* pKey,
  150. aiColor4D* pOut)
  151. {
  152. unsigned int iMax = 4;
  153. aiReturn eRet = aiGetMaterialFloatArray(pMat,pKey,(float*)pOut,&iMax);
  154. // if no alpha channel is provided set it to 1.0 by default
  155. if (3 == iMax)pOut->a = 1.0f;
  156. return eRet;
  157. }
  158. // ---------------------------------------------------------------------------
  159. // ---------------------------------------------------------------------------
  160. aiReturn aiGetMaterialString(const aiMaterial* pMat,
  161. const char* pKey,
  162. aiString* pOut)
  163. {
  164. #if (defined DEBUG)
  165. ai_assert (pMat != NULL);
  166. ai_assert (pKey != NULL);
  167. ai_assert (pOut != NULL);
  168. #endif // ASSIMP_DEBUG
  169. for (unsigned int i = 0; i < pMat->mNumProperties;++i)
  170. {
  171. if (NULL != pMat->mProperties[i])
  172. {
  173. if (0 == ASSIMP_stricmp( pMat->mProperties[i]->mKey->data, pKey ))
  174. {
  175. if( aiPTI_String == pMat->mProperties[i]->mType)
  176. {
  177. memcpy (pOut, pMat->mProperties[i]->mData,
  178. sizeof(aiString));
  179. }
  180. // wrong type
  181. else return AI_FAILURE;
  182. return AI_SUCCESS;
  183. }
  184. }
  185. }
  186. return AI_FAILURE;
  187. }
  188. // ---------------------------------------------------------------------------
  189. // ---------------------------------------------------------------------------
  190. aiReturn MaterialHelper::AddBinaryProperty (const void* pInput,
  191. const unsigned int pSizeInBytes,
  192. const char* pKey,
  193. aiPropertyTypeInfo pType)
  194. {
  195. #if (defined DEBUG)
  196. ai_assert (pInput != NULL);
  197. ai_assert (pKey != NULL);
  198. ai_assert (0 != pSizeInBytes);
  199. #endif // ASSIMP_DEBUG
  200. aiMaterialProperty* pcNew = new aiMaterialProperty();
  201. // fill this
  202. pcNew->mKey = new aiString();
  203. pcNew->mType = pType;
  204. pcNew->mDataLength = pSizeInBytes;
  205. pcNew->mData = new char[pSizeInBytes];
  206. memcpy (pcNew->mData,pInput,pSizeInBytes);
  207. pcNew->mKey->length = strlen(pKey);
  208. ai_assert ( MAXLEN > pcNew->mKey->length);
  209. strcpy( pcNew->mKey->data, pKey );
  210. // resize the array ... allocate
  211. // storage for 5 other properties
  212. if (this->mNumProperties == this->mNumAllocated)
  213. {
  214. unsigned int iOld = this->mNumAllocated;
  215. this->mNumAllocated += 5;
  216. aiMaterialProperty** ppTemp = new aiMaterialProperty*[this->mNumAllocated];
  217. if (NULL == ppTemp)return AI_OUTOFMEMORY;
  218. memcpy (ppTemp,this->mProperties,iOld * sizeof(void*));
  219. delete[] this->mProperties;
  220. this->mProperties = ppTemp;
  221. }
  222. // push back ...
  223. this->mProperties[this->mNumProperties++] = pcNew;
  224. return AI_SUCCESS;
  225. }
  226. // ---------------------------------------------------------------------------
  227. // ---------------------------------------------------------------------------
  228. aiReturn MaterialHelper::AddProperty (const aiString* pInput,
  229. const char* pKey)
  230. {
  231. return this->AddBinaryProperty(pInput,
  232. sizeof(aiString),pKey,aiPTI_String);
  233. }