terrCellMaterial.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _TERRCELLMATERIAL_H_
  23. #define _TERRCELLMATERIAL_H_
  24. #ifndef _TVECTOR_H_
  25. #include "core/util/tVector.h"
  26. #endif
  27. #ifndef _MATTEXTURETARGET_H_
  28. #include "materials/matTextureTarget.h"
  29. #endif
  30. #ifndef _GFXTEXTUREHANDLE_H_
  31. #include "gfx/gfxTextureHandle.h"
  32. #endif
  33. #ifndef _GFXSHADER_H_
  34. #include "gfx/gfxShader.h"
  35. #endif
  36. #ifndef _GFXSTATEBLOCK_H_
  37. #include "gfx/gfxStateBlock.h"
  38. #endif
  39. class SceneRenderState;
  40. struct SceneData;
  41. class TerrainMaterial;
  42. class TerrainBlock;
  43. class BaseMatInstance;
  44. /// This is a complex material which holds one or more
  45. /// optimized shaders for rendering a single cell.
  46. class TerrainCellMaterial
  47. {
  48. protected:
  49. class MaterialInfo
  50. {
  51. public:
  52. MaterialInfo()
  53. {
  54. }
  55. ~MaterialInfo()
  56. {
  57. }
  58. TerrainMaterial *mat;
  59. U32 layerId;
  60. GFXShaderConstHandle *detailTexConst;
  61. GFXTexHandle detailTex;
  62. GFXShaderConstHandle *macroTexConst;
  63. GFXTexHandle macroTex;
  64. GFXShaderConstHandle *normalTexConst;
  65. GFXTexHandle normalTex;
  66. GFXShaderConstHandle *detailInfoVConst;
  67. GFXShaderConstHandle *detailInfoPConst;
  68. GFXShaderConstHandle *macroInfoVConst;
  69. GFXShaderConstHandle *macroInfoPConst;
  70. };
  71. class Pass
  72. {
  73. public:
  74. Pass()
  75. : shader( NULL )
  76. {
  77. }
  78. ~Pass()
  79. {
  80. for ( U32 i=0; i < materials.size(); i++ )
  81. delete materials[i];
  82. }
  83. Vector<MaterialInfo*> materials;
  84. ///
  85. GFXShader *shader;
  86. GFXShaderConstBufferRef consts;
  87. GFXStateBlockRef stateBlock;
  88. GFXStateBlockRef wireframeStateBlock;
  89. GFXShaderConstHandle *modelViewProjConst;
  90. GFXShaderConstHandle *worldViewOnly;
  91. GFXShaderConstHandle *viewToObj;
  92. GFXShaderConstHandle *eyePosWorldConst;
  93. GFXShaderConstHandle *eyePosConst;
  94. GFXShaderConstHandle *objTransConst;
  95. GFXShaderConstHandle *worldToObjConst;
  96. GFXShaderConstHandle *vEyeConst;
  97. GFXShaderConstHandle *layerSizeConst;
  98. GFXShaderConstHandle *lightParamsConst;
  99. GFXShaderConstHandle *lightInfoBufferConst;
  100. GFXShaderConstHandle *baseTexMapConst;
  101. GFXShaderConstHandle *layerTexConst;
  102. GFXShaderConstHandle *lightMapTexConst;
  103. GFXShaderConstHandle *squareSize;
  104. GFXShaderConstHandle *oneOverTerrainSize;
  105. GFXShaderConstHandle *fogDataConst;
  106. GFXShaderConstHandle *fogColorConst;
  107. };
  108. TerrainBlock *mTerrain;
  109. U64 mMaterials;
  110. Vector<Pass> mPasses;
  111. U32 mCurrPass;
  112. static const Vector<String> mSamplerNames;
  113. GFXTexHandle mBaseMapTexture;
  114. GFXTexHandle mLayerMapTexture;
  115. NamedTexTargetRef mLightInfoTarget;
  116. /// The prepass material for this material.
  117. TerrainCellMaterial *mPrePassMat;
  118. /// The reflection material for this material.
  119. TerrainCellMaterial *mReflectMat;
  120. /// A vector of all terrain cell materials loaded in the system.
  121. static Vector<TerrainCellMaterial*> smAllMaterials;
  122. bool _createPass( Vector<MaterialInfo*> *materials,
  123. Pass *pass,
  124. bool firstPass,
  125. bool prePassMat,
  126. bool reflectMat,
  127. bool baseOnly );
  128. void _updateMaterialConsts( Pass *pass );
  129. public:
  130. TerrainCellMaterial();
  131. ~TerrainCellMaterial();
  132. void init( TerrainBlock *block,
  133. U64 activeMaterials,
  134. bool prePassMat = false,
  135. bool reflectMat = false,
  136. bool baseOnly = false );
  137. /// Returns a prepass material from this material.
  138. TerrainCellMaterial* getPrePassMat();
  139. /// Returns the reflection material from this material.
  140. TerrainCellMaterial* getReflectMat();
  141. void setTransformAndEye( const MatrixF &modelXfm,
  142. const MatrixF &viewXfm,
  143. const MatrixF &projectXfm,
  144. F32 farPlane );
  145. ///
  146. bool setupPass( const SceneRenderState *state,
  147. const SceneData &sceneData );
  148. ///
  149. static BaseMatInstance* getShadowMat();
  150. ///
  151. static void _updateDefaultAnisotropy();
  152. };
  153. #endif // _TERRCELLMATERIAL_H_