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 << 12) | (modulate << 8) | (srcFact << 4) | 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 & 0x0000F000) >> 12;
    00114                 modulo  = E_MODULATE_FUNC( ( state & 0x00000F00 ) >> 8 );
    00115                 srcFact = E_BLEND_FACTOR ( ( state & 0x000000F0 ) >> 4 );
    00116                 dstFact = E_BLEND_FACTOR ( ( state & 0x0000000F ) );
    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                                         {
    00481                                                 TextureLayer[i].TextureWrapU = (E_TEXTURE_CLAMP)value;
    00482                                                 TextureLayer[i].TextureWrapV = (E_TEXTURE_CLAMP)value;
    00483                                         }
    00484                                 }
    00485                                 break;
    00486                                 case EMF_ANTI_ALIASING:
    00487                                         AntiAliasing = value?EAAM_SIMPLE:EAAM_OFF;
    00488                                         break;
    00489                                 case EMF_COLOR_MASK:
    00490                                         ColorMask = value?ECP_ALL:ECP_NONE;
    00491                                         break;
    00492                                 case EMF_COLOR_MATERIAL:
    00493                                         ColorMaterial = value?ECM_DIFFUSE:ECM_NONE;
    00494                                         break;
    00495                                 default:
    00496                                         break;
    00497                         }
    00498                 }
    00499
    00501
    00503                 bool getFlag(E_MATERIAL_FLAG flag) const
    00504                 {
    00505                         switch (flag)
    00506                         {
    00507                                 case EMF_WIREFRAME:
    00508                                         return Wireframe;
    00509                                 case EMF_POINTCLOUD:
    00510                                         return PointCloud;
    00511                                 case EMF_GOURAUD_SHADING:
    00512                                         return GouraudShading;
    00513                                 case EMF_LIGHTING:
    00514                                         return Lighting;
    00515                                 case EMF_ZBUFFER:
    00516                                         return ZBuffer!=ECFN_NEVER;
    00517                                 case EMF_ZWRITE_ENABLE:
    00518                                         return ZWriteEnable;
    00519                                 case EMF_BACK_FACE_CULLING:
    00520                                         return BackfaceCulling;
    00521                                 case EMF_FRONT_FACE_CULLING:
    00522                                         return FrontfaceCulling;
    00523                                 case EMF_BILINEAR_FILTER:
    00524                                         return TextureLayer[0].BilinearFilter;
    00525                                 case EMF_TRILINEAR_FILTER:
    00526                                         return TextureLayer[0].TrilinearFilter;
    00527                                 case EMF_ANISOTROPIC_FILTER:
    00528                                         return TextureLayer[0].AnisotropicFilter!=0;
    00529                                 case EMF_FOG_ENABLE:
    00530                                         return FogEnable;
    00531                                 case EMF_NORMALIZE_NORMALS:
    00532                                         return NormalizeNormals;
    00533                                 case EMF_TEXTURE_WRAP:
    00534                                         return !(TextureLayer[0].TextureWrapU ||
    00535                                                         TextureLayer[0].TextureWrapV ||
    00536                                                         TextureLayer[1].TextureWrapU ||
    00537                                                         TextureLayer[1].TextureWrapV ||
    00538                                                         TextureLayer[2].TextureWrapU ||
    00539                                                         TextureLayer[2].TextureWrapV ||
    00540                                                         TextureLayer[3].TextureWrapU ||
    00541                                                         TextureLayer[3].TextureWrapV);
    00542                                 case EMF_ANTI_ALIASING:
    00543                                         return (AntiAliasing==1);
    00544                                 case EMF_COLOR_MASK:
    00545                                         return (ColorMask!=ECP_NONE);
    00546                                 case EMF_COLOR_MATERIAL:
    00547                                         return (ColorMaterial != ECM_NONE);
    00548                         }
    00549
    00550                         return false;
    00551                 }
    00552
    00554
    00556                 inline bool operator!=(const SMaterial& b) const
    00557                 {
    00558                         bool different =
    00559                                 MaterialType != b.MaterialType ||
    00560                                 AmbientColor != b.AmbientColor ||
    00561                                 DiffuseColor != b.DiffuseColor ||
    00562                                 EmissiveColor != b.EmissiveColor ||
    00563                                 SpecularColor != b.SpecularColor ||
    00564                                 Shininess != b.Shininess ||
    00565                                 MaterialTypeParam != b.MaterialTypeParam ||
    00566                                 MaterialTypeParam2 != b.MaterialTypeParam2 ||
    00567                                 Thickness != b.Thickness ||
    00568                                 Wireframe != b.Wireframe ||
    00569                                 PointCloud != b.PointCloud ||
    00570                                 GouraudShading != b.GouraudShading ||
    00571                                 Lighting != b.Lighting ||
    00572                                 ZBuffer != b.ZBuffer ||
    00573                                 ZWriteEnable != b.ZWriteEnable ||
    00574                                 BackfaceCulling != b.BackfaceCulling ||
    00575                                 FrontfaceCulling != b.FrontfaceCulling ||
    00576                                 FogEnable != b.FogEnable ||
    00577                                 NormalizeNormals != b.NormalizeNormals ||
    00578                                 AntiAliasing != b.AntiAliasing ||
    00579                                 ColorMask != b.ColorMask ||
    00580                                 ColorMaterial != b.ColorMaterial;
    00581                         for (u32 i=0; (i<MATERIAL_MAX_TEXTURES) && !different; ++i)
    00582                         {
    00583                                 different |= (TextureLayer[i] != b.TextureLayer[i]);
    00584                         }
    00585                         return different;
    00586                 }
    00587
    00589
    00591                 inline bool operator==(const SMaterial& b) const
    00592                 { return !(b!=*this); }
    00593
    00594                 bool isTransparent() const
    00595                 {
    00596                         return MaterialType==EMT_TRANSPARENT_ADD_COLOR ||
    00597                                 MaterialType==EMT_TRANSPARENT_ALPHA_CHANNEL ||
    00598                                 MaterialType==EMT_TRANSPARENT_VERTEX_ALPHA ||
    00599                                 MaterialType==EMT_TRANSPARENT_REFLECTION_2_LAYER;
    00600                 }
    00601         };
    00602
    00604         IRRLICHT_API extern SMaterial IdentityMaterial;
    00605
    00606 } // end namespace video
    00607 } // end namespace irr
    00608
    00609 #endif

| The Irrlicht Engine Documentation © 2003-2010 by Nikolaus Gebhardt. Generated on Sun Feb 14 13:06:58 2010 by Doxygen (1.6.2)