MaterialSystem.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. /** @file Definition of the base class for all importer worker classes. */
  34. #ifndef AI_MATERIALSYSTEM_H_INC
  35. #define AI_MATERIALSYSTEM_H_INC
  36. #include "../include/aiMaterial.h"
  37. namespace Assimp
  38. {
  39. // ---------------------------------------------------------------------------
  40. /** \brief Helper function to do platform independent string comparison.
  41. *
  42. * This is required since stricmp() is not consistently available on
  43. * all platforms. Some platforms use the '_' prefix, others don't even
  44. * have such a function. Yes, this is called an ISO standard.
  45. *
  46. * \param s1 First input string
  47. * \param s2 Second input string
  48. */
  49. // ---------------------------------------------------------------------------
  50. inline int ASSIMP_stricmp(const char *s1, const char *s2)
  51. {
  52. #if (defined _MSC_VER)
  53. return _stricmp(s1,s2);
  54. #else
  55. const char *a1, *a2;
  56. a1 = s1;
  57. a2 = s2;
  58. while (true)
  59. {
  60. char c1 = (char)tolower(*a1);
  61. char c2 = (char)tolower(*a2);
  62. if ((0 == c1) && (0 == c2)) return 0;
  63. if (c1 < c2) return-1;
  64. if (c1 > c2) return 1;
  65. ++a1;
  66. ++a2;
  67. }
  68. #endif
  69. }
  70. // ---------------------------------------------------------------------------
  71. /** \brief Helper function to do platform independent string comparison.
  72. *
  73. * This is required since strincmp() is not consistently available on
  74. * all platforms. Some platforms use the '_' prefix, others don't even
  75. * have such a function. Yes, this is called an ISO standard.
  76. *
  77. * \param s1 First input string
  78. * \param s2 Second input string
  79. * \param n Macimum number of characters to compare
  80. */
  81. // ---------------------------------------------------------------------------
  82. inline int ASSIMP_strincmp(const char *s1, const char *s2, unsigned int n)
  83. {
  84. #if (defined _MSC_VER)
  85. return _strnicmp(s1,s2,n);
  86. #else
  87. const char *a1, *a2;
  88. a1 = s1;
  89. a2 = s2;
  90. unsigned int p = 0;
  91. while (true)
  92. {
  93. if (p >= n)return 0;
  94. char c1 = (char)tolower(*a1);
  95. char c2 = (char)tolower(*a2);
  96. if ((0 == c1) && (0 == c2)) return 0;
  97. if (c1 < c2) return-1;
  98. if (c1 > c2) return 1;
  99. ++a1;
  100. ++a2;
  101. ++p;
  102. }
  103. #endif
  104. }
  105. // ---------------------------------------------------------------------------
  106. /** Internal material helper class. Can be used to fill an aiMaterial
  107. structure easily. */
  108. class MaterialHelper : public ::aiMaterial
  109. {
  110. public:
  111. inline MaterialHelper();
  112. inline ~MaterialHelper();
  113. // -------------------------------------------------------------------
  114. /** Add a property with a given key and type info to the material
  115. * structure
  116. *
  117. * \param pInput Pointer to input data
  118. * \param pSizeInBytes Size of input data
  119. * \param pKey Key/Usage of the property (AI_MATKEY_XXX)
  120. * \param pType Type information hint
  121. */
  122. aiReturn AddBinaryProperty (const void* pInput,
  123. const unsigned int pSizeInBytes,
  124. const char* pKey,
  125. aiPropertyTypeInfo pType);
  126. // -------------------------------------------------------------------
  127. /** Add a string property with a given key and type info to the
  128. * material structure
  129. *
  130. * \param pInput Input string
  131. * \param pKey Key/Usage of the property (AI_MATKEY_XXX)
  132. */
  133. aiReturn AddProperty (const aiString* pInput,
  134. const char* pKey);
  135. // -------------------------------------------------------------------
  136. /** Add a property with a given key to the material structure
  137. * \param pInput Pointer to the input data
  138. * \param pNumValues Number of values in the array
  139. * \param pKey Key/Usage of the property (AI_MATKEY_XXX)
  140. */
  141. template<class TYPE>
  142. aiReturn AddProperty (const TYPE* pInput,
  143. const unsigned int pNumValues,
  144. const char* pKey);
  145. // -------------------------------------------------------------------
  146. /** Remove a given key from the list
  147. * The function fails if the key isn't found
  148. *
  149. * \param pKey Key/Usage to be deleted
  150. */
  151. aiReturn RemoveProperty (const char* pKey);
  152. // -------------------------------------------------------------------
  153. /** Copy the property list of a material
  154. * \param pcDest Destination material
  155. * \param pcSrc Source material
  156. */
  157. static void CopyPropertyList(MaterialHelper* pcDest,
  158. const MaterialHelper* pcSrc);
  159. };
  160. // ---------------------------------------------------------------------------
  161. // ---------------------------------------------------------------------------
  162. inline MaterialHelper::MaterialHelper()
  163. {
  164. // allocate 5 entries by default
  165. this->mNumProperties = 0;
  166. this->mNumAllocated = 5;
  167. this->mProperties = new aiMaterialProperty*[5];
  168. return;
  169. }
  170. // ---------------------------------------------------------------------------
  171. // ---------------------------------------------------------------------------
  172. inline MaterialHelper::~MaterialHelper()
  173. {
  174. for (unsigned int i = 0; i < this->mNumProperties;++i)
  175. {
  176. // be careful ...
  177. if(NULL != this->mProperties[i])
  178. {
  179. delete[] this->mProperties[i]->mKey;
  180. delete[] this->mProperties[i]->mData;
  181. delete this->mProperties[i];
  182. }
  183. }
  184. return;
  185. }
  186. // ---------------------------------------------------------------------------
  187. // ---------------------------------------------------------------------------
  188. template<class TYPE>
  189. aiReturn MaterialHelper::AddProperty (const TYPE* pInput,
  190. const unsigned int pNumValues,
  191. const char* pKey)
  192. {
  193. return this->AddBinaryProperty((const void*)pInput,
  194. pNumValues * sizeof(TYPE),
  195. pKey,aiPTI_Buffer);
  196. }
  197. // ---------------------------------------------------------------------------
  198. // ---------------------------------------------------------------------------
  199. template<>
  200. inline aiReturn MaterialHelper::AddProperty<float> (const float* pInput,
  201. const unsigned int pNumValues,
  202. const char* pKey)
  203. {
  204. return this->AddBinaryProperty((const void*)pInput,
  205. pNumValues * sizeof(float),
  206. pKey,aiPTI_Float);
  207. }
  208. // ---------------------------------------------------------------------------
  209. // ---------------------------------------------------------------------------
  210. template<>
  211. inline aiReturn MaterialHelper::AddProperty<aiColor4D> (const aiColor4D* pInput,
  212. const unsigned int pNumValues,
  213. const char* pKey)
  214. {
  215. return this->AddBinaryProperty((const void*)pInput,
  216. pNumValues * sizeof(aiColor4D),
  217. pKey,aiPTI_Float);
  218. }
  219. // ---------------------------------------------------------------------------
  220. // ---------------------------------------------------------------------------
  221. template<>
  222. inline aiReturn MaterialHelper::AddProperty<aiColor3D> (const aiColor3D* pInput,
  223. const unsigned int pNumValues,
  224. const char* pKey)
  225. {
  226. return this->AddBinaryProperty((const void*)pInput,
  227. pNumValues * sizeof(aiColor3D),
  228. pKey,aiPTI_Float);
  229. }
  230. // ---------------------------------------------------------------------------
  231. // ---------------------------------------------------------------------------
  232. template<>
  233. inline aiReturn MaterialHelper::AddProperty<int> (const int* pInput,
  234. const unsigned int pNumValues,
  235. const char* pKey)
  236. {
  237. return this->AddBinaryProperty((const void*)pInput,
  238. pNumValues * sizeof(int),
  239. pKey,aiPTI_Integer);
  240. }
  241. }
  242. #endif //!! AI_MATERIALSYSTEM_H_INC