Home | Namespaces | Hierarchy | Alphabetical List | Class list | Files | Namespace Members | Class members | File members | Tutorials

# SMaterial.h

Go to the documentation of this file.

    00001 // Copyright (C) 2002-2009 Nikolaus Gebhardt
    00002 // This file is part of the "Irrlicht Engine".
    00003 // For conditions of distribution and use, see copyright notice in irrlicht.h
    00004
    00005 #ifndef __S_MATERIAL_H_INCLUDED__
    00006 #define __S_MATERIAL_H_INCLUDED__
    00007
    00008 #include "SColor.h"
    00009 #include "matrix4.h"
    00010 #include "irrArray.h"
    00011 #include "irrMath.h"
    00012 #include "EMaterialTypes.h"
    00013 #include "EMaterialFlags.h"
    00014 #include "SMaterialLayer.h"
    00015
    00016 namespace irr
    00017 {
    00018 namespace video
    00019 {
    00020         class ITexture;
    00021
    00023         enum E_BLEND_FACTOR
    00024         {
    00025                 EBF_ZERO        = 0,
    00026                 EBF_ONE,
    00027                 EBF_DST_COLOR,
    00028                 EBF_ONE_MINUS_DST_COLOR,
    00029                 EBF_SRC_COLOR,
    00030                 EBF_ONE_MINUS_SRC_COLOR,
    00031                 EBF_SRC_ALPHA,
    00032                 EBF_ONE_MINUS_SRC_ALPHA,
    00033                 EBF_DST_ALPHA,
    00034                 EBF_ONE_MINUS_DST_ALPHA,
    00035                 EBF_SRC_ALPHA_SATURATE
    00036         };
    00037
    00039         enum E_MODULATE_FUNC
    00040         {
    00041                 EMFN_MODULATE_1X        = 1,
    00042                 EMFN_MODULATE_2X        = 2,
    00043                 EMFN_MODULATE_4X        = 4
    00044         };
    00045
    00047         enum E_COMPARISON_FUNC
    00048         {
    00050                 ECFN_NEVER=0,
    00052                 ECFN_LESSEQUAL=1,
    00054                 ECFN_EQUAL=2,
    00056                 ECFN_LESS,
    00058                 ECFN_NOTEQUAL,
    00060                 ECFN_GREATEREQUAL,
    00062                 ECFN_GREATER,
    00064                 ECFN_ALWAYS
    00065         };
    00066
    00068         enum E_COLOR_PLANE
    00069         {
    00071                 ECP_NONE=0,
    00073                 ECP_ALPHA=1,
    00075                 ECP_RED=2,
    00077                 ECP_GREEN=4,
    00079                 ECP_BLUE=8,
    00081                 ECP_RGB=14,
    00083                 ECP_ALL=15
    00084         };
    00085
    00087
    00089         enum E_ALPHA_SOURCE
    00090         {
    00092                 EAS_NONE=0,
    00094                 EAS_VERTEX_COLOR,
    00096                 EAS_TEXTURE
    00097         };
    00098
    00100
    00101         inline f32 pack_texureBlendFunc ( const E_BLEND_FACTOR srcFact, const E_BLEND_FACTOR dstFact, const E_MODULATE_FUNC modulate=EMFN_MODULATE_1X, const u32 alphaSource=EAS_TEXTURE )
    00102         {
    00103                 const u32 tmp = (alphaSource << 24) | (modulate << 16) | (srcFact << 8) | dstFact;
    00104                 return FR(tmp);
    00105         }
    00106
    00108
    00109         inline void unpack_texureBlendFunc ( E_BLEND_FACTOR &srcFact, E_BLEND_FACTOR &dstFact,
    00110                         E_MODULATE_FUNC &modulo, u32& alphaSource, const f32 param )
    00111         {
    00112                 const u32 state = IR(param);
    00113                 alphaSource = (state & 0xFF000000) >> 24;
    00114                 modulo  = E_MODULATE_FUNC( ( state & 0x00FF0000 ) >> 16 );
    00115                 srcFact = E_BLEND_FACTOR ( ( state & 0x0000FF00 ) >> 8 );
    00116                 dstFact = E_BLEND_FACTOR ( ( state & 0x000000FF ) );
    00117         }
    00118
    00120         inline bool textureBlendFunc_hasAlpha ( const E_BLEND_FACTOR factor )
    00121         {
    00122                 switch ( factor )
    00123                 {
    00124                         case EBF_SRC_ALPHA:
    00125                         case EBF_ONE_MINUS_SRC_ALPHA:
    00126                         case EBF_DST_ALPHA:
    00127                         case EBF_ONE_MINUS_DST_ALPHA:
    00128                         case EBF_SRC_ALPHA_SATURATE:
    00129                                 return true;
    00130                         default:
    00131                                 return false;
    00132                 }
    00133         }
    00134
    00135
    00137
    00143         enum E_ANTI_ALIASING_MODE
    00144         {
    00146                 EAAM_OFF=0,
    00148                 EAAM_SIMPLE=1,
    00150                 EAAM_QUALITY=3,
    00152                 EAAM_LINE_SMOOTH=4,
    00154                 EAAM_POINT_SMOOTH=8,
    00156                 EAAM_FULL_BASIC=15,
    00158
    00159                 EAAM_ALPHA_TO_COVERAGE=16
    00160         };
    00161
    00163
    00169         enum E_COLOR_MATERIAL
    00170         {
    00172                 ECM_NONE=0,
    00174                 ECM_DIFFUSE,
    00176                 ECM_AMBIENT,
    00178                 ECM_EMISSIVE,
    00180                 ECM_SPECULAR,
    00182                 ECM_DIFFUSE_AND_AMBIENT
    00183         };
    00184
    00186         const u32 MATERIAL_MAX_TEXTURES = _IRR_MATERIAL_MAX_TEXTURES_;
    00187
    00189         class SMaterial
    00190         {
    00191         public:
    00193                 SMaterial()
    00194                 : MaterialType(EMT_SOLID), AmbientColor(255,255,255,255), DiffuseColor(255,255,255,255),
    00195                         EmissiveColor(0,0,0,0), SpecularColor(255,255,255,255),
    00196                         Shininess(0.0f), MaterialTypeParam(0.0f), MaterialTypeParam2(0.0f), Thickness(1.0f),
    00197                         ZBuffer(ECFN_LESSEQUAL), AntiAliasing(EAAM_SIMPLE), ColorMask(ECP_ALL),
    00198                         ColorMaterial(ECM_DIFFUSE),
    00199                         Wireframe(false), PointCloud(false), GouraudShading(true), Lighting(true), ZWriteEnable(true),
    00200                         BackfaceCulling(true), FrontfaceCulling(false), FogEnable(false), NormalizeNormals(false)
    00201                 { }
    00202
    00204
    00205                 SMaterial(const SMaterial& other)
    00206                 {
    00207                         // These pointers are checked during assignment
    00208                         for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
    00209                                 TextureLayer[i].TextureMatrix = 0;
    00210                         *this = other;
    00211                 }
    00212
    00214
    00215                 SMaterial& operator=(const SMaterial& other)
    00216                 {
    00217                         // Check for self-assignment!
    00218                         if (this == &other)
    00219                                 return *this;
    00220
    00221                         MaterialType = other.MaterialType;
    00222
    00223                         AmbientColor = other.AmbientColor;
    00224                         DiffuseColor = other.DiffuseColor;
    00225                         EmissiveColor = other.EmissiveColor;
    00226                         SpecularColor = other.SpecularColor;
    00227                         Shininess = other.Shininess;
    00228                         MaterialTypeParam = other.MaterialTypeParam;
    00229                         MaterialTypeParam2 = other.MaterialTypeParam2;
    00230                         Thickness = other.Thickness;
    00231                         for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
    00232                         {
    00233                                 TextureLayer[i] = other.TextureLayer[i];
    00234                         }
    00235
    00236                         Wireframe = other.Wireframe;
    00237                         PointCloud = other.PointCloud;
    00238                         GouraudShading = other.GouraudShading;
    00239                         Lighting = other.Lighting;
    00240                         ZWriteEnable = other.ZWriteEnable;
    00241                         BackfaceCulling = other.BackfaceCulling;
    00242                         FrontfaceCulling = other.FrontfaceCulling;
    00243                         FogEnable = other.FogEnable;
    00244                         NormalizeNormals = other.NormalizeNormals;
    00245                         ZBuffer = other.ZBuffer;
    00246                         AntiAliasing = other.AntiAliasing;
    00247                         ColorMask = other.ColorMask;
    00248                         ColorMaterial = other.ColorMaterial;
    00249
    00250                         return *this;
    00251                 }
    00252
    00254                 SMaterialLayer TextureLayer[MATERIAL_MAX_TEXTURES];
    00255
    00257                 E_MATERIAL_TYPE MaterialType;
    00258
    00260
    00263                 SColor AmbientColor;
    00264
    00266
    00267                 SColor DiffuseColor;
    00268
    00270                 SColor EmissiveColor;
    00271
    00273
    00275                 SColor SpecularColor;
    00276
    00278
    00307                 f32 Shininess;
    00308
    00310
    00312                 f32 MaterialTypeParam;
    00313
    00315
    00316                 f32 MaterialTypeParam2;
    00317
    00319                 f32 Thickness;
    00320
    00322
    00323                 u8 ZBuffer;
    00324
    00326
    00329                 u8 AntiAliasing;
    00330
    00332
    00336                 u8 ColorMask:4;
    00337
    00339
    00344                 u8 ColorMaterial:3;
    00345
    00347
    00350                 bool Wireframe:1;
    00351
    00353                 bool PointCloud:1;
    00354
    00356                 bool GouraudShading:1;
    00357
    00359                 bool Lighting:1;
    00360
    00362
    00365                 bool ZWriteEnable:1;
    00366
    00368                 bool BackfaceCulling:1;
    00369
    00371                 bool FrontfaceCulling:1;
    00372
    00374                 bool FogEnable:1;
    00375
    00377
    00378                 bool NormalizeNormals:1;
    00379
    00381
    00383                 core::matrix4& getTextureMatrix(u32 i)
    00384                 {
    00385                         return TextureLayer[i].getTextureMatrix();
    00386                 }
    00387
    00389
    00391                 const core::matrix4& getTextureMatrix(u32 i) const
    00392                 {
    00393                         if (i<MATERIAL_MAX_TEXTURES)
    00394                                 return TextureLayer[i].getTextureMatrix();
    00395                         else
    00396                                 return core::IdentityMatrix;
    00397                 }
    00398
    00400
    00402                 void setTextureMatrix(u32 i, const core::matrix4& mat)
    00403                 {
    00404                         if (i>=MATERIAL_MAX_TEXTURES)
    00405                                 return;
    00406                         TextureLayer[i].setTextureMatrix(mat);
    00407                 }
    00408
    00410
    00412                 ITexture* getTexture(u32 i) const
    00413                 {
    00414                         return i < MATERIAL_MAX_TEXTURES ? TextureLayer[i].Texture : 0;
    00415                 }
    00416
    00418
    00421                 void setTexture(u32 i, ITexture* tex)
    00422                 {
    00423                         if (i>=MATERIAL_MAX_TEXTURES)
    00424                                 return;
    00425                         TextureLayer[i].Texture = tex;
    00426                 }
    00427
    00429
    00431                 void setFlag(E_MATERIAL_FLAG flag, bool value)
    00432                 {
    00433                         switch (flag)
    00434                         {
    00435                                 case EMF_WIREFRAME:
    00436                                         Wireframe = value; break;
    00437                                 case EMF_POINTCLOUD:
    00438                                         PointCloud = value; break;
    00439                                 case EMF_GOURAUD_SHADING:
    00440                                         GouraudShading = value; break;
    00441                                 case EMF_LIGHTING:
    00442                                         Lighting = value; break;
    00443                                 case EMF_ZBUFFER:
    00444                                         ZBuffer = value; break;
    00445                                 case EMF_ZWRITE_ENABLE:
    00446                                         ZWriteEnable = value; break;
    00447                                 case EMF_BACK_FACE_CULLING:
    00448                                         BackfaceCulling = value; break;
    00449                                 case EMF_FRONT_FACE_CULLING:
    00450                                         FrontfaceCulling = value; break;
    00451                                 case EMF_BILINEAR_FILTER:
    00452                                 {
    00453                                         for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
    00454                                                 TextureLayer[i].BilinearFilter = value;
    00455                                 }
    00456                                 break;
    00457                                 case EMF_TRILINEAR_FILTER:
    00458                                 {
    00459                                         for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
    00460                                                 TextureLayer[i].TrilinearFilter = value;
    00461                                 }
    00462                                 break;
    00463                                 case EMF_ANISOTROPIC_FILTER:
    00464                                 {
    00465                                         if (value)
    00466                                                 for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
    00467                                                         TextureLayer[i].AnisotropicFilter = 0xFF;
    00468                                         else
    00469                                                 for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
    00470                                                         TextureLayer[i].AnisotropicFilter = 0;
    00471                                 }
    00472                                 break;
    00473                                 case EMF_FOG_ENABLE:
    00474                                         FogEnable = value; break;
    00475                                 case EMF_NORMALIZE_NORMALS:
    00476                                         NormalizeNormals = value; break;
    00477                                 case EMF_TEXTURE_WRAP:
    00478                                 {
    00479                                         for (u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
    00480                                                 TextureLayer[i].TextureWrap = (E_TEXTURE_CLAMP)value;
    00481                                 }
    00482                                 break;
    00483                                 case EMF_ANTI_ALIASING:
    00484                                         AntiAliasing = value?EAAM_SIMPLE:EAAM_OFF;
    00485                                         break;
    00486                                 case EMF_COLOR_MASK:
    00487                                         ColorMask = value?ECP_ALL:ECP_NONE;
    00488                                         break;
    00489                                 case EMF_COLOR_MATERIAL:
    00490                                         ColorMaterial = value?ECM_DIFFUSE:ECM_NONE;
    00491                                         break;
    00492                                 default:
    00493                                         break;
    00494                         }
    00495                 }
    00496
    00498
    00500                 bool getFlag(E_MATERIAL_FLAG flag) const
    00501                 {
    00502                         switch (flag)
    00503                         {
    00504                                 case EMF_WIREFRAME:
    00505                                         return Wireframe;
    00506                                 case EMF_POINTCLOUD:
    00507                                         return PointCloud;
    00508                                 case EMF_GOURAUD_SHADING:
    00509                                         return GouraudShading;
    00510                                 case EMF_LIGHTING:
    00511                                         return Lighting;
    00512                                 case EMF_ZBUFFER:
    00513                                         return ZBuffer!=ECFN_NEVER;
    00514                                 case EMF_ZWRITE_ENABLE:
    00515                                         return ZWriteEnable;
    00516                                 case EMF_BACK_FACE_CULLING:
    00517                                         return BackfaceCulling;
    00518                                 case EMF_FRONT_FACE_CULLING:
    00519                                         return FrontfaceCulling;
    00520                                 case EMF_BILINEAR_FILTER:
    00521                                         return TextureLayer[0].BilinearFilter;
    00522                                 case EMF_TRILINEAR_FILTER:
    00523                                         return TextureLayer[0].TrilinearFilter;
    00524                                 case EMF_ANISOTROPIC_FILTER:
    00525                                         return TextureLayer[0].AnisotropicFilter!=0;
    00526                                 case EMF_FOG_ENABLE:
    00527                                         return FogEnable;
    00528                                 case EMF_NORMALIZE_NORMALS:
    00529                                         return NormalizeNormals;
    00530                                 case EMF_TEXTURE_WRAP:
    00531                                         return !(TextureLayer[0].TextureWrap ||
    00532                                                         TextureLayer[1].TextureWrap ||
    00533                                                         TextureLayer[2].TextureWrap ||
    00534                                                         TextureLayer[3].TextureWrap);
    00535                                 case EMF_ANTI_ALIASING:
    00536                                         return (AntiAliasing==1);
    00537                                 case EMF_COLOR_MASK:
    00538                                         return (ColorMask!=ECP_NONE);
    00539                                 case EMF_COLOR_MATERIAL:
    00540                                         return (ColorMaterial != ECM_NONE);
    00541                         }
    00542
    00543                         return false;
    00544                 }
    00545
    00547
    00549                 inline bool operator!=(const SMaterial& b) const
    00550                 {
    00551                         bool different =
    00552                                 MaterialType != b.MaterialType ||
    00553                                 AmbientColor != b.AmbientColor ||
    00554                                 DiffuseColor != b.DiffuseColor ||
    00555                                 EmissiveColor != b.EmissiveColor ||
    00556                                 SpecularColor != b.SpecularColor ||
    00557                                 Shininess != b.Shininess ||
    00558                                 MaterialTypeParam != b.MaterialTypeParam ||
    00559                                 MaterialTypeParam2 != b.MaterialTypeParam2 ||
    00560                                 Thickness != b.Thickness ||
    00561                                 Wireframe != b.Wireframe ||
    00562                                 PointCloud != b.PointCloud ||
    00563                                 GouraudShading != b.GouraudShading ||
    00564                                 Lighting != b.Lighting ||
    00565                                 ZBuffer != b.ZBuffer ||
    00566                                 ZWriteEnable != b.ZWriteEnable ||
    00567                                 BackfaceCulling != b.BackfaceCulling ||
    00568                                 FrontfaceCulling != b.FrontfaceCulling ||
    00569                                 FogEnable != b.FogEnable ||
    00570                                 NormalizeNormals != b.NormalizeNormals ||
    00571                                 AntiAliasing != b.AntiAliasing ||
    00572                                 ColorMask != b.ColorMask ||
    00573                                 ColorMaterial != b.ColorMaterial;
    00574                         for (u32 i=0; (i<MATERIAL_MAX_TEXTURES) && !different; ++i)
    00575                         {
    00576                                 different |= (TextureLayer[i] != b.TextureLayer[i]);
    00577                         }
    00578                         return different;
    00579                 }
    00580
    00582
    00584                 inline bool operator==(const SMaterial& b) const
    00585                 { return !(b!=*this); }
    00586
    00587                 bool isTransparent() const
    00588                 {
    00589                         return MaterialType==EMT_TRANSPARENT_ADD_COLOR ||
    00590                                 MaterialType==EMT_TRANSPARENT_ALPHA_CHANNEL ||
    00591                                 MaterialType==EMT_TRANSPARENT_VERTEX_ALPHA ||
    00592                                 MaterialType==EMT_TRANSPARENT_REFLECTION_2_LAYER;
    00593                 }
    00594         };
    00595
    00597         IRRLICHT_API extern SMaterial IdentityMaterial;
    00598
    00599 } // end namespace video
    00600 } // end namespace irr
    00601
    00602 #endif

| The Irrlicht Engine Documentation © 2003-2009 by Nikolaus Gebhardt. Generated on Sun Jan 10 09:24:05 2010 by Doxygen (1.5.6)