SOverrideMaterial.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright (C) 2017 Michael Zeilfelder
  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_OVERRIDE_MATERIAL_H_INCLUDED
  5. #define S_OVERRIDE_MATERIAL_H_INCLUDED
  6. #include "SMaterial.h"
  7. namespace irr
  8. {
  9. namespace video
  10. {
  11. struct SOverrideMaterial
  12. {
  13. //! The Material values
  14. SMaterial Material;
  15. //! Which values are overridden
  16. /** OR'ed values from E_MATERIAL_FLAGS. */
  17. u32 EnableFlags;
  18. //! For those flags in EnableFlags which affect layers, set which of the layers are affected
  19. bool EnableLayerFlags[MATERIAL_MAX_TEXTURES];
  20. //! Which textures are overridden
  21. bool EnableTextures[MATERIAL_MAX_TEXTURES];
  22. //! Overwrite complete layers (settings of EnableLayerFlags and EnableTextures don't matter then for layer data)
  23. bool EnableLayers[MATERIAL_MAX_TEXTURES];
  24. //! Set in which render passes the material override is active.
  25. /** OR'ed values from E_SCENE_NODE_RENDER_PASS. */
  26. u16 EnablePasses;
  27. //! Global enable flag, overwritten by the SceneManager in each pass
  28. /** NOTE: This is generally _not_ set by users of the engine, but the
  29. Scenemanager uses the EnablePass array and sets Enabled to true if the
  30. Override material is enabled in the current pass.
  31. As user you generally _only_ set EnablePasses.
  32. The exception is when rendering without SceneManager but using draw calls in the VideoDriver. */
  33. bool Enabled;
  34. struct SMaterialTypeReplacement
  35. {
  36. SMaterialTypeReplacement(s32 original, u32 replacement) : Original(original), Replacement(replacement) {}
  37. SMaterialTypeReplacement(u32 replacement) : Original(-1), Replacement(replacement) {}
  38. //! SMaterial.MaterialType to replace.
  39. //! -1 for all types or a specific value to only replace that one (which is either one of E_MATERIAL_TYPE or a shader material id)
  40. s32 Original;
  41. //! MaterialType to used to override Original (either one of E_MATERIAL_TYPE or a shader material id)
  42. u32 Replacement;
  43. };
  44. //! To overwrite SMaterial::MaterialType
  45. core::array<SMaterialTypeReplacement> MaterialTypes;
  46. //! Default constructor
  47. SOverrideMaterial() : EnableFlags(0), EnablePasses(0), Enabled(false)
  48. {
  49. }
  50. //! disable overrides and reset all flags
  51. void reset()
  52. {
  53. EnableFlags = 0;
  54. EnablePasses = 0;
  55. Enabled = false;
  56. for (u32 i = 0; i < MATERIAL_MAX_TEXTURES; ++i)
  57. {
  58. EnableLayerFlags[i] = true; // doesn't do anything unless EnableFlags is set, just saying by default all texture layers are affected by flags
  59. EnableTextures[i] = false;
  60. EnableLayers[i] = false;
  61. }
  62. MaterialTypes.clear();
  63. }
  64. //! Apply the enabled overrides
  65. void apply(SMaterial& material)
  66. {
  67. if (Enabled)
  68. {
  69. for (u32 i = 0; i < MaterialTypes.size(); ++i)
  70. {
  71. const SMaterialTypeReplacement& mtr = MaterialTypes[i];
  72. if (mtr.Original < 0 || (s32)mtr.Original == material.MaterialType)
  73. material.MaterialType = (E_MATERIAL_TYPE)mtr.Replacement;
  74. }
  75. for (u32 f=0; f<32; ++f)
  76. {
  77. const u32 num=(1<<f);
  78. if (EnableFlags & num)
  79. {
  80. switch (num)
  81. {
  82. case EMF_WIREFRAME: material.Wireframe = Material.Wireframe; break;
  83. case EMF_POINTCLOUD: material.PointCloud = Material.PointCloud; break;
  84. case EMF_GOURAUD_SHADING: material.GouraudShading = Material.GouraudShading; break;
  85. case EMF_LIGHTING: material.Lighting = Material.Lighting; break;
  86. case EMF_ZBUFFER: material.ZBuffer = Material.ZBuffer; break;
  87. case EMF_ZWRITE_ENABLE: material.ZWriteEnable = Material.ZWriteEnable; break;
  88. case EMF_BACK_FACE_CULLING: material.BackfaceCulling = Material.BackfaceCulling; break;
  89. case EMF_FRONT_FACE_CULLING: material.FrontfaceCulling = Material.FrontfaceCulling; break;
  90. case EMF_BILINEAR_FILTER:
  91. for ( u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  92. {
  93. if ( EnableLayerFlags[i] )
  94. {
  95. material.TextureLayer[i].BilinearFilter = Material.TextureLayer[i].BilinearFilter;
  96. }
  97. }
  98. break;
  99. case EMF_TRILINEAR_FILTER:
  100. for ( u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  101. {
  102. if ( EnableLayerFlags[i] )
  103. {
  104. material.TextureLayer[i].TrilinearFilter = Material.TextureLayer[i].TrilinearFilter;
  105. }
  106. }
  107. break;
  108. case EMF_ANISOTROPIC_FILTER:
  109. for ( u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  110. {
  111. if ( EnableLayerFlags[i] )
  112. {
  113. material.TextureLayer[i].AnisotropicFilter = Material.TextureLayer[i].AnisotropicFilter;
  114. }
  115. }
  116. break;
  117. case EMF_FOG_ENABLE: material.FogEnable = Material.FogEnable; break;
  118. case EMF_NORMALIZE_NORMALS: material.NormalizeNormals = Material.NormalizeNormals; break;
  119. case EMF_TEXTURE_WRAP:
  120. for ( u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i)
  121. {
  122. if ( EnableLayerFlags[i] )
  123. {
  124. material.TextureLayer[i].TextureWrapU = Material.TextureLayer[i].TextureWrapU;
  125. material.TextureLayer[i].TextureWrapV = Material.TextureLayer[i].TextureWrapV;
  126. material.TextureLayer[i].TextureWrapW = Material.TextureLayer[i].TextureWrapW;
  127. }
  128. }
  129. break;
  130. case EMF_ANTI_ALIASING: material.AntiAliasing = Material.AntiAliasing; break;
  131. case EMF_COLOR_MASK: material.ColorMask = Material.ColorMask; break;
  132. case EMF_COLOR_MATERIAL: material.ColorMaterial = Material.ColorMaterial; break;
  133. case EMF_USE_MIP_MAPS: material.UseMipMaps = Material.UseMipMaps; break;
  134. case EMF_BLEND_OPERATION: material.BlendOperation = Material.BlendOperation; break;
  135. case EMF_BLEND_FACTOR: material.BlendFactor = Material.BlendFactor; break;
  136. case EMF_POLYGON_OFFSET:
  137. material.PolygonOffsetDirection = Material.PolygonOffsetDirection;
  138. material.PolygonOffsetFactor = Material.PolygonOffsetFactor;
  139. material.PolygonOffsetDepthBias = Material.PolygonOffsetDepthBias;
  140. material.PolygonOffsetSlopeScale = Material.PolygonOffsetSlopeScale;
  141. break;
  142. }
  143. }
  144. }
  145. for(u32 i=0; i<MATERIAL_MAX_TEXTURES; ++i )
  146. {
  147. if ( EnableLayers[i] )
  148. {
  149. material.TextureLayer[i] = Material.TextureLayer[i];
  150. }
  151. else if ( EnableTextures[i] )
  152. {
  153. material.TextureLayer[i].Texture = Material.TextureLayer[i].Texture;
  154. }
  155. }
  156. }
  157. }
  158. };
  159. } // end namespace video
  160. } // end namespace irr
  161. #endif // S_OVERRIDE_MATERIAL_H_INCLUDED