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