material.inl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2021, 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. iNum = static_cast<unsigned int>(std::min(static_cast<size_t>(iNum),prop->mDataLength / sizeof(Type)));
  82. ::memcpy(pOut,prop->mData,iNum * sizeof(Type));
  83. if (pMax) {
  84. *pMax = iNum;
  85. }
  86. }
  87. return ret;
  88. }
  89. // ---------------------------------------------------------------------------
  90. template <typename Type>
  91. AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type,
  92. unsigned int idx,Type& pOut) const {
  93. const aiMaterialProperty* prop;
  94. const aiReturn ret = ::aiGetMaterialProperty(this,pKey,type,idx,
  95. (const aiMaterialProperty**)&prop);
  96. if ( AI_SUCCESS == ret ) {
  97. if (prop->mDataLength < sizeof(Type)) {
  98. return AI_FAILURE;
  99. }
  100. if (prop->mType != aiPTI_Buffer) {
  101. return AI_FAILURE;
  102. }
  103. ::memcpy( &pOut, prop->mData, sizeof( Type ) );
  104. }
  105. return ret;
  106. }
  107. // ---------------------------------------------------------------------------
  108. AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type,
  109. unsigned int idx,ai_real* pOut,
  110. unsigned int* pMax) const {
  111. return ::aiGetMaterialFloatArray(this,pKey,type,idx,pOut,pMax);
  112. }
  113. // ---------------------------------------------------------------------------
  114. AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type,
  115. unsigned int idx,int* pOut,
  116. unsigned int* pMax) const {
  117. return ::aiGetMaterialIntegerArray(this,pKey,type,idx,pOut,pMax);
  118. }
  119. // ---------------------------------------------------------------------------
  120. AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type,
  121. unsigned int idx,ai_real& pOut) const {
  122. return aiGetMaterialFloat(this,pKey,type,idx,&pOut);
  123. }
  124. // ---------------------------------------------------------------------------
  125. AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type,
  126. unsigned int idx,int& pOut) const {
  127. return aiGetMaterialInteger(this,pKey,type,idx,&pOut);
  128. }
  129. // ---------------------------------------------------------------------------
  130. AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type,
  131. unsigned int idx,aiColor4D& pOut) const {
  132. return aiGetMaterialColor(this,pKey,type,idx,&pOut);
  133. }
  134. // ---------------------------------------------------------------------------
  135. AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type,
  136. unsigned int idx,aiColor3D& pOut) const {
  137. aiColor4D c;
  138. const aiReturn ret = aiGetMaterialColor(this,pKey,type,idx,&c);
  139. pOut = aiColor3D(c.r,c.g,c.b);
  140. return ret;
  141. }
  142. // ---------------------------------------------------------------------------
  143. AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type,
  144. unsigned int idx,aiString& pOut) const {
  145. return aiGetMaterialString(this,pKey,type,idx,&pOut);
  146. }
  147. // ---------------------------------------------------------------------------
  148. AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type,
  149. unsigned int idx,aiUVTransform& pOut) const {
  150. return aiGetMaterialUVTransform(this,pKey,type,idx,&pOut);
  151. }
  152. // ---------------------------------------------------------------------------
  153. template<class TYPE>
  154. aiReturn aiMaterial::AddProperty (const TYPE* pInput,
  155. const unsigned int pNumValues, const char* pKey, unsigned int type,
  156. unsigned int index) {
  157. return AddBinaryProperty((const void*)pInput, pNumValues * sizeof(TYPE),
  158. pKey,type,index,aiPTI_Buffer);
  159. }
  160. // ---------------------------------------------------------------------------
  161. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty(const float* pInput,
  162. const unsigned int pNumValues,
  163. const char* pKey,
  164. unsigned int type,
  165. unsigned int index) {
  166. return AddBinaryProperty((const void*)pInput,
  167. pNumValues * sizeof(float),
  168. pKey,type,index,aiPTI_Float);
  169. }
  170. // ---------------------------------------------------------------------------
  171. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty(const double* pInput,
  172. const unsigned int pNumValues,
  173. const char* pKey,
  174. unsigned int type,
  175. unsigned int index) {
  176. return AddBinaryProperty((const void*)pInput,
  177. pNumValues * sizeof(double),
  178. pKey,type,index,aiPTI_Double);
  179. }
  180. // ---------------------------------------------------------------------------
  181. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty(const aiUVTransform* pInput,
  182. const unsigned int pNumValues,
  183. const char* pKey,
  184. unsigned int type,
  185. unsigned int index) {
  186. return AddBinaryProperty((const void*)pInput,
  187. pNumValues * sizeof(aiUVTransform),
  188. pKey,type,index,ai_real_to_property_type_info(pInput->mRotation));
  189. }
  190. // ---------------------------------------------------------------------------
  191. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty(const aiColor4D* pInput,
  192. const unsigned int pNumValues,
  193. const char* pKey,
  194. unsigned int type,
  195. unsigned int index) {
  196. return AddBinaryProperty((const void*)pInput,
  197. pNumValues * sizeof(aiColor4D),
  198. pKey,type,index,ai_real_to_property_type_info(pInput->a));
  199. }
  200. // ---------------------------------------------------------------------------
  201. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty(const aiColor3D* pInput,
  202. const unsigned int pNumValues,
  203. const char* pKey,
  204. unsigned int type,
  205. unsigned int index) {
  206. return AddBinaryProperty((const void*)pInput,
  207. pNumValues * sizeof(aiColor3D),
  208. pKey,type,index,ai_real_to_property_type_info(pInput->b));
  209. }
  210. // ---------------------------------------------------------------------------
  211. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty(const aiVector3D* pInput,
  212. const unsigned int pNumValues,
  213. const char* pKey,
  214. unsigned int type,
  215. unsigned int index) {
  216. return AddBinaryProperty((const void*)pInput,
  217. pNumValues * sizeof(aiVector3D),
  218. pKey,type,index,ai_real_to_property_type_info(pInput->x));
  219. }
  220. // ---------------------------------------------------------------------------
  221. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty(const int* pInput,
  222. const unsigned int pNumValues,
  223. const char* pKey,
  224. unsigned int type,
  225. unsigned int index) {
  226. return AddBinaryProperty((const void*)pInput,
  227. pNumValues * sizeof(int),
  228. pKey,type,index,aiPTI_Integer);
  229. }
  230. // ---------------------------------------------------------------------------
  231. // The template specializations below are for backwards compatibility.
  232. // The recommended way to add material properties is using the non-template
  233. // overloads.
  234. // ---------------------------------------------------------------------------
  235. // ---------------------------------------------------------------------------
  236. template<>
  237. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty<float>(const float* 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(float),
  244. pKey,type,index,aiPTI_Float);
  245. }
  246. // ---------------------------------------------------------------------------
  247. template<>
  248. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty<double>(const double* pInput,
  249. const unsigned int pNumValues,
  250. const char* pKey,
  251. unsigned int type,
  252. unsigned int index) {
  253. return AddBinaryProperty((const void*)pInput,
  254. pNumValues * sizeof(double),
  255. pKey,type,index,aiPTI_Double);
  256. }
  257. // ---------------------------------------------------------------------------
  258. template<>
  259. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty<aiUVTransform>(const aiUVTransform* pInput,
  260. const unsigned int pNumValues,
  261. const char* pKey,
  262. unsigned int type,
  263. unsigned int index) {
  264. return AddBinaryProperty((const void*)pInput,
  265. pNumValues * sizeof(aiUVTransform),
  266. pKey,type,index,aiPTI_Float);
  267. }
  268. // ---------------------------------------------------------------------------
  269. template<>
  270. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty<aiColor4D>(const aiColor4D* pInput,
  271. const unsigned int pNumValues,
  272. const char* pKey,
  273. unsigned int type,
  274. unsigned int index) {
  275. return AddBinaryProperty((const void*)pInput,
  276. pNumValues * sizeof(aiColor4D),
  277. pKey,type,index,aiPTI_Float);
  278. }
  279. // ---------------------------------------------------------------------------
  280. template<>
  281. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty<aiColor3D>(const aiColor3D* pInput,
  282. const unsigned int pNumValues,
  283. const char* pKey,
  284. unsigned int type,
  285. unsigned int index) {
  286. return AddBinaryProperty((const void*)pInput,
  287. pNumValues * sizeof(aiColor3D),
  288. pKey,type,index,aiPTI_Float);
  289. }
  290. // ---------------------------------------------------------------------------
  291. template<>
  292. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty<aiVector3D>(const aiVector3D* pInput,
  293. const unsigned int pNumValues,
  294. const char* pKey,
  295. unsigned int type,
  296. unsigned int index) {
  297. return AddBinaryProperty((const void*)pInput,
  298. pNumValues * sizeof(aiVector3D),
  299. pKey,type,index,aiPTI_Float);
  300. }
  301. // ---------------------------------------------------------------------------
  302. template<>
  303. AI_FORCE_INLINE aiReturn aiMaterial::AddProperty<int>(const int* pInput,
  304. const unsigned int pNumValues,
  305. const char* pKey,
  306. unsigned int type,
  307. unsigned int index) {
  308. return AddBinaryProperty((const void*)pInput,
  309. pNumValues * sizeof(int),
  310. pKey,type,index,aiPTI_Integer);
  311. }
  312. //! @endcond