SMaterial.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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 EMT_ONETEXTURE_BLEND, ( BlendFactor ) 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 used when blend is enabled
  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. //! Test never succeeds, this equals disable
  58. ECFN_NEVER=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. };
  74. //! Enum values for enabling/disabling color planes for rendering
  75. enum E_COLOR_PLANE
  76. {
  77. //! No color enabled
  78. ECP_NONE=0,
  79. //! Alpha enabled
  80. ECP_ALPHA=1,
  81. //! Red enabled
  82. ECP_RED=2,
  83. //! Green enabled
  84. ECP_GREEN=4,
  85. //! Blue enabled
  86. ECP_BLUE=8,
  87. //! All colors, no alpha
  88. ECP_RGB=14,
  89. //! All planes enabled
  90. ECP_ALL=15
  91. };
  92. //! Source of the alpha value to take
  93. /** This is currently only supported in EMT_ONETEXTURE_BLEND. You can use an
  94. or'ed combination of values. Alpha values are modulated (multiplicated). */
  95. enum E_ALPHA_SOURCE
  96. {
  97. //! Use no alpha, somewhat redundant with other settings
  98. EAS_NONE=0,
  99. //! Use vertex color alpha
  100. EAS_VERTEX_COLOR,
  101. //! Use texture alpha channel
  102. EAS_TEXTURE
  103. };
  104. //! EMT_ONETEXTURE_BLEND: pack srcFact, dstFact, Modulate and alpha source to MaterialTypeParam
  105. /** alpha source can be an OR'ed combination of E_ALPHA_SOURCE values. */
  106. inline f32 pack_textureBlendFunc ( const E_BLEND_FACTOR srcFact, const E_BLEND_FACTOR dstFact, const E_MODULATE_FUNC modulate=EMFN_MODULATE_1X, const u32 alphaSource=EAS_TEXTURE )
  107. {
  108. const u32 tmp = (alphaSource << 12) | (modulate << 8) | (srcFact << 4) | dstFact;
  109. return FR(tmp);
  110. }
  111. //! EMT_ONETEXTURE_BLEND: unpack srcFact & dstFact and Modulo to MaterialTypeParam
  112. /** The fields don't use the full byte range, so we could pack even more... */
  113. inline void unpack_textureBlendFunc ( E_BLEND_FACTOR &srcFact, E_BLEND_FACTOR &dstFact,
  114. E_MODULATE_FUNC &modulo, u32& alphaSource, const f32 param )
  115. {
  116. const u32 state = IR(param);
  117. alphaSource = (state & 0x0000F000) >> 12;
  118. modulo = E_MODULATE_FUNC( ( state & 0x00000F00 ) >> 8 );
  119. srcFact = E_BLEND_FACTOR ( ( state & 0x000000F0 ) >> 4 );
  120. dstFact = E_BLEND_FACTOR ( ( state & 0x0000000F ) );
  121. }
  122. //! EMT_ONETEXTURE_BLEND: has BlendFactor Alphablending
  123. inline bool textureBlendFunc_hasAlpha ( const E_BLEND_FACTOR factor )
  124. {
  125. switch ( factor )
  126. {
  127. case EBF_SRC_ALPHA:
  128. case EBF_ONE_MINUS_SRC_ALPHA:
  129. case EBF_DST_ALPHA:
  130. case EBF_ONE_MINUS_DST_ALPHA:
  131. case EBF_SRC_ALPHA_SATURATE:
  132. return true;
  133. default:
  134. return false;
  135. }
  136. }
  137. //! These flags are used to specify the anti-aliasing and smoothing modes
  138. /** Techniques supported are multisampling, geometry smoothing, and alpha
  139. to coverage.
  140. Some drivers don't support a per-material setting of the anti-aliasing
  141. modes. In those cases, FSAA/multisampling is defined by the device mode
  142. chosen upon creation via irr::SIrrCreationParameters.
  143. */
  144. enum E_ANTI_ALIASING_MODE
  145. {
  146. //! Use to turn off anti-aliasing for this material
  147. EAAM_OFF=0,
  148. //! Default anti-aliasing mode
  149. EAAM_SIMPLE=1,
  150. //! High-quality anti-aliasing, not always supported, automatically enables SIMPLE mode
  151. EAAM_QUALITY=3,
  152. //! Line smoothing
  153. EAAM_LINE_SMOOTH=4,
  154. //! point smoothing, often in software and slow, only with OpenGL
  155. EAAM_POINT_SMOOTH=8,
  156. //! All typical anti-alias and smooth modes
  157. EAAM_FULL_BASIC=15,
  158. //! Enhanced anti-aliasing for transparent materials
  159. /** Usually used with EMT_TRANSPARENT_ALPHA_REF and multisampling. */
  160. EAAM_ALPHA_TO_COVERAGE=16
  161. };
  162. //! These flags allow to define the interpretation of vertex color when lighting is enabled
  163. /** Without lighting being enabled the vertex color is the only value defining the fragment color.
  164. Once lighting is enabled, the four values for diffuse, ambient, emissive, and specular take over.
  165. With these flags it is possible to define which lighting factor shall be defined by the vertex color
  166. instead of the lighting factor which is the same for all faces of that material.
  167. The default is to use vertex color for the diffuse value, another pretty common value is to use
  168. vertex color for both diffuse and ambient factor. */
  169. enum E_COLOR_MATERIAL
  170. {
  171. //! Don't use vertex color for lighting
  172. ECM_NONE=0,
  173. //! Use vertex color for diffuse light, this is default
  174. ECM_DIFFUSE,
  175. //! Use vertex color for ambient light
  176. ECM_AMBIENT,
  177. //! Use vertex color for emissive light
  178. ECM_EMISSIVE,
  179. //! Use vertex color for specular light
  180. ECM_SPECULAR,
  181. //! Use vertex color for both diffuse and ambient light
  182. ECM_DIFFUSE_AND_AMBIENT
  183. };
  184. //! Flags for the definition of the polygon offset feature
  185. /** These flags define whether the offset should be into the screen, or towards the eye. */
  186. enum E_POLYGON_OFFSET
  187. {
  188. //! Push pixel towards the far plane, away from the eye
  189. /** This is typically used for rendering inner areas. */
  190. EPO_BACK=0,
  191. //! Pull pixels towards the camera.
  192. /** This is typically used for polygons which should appear on top
  193. of other elements, such as decals. */
  194. EPO_FRONT=1
  195. };
  196. //! Names for polygon offset direction
  197. const c8* const PolygonOffsetDirectionNames[] =
  198. {
  199. "Back",
  200. "Front",
  201. 0
  202. };
  203. //! Maximum number of texture an SMaterial can have.
  204. const u32 MATERIAL_MAX_TEXTURES = _IRR_MATERIAL_MAX_TEXTURES_;
  205. //! Struct for holding parameters for a material renderer
  206. class SMaterial
  207. {
  208. public:
  209. //! Default constructor. Creates a solid, lit material with white colors
  210. SMaterial()
  211. : MaterialType(EMT_SOLID), AmbientColor(255,255,255,255), DiffuseColor(255,255,255,255),
  212. EmissiveColor(0,0,0,0), SpecularColor(255,255,255,255),
  213. Shininess(0.0f), MaterialTypeParam(0.0f), MaterialTypeParam2(0.0f), Thickness(1.0f),
  214. ZBuffer(ECFN_LESSEQUAL), AntiAliasing(EAAM_SIMPLE), ColorMask(ECP_ALL),
  215. ColorMaterial(ECM_DIFFUSE), BlendOperation(EBO_NONE),
  216. PolygonOffsetFactor(0), PolygonOffsetDirection(EPO_FRONT),
  217. Wireframe(false), PointCloud(false), GouraudShading(true),
  218. Lighting(true), ZWriteEnable(true), BackfaceCulling(true), FrontfaceCulling(false),
  219. FogEnable(false), NormalizeNormals(false), UseMipMaps(true)
  220. { }
  221. //! Copy constructor
  222. /** \param other Material to copy from. */
  223. SMaterial(const SMaterial& other)
  224. {
  225. // These pointers are checked during assignment
  226. for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  227. TextureLayer[i].TextureMatrix = 0;
  228. *this = other;
  229. }
  230. //! Assignment operator
  231. /** \param other Material to copy from. */
  232. SMaterial& operator=(const SMaterial& other)
  233. {
  234. // Check for self-assignment!
  235. if (this == &other)
  236. return *this;
  237. MaterialType = other.MaterialType;
  238. AmbientColor = other.AmbientColor;
  239. DiffuseColor = other.DiffuseColor;
  240. EmissiveColor = other.EmissiveColor;
  241. SpecularColor = other.SpecularColor;
  242. Shininess = other.Shininess;
  243. MaterialTypeParam = other.MaterialTypeParam;
  244. MaterialTypeParam2 = other.MaterialTypeParam2;
  245. Thickness = other.Thickness;
  246. for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  247. {
  248. TextureLayer[i] = other.TextureLayer[i];
  249. }
  250. Wireframe = other.Wireframe;
  251. PointCloud = other.PointCloud;
  252. GouraudShading = other.GouraudShading;
  253. Lighting = other.Lighting;
  254. ZWriteEnable = other.ZWriteEnable;
  255. BackfaceCulling = other.BackfaceCulling;
  256. FrontfaceCulling = other.FrontfaceCulling;
  257. FogEnable = other.FogEnable;
  258. NormalizeNormals = other.NormalizeNormals;
  259. ZBuffer = other.ZBuffer;
  260. AntiAliasing = other.AntiAliasing;
  261. ColorMask = other.ColorMask;
  262. ColorMaterial = other.ColorMaterial;
  263. BlendOperation = other.BlendOperation;
  264. PolygonOffsetFactor = other.PolygonOffsetFactor;
  265. PolygonOffsetDirection = other.PolygonOffsetDirection;
  266. UseMipMaps = other.UseMipMaps;
  267. return *this;
  268. }
  269. //! Texture layer array.
  270. SMaterialLayer TextureLayer[MATERIAL_MAX_TEXTURES];
  271. //! Type of the material. Specifies how everything is blended together
  272. E_MATERIAL_TYPE MaterialType;
  273. //! How much ambient light (a global light) is reflected by this material.
  274. /** The default is full white, meaning objects are completely
  275. globally illuminated. Reduce this if you want to see diffuse
  276. or specular light effects. */
  277. SColor AmbientColor;
  278. //! How much diffuse light coming from a light source is reflected by this material.
  279. /** The default is full white. */
  280. SColor DiffuseColor;
  281. //! Light emitted by this material. Default is to emit no light.
  282. SColor EmissiveColor;
  283. //! How much specular light (highlights from a light) is reflected.
  284. /** The default is to reflect white specular light. See
  285. SMaterial::Shininess on how to enable specular lights. */
  286. SColor SpecularColor;
  287. //! Value affecting the size of specular highlights.
  288. /** A value of 20 is common. If set to 0, no specular
  289. highlights are being used. To activate, simply set the
  290. shininess of a material to a value in the range [0.5;128]:
  291. \code
  292. sceneNode->getMaterial(0).Shininess = 20.0f;
  293. \endcode
  294. You can change the color of the highlights using
  295. \code
  296. sceneNode->getMaterial(0).SpecularColor.set(255,255,255,255);
  297. \endcode
  298. The specular color of the dynamic lights
  299. (SLight::SpecularColor) will influence the the highlight color
  300. too, but they are set to a useful value by default when
  301. creating the light scene node. Here is a simple example on how
  302. to use specular highlights:
  303. \code
  304. // load and display mesh
  305. scene::IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(
  306. smgr->getMesh("data/faerie.md2"));
  307. node->setMaterialTexture(0, driver->getTexture("data/Faerie2.pcx")); // set diffuse texture
  308. node->setMaterialFlag(video::EMF_LIGHTING, true); // enable dynamic lighting
  309. node->getMaterial(0).Shininess = 20.0f; // set size of specular highlights
  310. // add white light
  311. scene::ILightSceneNode* light = smgr->addLightSceneNode(0,
  312. core::vector3df(5,5,5), video::SColorf(1.0f, 1.0f, 1.0f));
  313. \endcode */
  314. f32 Shininess;
  315. //! Free parameter, dependent on the material type.
  316. /** Mostly ignored, used for example in EMT_PARALLAX_MAP_SOLID
  317. and EMT_TRANSPARENT_ALPHA_CHANNEL. */
  318. f32 MaterialTypeParam;
  319. //! Second free parameter, dependent on the material type.
  320. /** Mostly ignored. */
  321. f32 MaterialTypeParam2;
  322. //! Thickness of non-3dimensional elements such as lines and points.
  323. f32 Thickness;
  324. //! Is the ZBuffer enabled? Default: ECFN_LESSEQUAL
  325. /** Values are from E_COMPARISON_FUNC. */
  326. u8 ZBuffer;
  327. //! Sets the antialiasing mode
  328. /** Values are chosen from E_ANTI_ALIASING_MODE. Default is
  329. EAAM_SIMPLE|EAAM_LINE_SMOOTH, i.e. simple multi-sample
  330. anti-aliasing and lime smoothing is enabled. */
  331. u8 AntiAliasing;
  332. //! Defines the enabled color planes
  333. /** Values are defined as or'ed values of the E_COLOR_PLANE enum.
  334. Only enabled color planes will be rendered to the current render
  335. target. Typical use is to disable all colors when rendering only to
  336. depth or stencil buffer, or using Red and Green for Stereo rendering. */
  337. u8 ColorMask:4;
  338. //! Defines the interpretation of vertex color in the lighting equation
  339. /** Values should be chosen from E_COLOR_MATERIAL.
  340. When lighting is enabled, vertex color can be used instead of the
  341. material values for light modulation. This allows to easily change e.g. the
  342. diffuse light behavior of each face. The default, ECM_DIFFUSE, will result in
  343. a very similar rendering as with lighting turned off, just with light shading. */
  344. u8 ColorMaterial:3;
  345. //! Store the blend operation of choice
  346. /** Values to be chosen from E_BLEND_OPERATION. The actual way to use this value
  347. is not yet determined, so ignore it for now. */
  348. E_BLEND_OPERATION BlendOperation:4;
  349. //! Factor specifying how far the polygon offset should be made
  350. /** Specifying 0 disables the polygon offset. The direction is specified spearately.
  351. The factor can be from 0 to 7.*/
  352. u8 PolygonOffsetFactor:3;
  353. //! Flag defining the direction the polygon offset is applied to.
  354. /** Can be to front or to back, specififed by values from E_POLYGON_OFFSET. */
  355. E_POLYGON_OFFSET PolygonOffsetDirection:1;
  356. //! Draw as wireframe or filled triangles? Default: false
  357. /** The user can access a material flag using
  358. \code material.Wireframe=true \endcode
  359. or \code material.setFlag(EMF_WIREFRAME, true); \endcode */
  360. bool Wireframe:1;
  361. //! Draw as point cloud or filled triangles? Default: false
  362. bool PointCloud:1;
  363. //! Flat or Gouraud shading? Default: true
  364. bool GouraudShading:1;
  365. //! Will this material be lighted? Default: true
  366. bool Lighting:1;
  367. //! Is the zbuffer writeable or is it read-only. Default: true.
  368. /** This flag is forced to false if the MaterialType is a
  369. transparent type and the scene parameter
  370. ALLOW_ZWRITE_ON_TRANSPARENT is not set. */
  371. bool ZWriteEnable:1;
  372. //! Is backface culling enabled? Default: true
  373. bool BackfaceCulling:1;
  374. //! Is frontface culling enabled? Default: false
  375. bool FrontfaceCulling:1;
  376. //! Is fog enabled? Default: false
  377. bool FogEnable:1;
  378. //! Should normals be normalized?
  379. /** Always use this if the mesh lit and scaled. Default: false */
  380. bool NormalizeNormals:1;
  381. //! Shall mipmaps be used if available
  382. /** Sometimes, disabling mipmap usage can be useful. Default: true */
  383. bool UseMipMaps:1;
  384. //! Gets the texture transformation matrix for level i
  385. /** \param i The desired level. Must not be larger than MATERIAL_MAX_TEXTURES.
  386. \return Texture matrix for texture level i. */
  387. core::matrix4& getTextureMatrix(u32 i)
  388. {
  389. return TextureLayer[i].getTextureMatrix();
  390. }
  391. //! Gets the immutable texture transformation matrix for level i
  392. /** \param i The desired level.
  393. \return Texture matrix for texture level i, or identity matrix for levels larger than MATERIAL_MAX_TEXTURES. */
  394. const core::matrix4& getTextureMatrix(u32 i) const
  395. {
  396. if (i<MATERIAL_MAX_TEXTURES)
  397. return TextureLayer[i].getTextureMatrix();
  398. else
  399. return core::IdentityMatrix;
  400. }
  401. //! Sets the i-th texture transformation matrix
  402. /** \param i The desired level.
  403. \param mat Texture matrix for texture level i. */
  404. void setTextureMatrix(u32 i, const core::matrix4& mat)
  405. {
  406. if (i>=MATERIAL_MAX_TEXTURES)
  407. return;
  408. TextureLayer[i].setTextureMatrix(mat);
  409. }
  410. //! Gets the i-th texture
  411. /** \param i The desired level.
  412. \return Texture for texture level i, if defined, else 0. */
  413. ITexture* getTexture(u32 i) const
  414. {
  415. return i < MATERIAL_MAX_TEXTURES ? TextureLayer[i].Texture : 0;
  416. }
  417. //! Sets the i-th texture
  418. /** If i>=MATERIAL_MAX_TEXTURES this setting will be ignored.
  419. \param i The desired level.
  420. \param tex Texture for texture level i. */
  421. void setTexture(u32 i, ITexture* tex)
  422. {
  423. if (i>=MATERIAL_MAX_TEXTURES)
  424. return;
  425. TextureLayer[i].Texture = tex;
  426. }
  427. //! Sets the Material flag to the given value
  428. /** \param flag The flag to be set.
  429. \param value The new value for the flag. */
  430. void setFlag(E_MATERIAL_FLAG flag, bool value)
  431. {
  432. switch (flag)
  433. {
  434. case EMF_WIREFRAME:
  435. Wireframe = value; break;
  436. case EMF_POINTCLOUD:
  437. PointCloud = value; break;
  438. case EMF_GOURAUD_SHADING:
  439. GouraudShading = value; break;
  440. case EMF_LIGHTING:
  441. Lighting = value; break;
  442. case EMF_ZBUFFER:
  443. ZBuffer = value; break;
  444. case EMF_ZWRITE_ENABLE:
  445. ZWriteEnable = value; break;
  446. case EMF_BACK_FACE_CULLING:
  447. BackfaceCulling = value; break;
  448. case EMF_FRONT_FACE_CULLING:
  449. FrontfaceCulling = value; break;
  450. case EMF_BILINEAR_FILTER:
  451. {
  452. for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  453. TextureLayer[i].BilinearFilter = value;
  454. }
  455. break;
  456. case EMF_TRILINEAR_FILTER:
  457. {
  458. for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  459. TextureLayer[i].TrilinearFilter = value;
  460. }
  461. break;
  462. case EMF_ANISOTROPIC_FILTER:
  463. {
  464. if (value)
  465. for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  466. TextureLayer[i].AnisotropicFilter = 0xFF;
  467. else
  468. for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  469. TextureLayer[i].AnisotropicFilter = 0;
  470. }
  471. break;
  472. case EMF_FOG_ENABLE:
  473. FogEnable = value; break;
  474. case EMF_NORMALIZE_NORMALS:
  475. NormalizeNormals = value; break;
  476. case EMF_TEXTURE_WRAP:
  477. {
  478. for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  479. {
  480. TextureLayer[i].TextureWrapU = (E_TEXTURE_CLAMP)value;
  481. TextureLayer[i].TextureWrapV = (E_TEXTURE_CLAMP)value;
  482. }
  483. }
  484. break;
  485. case EMF_ANTI_ALIASING:
  486. AntiAliasing = value?EAAM_SIMPLE:EAAM_OFF; break;
  487. case EMF_COLOR_MASK:
  488. ColorMask = value?ECP_ALL:ECP_NONE; break;
  489. case EMF_COLOR_MATERIAL:
  490. ColorMaterial = value?ECM_DIFFUSE:ECM_NONE; break;
  491. case EMF_USE_MIP_MAPS:
  492. UseMipMaps = value; break;
  493. case EMF_BLEND_OPERATION:
  494. BlendOperation = value?EBO_ADD:EBO_NONE; break;
  495. case EMF_POLYGON_OFFSET:
  496. PolygonOffsetFactor = value?1:0;
  497. PolygonOffsetDirection = EPO_BACK;
  498. break;
  499. default:
  500. break;
  501. }
  502. }
  503. //! Gets the Material flag
  504. /** \param flag The flag to query.
  505. \return The current value of the flag. */
  506. bool getFlag(E_MATERIAL_FLAG flag) const
  507. {
  508. switch (flag)
  509. {
  510. case EMF_WIREFRAME:
  511. return Wireframe;
  512. case EMF_POINTCLOUD:
  513. return PointCloud;
  514. case EMF_GOURAUD_SHADING:
  515. return GouraudShading;
  516. case EMF_LIGHTING:
  517. return Lighting;
  518. case EMF_ZBUFFER:
  519. return ZBuffer!=ECFN_NEVER;
  520. case EMF_ZWRITE_ENABLE:
  521. return ZWriteEnable;
  522. case EMF_BACK_FACE_CULLING:
  523. return BackfaceCulling;
  524. case EMF_FRONT_FACE_CULLING:
  525. return FrontfaceCulling;
  526. case EMF_BILINEAR_FILTER:
  527. return TextureLayer[0].BilinearFilter;
  528. case EMF_TRILINEAR_FILTER:
  529. return TextureLayer[0].TrilinearFilter;
  530. case EMF_ANISOTROPIC_FILTER:
  531. return TextureLayer[0].AnisotropicFilter!=0;
  532. case EMF_FOG_ENABLE:
  533. return FogEnable;
  534. case EMF_NORMALIZE_NORMALS:
  535. return NormalizeNormals;
  536. case EMF_TEXTURE_WRAP:
  537. return !(TextureLayer[0].TextureWrapU ||
  538. TextureLayer[0].TextureWrapV ||
  539. TextureLayer[1].TextureWrapU ||
  540. TextureLayer[1].TextureWrapV ||
  541. TextureLayer[2].TextureWrapU ||
  542. TextureLayer[2].TextureWrapV ||
  543. TextureLayer[3].TextureWrapU ||
  544. TextureLayer[3].TextureWrapV);
  545. case EMF_ANTI_ALIASING:
  546. return (AntiAliasing==1);
  547. case EMF_COLOR_MASK:
  548. return (ColorMask!=ECP_NONE);
  549. case EMF_COLOR_MATERIAL:
  550. return (ColorMaterial != ECM_NONE);
  551. case EMF_USE_MIP_MAPS:
  552. return UseMipMaps;
  553. case EMF_BLEND_OPERATION:
  554. return BlendOperation != EBO_NONE;
  555. case EMF_POLYGON_OFFSET:
  556. return PolygonOffsetFactor != 0;
  557. default:
  558. break;
  559. }
  560. return false;
  561. }
  562. //! Inequality operator
  563. /** \param b Material to compare to.
  564. \return True if the materials differ, else false. */
  565. inline bool operator!=(const SMaterial& b) const
  566. {
  567. bool different =
  568. MaterialType != b.MaterialType ||
  569. AmbientColor != b.AmbientColor ||
  570. DiffuseColor != b.DiffuseColor ||
  571. EmissiveColor != b.EmissiveColor ||
  572. SpecularColor != b.SpecularColor ||
  573. Shininess != b.Shininess ||
  574. MaterialTypeParam != b.MaterialTypeParam ||
  575. MaterialTypeParam2 != b.MaterialTypeParam2 ||
  576. Thickness != b.Thickness ||
  577. Wireframe != b.Wireframe ||
  578. PointCloud != b.PointCloud ||
  579. GouraudShading != b.GouraudShading ||
  580. Lighting != b.Lighting ||
  581. ZBuffer != b.ZBuffer ||
  582. ZWriteEnable != b.ZWriteEnable ||
  583. BackfaceCulling != b.BackfaceCulling ||
  584. FrontfaceCulling != b.FrontfaceCulling ||
  585. FogEnable != b.FogEnable ||
  586. NormalizeNormals != b.NormalizeNormals ||
  587. AntiAliasing != b.AntiAliasing ||
  588. ColorMask != b.ColorMask ||
  589. ColorMaterial != b.ColorMaterial ||
  590. BlendOperation != b.BlendOperation ||
  591. PolygonOffsetFactor != b.PolygonOffsetFactor ||
  592. PolygonOffsetDirection != b.PolygonOffsetDirection ||
  593. UseMipMaps != b.UseMipMaps;
  594. for (u32 i=0; (i<MATERIAL_MAX_TEXTURES) && !different; ++i)
  595. {
  596. different |= (TextureLayer[i] != b.TextureLayer[i]);
  597. }
  598. return different;
  599. }
  600. //! Equality operator
  601. /** \param b Material to compare to.
  602. \return True if the materials are equal, else false. */
  603. inline bool operator==(const SMaterial& b) const
  604. { return !(b!=*this); }
  605. bool isTransparent() const
  606. {
  607. return MaterialType==EMT_TRANSPARENT_ADD_COLOR ||
  608. MaterialType==EMT_TRANSPARENT_ALPHA_CHANNEL ||
  609. MaterialType==EMT_TRANSPARENT_VERTEX_ALPHA ||
  610. MaterialType==EMT_TRANSPARENT_REFLECTION_2_LAYER;
  611. }
  612. };
  613. //! global const identity Material
  614. IRRLICHT_API extern SMaterial IdentityMaterial;
  615. } // end namespace video
  616. } // end namespace irr
  617. #endif