VolumetricFog.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 _VolumetricFog_H_
  23. #define _VolumetricFog_H_
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. #ifndef _MATTEXTURETARGET_H_
  28. #include "materials/matTextureTarget.h"
  29. #endif
  30. #ifndef _GFXSHADER_H_
  31. #include "gfx/gfxShader.h"
  32. #endif
  33. #ifndef _GFXTARGET_H_
  34. #include "gfx/gfxTarget.h"
  35. #endif
  36. #ifndef _GFXVERTEXBUFFER_H_
  37. #include "gfx/gfxVertexBuffer.h"
  38. #endif
  39. #ifndef _TSSHAPE_H_
  40. #include "ts/tsShape.h"
  41. #endif
  42. #ifndef _POST_EFFECT_H_
  43. #include "postFx/postEffect.h"
  44. #endif
  45. #include "gui/core/guiCanvas.h"
  46. class VolumetricFogRTManager;
  47. class VolumetricFog : public SceneObject
  48. {
  49. typedef SceneObject Parent;
  50. // Maskbits for updating
  51. enum
  52. {
  53. VolumetricFogMask = Parent::NextFreeMask,
  54. FogColorMask = Parent::NextFreeMask << 1,
  55. FogDensityMask = Parent::NextFreeMask << 2,
  56. FogModulationMask = Parent::NextFreeMask << 3,
  57. FogPostFXMask = Parent::NextFreeMask << 4,
  58. FogShapeMask = Parent::NextFreeMask << 5,
  59. NextFreeMask = Parent::NextFreeMask << 6
  60. };
  61. // Struct which holds the shape details
  62. struct meshes
  63. {
  64. F32 det_size;
  65. S32 sub_shape;
  66. S32 obj_det;
  67. U32 num_verts;
  68. GFXVertexPNTT *verts;
  69. Vector <GFXPrimitive> *piArray;
  70. Vector <U32> *indices;
  71. };
  72. protected:
  73. // Rendertargets;
  74. GFXTextureTargetRef z_buf;
  75. NamedTexTargetRef mPrepassTarget;
  76. NamedTexTargetRef mDepthBufferTarget;
  77. NamedTexTargetRef mFrontBufferTarget;
  78. // Fog Modulation texture
  79. GFXTexHandle mTexture;
  80. // Shaders
  81. GFXShaderRef mShader;
  82. GFXShaderRef mPrePassShader;
  83. GFXShaderRef mReflectionShader;
  84. // Stateblocks
  85. GFXStateBlockDesc descD;
  86. GFXStateBlockDesc descF;
  87. GFXStateBlockDesc desc_preD;
  88. GFXStateBlockDesc desc_preF;
  89. GFXStateBlockDesc desc_refl;
  90. GFXStateBlockRef mStateblockD;
  91. GFXStateBlockRef mStateblockF;
  92. GFXStateBlockRef mStateblock_preD;
  93. GFXStateBlockRef mStateblock_preF;
  94. GFXStateBlockRef mStateblock_refl;
  95. // Shaderconstants
  96. GFXShaderConstBufferRef mShaderConsts;
  97. GFXShaderConstHandle *mModelViewProjSC;
  98. GFXShaderConstHandle *mFadeSizeSC;
  99. GFXShaderConstHandle *mFogColorSC;
  100. GFXShaderConstHandle *mFogDensitySC;
  101. GFXShaderConstHandle *mPreBias;
  102. GFXShaderConstHandle *mAccumTime;
  103. GFXShaderConstHandle *mIsTexturedSC;
  104. GFXShaderConstHandle *mModSpeedSC;
  105. GFXShaderConstHandle *mModStrengthSC;
  106. GFXShaderConstHandle *mViewPointSC;
  107. GFXShaderConstHandle *mTexScaleSC;
  108. GFXShaderConstHandle *mTexTilesSC;
  109. GFXShaderConstBufferRef mPPShaderConsts;
  110. GFXShaderConstHandle *mPPModelViewProjSC;
  111. GFXShaderConstHandle *mAmbientColorSC;
  112. GFXShaderConstBufferRef mReflShaderConsts;
  113. GFXShaderConstHandle *mReflModelViewProjSC;
  114. GFXShaderConstHandle *mReflFogColorSC;
  115. GFXShaderConstHandle *mReflFogDensitySC;
  116. GFXShaderConstHandle *mReflFogStrengthSC;
  117. // Vertex and Prim. Buffer
  118. GFXVertexBufferHandle<GFXVertexPNTT> mVB;
  119. GFXPrimitiveBufferHandle mPB;
  120. // Fog volume data;
  121. StringTableEntry mShapeName;
  122. ColorI mFogColor;
  123. F32 mFogDensity;
  124. bool mIgnoreWater;
  125. bool mReflect;
  126. Vector<meshes> det_size;
  127. bool mShapeLoaded;
  128. F32 mPixelSize;
  129. F32 mFadeSize;
  130. U32 mCurDetailLevel;
  131. U32 mNumDetailLevels;
  132. F32 mObjSize;
  133. F32 mRadius;
  134. OrientedBox3F ColBox;
  135. VectorF mObjScale;
  136. F32 mMinDisplaySize;
  137. F32 mInvScale;
  138. // Fog Modulation data
  139. String mTextureName;
  140. bool mIsTextured;
  141. F32 mTexTiles;
  142. F32 mStrength;
  143. Point2F mSpeed1;
  144. Point2F mSpeed2;
  145. Point4F mSpeed;
  146. Point2F mTexScale;
  147. // Fog Rendering data
  148. Point3F camPos;
  149. Point2F mViewPoint;
  150. F32 mFOV;
  151. F32 viewDist;
  152. bool mIsVBDirty;
  153. bool mIsPBDirty;
  154. bool mCamInFog;
  155. bool mResizing;
  156. PlatformWindow *mPlatformWindow;
  157. // Reflections
  158. F32 mFogReflStrength;
  159. // PostFX
  160. PostEffect *glowFX;
  161. bool mUseGlow;
  162. F32 mGlowStrength;
  163. U8 mGlowing;
  164. F32 mCurGlow;
  165. bool mModifLightRays;
  166. F32 mLightRayMod;
  167. F32 mOldLightRayStrength;
  168. GameConnection* conn;
  169. U32 mCounter;
  170. void ResizeRT(PlatformWindow *win, bool resize);
  171. protected:
  172. // Protected methods
  173. bool onAdd();
  174. void onRemove();
  175. void handleResize(VolumetricFogRTManager *RTM, bool resize);
  176. void handleCanvasResize(GuiCanvas* canvas);
  177. bool LoadShape();
  178. bool setupRenderer();
  179. void InitTexture();
  180. bool UpdateBuffers(U32 dl,bool force=true);
  181. void processTick(const Move *move);
  182. void _enterFog(ShapeBase *control);
  183. void _leaveFog(ShapeBase *control);
  184. public:
  185. // Public methods
  186. VolumetricFog();
  187. ~VolumetricFog();
  188. static void initPersistFields();
  189. virtual void inspectPostApply();
  190. U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream);
  191. void unpackUpdate(NetConnection *conn, BitStream *stream);
  192. void prepRenderImage(SceneRenderState* state);
  193. void render(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat);
  194. void reflect_render(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat);
  195. // Methods for modifying & networking various fog elements
  196. // Used in script
  197. void setFogColor(ColorF color);
  198. void setFogColor(ColorI color);
  199. void setFogDensity(F32 density);
  200. void setFogModulation(F32 strength, Point2F speed1, Point2F speed2);
  201. void setFogGlow(bool on_off, F32 strength);
  202. void setFogLightray(bool on_off, F32 strength);
  203. bool isInsideFog();
  204. DECLARE_CONOBJECT(VolumetricFog);
  205. DECLARE_CALLBACK(void, onEnterFog, (SimObjectId obj));
  206. DECLARE_CALLBACK(void, onLeaveFog, (SimObjectId obj));
  207. };
  208. #endif