material.inl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file material.inl
  35. * @brief Defines the C++ getters for the material system
  36. */
  37. #pragma once
  38. #ifdef __GNUC__
  39. # pragma GCC system_header
  40. #endif
  41. // ---------------------------------------------------------------------------
  42. AI_FORCE_INLINE aiPropertyTypeInfo ai_real_to_property_type_info(float) {
  43. return aiPTI_Float;
  44. }
  45. AI_FORCE_INLINE aiPropertyTypeInfo ai_real_to_property_type_info(double) {
  46. return aiPTI_Double;
  47. }
  48. // ---------------------------------------------------------------------------
  49. //! @cond never
  50. // ---------------------------------------------------------------------------
  51. AI_FORCE_INLINE aiReturn aiMaterial::GetTexture( aiTextureType type,
  52. unsigned int index,
  53. C_STRUCT aiString* path,
  54. aiTextureMapping* mapping /*= NULL*/,
  55. unsigned int* uvindex /*= NULL*/,
  56. ai_real* blend /*= NULL*/,
  57. aiTextureOp* op /*= NULL*/,
  58. aiTextureMapMode* mapmode /*= NULL*/) const {
  59. return ::aiGetMaterialTexture(this,type,index,path,mapping,uvindex,blend,op,mapmode);
  60. }
  61. // ---------------------------------------------------------------------------
  62. AI_FORCE_INLINE unsigned int aiMaterial::GetTextureCount(aiTextureType type) const {
  63. return ::aiGetMaterialTextureCount(this,type);
  64. }
  65. // ---------------------------------------------------------------------------
  66. template <typename Type>
  67. AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type,
  68. unsigned int idx, Type* pOut,
  69. unsigned int* pMax) const {
  70. unsigned int iNum = pMax ? *pMax : 1;
  71. const aiMaterialProperty* prop;
  72. const aiReturn ret = ::aiGetMaterialProperty(this,pKey,type,idx,
  73. (const aiMaterialProperty**)&prop);
  74. if ( AI_SUCCESS == ret ) {
  75. if (prop->mDataLength < sizeof(Type)*iNum) {
  76. return AI_FAILURE;
  77. }
  78. if (prop->mType != aiPTI_Buffer) {
  79. return AI_FAILURE;
  80. }
  81. // std::min has in some cases a conflict with a defined min
  82. #ifdef min
  83. # undef min
  84. #endif
  85. iNum = static_cast<unsigned int>(std::min(static_cast<size_t>(iNum),prop->mDataLength / sizeof(Type)));
  86. std::memcpy(pOut,prop->mData,iNum * sizeof(Type));
  87. if (pMax) {
  88. *pMax = iNum;
  89. }
  90. }
  91. return ret;
  92. }
  93. // ---------------------------------------------------------------------------
  94. template <typename Type>
  95. AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type,
  96. unsigned int idx,Type& pOut) const {
  97. const aiMaterialProperty* prop;
  98. const aiReturn ret = ::aiGetMaterialProperty(this,pKey,type,idx,
  99. (const aiMaterialProperty**)&prop);
  100. if ( AI_SUCCESS == ret ) {
  101. if (prop->mDataLength < sizeof(Type)) {
  102. return AI_FAILURE;
  103. }
  104. if (prop->mType != aiPTI_Buffer) {
  105. return AI_FAILURE;
  106. }
  107. ::memcpy( &pOut, prop->mData, sizeof( Type ) );
  108. }
  109. return ret;
  110. }
  111. // ---------------------------------------------------------------------------
  112. // Specialisation for a single bool.
  113. // Casts floating point and integer to bool
  114. template <>
  115. AI_FORCE_INLINE aiReturn aiMaterial::Get(const char *pKey, unsigned int type,
  116. unsigned int idx, bool &pOut) const {
  117. const aiMaterialProperty *prop;
  118. const aiReturn ret = ::aiGetMaterialProperty(this, pKey, type, idx,
  119. (const aiMaterialProperty **)&prop);
  120. if (AI_SUCCESS == ret) {
  121. switch (prop->mType) {
  122. // Type cannot be converted
  123. default: return AI_FAILURE;
  124. case aiPTI_Buffer: {
  125. // Native bool value storage
  126. if (prop->mDataLength < sizeof(bool)) {
  127. return AI_FAILURE;
  128. }
  129. ::memcpy(&pOut, prop->mData, sizeof(bool));
  130. } break;
  131. case aiPTI_Float:
  132. case aiPTI_Double: {
  133. // Read as float and cast to bool
  134. ai_real value = 0.0f;
  135. if (AI_SUCCESS == ::aiGetMaterialFloat(this, pKey, type, idx, &value)) {
  136. pOut = static_cast<bool>(value);
  137. return AI_SUCCESS;
  138. }
  139. return AI_FAILURE;
  140. }
  141. case aiPTI_Integer: {
  142. // Cast to bool
  143. const int value = static_cast<int>(*prop->mData);
  144. pOut = static_cast<bool>(value);
  145. return AI_SUCCESS;
  146. }
  147. }
  148. }
  149. return ret;
  150. }
  151. // ---------------------------------------------------------------------------
  152. AI_FORCE_INLINE
  153. aiReturn aiMaterial::Get(const char* pKey,unsigned int type,
  154. unsigned int idx,ai_real* pOut,
  155. unsigned int* pMax) const {
  156. return ::aiGetMaterialFloatArray(this,pKey,type,idx,pOut,pMax);
  157. }
  158. // ---------------------------------------------------------------------------
  159. AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type,
  160. unsigned int idx,int* pOut,
  161. unsigned int* pMax) const {
  162. return ::aiGetMaterialIntegerArray(this,pKey,type,idx,pOut,pMax);
  163. }
  164. // ---------------------------------------------------------------------------
  165. AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type,
  166. unsigned int idx, ai_real& pOut) const {
  167. return aiGetMaterialFloat(this,pKey,type,idx,&pOut);
  168. }
  169. // ---------------------------------------------------------------------------
  170. AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type,
  171. unsigned int idx,int& pOut) const {
  172. return aiGetMaterialInteger(this,pKey,type,idx,&pOut);
  173. }
  174. // ---------------------------------------------------------------------------
  175. AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type,
  176. unsigned int idx,aiColor4D& pOut) const {
  177. return aiGetMaterialColor(this,pKey,type,idx,&pOut);
  178. }
  179. // ---------------------------------------------------------------------------
  180. AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type,
  181. unsigned int idx,aiColor3D& pOut) const {
  182. aiColor4D c;
  183. const aiReturn ret = aiGetMaterialColor(this,pKey,type,idx,&c);
  184. if (ret == aiReturn_SUCCESS)
  185. pOut = aiColor3D(c.r,c.g,c.b);
  186. return ret;
  187. }
  188. // ---------------------------------------------------------------------------
  189. AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type,
  190. unsigned int idx,aiString& pOut) const {
  191. return aiGetMaterialString(this,pKey,type,idx,&pOut);
  192. }
  193. // ---------------------------------------------------------------------------
  194. AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type,
  195. unsigned int idx,aiUVTransform& pOut) const {
  196. return aiGetMaterialUVTransform(this,pKey,type,idx,&pOut);
  197. }
  198. // ---------------------------------------------------------------------------
  199. template<class TYPE>
  200. aiReturn aiMaterial::AddProperty (const TYPE* pInput,
  201. const unsigned int pNumValues, const char* pKey, unsigned int type,
  202. unsigned int index) {
  203. return AddBinaryProperty((const void*)pInput, pNumValues * sizeof(TYPE),
  204. pKey,type,index,aiPTI_Buffer);
  205. }
  206. // ---------------------------------------------------------------------------
  207. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty(const float* pInput,
  208. const unsigned int pNumValues,
  209. const char* pKey,
  210. unsigned int type,
  211. unsigned int index) {
  212. return AddBinaryProperty((const void*)pInput,
  213. pNumValues * sizeof(float),
  214. pKey,type,index,aiPTI_Float);
  215. }
  216. // ---------------------------------------------------------------------------
  217. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty(const double* pInput,
  218. const unsigned int pNumValues,
  219. const char* pKey,
  220. unsigned int type,
  221. unsigned int index) {
  222. return AddBinaryProperty((const void*)pInput,
  223. pNumValues * sizeof(double),
  224. pKey,type,index,aiPTI_Double);
  225. }
  226. // ---------------------------------------------------------------------------
  227. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty(const aiUVTransform* pInput,
  228. const unsigned int pNumValues,
  229. const char* pKey,
  230. unsigned int type,
  231. unsigned int index) {
  232. return AddBinaryProperty((const void*)pInput,
  233. pNumValues * sizeof(aiUVTransform),
  234. pKey,type,index,ai_real_to_property_type_info(pInput->mRotation));
  235. }
  236. // ---------------------------------------------------------------------------
  237. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty(const aiColor4D* pInput,
  238. const unsigned int pNumValues,
  239. const char* pKey,
  240. unsigned int type,
  241. unsigned int index) {
  242. return AddBinaryProperty((const void*)pInput,
  243. pNumValues * sizeof(aiColor4D),
  244. pKey,type,index,ai_real_to_property_type_info(pInput->a));
  245. }
  246. // ---------------------------------------------------------------------------
  247. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty(const aiColor3D* pInput,
  248. const unsigned int pNumValues,
  249. const char* pKey,
  250. unsigned int type,
  251. unsigned int index) {
  252. return AddBinaryProperty((const void*)pInput,
  253. pNumValues * sizeof(aiColor3D),
  254. pKey,type,index,ai_real_to_property_type_info(pInput->b));
  255. }
  256. // ---------------------------------------------------------------------------
  257. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty(const aiVector3D* pInput,
  258. const unsigned int pNumValues,
  259. const char* pKey,
  260. unsigned int type,
  261. unsigned int index) {
  262. return AddBinaryProperty((const void*)pInput,
  263. pNumValues * sizeof(aiVector3D),
  264. pKey,type,index,ai_real_to_property_type_info(pInput->x));
  265. }
  266. // ---------------------------------------------------------------------------
  267. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty(const int* pInput,
  268. const unsigned int pNumValues,
  269. const char* pKey,
  270. unsigned int type,
  271. unsigned int index) {
  272. return AddBinaryProperty((const void*)pInput,
  273. pNumValues * sizeof(int),
  274. pKey,type,index,aiPTI_Integer);
  275. }
  276. // ---------------------------------------------------------------------------
  277. // The template specializations below are for backwards compatibility.
  278. // The recommended way to add material properties is using the non-template
  279. // overloads.
  280. // ---------------------------------------------------------------------------
  281. // ---------------------------------------------------------------------------
  282. template<>
  283. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty<float>(const float* pInput,
  284. const unsigned int pNumValues,
  285. const char* pKey,
  286. unsigned int type,
  287. unsigned int index) {
  288. return AddBinaryProperty((const void*)pInput,
  289. pNumValues * sizeof(float),
  290. pKey,type,index,aiPTI_Float);
  291. }
  292. // ---------------------------------------------------------------------------
  293. template<>
  294. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty<double>(const double* pInput,
  295. const unsigned int pNumValues,
  296. const char* pKey,
  297. unsigned int type,
  298. unsigned int index) {
  299. return AddBinaryProperty((const void*)pInput,
  300. pNumValues * sizeof(double),
  301. pKey,type,index,aiPTI_Double);
  302. }
  303. // ---------------------------------------------------------------------------
  304. template<>
  305. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty<aiUVTransform>(const aiUVTransform* pInput,
  306. const unsigned int pNumValues,
  307. const char* pKey,
  308. unsigned int type,
  309. unsigned int index) {
  310. return AddBinaryProperty((const void*)pInput,
  311. pNumValues * sizeof(aiUVTransform),
  312. pKey,type,index,aiPTI_Float);
  313. }
  314. // ---------------------------------------------------------------------------
  315. template<>
  316. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty<aiColor4D>(const aiColor4D* pInput,
  317. const unsigned int pNumValues,
  318. const char* pKey,
  319. unsigned int type,
  320. unsigned int index) {
  321. return AddBinaryProperty((const void*)pInput,
  322. pNumValues * sizeof(aiColor4D),
  323. pKey,type,index,aiPTI_Float);
  324. }
  325. // ---------------------------------------------------------------------------
  326. template<>
  327. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty<aiColor3D>(const aiColor3D* pInput,
  328. const unsigned int pNumValues,
  329. const char* pKey,
  330. unsigned int type,
  331. unsigned int index) {
  332. return AddBinaryProperty((const void*)pInput,
  333. pNumValues * sizeof(aiColor3D),
  334. pKey,type,index,aiPTI_Float);
  335. }
  336. // ---------------------------------------------------------------------------
  337. template<>
  338. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty<aiVector3D>(const aiVector3D* pInput,
  339. const unsigned int pNumValues,
  340. const char* pKey,
  341. unsigned int type,
  342. unsigned int index) {
  343. return AddBinaryProperty((const void*)pInput,
  344. pNumValues * sizeof(aiVector3D),
  345. pKey,type,index,aiPTI_Float);
  346. }
  347. // ---------------------------------------------------------------------------
  348. template<>
  349. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty<int>(const int* pInput,
  350. const unsigned int pNumValues,
  351. const char* pKey,
  352. unsigned int type,
  353. unsigned int index) {
  354. return AddBinaryProperty((const void*)pInput,
  355. pNumValues * sizeof(int),
  356. pKey,type,index,aiPTI_Integer);
  357. }
  358. //! @endcond