MaterialSystem.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /** @file Definition of the base class for all importer worker classes. */
  2. #ifndef AI_MATERIALSYSTEM_H_INC
  3. #define AI_MATERIALSYSTEM_H_INC
  4. #include "../include/aiMaterial.h"
  5. namespace Assimp
  6. {
  7. // ---------------------------------------------------------------------------
  8. // ---------------------------------------------------------------------------
  9. inline int ASSIMP_stricmp(const char *s1, const char *s2)
  10. {
  11. const char *a1, *a2;
  12. a1 = s1;
  13. a2 = s2;
  14. while (true)
  15. {
  16. char c1 = (char)tolower(*a1);
  17. char c2 = (char)tolower(*a2);
  18. if ((0 == c1) && (0 == c2)) return 0;
  19. if (c1 < c2) return-1;
  20. if (c1 > c2) return 1;
  21. ++a1;
  22. ++a2;
  23. }
  24. }
  25. // ---------------------------------------------------------------------------
  26. // ---------------------------------------------------------------------------
  27. inline int ASSIMP_strincmp(const char *s1, const char *s2, unsigned int n)
  28. {
  29. const char *a1, *a2;
  30. a1 = s1;
  31. a2 = s2;
  32. unsigned int p = 0;
  33. while (true)
  34. {
  35. if (p >= n)return 0;
  36. char c1 = (char)tolower(*a1);
  37. char c2 = (char)tolower(*a2);
  38. if ((0 == c1) && (0 == c2)) return 0;
  39. if (c1 < c2) return-1;
  40. if (c1 > c2) return 1;
  41. ++a1;
  42. ++a2;
  43. ++p;
  44. }
  45. }
  46. // ---------------------------------------------------------------------------
  47. /** Internal material helper class. Can be used to fill an aiMaterial
  48. structure easily. */
  49. class MaterialHelper : public ::aiMaterial
  50. {
  51. public:
  52. inline MaterialHelper();
  53. inline ~MaterialHelper();
  54. // -------------------------------------------------------------------
  55. /** Add a property with a given key and type info to the material
  56. structure */
  57. aiReturn AddBinaryProperty (const void* pInput,
  58. const unsigned int pSizeInBytes,
  59. const char* pKey,
  60. aiPropertyTypeInfo pType);
  61. // -------------------------------------------------------------------
  62. /** Add a string property with a given key and type info to the
  63. material structure */
  64. aiReturn AddProperty (const aiString* pInput,
  65. const char* pKey);
  66. // -------------------------------------------------------------------
  67. /** Add a property with a given key to the material structure */
  68. template<class TYPE>
  69. aiReturn AddProperty (const TYPE* pInput,
  70. const unsigned int pNumValues,
  71. const char* pKey);
  72. };
  73. // ---------------------------------------------------------------------------
  74. // ---------------------------------------------------------------------------
  75. inline MaterialHelper::MaterialHelper()
  76. {
  77. // allocate 5 entries by default
  78. this->mNumProperties = 0;
  79. this->mNumAllocated = 5;
  80. this->mProperties = new aiMaterialProperty*[5];
  81. return;
  82. }
  83. // ---------------------------------------------------------------------------
  84. // ---------------------------------------------------------------------------
  85. inline MaterialHelper::~MaterialHelper()
  86. {
  87. for (unsigned int i = 0; i < this->mNumProperties;++i)
  88. {
  89. // be careful ...
  90. if(NULL != this->mProperties[i])
  91. {
  92. delete[] this->mProperties[i]->mKey;
  93. delete[] this->mProperties[i]->mData;
  94. delete this->mProperties[i];
  95. }
  96. }
  97. return;
  98. }
  99. // ---------------------------------------------------------------------------
  100. // ---------------------------------------------------------------------------
  101. template<class TYPE>
  102. aiReturn MaterialHelper::AddProperty (const TYPE* pInput,
  103. const unsigned int pNumValues,
  104. const char* pKey)
  105. {
  106. return this->AddBinaryProperty((const void*)pInput,
  107. pNumValues * sizeof(TYPE),
  108. pKey,aiPTI_Buffer);
  109. }
  110. // ---------------------------------------------------------------------------
  111. // ---------------------------------------------------------------------------
  112. template<>
  113. inline aiReturn MaterialHelper::AddProperty<float> (const float* pInput,
  114. const unsigned int pNumValues,
  115. const char* pKey)
  116. {
  117. return this->AddBinaryProperty((const void*)pInput,
  118. pNumValues * sizeof(float),
  119. pKey,aiPTI_Float);
  120. }
  121. // ---------------------------------------------------------------------------
  122. // ---------------------------------------------------------------------------
  123. template<>
  124. inline aiReturn MaterialHelper::AddProperty<aiColor4D> (const aiColor4D* pInput,
  125. const unsigned int pNumValues,
  126. const char* pKey)
  127. {
  128. return this->AddBinaryProperty((const void*)pInput,
  129. pNumValues * sizeof(aiColor4D),
  130. pKey,aiPTI_Float);
  131. }
  132. // ---------------------------------------------------------------------------
  133. // ---------------------------------------------------------------------------
  134. template<>
  135. inline aiReturn MaterialHelper::AddProperty<aiColor3D> (const aiColor3D* pInput,
  136. const unsigned int pNumValues,
  137. const char* pKey)
  138. {
  139. return this->AddBinaryProperty((const void*)pInput,
  140. pNumValues * sizeof(aiColor3D),
  141. pKey,aiPTI_Float);
  142. }
  143. // ---------------------------------------------------------------------------
  144. // ---------------------------------------------------------------------------
  145. template<>
  146. inline aiReturn MaterialHelper::AddProperty<int> (const int* pInput,
  147. const unsigned int pNumValues,
  148. const char* pKey)
  149. {
  150. return this->AddBinaryProperty((const void*)pInput,
  151. pNumValues * sizeof(int),
  152. pKey,aiPTI_Integer);
  153. }
  154. }
  155. #endif //!! AI_MATERIALSYSTEM_H_INC