SMaterial.h 17 KB

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