SMaterial.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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_H_INCLUDED__
  5. #define __S_MATERIAL_H_INCLUDED__
  6. #include "SColor.h"
  7. #include "matrix4.h"
  8. #include "irrArray.h"
  9. #include "irrMath.h"
  10. #include "EMaterialTypes.h"
  11. #include "EMaterialFlags.h"
  12. #include "SMaterialLayer.h"
  13. namespace irr
  14. {
  15. namespace video
  16. {
  17. class ITexture;
  18. //! Flag for MaterialTypeParam (in combination with EMT_ONETEXTURE_BLEND) or for BlendFactor
  19. //! BlendFunc = source * sourceFactor + dest * destFactor
  20. enum E_BLEND_FACTOR
  21. {
  22. EBF_ZERO = 0, //!< src & dest (0, 0, 0, 0)
  23. EBF_ONE, //!< src & dest (1, 1, 1, 1)
  24. EBF_DST_COLOR, //!< src (destR, destG, destB, destA)
  25. EBF_ONE_MINUS_DST_COLOR, //!< src (1-destR, 1-destG, 1-destB, 1-destA)
  26. EBF_SRC_COLOR, //!< dest (srcR, srcG, srcB, srcA)
  27. EBF_ONE_MINUS_SRC_COLOR, //!< dest (1-srcR, 1-srcG, 1-srcB, 1-srcA)
  28. EBF_SRC_ALPHA, //!< src & dest (srcA, srcA, srcA, srcA)
  29. EBF_ONE_MINUS_SRC_ALPHA, //!< src & dest (1-srcA, 1-srcA, 1-srcA, 1-srcA)
  30. EBF_DST_ALPHA, //!< src & dest (destA, destA, destA, destA)
  31. EBF_ONE_MINUS_DST_ALPHA, //!< src & dest (1-destA, 1-destA, 1-destA, 1-destA)
  32. EBF_SRC_ALPHA_SATURATE //!< src (min(srcA, 1-destA), idem, ...)
  33. };
  34. //! Values defining the blend operation
  35. enum E_BLEND_OPERATION
  36. {
  37. EBO_NONE = 0, //!< No blending happens
  38. EBO_ADD, //!< Default blending adds the color values
  39. EBO_SUBTRACT, //!< This mode subtracts the color values
  40. EBO_REVSUBTRACT,//!< This modes subtracts destination from source
  41. EBO_MIN, //!< Choose minimum value of each color channel
  42. EBO_MAX, //!< Choose maximum value of each color channel
  43. EBO_MIN_FACTOR, //!< Choose minimum value of each color channel after applying blend factors, not widely supported
  44. EBO_MAX_FACTOR, //!< Choose maximum value of each color channel after applying blend factors, not widely supported
  45. EBO_MIN_ALPHA, //!< Choose minimum value of each color channel based on alpha value, not widely supported
  46. EBO_MAX_ALPHA //!< Choose maximum value of each color channel based on alpha value, not widely supported
  47. };
  48. //! MaterialTypeParam: e.g. DirectX: D3DTOP_MODULATE, D3DTOP_MODULATE2X, D3DTOP_MODULATE4X
  49. enum E_MODULATE_FUNC
  50. {
  51. EMFN_MODULATE_1X = 1,
  52. EMFN_MODULATE_2X = 2,
  53. EMFN_MODULATE_4X = 4
  54. };
  55. //! Comparison function, e.g. for depth buffer test
  56. enum E_COMPARISON_FUNC
  57. {
  58. //! Depth test disabled (disable also write to depth buffer)
  59. ECFN_DISABLED=0,
  60. //! <= test, default for e.g. depth test
  61. ECFN_LESSEQUAL=1,
  62. //! Exact equality
  63. ECFN_EQUAL=2,
  64. //! exclusive less comparison, i.e. <
  65. ECFN_LESS,
  66. //! Succeeds almost always, except for exact equality
  67. ECFN_NOTEQUAL,
  68. //! >= test
  69. ECFN_GREATEREQUAL,
  70. //! inverse of <=
  71. ECFN_GREATER,
  72. //! test succeeds always
  73. ECFN_ALWAYS,
  74. //! Test never succeeds
  75. ECFN_NEVER
  76. };
  77. //! Enum values for enabling/disabling color planes for rendering
  78. enum E_COLOR_PLANE
  79. {
  80. //! No color enabled
  81. ECP_NONE=0,
  82. //! Alpha enabled
  83. ECP_ALPHA=1,
  84. //! Red enabled
  85. ECP_RED=2,
  86. //! Green enabled
  87. ECP_GREEN=4,
  88. //! Blue enabled
  89. ECP_BLUE=8,
  90. //! All colors, no alpha
  91. ECP_RGB=14,
  92. //! All planes enabled
  93. ECP_ALL=15
  94. };
  95. //! Source of the alpha value to take
  96. /** This is currently only supported in EMT_ONETEXTURE_BLEND. You can use an
  97. or'ed combination of values. Alpha values are modulated (multiplied). */
  98. enum E_ALPHA_SOURCE
  99. {
  100. //! Use no alpha, somewhat redundant with other settings
  101. EAS_NONE=0,
  102. //! Use vertex color alpha
  103. EAS_VERTEX_COLOR,
  104. //! Use texture alpha channel
  105. EAS_TEXTURE
  106. };
  107. //! Pack srcFact, dstFact, Modulate and alpha source to MaterialTypeParam or BlendFactor
  108. /** alpha source can be an OR'ed combination of E_ALPHA_SOURCE values. */
  109. inline f32 pack_textureBlendFunc(const E_BLEND_FACTOR srcFact, const E_BLEND_FACTOR dstFact,
  110. const E_MODULATE_FUNC modulate=EMFN_MODULATE_1X, const u32 alphaSource=EAS_TEXTURE)
  111. {
  112. const u32 tmp = (alphaSource << 20) | (modulate << 16) | (srcFact << 12) | (dstFact << 8) | (srcFact << 4) | dstFact;
  113. return FR(tmp);
  114. }
  115. //! Pack srcRGBFact, dstRGBFact, srcAlphaFact, dstAlphaFact, Modulate and alpha source to MaterialTypeParam or BlendFactor
  116. /** alpha source can be an OR'ed combination of E_ALPHA_SOURCE values. */
  117. inline f32 pack_textureBlendFuncSeparate(const E_BLEND_FACTOR srcRGBFact, const E_BLEND_FACTOR dstRGBFact,
  118. const E_BLEND_FACTOR srcAlphaFact, const E_BLEND_FACTOR dstAlphaFact,
  119. const E_MODULATE_FUNC modulate=EMFN_MODULATE_1X, const u32 alphaSource=EAS_TEXTURE)
  120. {
  121. const u32 tmp = (alphaSource << 20) | (modulate << 16) | (srcAlphaFact << 12) | (dstAlphaFact << 8) | (srcRGBFact << 4) | dstRGBFact;
  122. return FR(tmp);
  123. }
  124. //! Unpack srcFact, dstFact, modulo and alphaSource factors
  125. /** The fields don't use the full byte range, so we could pack even more... */
  126. inline void unpack_textureBlendFunc(E_BLEND_FACTOR &srcFact, E_BLEND_FACTOR &dstFact,
  127. E_MODULATE_FUNC &modulo, u32& alphaSource, const f32 param)
  128. {
  129. const u32 state = IR(param);
  130. alphaSource = (state & 0x00F00000) >> 20;
  131. modulo = E_MODULATE_FUNC( ( state & 0x000F0000 ) >> 16 );
  132. srcFact = E_BLEND_FACTOR ( ( state & 0x000000F0 ) >> 4 );
  133. dstFact = E_BLEND_FACTOR ( ( state & 0x0000000F ) );
  134. }
  135. //! Unpack srcRGBFact, dstRGBFact, srcAlphaFact, dstAlphaFact, modulo and alphaSource factors
  136. /** The fields don't use the full byte range, so we could pack even more... */
  137. inline void unpack_textureBlendFuncSeparate(E_BLEND_FACTOR &srcRGBFact, E_BLEND_FACTOR &dstRGBFact,
  138. E_BLEND_FACTOR &srcAlphaFact, E_BLEND_FACTOR &dstAlphaFact,
  139. E_MODULATE_FUNC &modulo, u32& alphaSource, const f32 param)
  140. {
  141. const u32 state = IR(param);
  142. alphaSource = (state & 0x00F00000) >> 20;
  143. modulo = E_MODULATE_FUNC( ( state & 0x000F0000 ) >> 16 );
  144. srcAlphaFact = E_BLEND_FACTOR ( ( state & 0x0000F000 ) >> 12 );
  145. dstAlphaFact = E_BLEND_FACTOR ( ( state & 0x00000F00 ) >> 8 );
  146. srcRGBFact = E_BLEND_FACTOR ( ( state & 0x000000F0 ) >> 4 );
  147. dstRGBFact = E_BLEND_FACTOR ( ( state & 0x0000000F ) );
  148. }
  149. //! has blend factor alphablending
  150. inline bool textureBlendFunc_hasAlpha ( const E_BLEND_FACTOR factor )
  151. {
  152. switch ( factor )
  153. {
  154. case EBF_SRC_ALPHA:
  155. case EBF_ONE_MINUS_SRC_ALPHA:
  156. case EBF_DST_ALPHA:
  157. case EBF_ONE_MINUS_DST_ALPHA:
  158. case EBF_SRC_ALPHA_SATURATE:
  159. return true;
  160. default:
  161. return false;
  162. }
  163. }
  164. //! These flags are used to specify the anti-aliasing and smoothing modes
  165. /** Techniques supported are multisampling, geometry smoothing, and alpha
  166. to coverage.
  167. Some drivers don't support a per-material setting of the anti-aliasing
  168. modes. In those cases, FSAA/multisampling is defined by the device mode
  169. chosen upon creation via irr::SIrrCreationParameters.
  170. */
  171. enum E_ANTI_ALIASING_MODE
  172. {
  173. //! Use to turn off anti-aliasing for this material
  174. EAAM_OFF=0,
  175. //! Default anti-aliasing mode
  176. EAAM_SIMPLE=1,
  177. //! High-quality anti-aliasing, not always supported, automatically enables SIMPLE mode
  178. EAAM_QUALITY=3,
  179. //! Line smoothing
  180. //! Careful, enabling this can lead to software emulation under OpenGL
  181. EAAM_LINE_SMOOTH=4,
  182. //! point smoothing, often in software and slow, only with OpenGL
  183. EAAM_POINT_SMOOTH=8,
  184. //! All typical anti-alias and smooth modes
  185. EAAM_FULL_BASIC=15,
  186. //! Enhanced anti-aliasing for transparent materials
  187. /** Usually used with EMT_TRANSPARENT_ALPHA_CHANNEL_REF and multisampling. */
  188. EAAM_ALPHA_TO_COVERAGE=16
  189. };
  190. //! These flags allow to define the interpretation of vertex color when lighting is enabled
  191. /** Without lighting being enabled the vertex color is the only value defining the fragment color.
  192. Once lighting is enabled, the four values for diffuse, ambient, emissive, and specular take over.
  193. With these flags it is possible to define which lighting factor shall be defined by the vertex color
  194. instead of the lighting factor which is the same for all faces of that material.
  195. The default is to use vertex color for the diffuse value, another pretty common value is to use
  196. vertex color for both diffuse and ambient factor. */
  197. enum E_COLOR_MATERIAL
  198. {
  199. //! Don't use vertex color for lighting
  200. ECM_NONE=0,
  201. //! Use vertex color for diffuse light, this is default
  202. ECM_DIFFUSE,
  203. //! Use vertex color for ambient light
  204. ECM_AMBIENT,
  205. //! Use vertex color for emissive light
  206. ECM_EMISSIVE,
  207. //! Use vertex color for specular light
  208. ECM_SPECULAR,
  209. //! Use vertex color for both diffuse and ambient light
  210. ECM_DIFFUSE_AND_AMBIENT
  211. };
  212. //! DEPRECATED. Will be removed after Irrlicht 1.9.
  213. /** Flags for the definition of the polygon offset feature. These flags define whether the offset should be into the screen, or towards the eye. */
  214. enum E_POLYGON_OFFSET
  215. {
  216. //! Push pixel towards the far plane, away from the eye
  217. /** This is typically used for rendering inner areas. */
  218. EPO_BACK=0,
  219. //! Pull pixels towards the camera.
  220. /** This is typically used for polygons which should appear on top
  221. of other elements, such as decals. */
  222. EPO_FRONT=1
  223. };
  224. //! Names for polygon offset direction
  225. const c8* const PolygonOffsetDirectionNames[] =
  226. {
  227. "Back",
  228. "Front",
  229. 0
  230. };
  231. //! For SMaterial.ZWriteEnable
  232. enum E_ZWRITE
  233. {
  234. //! zwrite always disabled for this material
  235. EZW_OFF = 0,
  236. //! This is the default setting for SMaterial and tries to handle things automatically.
  237. //! This is also the value which is set when SMaterial::setFlag(EMF_ZWRITE_ENABLE) is enabled.
  238. //! Usually zwriting is enabled non-transparent materials - as far as Irrlicht can recognize those.
  239. //! Basically Irrlicht tries to handle the zwriting for you and assumes transparent materials don't need it.
  240. //! This is addionally affected by IVideoDriver::setAllowZWriteOnTransparent
  241. EZW_AUTO,
  242. //! zwrite always enabled for this material
  243. EZW_ON
  244. };
  245. //! Names for E_ZWRITE
  246. const c8* const ZWriteNames[] =
  247. {
  248. "Off",
  249. "Auto",
  250. "On",
  251. 0
  252. };
  253. //! Maximum number of texture an SMaterial can have.
  254. /** SMaterial might ignore some textures in most function, like assignment and comparison,
  255. when SIrrlichtCreationParameters::MaxTextureUnits is set to a lower number.
  256. */
  257. const u32 MATERIAL_MAX_TEXTURES = _IRR_MATERIAL_MAX_TEXTURES_;
  258. //! By default this is identical to MATERIAL_MAX_TEXTURES
  259. /** Users can modify this value if they are certain they don't need all
  260. available textures per material in their application. For example if you
  261. never need more than 2 textures per material you can set this to 2.
  262. We (mostly) avoid dynamic memory in SMaterial, so the extra memory
  263. will still be allocated. But by lowering MATERIAL_MAX_TEXTURES_USED the
  264. material comparisons and assignments can be faster. Also several other
  265. places in the engine can be faster when reducing this value to the limit
  266. you need.
  267. NOTE: This should only be changed once and before any call to createDevice.
  268. NOTE: Do not set it below 1 or above the value of _IRR_MATERIAL_MAX_TEXTURES_.
  269. NOTE: Going below 4 is usually not worth it.
  270. */
  271. IRRLICHT_API extern u32 MATERIAL_MAX_TEXTURES_USED;
  272. //! Struct for holding parameters for a material renderer
  273. // Note for implementors: Serialization is in CNullDriver
  274. class SMaterial
  275. {
  276. public:
  277. //! Default constructor. Creates a solid, lit material with white colors
  278. SMaterial()
  279. : MaterialType(EMT_SOLID), AmbientColor(255,255,255,255), DiffuseColor(255,255,255,255),
  280. EmissiveColor(0,0,0,0), SpecularColor(255,255,255,255),
  281. Shininess(0.0f), MaterialTypeParam(0.0f), MaterialTypeParam2(0.0f), Thickness(1.0f),
  282. ZBuffer(ECFN_LESSEQUAL), AntiAliasing(EAAM_SIMPLE), ColorMask(ECP_ALL),
  283. ColorMaterial(ECM_DIFFUSE), BlendOperation(EBO_NONE), BlendFactor(0.0f),
  284. PolygonOffsetFactor(0), PolygonOffsetDirection(EPO_FRONT),
  285. PolygonOffsetDepthBias(0.f), PolygonOffsetSlopeScale(0.f),
  286. Wireframe(false), PointCloud(false), GouraudShading(true),
  287. Lighting(true), ZWriteEnable(EZW_AUTO), BackfaceCulling(true), FrontfaceCulling(false),
  288. FogEnable(false), NormalizeNormals(false), UseMipMaps(true)
  289. { }
  290. //! Copy constructor
  291. /** \param other Material to copy from. */
  292. SMaterial(const SMaterial& other)
  293. {
  294. // These pointers are checked during assignment
  295. for (u32 i=0; i<MATERIAL_MAX_TEXTURES_USED; ++i)
  296. TextureLayer[i].TextureMatrix = 0;
  297. *this = other;
  298. }
  299. //! Assignment operator
  300. /** \param other Material to copy from. */
  301. SMaterial& operator=(const SMaterial& other)
  302. {
  303. // Check for self-assignment!
  304. if (this == &other)
  305. return *this;
  306. MaterialType = other.MaterialType;
  307. AmbientColor = other.AmbientColor;
  308. DiffuseColor = other.DiffuseColor;
  309. EmissiveColor = other.EmissiveColor;
  310. SpecularColor = other.SpecularColor;
  311. Shininess = other.Shininess;
  312. MaterialTypeParam = other.MaterialTypeParam;
  313. MaterialTypeParam2 = other.MaterialTypeParam2;
  314. Thickness = other.Thickness;
  315. for (u32 i=0; i<MATERIAL_MAX_TEXTURES_USED; ++i)
  316. {
  317. TextureLayer[i] = other.TextureLayer[i];
  318. }
  319. Wireframe = other.Wireframe;
  320. PointCloud = other.PointCloud;
  321. GouraudShading = other.GouraudShading;
  322. Lighting = other.Lighting;
  323. ZWriteEnable = other.ZWriteEnable;
  324. BackfaceCulling = other.BackfaceCulling;
  325. FrontfaceCulling = other.FrontfaceCulling;
  326. FogEnable = other.FogEnable;
  327. NormalizeNormals = other.NormalizeNormals;
  328. ZBuffer = other.ZBuffer;
  329. AntiAliasing = other.AntiAliasing;
  330. ColorMask = other.ColorMask;
  331. ColorMaterial = other.ColorMaterial;
  332. BlendOperation = other.BlendOperation;
  333. BlendFactor = other.BlendFactor;
  334. PolygonOffsetFactor = other.PolygonOffsetFactor;
  335. PolygonOffsetDirection = other.PolygonOffsetDirection;
  336. PolygonOffsetDepthBias = other.PolygonOffsetDepthBias;
  337. PolygonOffsetSlopeScale = other.PolygonOffsetSlopeScale;
  338. UseMipMaps = other.UseMipMaps;
  339. return *this;
  340. }
  341. //! Texture layer array.
  342. SMaterialLayer TextureLayer[MATERIAL_MAX_TEXTURES];
  343. //! Type of the material. Specifies how everything is blended together
  344. E_MATERIAL_TYPE MaterialType;
  345. //! How much ambient light (a global light) is reflected by this material.
  346. /** The default is full white, meaning objects are completely
  347. globally illuminated. Reduce this if you want to see diffuse
  348. or specular light effects. */
  349. SColor AmbientColor;
  350. //! How much diffuse light coming from a light source is reflected by this material.
  351. /** The default is full white. */
  352. SColor DiffuseColor;
  353. //! Light emitted by this material. Default is to emit no light.
  354. SColor EmissiveColor;
  355. //! How much specular light (highlights from a light) is reflected.
  356. /** The default is to reflect white specular light. See
  357. SMaterial::Shininess on how to enable specular lights. */
  358. SColor SpecularColor;
  359. //! Value affecting the size of specular highlights.
  360. /** A value of 20 is common. If set to 0, no specular
  361. highlights are being used. To activate, simply set the
  362. shininess of a material to a value in the range [0.5;128]:
  363. \code
  364. sceneNode->getMaterial(0).Shininess = 20.0f;
  365. \endcode
  366. You can change the color of the highlights using
  367. \code
  368. sceneNode->getMaterial(0).SpecularColor.set(255,255,255,255);
  369. \endcode
  370. The specular color of the dynamic lights
  371. (SLight::SpecularColor) will influence the the highlight color
  372. too, but they are set to a useful value by default when
  373. creating the light scene node. Here is a simple example on how
  374. to use specular highlights:
  375. \code
  376. // load and display mesh
  377. scene::IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(
  378. smgr->getMesh("data/faerie.md2"));
  379. node->setMaterialTexture(0, driver->getTexture("data/Faerie2.pcx")); // set diffuse texture
  380. node->setMaterialFlag(video::EMF_LIGHTING, true); // enable dynamic lighting
  381. node->getMaterial(0).Shininess = 20.0f; // set size of specular highlights
  382. // add white light
  383. scene::ILightSceneNode* light = smgr->addLightSceneNode(0,
  384. core::vector3df(5,5,5), video::SColorf(1.0f, 1.0f, 1.0f));
  385. \endcode */
  386. f32 Shininess;
  387. //! Free parameter, dependent on the material type.
  388. /** Mostly ignored, used for example in EMT_PARALLAX_MAP_SOLID,
  389. EMT_TRANSPARENT_ALPHA_CHANNEL and EMT_ONETEXTURE_BLEND. */
  390. f32 MaterialTypeParam;
  391. //! Second free parameter, dependent on the material type.
  392. /** Mostly ignored. */
  393. f32 MaterialTypeParam2;
  394. //! Thickness of non-3dimensional elements such as lines and points.
  395. f32 Thickness;
  396. //! Is the ZBuffer enabled? Default: ECFN_LESSEQUAL
  397. /** If you want to disable depth test for this material
  398. just set this parameter to ECFN_DISABLED.
  399. Values are from E_COMPARISON_FUNC. */
  400. u8 ZBuffer;
  401. //! Sets the antialiasing mode
  402. /** Values are chosen from E_ANTI_ALIASING_MODE. Default is
  403. EAAM_SIMPLE, i.e. simple multi-sample anti-aliasing. */
  404. u8 AntiAliasing;
  405. //! Defines the enabled color planes
  406. /** Values are defined as or'ed values of the E_COLOR_PLANE enum.
  407. Only enabled color planes will be rendered to the current render
  408. target. Typical use is to disable all colors when rendering only to
  409. depth or stencil buffer, or using Red and Green for Stereo rendering. */
  410. u8 ColorMask:4;
  411. //! Defines the interpretation of vertex color in the lighting equation
  412. /** Values should be chosen from E_COLOR_MATERIAL.
  413. When lighting is enabled, vertex color can be used instead of the
  414. material values for light modulation. This allows to easily change e.g. the
  415. diffuse light behavior of each face. The default, ECM_DIFFUSE, will result in
  416. a very similar rendering as with lighting turned off, just with light shading. */
  417. u8 ColorMaterial:3;
  418. //! Store the blend operation of choice
  419. /** Values to be chosen from E_BLEND_OPERATION. */
  420. E_BLEND_OPERATION BlendOperation:4;
  421. //! Store the blend factors
  422. /** textureBlendFunc/textureBlendFuncSeparate functions should be used to write
  423. properly blending factors to this parameter.
  424. Due to historical reasons this parameter is not used for material type
  425. EMT_ONETEXTURE_BLEND which uses MaterialTypeParam instead for the blend factor.
  426. It's generally used only for materials without any blending otherwise (like EMT_SOLID).
  427. It's main use is to allow having shader materials which can enable/disable
  428. blending after they have been created.
  429. When you set this you usually also have to set BlendOperation to a value != EBO_NONE
  430. (setting it to EBO_ADD is probably the most common one value). */
  431. f32 BlendFactor;
  432. //! DEPRECATED. Will be removed after Irrlicht 1.9. Please use PolygonOffsetDepthBias instead.
  433. /** Factor specifying how far the polygon offset should be made.
  434. Specifying 0 disables the polygon offset. The direction is specified separately.
  435. The factor can be from 0 to 7.
  436. Note: This probably never worked on Direct3D9 (was coded for D3D8 which had different value ranges) */
  437. u8 PolygonOffsetFactor:3;
  438. //! DEPRECATED. Will be removed after Irrlicht 1.9.
  439. /** Flag defining the direction the polygon offset is applied to.
  440. Can be to front or to back, specified by values from E_POLYGON_OFFSET. */
  441. E_POLYGON_OFFSET PolygonOffsetDirection:1;
  442. //! A constant z-buffer offset for a polygon/line/point
  443. /** The range of the value is driver specific.
  444. On OpenGL you get units which are multiplied by the smallest value that is guaranteed to produce a resolvable offset.
  445. On D3D9 you can pass a range between -1 and 1. But you should likely divide it by the range of the depthbuffer.
  446. Like dividing by 65535.0 for a 16 bit depthbuffer. Thought it still might produce too large of a bias.
  447. Some article (https://aras-p.info/blog/2008/06/12/depth-bias-and-the-power-of-deceiving-yourself/)
  448. recommends multiplying by 2.0*4.8e-7 (and strangely on both 16 bit and 24 bit). */
  449. f32 PolygonOffsetDepthBias;
  450. //! Variable Z-Buffer offset based on the slope of the polygon.
  451. /** For polygons looking flat at a camera you could use 0 (for example in a 2D game)
  452. But in most cases you will have polygons rendered at a certain slope.
  453. The driver will calculate the slope for you and this value allows to scale that slope.
  454. The complete polygon offset is: PolygonOffsetSlopeScale*slope + PolygonOffsetDepthBias
  455. A good default here is to use 1.f if you want to push the polygons away from the camera
  456. and -1.f to pull them towards the camera. */
  457. f32 PolygonOffsetSlopeScale;
  458. //! Draw as wireframe or filled triangles? Default: false
  459. /** The user can access a material flag using
  460. \code material.Wireframe=true \endcode
  461. or \code material.setFlag(EMF_WIREFRAME, true); \endcode */
  462. bool Wireframe:1;
  463. //! Draw as point cloud or filled triangles? Default: false
  464. bool PointCloud:1;
  465. //! Flat or Gouraud shading? Default: true
  466. bool GouraudShading:1;
  467. //! Will this material be lighted? Default: true
  468. bool Lighting:1;
  469. //! Is the zbuffer writable or is it read-only. Default: EZW_AUTO.
  470. /** If this parameter is not EZW_OFF, you probably also want to set ZBuffer
  471. to values other than ECFN_DISABLED */
  472. E_ZWRITE ZWriteEnable:2;
  473. //! Is backface culling enabled? Default: true
  474. bool BackfaceCulling:1;
  475. //! Is frontface culling enabled? Default: false
  476. bool FrontfaceCulling:1;
  477. //! Is fog enabled? Default: false
  478. bool FogEnable:1;
  479. //! Should normals be normalized?
  480. /** Always use this if the mesh lit and scaled. Default: false */
  481. bool NormalizeNormals:1;
  482. //! Shall mipmaps be used if available
  483. /** Sometimes, disabling mipmap usage can be useful. Default: true */
  484. bool UseMipMaps:1;
  485. //! Gets the texture transformation matrix for level i
  486. /** \param i The desired level. Must not be larger than MATERIAL_MAX_TEXTURES
  487. \return Texture matrix for texture level i. */
  488. core::matrix4& getTextureMatrix(u32 i)
  489. {
  490. return TextureLayer[i].getTextureMatrix();
  491. }
  492. //! Gets the immutable texture transformation matrix for level i
  493. /** \param i The desired level.
  494. \return Texture matrix for texture level i, or identity matrix for levels larger than MATERIAL_MAX_TEXTURES. */
  495. const core::matrix4& getTextureMatrix(u32 i) const
  496. {
  497. if (i<MATERIAL_MAX_TEXTURES)
  498. return TextureLayer[i].getTextureMatrix();
  499. else
  500. return core::IdentityMatrix;
  501. }
  502. //! Sets the i-th texture transformation matrix
  503. /** \param i The desired level.
  504. \param mat Texture matrix for texture level i. */
  505. void setTextureMatrix(u32 i, const core::matrix4& mat)
  506. {
  507. if (i>=MATERIAL_MAX_TEXTURES)
  508. return;
  509. TextureLayer[i].setTextureMatrix(mat);
  510. }
  511. //! Gets the i-th texture
  512. /** \param i The desired level.
  513. \return Texture for texture level i, if defined, else 0. */
  514. ITexture* getTexture(u32 i) const
  515. {
  516. return i < MATERIAL_MAX_TEXTURES ? TextureLayer[i].Texture : 0;
  517. }
  518. //! Sets the i-th texture
  519. /** If i>=MATERIAL_MAX_TEXTURES this setting will be ignored.
  520. \param i The desired level.
  521. \param tex Texture for texture level i. */
  522. void setTexture(u32 i, ITexture* tex)
  523. {
  524. if (i>=MATERIAL_MAX_TEXTURES)
  525. return;
  526. TextureLayer[i].Texture = tex;
  527. }
  528. //! Sets the Material flag to the given value
  529. /** \param flag The flag to be set.
  530. \param value The new value for the flag. */
  531. void setFlag(E_MATERIAL_FLAG flag, bool value)
  532. {
  533. switch (flag)
  534. {
  535. case EMF_WIREFRAME:
  536. Wireframe = value; break;
  537. case EMF_POINTCLOUD:
  538. PointCloud = value; break;
  539. case EMF_GOURAUD_SHADING:
  540. GouraudShading = value; break;
  541. case EMF_LIGHTING:
  542. Lighting = value; break;
  543. case EMF_ZBUFFER:
  544. ZBuffer = value; break;
  545. case EMF_ZWRITE_ENABLE:
  546. ZWriteEnable = value ? EZW_AUTO : EZW_OFF; break;
  547. case EMF_BACK_FACE_CULLING:
  548. BackfaceCulling = value; break;
  549. case EMF_FRONT_FACE_CULLING:
  550. FrontfaceCulling = value; break;
  551. case EMF_BILINEAR_FILTER:
  552. {
  553. for (u32 i=0; i<MATERIAL_MAX_TEXTURES_USED; ++i)
  554. TextureLayer[i].BilinearFilter = value;
  555. }
  556. break;
  557. case EMF_TRILINEAR_FILTER:
  558. {
  559. for (u32 i=0; i<MATERIAL_MAX_TEXTURES_USED; ++i)
  560. TextureLayer[i].TrilinearFilter = value;
  561. }
  562. break;
  563. case EMF_ANISOTROPIC_FILTER:
  564. {
  565. if (value)
  566. for (u32 i=0; i<MATERIAL_MAX_TEXTURES_USED; ++i)
  567. TextureLayer[i].AnisotropicFilter = 0xFF;
  568. else
  569. for (u32 i=0; i<MATERIAL_MAX_TEXTURES_USED; ++i)
  570. TextureLayer[i].AnisotropicFilter = 0;
  571. }
  572. break;
  573. case EMF_FOG_ENABLE:
  574. FogEnable = value; break;
  575. case EMF_NORMALIZE_NORMALS:
  576. NormalizeNormals = value; break;
  577. case EMF_TEXTURE_WRAP:
  578. {
  579. for (u32 i=0; i<MATERIAL_MAX_TEXTURES_USED; ++i)
  580. {
  581. TextureLayer[i].TextureWrapU = (E_TEXTURE_CLAMP)value;
  582. TextureLayer[i].TextureWrapV = (E_TEXTURE_CLAMP)value;
  583. TextureLayer[i].TextureWrapW = (E_TEXTURE_CLAMP)value;
  584. }
  585. }
  586. break;
  587. case EMF_ANTI_ALIASING:
  588. AntiAliasing = value?EAAM_SIMPLE:EAAM_OFF; break;
  589. case EMF_COLOR_MASK:
  590. ColorMask = value?ECP_ALL:ECP_NONE; break;
  591. case EMF_COLOR_MATERIAL:
  592. ColorMaterial = value?ECM_DIFFUSE:ECM_NONE; break;
  593. case EMF_USE_MIP_MAPS:
  594. UseMipMaps = value; break;
  595. case EMF_BLEND_OPERATION:
  596. BlendOperation = value?EBO_ADD:EBO_NONE; break;
  597. case EMF_BLEND_FACTOR:
  598. break;
  599. case EMF_POLYGON_OFFSET:
  600. PolygonOffsetFactor = value?1:0;
  601. PolygonOffsetDirection = EPO_BACK;
  602. PolygonOffsetSlopeScale = value?1.f:0.f;
  603. PolygonOffsetDepthBias = value?1.f:0.f;
  604. default:
  605. break;
  606. }
  607. }
  608. //! Gets the Material flag
  609. /** \param flag The flag to query.
  610. \return The current value of the flag. */
  611. bool getFlag(E_MATERIAL_FLAG flag) const
  612. {
  613. switch (flag)
  614. {
  615. case EMF_WIREFRAME:
  616. return Wireframe;
  617. case EMF_POINTCLOUD:
  618. return PointCloud;
  619. case EMF_GOURAUD_SHADING:
  620. return GouraudShading;
  621. case EMF_LIGHTING:
  622. return Lighting;
  623. case EMF_ZBUFFER:
  624. return ZBuffer!=ECFN_DISABLED;
  625. case EMF_ZWRITE_ENABLE:
  626. return ZWriteEnable != EZW_OFF;
  627. case EMF_BACK_FACE_CULLING:
  628. return BackfaceCulling;
  629. case EMF_FRONT_FACE_CULLING:
  630. return FrontfaceCulling;
  631. case EMF_BILINEAR_FILTER:
  632. return TextureLayer[0].BilinearFilter;
  633. case EMF_TRILINEAR_FILTER:
  634. return TextureLayer[0].TrilinearFilter;
  635. case EMF_ANISOTROPIC_FILTER:
  636. return TextureLayer[0].AnisotropicFilter!=0;
  637. case EMF_FOG_ENABLE:
  638. return FogEnable;
  639. case EMF_NORMALIZE_NORMALS:
  640. return NormalizeNormals;
  641. case EMF_TEXTURE_WRAP:
  642. return !(TextureLayer[0].TextureWrapU ||
  643. TextureLayer[0].TextureWrapV ||
  644. TextureLayer[0].TextureWrapW);
  645. case EMF_ANTI_ALIASING:
  646. return (AntiAliasing==1);
  647. case EMF_COLOR_MASK:
  648. return (ColorMask!=ECP_NONE);
  649. case EMF_COLOR_MATERIAL:
  650. return (ColorMaterial != ECM_NONE);
  651. case EMF_USE_MIP_MAPS:
  652. return UseMipMaps;
  653. case EMF_BLEND_OPERATION:
  654. return BlendOperation != EBO_NONE;
  655. case EMF_BLEND_FACTOR:
  656. return BlendFactor != 0.f;
  657. case EMF_POLYGON_OFFSET:
  658. return PolygonOffsetFactor != 0 || PolygonOffsetDepthBias != 0.f;
  659. }
  660. return false;
  661. }
  662. //! Inequality operator
  663. /** \param b Material to compare to.
  664. \return True if the materials differ, else false. */
  665. inline bool operator!=(const SMaterial& b) const
  666. {
  667. bool different =
  668. MaterialType != b.MaterialType ||
  669. AmbientColor != b.AmbientColor ||
  670. DiffuseColor != b.DiffuseColor ||
  671. EmissiveColor != b.EmissiveColor ||
  672. SpecularColor != b.SpecularColor ||
  673. Shininess != b.Shininess ||
  674. MaterialTypeParam != b.MaterialTypeParam ||
  675. MaterialTypeParam2 != b.MaterialTypeParam2 ||
  676. Thickness != b.Thickness ||
  677. Wireframe != b.Wireframe ||
  678. PointCloud != b.PointCloud ||
  679. GouraudShading != b.GouraudShading ||
  680. Lighting != b.Lighting ||
  681. ZBuffer != b.ZBuffer ||
  682. ZWriteEnable != b.ZWriteEnable ||
  683. BackfaceCulling != b.BackfaceCulling ||
  684. FrontfaceCulling != b.FrontfaceCulling ||
  685. FogEnable != b.FogEnable ||
  686. NormalizeNormals != b.NormalizeNormals ||
  687. AntiAliasing != b.AntiAliasing ||
  688. ColorMask != b.ColorMask ||
  689. ColorMaterial != b.ColorMaterial ||
  690. BlendOperation != b.BlendOperation ||
  691. BlendFactor != b.BlendFactor ||
  692. PolygonOffsetFactor != b.PolygonOffsetFactor ||
  693. PolygonOffsetDirection != b.PolygonOffsetDirection ||
  694. PolygonOffsetDepthBias != b.PolygonOffsetDepthBias ||
  695. PolygonOffsetSlopeScale != b.PolygonOffsetSlopeScale ||
  696. UseMipMaps != b.UseMipMaps
  697. ;
  698. for (u32 i=0; (i<MATERIAL_MAX_TEXTURES_USED) && !different; ++i)
  699. {
  700. different |= (TextureLayer[i] != b.TextureLayer[i]);
  701. }
  702. return different;
  703. }
  704. //! Equality operator
  705. /** \param b Material to compare to.
  706. \return True if the materials are equal, else false. */
  707. inline bool operator==(const SMaterial& b) const
  708. { return !(b!=*this); }
  709. //! Check if material needs alpha blending
  710. bool isAlphaBlendOperation() const
  711. {
  712. if (BlendOperation != EBO_NONE && BlendFactor != 0.f)
  713. {
  714. E_BLEND_FACTOR srcRGBFact = EBF_ZERO;
  715. E_BLEND_FACTOR dstRGBFact = EBF_ZERO;
  716. E_BLEND_FACTOR srcAlphaFact = EBF_ZERO;
  717. E_BLEND_FACTOR dstAlphaFact = EBF_ZERO;
  718. E_MODULATE_FUNC modulo = EMFN_MODULATE_1X;
  719. u32 alphaSource = 0;
  720. unpack_textureBlendFuncSeparate(srcRGBFact, dstRGBFact, srcAlphaFact, dstAlphaFact, modulo, alphaSource, BlendFactor);
  721. if (textureBlendFunc_hasAlpha(srcRGBFact) || textureBlendFunc_hasAlpha(dstRGBFact) ||
  722. textureBlendFunc_hasAlpha(srcAlphaFact) || textureBlendFunc_hasAlpha(dstAlphaFact))
  723. {
  724. return true;
  725. }
  726. }
  727. return false;
  728. }
  729. //! Check for some fixed-function transparent types. Still used internally, but might be deprecated soon.
  730. //! You probably should not use this anymore, IVideoDriver::needsTransparentRenderPass is more useful in most situations
  731. //! as it asks the material renders directly what they do with the material.
  732. bool isTransparent() const
  733. {
  734. if ( MaterialType==EMT_TRANSPARENT_ADD_COLOR ||
  735. MaterialType==EMT_TRANSPARENT_ALPHA_CHANNEL ||
  736. MaterialType==EMT_TRANSPARENT_VERTEX_ALPHA ||
  737. MaterialType==EMT_TRANSPARENT_REFLECTION_2_LAYER )
  738. return true;
  739. return false;
  740. }
  741. };
  742. //! global const identity Material
  743. IRRLICHT_API extern SMaterial IdentityMaterial;
  744. } // end namespace video
  745. } // end namespace irr
  746. #endif