SMaterialLayer.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef __S_MATERIAL_LAYER_H_INCLUDED__
  5. #define __S_MATERIAL_LAYER_H_INCLUDED__
  6. #include "matrix4.h"
  7. #include "irrAllocator.h"
  8. namespace irr
  9. {
  10. namespace video
  11. {
  12. class ITexture;
  13. //! Texture coord clamp mode outside [0.0, 1.0]
  14. enum E_TEXTURE_CLAMP
  15. {
  16. //! Texture repeats
  17. ETC_REPEAT = 0,
  18. //! Texture is clamped to the last pixel
  19. ETC_CLAMP,
  20. //! Texture is clamped to the edge pixel
  21. ETC_CLAMP_TO_EDGE,
  22. //! Texture is clamped to the border pixel (if exists)
  23. ETC_CLAMP_TO_BORDER,
  24. //! Texture is alternatingly mirrored (0..1..0..1..0..)
  25. ETC_MIRROR,
  26. //! Texture is mirrored once and then clamped (0..1..0)
  27. ETC_MIRROR_CLAMP,
  28. //! Texture is mirrored once and then clamped to edge
  29. ETC_MIRROR_CLAMP_TO_EDGE,
  30. //! Texture is mirrored once and then clamped to border
  31. ETC_MIRROR_CLAMP_TO_BORDER
  32. };
  33. static const char* const aTextureClampNames[] = {
  34. "texture_clamp_repeat",
  35. "texture_clamp_clamp",
  36. "texture_clamp_clamp_to_edge",
  37. "texture_clamp_clamp_to_border",
  38. "texture_clamp_mirror",
  39. "texture_clamp_mirror_clamp",
  40. "texture_clamp_mirror_clamp_to_edge",
  41. "texture_clamp_mirror_clamp_to_border", 0};
  42. //! Struct for holding material parameters which exist per texture layer
  43. // Note for implementors: Serialization is in CNullDriver
  44. class SMaterialLayer
  45. {
  46. public:
  47. //! Default constructor
  48. SMaterialLayer() : Texture(0), TextureWrapU(ETC_REPEAT), TextureWrapV(ETC_REPEAT), TextureWrapW(ETC_REPEAT),
  49. BilinearFilter(true), TrilinearFilter(false), AnisotropicFilter(0), LODBias(0), TextureMatrix(0)
  50. {
  51. }
  52. //! Copy constructor
  53. /** \param other Material layer to copy from. */
  54. SMaterialLayer(const SMaterialLayer& other)
  55. {
  56. // This pointer is checked during assignment
  57. TextureMatrix = 0;
  58. *this = other;
  59. }
  60. //! Destructor
  61. ~SMaterialLayer()
  62. {
  63. if ( TextureMatrix )
  64. {
  65. MatrixAllocator.destruct(TextureMatrix);
  66. MatrixAllocator.deallocate(TextureMatrix);
  67. }
  68. }
  69. //! Assignment operator
  70. /** \param other Material layer to copy from.
  71. \return This material layer, updated. */
  72. SMaterialLayer& operator=(const SMaterialLayer& other)
  73. {
  74. // Check for self-assignment!
  75. if (this == &other)
  76. return *this;
  77. Texture = other.Texture;
  78. if (TextureMatrix)
  79. {
  80. if (other.TextureMatrix)
  81. *TextureMatrix = *other.TextureMatrix;
  82. else
  83. {
  84. MatrixAllocator.destruct(TextureMatrix);
  85. MatrixAllocator.deallocate(TextureMatrix);
  86. TextureMatrix = 0;
  87. }
  88. }
  89. else
  90. {
  91. if (other.TextureMatrix)
  92. {
  93. TextureMatrix = MatrixAllocator.allocate(1);
  94. MatrixAllocator.construct(TextureMatrix,*other.TextureMatrix);
  95. }
  96. else
  97. TextureMatrix = 0;
  98. }
  99. TextureWrapU = other.TextureWrapU;
  100. TextureWrapV = other.TextureWrapV;
  101. TextureWrapW = other.TextureWrapW;
  102. BilinearFilter = other.BilinearFilter;
  103. TrilinearFilter = other.TrilinearFilter;
  104. AnisotropicFilter = other.AnisotropicFilter;
  105. LODBias = other.LODBias;
  106. return *this;
  107. }
  108. //! Gets the texture transformation matrix
  109. /** \return Texture matrix of this layer. */
  110. core::matrix4& getTextureMatrix()
  111. {
  112. if (!TextureMatrix)
  113. {
  114. TextureMatrix = MatrixAllocator.allocate(1);
  115. MatrixAllocator.construct(TextureMatrix,core::IdentityMatrix);
  116. }
  117. return *TextureMatrix;
  118. }
  119. //! Gets the immutable texture transformation matrix
  120. /** \return Texture matrix of this layer. */
  121. const core::matrix4& getTextureMatrix() const
  122. {
  123. if (TextureMatrix)
  124. return *TextureMatrix;
  125. else
  126. return core::IdentityMatrix;
  127. }
  128. //! Sets the texture transformation matrix to mat
  129. /** NOTE: Pipelines can ignore this matrix when the
  130. texture is 0.
  131. \param mat New texture matrix for this layer. */
  132. void setTextureMatrix(const core::matrix4& mat)
  133. {
  134. if (!TextureMatrix)
  135. {
  136. TextureMatrix = MatrixAllocator.allocate(1);
  137. MatrixAllocator.construct(TextureMatrix,mat);
  138. }
  139. else
  140. *TextureMatrix = mat;
  141. }
  142. //! Inequality operator
  143. /** \param b Layer to compare to.
  144. \return True if layers are different, else false. */
  145. inline bool operator!=(const SMaterialLayer& b) const
  146. {
  147. bool different =
  148. Texture != b.Texture ||
  149. TextureWrapU != b.TextureWrapU ||
  150. TextureWrapV != b.TextureWrapV ||
  151. TextureWrapW != b.TextureWrapW ||
  152. BilinearFilter != b.BilinearFilter ||
  153. TrilinearFilter != b.TrilinearFilter ||
  154. AnisotropicFilter != b.AnisotropicFilter ||
  155. LODBias != b.LODBias;
  156. if (different)
  157. return true;
  158. else
  159. different |= (TextureMatrix != b.TextureMatrix) &&
  160. (!TextureMatrix || !b.TextureMatrix || (*TextureMatrix != *(b.TextureMatrix)));
  161. return different;
  162. }
  163. //! Equality operator
  164. /** \param b Layer to compare to.
  165. \return True if layers are equal, else false. */
  166. inline bool operator==(const SMaterialLayer& b) const
  167. { return !(b!=*this); }
  168. //! Texture
  169. ITexture* Texture;
  170. //! Texture Clamp Mode
  171. /** Values are taken from E_TEXTURE_CLAMP. */
  172. u8 TextureWrapU:4;
  173. u8 TextureWrapV:4;
  174. u8 TextureWrapW:4;
  175. //! Is bilinear filtering enabled? Default: true
  176. bool BilinearFilter:1;
  177. //! Is trilinear filtering enabled? Default: false
  178. /** If the trilinear filter flag is enabled,
  179. the bilinear filtering flag is ignored. */
  180. bool TrilinearFilter:1;
  181. //! Is anisotropic filtering enabled? Default: 0, disabled
  182. /** In Irrlicht you can use anisotropic texture filtering
  183. in conjunction with bilinear or trilinear texture
  184. filtering to improve rendering results. Primitives
  185. will look less blurry with this flag switched on. The number gives
  186. the maximal anisotropy degree, and is often in the range 2-16.
  187. Value 1 is equivalent to 0, but should be avoided. */
  188. u8 AnisotropicFilter;
  189. //! Bias for the mipmap choosing decision.
  190. /** This value can make the textures more or less blurry than with the
  191. default value of 0. The value (divided by 8.f) is added to the mipmap level
  192. chosen initially, and thus takes a smaller mipmap for a region
  193. if the value is positive. */
  194. s8 LODBias;
  195. private:
  196. friend class SMaterial;
  197. irr::core::irrAllocator<irr::core::matrix4> MatrixAllocator;
  198. //! Texture Matrix
  199. /** Do not access this element directly as the internal
  200. resource management has to cope with Null pointers etc. */
  201. core::matrix4* TextureMatrix;
  202. };
  203. } // end namespace video
  204. } // end namespace irr
  205. #endif // __S_MATERIAL_LAYER_H_INCLUDED__