SMaterialLayer.h 6.3 KB

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