SMaterial.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 "SColor.h"
  6. #include "matrix4.h"
  7. #include "irrMath.h"
  8. #include "EMaterialTypes.h"
  9. #include "EMaterialProps.h"
  10. #include "SMaterialLayer.h"
  11. #include "IrrCompileConfig.h" // for IRRLICHT_API
  12. namespace irr
  13. {
  14. namespace video
  15. {
  16. class ITexture;
  17. //! Flag for MaterialTypeParam (in combination with EMT_ONETEXTURE_BLEND) or for BlendFactor
  18. //! BlendFunc = source * sourceFactor + dest * destFactor
  19. enum E_BLEND_FACTOR
  20. {
  21. EBF_ZERO = 0, //!< src & dest (0, 0, 0, 0)
  22. EBF_ONE, //!< src & dest (1, 1, 1, 1)
  23. EBF_DST_COLOR, //!< src (destR, destG, destB, destA)
  24. EBF_ONE_MINUS_DST_COLOR, //!< src (1-destR, 1-destG, 1-destB, 1-destA)
  25. EBF_SRC_COLOR, //!< dest (srcR, srcG, srcB, srcA)
  26. EBF_ONE_MINUS_SRC_COLOR, //!< dest (1-srcR, 1-srcG, 1-srcB, 1-srcA)
  27. EBF_SRC_ALPHA, //!< src & dest (srcA, srcA, srcA, srcA)
  28. EBF_ONE_MINUS_SRC_ALPHA, //!< src & dest (1-srcA, 1-srcA, 1-srcA, 1-srcA)
  29. EBF_DST_ALPHA, //!< src & dest (destA, destA, destA, destA)
  30. EBF_ONE_MINUS_DST_ALPHA, //!< src & dest (1-destA, 1-destA, 1-destA, 1-destA)
  31. EBF_SRC_ALPHA_SATURATE //!< src (min(srcA, 1-destA), idem, ...)
  32. };
  33. //! Values defining the blend operation
  34. enum E_BLEND_OPERATION
  35. {
  36. EBO_NONE = 0, //!< No blending happens
  37. EBO_ADD, //!< Default blending adds the color values
  38. EBO_SUBTRACT, //!< This mode subtracts the color values
  39. EBO_REVSUBTRACT, //!< This modes subtracts destination from source
  40. EBO_MIN, //!< Choose minimum value of each color channel
  41. EBO_MAX, //!< Choose maximum value of each color channel
  42. EBO_MIN_FACTOR, //!< Choose minimum value of each color channel after applying blend factors, not widely supported
  43. EBO_MAX_FACTOR, //!< Choose maximum value of each color channel after applying blend factors, not widely supported
  44. EBO_MIN_ALPHA, //!< Choose minimum value of each color channel based on alpha value, not widely supported
  45. EBO_MAX_ALPHA //!< Choose maximum value of each color channel based on alpha value, not widely supported
  46. };
  47. //! MaterialTypeParam: e.g. DirectX: D3DTOP_MODULATE, D3DTOP_MODULATE2X, D3DTOP_MODULATE4X
  48. enum E_MODULATE_FUNC
  49. {
  50. EMFN_MODULATE_1X = 1,
  51. EMFN_MODULATE_2X = 2,
  52. EMFN_MODULATE_4X = 4
  53. };
  54. //! Comparison function, e.g. for depth buffer test
  55. enum E_COMPARISON_FUNC
  56. {
  57. //! Depth test disabled (disable also write to depth buffer)
  58. ECFN_DISABLED = 0,
  59. //! <= test, default for e.g. depth test
  60. ECFN_LESSEQUAL = 1,
  61. //! Exact equality
  62. ECFN_EQUAL = 2,
  63. //! exclusive less comparison, i.e. <
  64. ECFN_LESS,
  65. //! Succeeds almost always, except for exact equality
  66. ECFN_NOTEQUAL,
  67. //! >= test
  68. ECFN_GREATEREQUAL,
  69. //! inverse of <=
  70. ECFN_GREATER,
  71. //! test succeeds always
  72. ECFN_ALWAYS,
  73. //! Test never succeeds
  74. ECFN_NEVER
  75. };
  76. //! Enum values for enabling/disabling color planes for rendering
  77. enum E_COLOR_PLANE
  78. {
  79. //! No color enabled
  80. ECP_NONE = 0,
  81. //! Alpha enabled
  82. ECP_ALPHA = 1,
  83. //! Red enabled
  84. ECP_RED = 2,
  85. //! Green enabled
  86. ECP_GREEN = 4,
  87. //! Blue enabled
  88. ECP_BLUE = 8,
  89. //! All colors, no alpha
  90. ECP_RGB = 14,
  91. //! All planes enabled
  92. ECP_ALL = 15
  93. };
  94. //! Source of the alpha value to take
  95. /** This is currently only supported in EMT_ONETEXTURE_BLEND. You can use an
  96. or'ed combination of values. Alpha values are modulated (multiplied). */
  97. enum E_ALPHA_SOURCE
  98. {
  99. //! Use no alpha, somewhat redundant with other settings
  100. EAS_NONE = 0,
  101. //! Use vertex color alpha
  102. EAS_VERTEX_COLOR,
  103. //! Use texture alpha channel
  104. EAS_TEXTURE
  105. };
  106. //! Pack srcFact, dstFact, Modulate and alpha source to MaterialTypeParam or BlendFactor
  107. /** alpha source can be an OR'ed combination of E_ALPHA_SOURCE values. */
  108. inline f32 pack_textureBlendFunc(const E_BLEND_FACTOR srcFact, const E_BLEND_FACTOR dstFact,
  109. const E_MODULATE_FUNC modulate = EMFN_MODULATE_1X, const u32 alphaSource = EAS_TEXTURE)
  110. {
  111. const u32 tmp = (alphaSource << 20) | (modulate << 16) | (srcFact << 12) | (dstFact << 8) | (srcFact << 4) | dstFact;
  112. return FR(tmp);
  113. }
  114. //! Pack srcRGBFact, dstRGBFact, srcAlphaFact, dstAlphaFact, Modulate and alpha source to MaterialTypeParam or BlendFactor
  115. /** alpha source can be an OR'ed combination of E_ALPHA_SOURCE values. */
  116. inline f32 pack_textureBlendFuncSeparate(const E_BLEND_FACTOR srcRGBFact, const E_BLEND_FACTOR dstRGBFact,
  117. const E_BLEND_FACTOR srcAlphaFact, const E_BLEND_FACTOR dstAlphaFact,
  118. const E_MODULATE_FUNC modulate = EMFN_MODULATE_1X, const u32 alphaSource = EAS_TEXTURE)
  119. {
  120. const u32 tmp = (alphaSource << 20) | (modulate << 16) | (srcAlphaFact << 12) | (dstAlphaFact << 8) | (srcRGBFact << 4) | dstRGBFact;
  121. return FR(tmp);
  122. }
  123. //! Unpack srcFact, dstFact, modulo and alphaSource factors
  124. /** The fields don't use the full byte range, so we could pack even more... */
  125. inline void unpack_textureBlendFunc(E_BLEND_FACTOR &srcFact, E_BLEND_FACTOR &dstFact,
  126. E_MODULATE_FUNC &modulo, u32 &alphaSource, const f32 param)
  127. {
  128. const u32 state = IR(param);
  129. alphaSource = (state & 0x00F00000) >> 20;
  130. modulo = E_MODULATE_FUNC((state & 0x000F0000) >> 16);
  131. srcFact = E_BLEND_FACTOR((state & 0x000000F0) >> 4);
  132. dstFact = E_BLEND_FACTOR((state & 0x0000000F));
  133. }
  134. //! Unpack srcRGBFact, dstRGBFact, srcAlphaFact, dstAlphaFact, modulo and alphaSource factors
  135. /** The fields don't use the full byte range, so we could pack even more... */
  136. inline void unpack_textureBlendFuncSeparate(E_BLEND_FACTOR &srcRGBFact, E_BLEND_FACTOR &dstRGBFact,
  137. E_BLEND_FACTOR &srcAlphaFact, E_BLEND_FACTOR &dstAlphaFact,
  138. E_MODULATE_FUNC &modulo, u32 &alphaSource, const f32 param)
  139. {
  140. const u32 state = IR(param);
  141. alphaSource = (state & 0x00F00000) >> 20;
  142. modulo = E_MODULATE_FUNC((state & 0x000F0000) >> 16);
  143. srcAlphaFact = E_BLEND_FACTOR((state & 0x0000F000) >> 12);
  144. dstAlphaFact = E_BLEND_FACTOR((state & 0x00000F00) >> 8);
  145. srcRGBFact = E_BLEND_FACTOR((state & 0x000000F0) >> 4);
  146. dstRGBFact = E_BLEND_FACTOR((state & 0x0000000F));
  147. }
  148. //! has blend factor alphablending
  149. inline bool textureBlendFunc_hasAlpha(const E_BLEND_FACTOR factor)
  150. {
  151. switch (factor) {
  152. case EBF_SRC_ALPHA:
  153. case EBF_ONE_MINUS_SRC_ALPHA:
  154. case EBF_DST_ALPHA:
  155. case EBF_ONE_MINUS_DST_ALPHA:
  156. case EBF_SRC_ALPHA_SATURATE:
  157. return true;
  158. default:
  159. return false;
  160. }
  161. }
  162. //! These flags are used to specify the anti-aliasing and smoothing modes
  163. /** Techniques supported are multisampling, geometry smoothing, and alpha
  164. to coverage.
  165. Some drivers don't support a per-material setting of the anti-aliasing
  166. modes. In those cases, FSAA/multisampling is defined by the device mode
  167. chosen upon creation via irr::SIrrCreationParameters.
  168. */
  169. enum E_ANTI_ALIASING_MODE
  170. {
  171. //! Use to turn off anti-aliasing for this material
  172. EAAM_OFF = 0,
  173. //! Default anti-aliasing mode
  174. EAAM_SIMPLE = 1,
  175. //! High-quality anti-aliasing, not always supported, automatically enables SIMPLE mode
  176. EAAM_QUALITY = 3,
  177. //! Enhanced anti-aliasing for transparent materials
  178. /** Usually used with EMT_TRANSPARENT_ALPHA_CHANNEL_REF and multisampling. */
  179. EAAM_ALPHA_TO_COVERAGE = 4
  180. };
  181. //! Names for polygon offset direction
  182. const c8 *const PolygonOffsetDirectionNames[] = {
  183. "Back",
  184. "Front",
  185. 0,
  186. };
  187. //! For SMaterial.ZWriteEnable
  188. enum E_ZWRITE
  189. {
  190. //! zwrite always disabled for this material
  191. EZW_OFF = 0,
  192. //! This is the default setting for SMaterial and tries to handle things automatically.
  193. //! This is what you want to set to enable zwriting.
  194. //! Usually zwriting is enabled non-transparent materials - as far as Irrlicht can recognize those.
  195. //! Basically Irrlicht tries to handle the zwriting for you and assumes transparent materials don't need it.
  196. //! This is addionally affected by IVideoDriver::setAllowZWriteOnTransparent
  197. EZW_AUTO,
  198. //! zwrite always enabled for this material
  199. EZW_ON
  200. };
  201. //! Names for E_ZWRITE
  202. const c8 *const ZWriteNames[] = {
  203. "Off",
  204. "Auto",
  205. "On",
  206. 0,
  207. };
  208. //! Maximum number of texture an SMaterial can have.
  209. /** SMaterial might ignore some textures in most function, like assignment and comparison,
  210. when SIrrlichtCreationParameters::MaxTextureUnits is set to a lower number.
  211. */
  212. const u32 MATERIAL_MAX_TEXTURES = 4;
  213. //! Struct for holding parameters for a material renderer
  214. // Note for implementors: Serialization is in CNullDriver
  215. class SMaterial
  216. {
  217. public:
  218. //! Default constructor. Creates a solid, lit material with white colors
  219. SMaterial() :
  220. MaterialType(EMT_SOLID), ColorParam(0, 0, 0, 0),
  221. MaterialTypeParam(0.0f), Thickness(1.0f), ZBuffer(ECFN_LESSEQUAL),
  222. AntiAliasing(EAAM_SIMPLE), ColorMask(ECP_ALL),
  223. BlendOperation(EBO_NONE), BlendFactor(0.0f), PolygonOffsetDepthBias(0.f),
  224. PolygonOffsetSlopeScale(0.f), Wireframe(false), PointCloud(false),
  225. ZWriteEnable(EZW_AUTO),
  226. BackfaceCulling(true), FrontfaceCulling(false), FogEnable(false),
  227. UseMipMaps(true)
  228. {
  229. }
  230. //! Texture layer array.
  231. SMaterialLayer TextureLayers[MATERIAL_MAX_TEXTURES];
  232. //! Type of the material. Specifies how everything is blended together
  233. E_MATERIAL_TYPE MaterialType;
  234. //! Custom color parameter, can be used by custom shader materials.
  235. // See MainShaderConstantSetter in Minetest.
  236. SColor ColorParam;
  237. //! Free parameter, dependent on the material type.
  238. /** Mostly ignored, used for example in
  239. EMT_TRANSPARENT_ALPHA_CHANNEL and EMT_ONETEXTURE_BLEND. */
  240. f32 MaterialTypeParam;
  241. //! Thickness of non-3dimensional elements such as lines and points.
  242. f32 Thickness;
  243. //! Is the ZBuffer enabled? Default: ECFN_LESSEQUAL
  244. /** If you want to disable depth test for this material
  245. just set this parameter to ECFN_DISABLED.
  246. Values are from E_COMPARISON_FUNC. */
  247. u8 ZBuffer;
  248. //! Sets the antialiasing mode
  249. /** Values are chosen from E_ANTI_ALIASING_MODE. Default is
  250. EAAM_SIMPLE, i.e. simple multi-sample anti-aliasing. */
  251. u8 AntiAliasing;
  252. //! Defines the enabled color planes
  253. /** Values are defined as or'ed values of the E_COLOR_PLANE enum.
  254. Only enabled color planes will be rendered to the current render
  255. target. Typical use is to disable all colors when rendering only to
  256. depth or stencil buffer, or using Red and Green for Stereo rendering. */
  257. u8 ColorMask : 4;
  258. //! Store the blend operation of choice
  259. /** Values to be chosen from E_BLEND_OPERATION. */
  260. E_BLEND_OPERATION BlendOperation : 4;
  261. //! Store the blend factors
  262. /** textureBlendFunc/textureBlendFuncSeparate functions should be used to write
  263. properly blending factors to this parameter.
  264. Due to historical reasons this parameter is not used for material type
  265. EMT_ONETEXTURE_BLEND which uses MaterialTypeParam instead for the blend factor.
  266. It's generally used only for materials without any blending otherwise (like EMT_SOLID).
  267. It's main use is to allow having shader materials which can enable/disable
  268. blending after they have been created.
  269. When you set this you usually also have to set BlendOperation to a value != EBO_NONE
  270. (setting it to EBO_ADD is probably the most common one value). */
  271. f32 BlendFactor;
  272. //! A constant z-buffer offset for a polygon/line/point
  273. /** The range of the value is driver specific.
  274. On OpenGL you get units which are multiplied by the smallest value that is guaranteed to produce a resolvable offset.
  275. On D3D9 you can pass a range between -1 and 1. But you should likely divide it by the range of the depthbuffer.
  276. Like dividing by 65535.0 for a 16 bit depthbuffer. Thought it still might produce too large of a bias.
  277. Some article (https://aras-p.info/blog/2008/06/12/depth-bias-and-the-power-of-deceiving-yourself/)
  278. recommends multiplying by 2.0*4.8e-7 (and strangely on both 16 bit and 24 bit). */
  279. f32 PolygonOffsetDepthBias;
  280. //! Variable Z-Buffer offset based on the slope of the polygon.
  281. /** For polygons looking flat at a camera you could use 0 (for example in a 2D game)
  282. But in most cases you will have polygons rendered at a certain slope.
  283. The driver will calculate the slope for you and this value allows to scale that slope.
  284. The complete polygon offset is: PolygonOffsetSlopeScale*slope + PolygonOffsetDepthBias
  285. A good default here is to use 1.f if you want to push the polygons away from the camera
  286. and -1.f to pull them towards the camera. */
  287. f32 PolygonOffsetSlopeScale;
  288. //! Draw as wireframe or filled triangles? Default: false
  289. bool Wireframe : 1;
  290. //! Draw as point cloud or filled triangles? Default: false
  291. bool PointCloud : 1;
  292. //! Is the zbuffer writable or is it read-only. Default: EZW_AUTO.
  293. /** If this parameter is not EZW_OFF, you probably also want to set ZBuffer
  294. to values other than ECFN_DISABLED */
  295. E_ZWRITE ZWriteEnable : 2;
  296. //! Is backface culling enabled? Default: true
  297. bool BackfaceCulling : 1;
  298. //! Is frontface culling enabled? Default: false
  299. bool FrontfaceCulling : 1;
  300. //! Is fog enabled? Default: false
  301. bool FogEnable : 1;
  302. //! Shall mipmaps be used if available
  303. /** Sometimes, disabling mipmap usage can be useful. Default: true */
  304. bool UseMipMaps : 1;
  305. //! Execute a function on all texture layers.
  306. /** Useful for setting properties which are not per material, but per
  307. texture layer, e.g. bilinear filtering. */
  308. template <typename F>
  309. void forEachTexture(F &&fn)
  310. {
  311. for (u32 i = 0; i < MATERIAL_MAX_TEXTURES; i++) {
  312. fn(TextureLayers[i]);
  313. }
  314. }
  315. //! Gets the texture transformation matrix for level i
  316. /** \param i The desired level. Must not be larger than MATERIAL_MAX_TEXTURES
  317. \return Texture matrix for texture level i. */
  318. core::matrix4 &getTextureMatrix(u32 i)
  319. {
  320. return TextureLayers[i].getTextureMatrix();
  321. }
  322. //! Gets the immutable texture transformation matrix for level i
  323. /** \param i The desired level.
  324. \return Texture matrix for texture level i, or identity matrix for levels larger than MATERIAL_MAX_TEXTURES. */
  325. const core::matrix4 &getTextureMatrix(u32 i) const
  326. {
  327. if (i < MATERIAL_MAX_TEXTURES)
  328. return TextureLayers[i].getTextureMatrix();
  329. else
  330. return core::IdentityMatrix;
  331. }
  332. //! Sets the i-th texture transformation matrix
  333. /** \param i The desired level.
  334. \param mat Texture matrix for texture level i. */
  335. void setTextureMatrix(u32 i, const core::matrix4 &mat)
  336. {
  337. if (i >= MATERIAL_MAX_TEXTURES)
  338. return;
  339. TextureLayers[i].setTextureMatrix(mat);
  340. }
  341. //! Gets the i-th texture
  342. /** \param i The desired level.
  343. \return Texture for texture level i, if defined, else 0. */
  344. ITexture *getTexture(u32 i) const
  345. {
  346. return i < MATERIAL_MAX_TEXTURES ? TextureLayers[i].Texture : 0;
  347. }
  348. //! Sets the i-th texture
  349. /** If i>=MATERIAL_MAX_TEXTURES this setting will be ignored.
  350. \param i The desired level.
  351. \param tex Texture for texture level i. */
  352. void setTexture(u32 i, ITexture *tex)
  353. {
  354. if (i >= MATERIAL_MAX_TEXTURES)
  355. return;
  356. TextureLayers[i].Texture = tex;
  357. }
  358. //! Inequality operator
  359. /** \param b Material to compare to.
  360. \return True if the materials differ, else false. */
  361. inline bool operator!=(const SMaterial &b) const
  362. {
  363. bool different =
  364. MaterialType != b.MaterialType ||
  365. MaterialTypeParam != b.MaterialTypeParam ||
  366. Thickness != b.Thickness ||
  367. Wireframe != b.Wireframe ||
  368. PointCloud != b.PointCloud ||
  369. ZBuffer != b.ZBuffer ||
  370. ZWriteEnable != b.ZWriteEnable ||
  371. BackfaceCulling != b.BackfaceCulling ||
  372. FrontfaceCulling != b.FrontfaceCulling ||
  373. FogEnable != b.FogEnable ||
  374. AntiAliasing != b.AntiAliasing ||
  375. ColorMask != b.ColorMask ||
  376. BlendOperation != b.BlendOperation ||
  377. BlendFactor != b.BlendFactor ||
  378. PolygonOffsetDepthBias != b.PolygonOffsetDepthBias ||
  379. PolygonOffsetSlopeScale != b.PolygonOffsetSlopeScale ||
  380. UseMipMaps != b.UseMipMaps;
  381. for (u32 i = 0; (i < MATERIAL_MAX_TEXTURES) && !different; ++i) {
  382. different |= (TextureLayers[i] != b.TextureLayers[i]);
  383. }
  384. return different;
  385. }
  386. //! Equality operator
  387. /** \param b Material to compare to.
  388. \return True if the materials are equal, else false. */
  389. inline bool operator==(const SMaterial &b) const
  390. {
  391. return !(b != *this);
  392. }
  393. //! Check if material needs alpha blending
  394. bool isAlphaBlendOperation() const
  395. {
  396. if (BlendOperation != EBO_NONE && BlendFactor != 0.f) {
  397. E_BLEND_FACTOR srcRGBFact = EBF_ZERO;
  398. E_BLEND_FACTOR dstRGBFact = EBF_ZERO;
  399. E_BLEND_FACTOR srcAlphaFact = EBF_ZERO;
  400. E_BLEND_FACTOR dstAlphaFact = EBF_ZERO;
  401. E_MODULATE_FUNC modulo = EMFN_MODULATE_1X;
  402. u32 alphaSource = 0;
  403. unpack_textureBlendFuncSeparate(srcRGBFact, dstRGBFact, srcAlphaFact, dstAlphaFact, modulo, alphaSource, BlendFactor);
  404. if (textureBlendFunc_hasAlpha(srcRGBFact) || textureBlendFunc_hasAlpha(dstRGBFact) ||
  405. textureBlendFunc_hasAlpha(srcAlphaFact) || textureBlendFunc_hasAlpha(dstAlphaFact)) {
  406. return true;
  407. }
  408. }
  409. return false;
  410. }
  411. //! Check for some fixed-function transparent types. Still used internally, but might be deprecated soon.
  412. //! You probably should not use this anymore, IVideoDriver::needsTransparentRenderPass is more useful in most situations
  413. //! as it asks the material renders directly what they do with the material.
  414. bool isTransparent() const
  415. {
  416. if (MaterialType == EMT_TRANSPARENT_ALPHA_CHANNEL ||
  417. MaterialType == EMT_TRANSPARENT_VERTEX_ALPHA)
  418. return true;
  419. return false;
  420. }
  421. };
  422. //! global const identity Material
  423. IRRLICHT_API extern SMaterial IdentityMaterial;
  424. } // end namespace video
  425. } // end namespace irr