scatterSky.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 _SCATTERSKY_H_
  23. #define _SCATTERSKY_H_
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. #ifndef _GFXPRIMITIVEBUFFER_H_
  28. #include "gfx/gfxPrimitiveBuffer.h"
  29. #endif
  30. #ifndef _GFXVERTEXBUFFER_H_
  31. #include "gfx/gfxVertexBuffer.h"
  32. #endif
  33. #ifndef _GFXSTATEBLOCK_H_
  34. #include "gfx/gfxStateBlock.h"
  35. #endif
  36. #ifndef _RENDERPASSMANAGER_H_
  37. #include "renderInstance/renderPassManager.h"
  38. #endif
  39. #ifndef _PRIMBUILDER_H_
  40. #include "gfx/primBuilder.h"
  41. #endif
  42. #ifndef _LIGHTINFO_H_
  43. #include "lighting/lightInfo.h"
  44. #endif
  45. #ifndef _LIGHTFLAREDATA_H_
  46. #include "T3D/lightFlareData.h"
  47. #endif
  48. #ifndef _TRESPONSECURVE_H_
  49. #include "math/util/tResponseCurve.h"
  50. #endif
  51. #include "T3D/assets/MaterialAsset.h"
  52. class LightInfo;
  53. class SphereMesh;
  54. class TimeOfDay;
  55. class CubemapData;
  56. class MatrixSet;
  57. class ScatterSky : public SceneObject, public ISceneLight
  58. {
  59. typedef SceneObject Parent;
  60. public:
  61. enum MaskBits
  62. {
  63. UpdateMask = Parent::NextFreeMask,
  64. TimeMask = Parent::NextFreeMask << 1,
  65. NextFreeMask = Parent::NextFreeMask << 2
  66. };
  67. ScatterSky();
  68. ~ScatterSky();
  69. // SimObject
  70. bool onAdd();
  71. void onRemove();
  72. // ISceneLight
  73. virtual void submitLights( LightManager *lm, bool staticLighting );
  74. virtual LightInfo* getLight() { return mLight; }
  75. // ConsoleObject
  76. DECLARE_CONOBJECT(ScatterSky);
  77. void inspectPostApply();
  78. static void initPersistFields();
  79. // Network
  80. U32 packUpdate ( NetConnection *conn, U32 mask, BitStream *stream );
  81. void unpackUpdate( NetConnection *conn, BitStream *stream );
  82. void prepRenderImage( SceneRenderState* state );
  83. ///
  84. void setAzimuth( F32 azimuth );
  85. ///
  86. void setElevation( F32 elevation );
  87. ///
  88. F32 getAzimuth() const { return mSunAzimuth; }
  89. ///
  90. F32 getElevation() const { return mSunElevation; }
  91. protected:
  92. void _render( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  93. void _debugRender( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  94. void _renderMoon( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  95. void _initVBIB();
  96. bool _initShader();
  97. void _initMoon();
  98. void _initCurves();
  99. F32 _getRayleighPhase( F32 fCos2 );
  100. F32 _getMiePhase( F32 fCos, F32 fCos2, F32 g, F32 g2 );
  101. F32 _vernierScale( F32 fCos );
  102. void _generateSkyPoints();
  103. void _getColor( const Point3F &pos, LinearColorF *outColor );
  104. void _getFogColor( LinearColorF *outColor );
  105. void _getAmbientColor( LinearColorF *outColor );
  106. void _getSunColor( LinearColorF *outColor );
  107. void _interpolateColors();
  108. void _conformLights();
  109. void _updateTimeOfDay( TimeOfDay *timeofDay, F32 time );
  110. // static protected field set methods
  111. static bool ptSetElevation( void *object, const char *index, const char *data );
  112. static bool ptSetAzimuth( void *object, const char *index, const char *data );
  113. // SimObject.
  114. virtual void _onSelected();
  115. virtual void _onUnselected();
  116. protected:
  117. static const F32 smEarthRadius;
  118. static const F32 smAtmosphereRadius;
  119. static const F32 smViewerHeight;
  120. #define CURVE_COUNT 5
  121. FloatCurve mCurves[CURVE_COUNT];
  122. U32 mVertCount;
  123. U32 mPrimCount;
  124. F32 mRayleighScattering;
  125. F32 mRayleighScattering4PI;
  126. F32 mSunSize;
  127. F32 mMieScattering;
  128. F32 mMieScattering4PI;
  129. F32 mSkyBrightness;
  130. F32 mMiePhaseAssymetry;
  131. F32 mOuterRadius;
  132. F32 mScale;
  133. LinearColorF mWavelength;
  134. F32 mWavelength4[3];
  135. F32 mRayleighScaleDepth;
  136. F32 mMieScaleDepth;
  137. F32 mSphereInnerRadius;
  138. F32 mSphereOuterRadius;
  139. F32 mExposure;
  140. F32 mNightInterpolant;
  141. F32 mZOffset;
  142. VectorF mLightDir;
  143. VectorF mSunDir;
  144. F32 mSunAzimuth;
  145. F32 mSunElevation;
  146. F32 mMoonAzimuth;
  147. F32 mMoonElevation;
  148. F32 mTimeOfDay;
  149. F32 mBrightness;
  150. LinearColorF mNightColor;
  151. LinearColorF mNightFogColor;
  152. LinearColorF mAmbientColor; ///< Not a field
  153. LinearColorF mSunColor; ///< Not a field
  154. LinearColorF mFogColor; ///< Not a field
  155. LinearColorF mAmbientScale;
  156. LinearColorF mSunScale;
  157. LinearColorF mFogScale;
  158. LightInfo *mLight;
  159. bool mCastShadows;
  160. S32 mStaticRefreshFreq;
  161. S32 mDynamicRefreshFreq;
  162. bool mDirty;
  163. LightFlareData *mFlareData;
  164. LightFlareState mFlareState;
  165. F32 mFlareScale;
  166. bool mMoonEnabled;
  167. DECLARE_MATERIALASSET(ScatterSky, MoonMat);
  168. DECLARE_MATERIALASSET_NET_SETGET(ScatterSky, MoonMat, UpdateMask);
  169. BaseMatInstance *mMoonMatInst;
  170. F32 mMoonScale;
  171. LinearColorF mMoonTint;
  172. VectorF mMoonLightDir;
  173. CubemapData *mNightCubemap;
  174. StringTableEntry mNightCubemapName;
  175. bool mUseNightCubemap;
  176. MatrixSet *mMatrixSet;
  177. Vector<Point3F> mSkyPoints;
  178. // Prim buffer, vertex buffer and shader for rendering.
  179. GFXPrimitiveBufferHandle mPrimBuffer;
  180. GFXVertexBufferHandle<GFXVertexP> mVB;
  181. GFXShaderRef mShader;
  182. GFXStateBlockRef mStateBlock;
  183. // Shared shader constant blocks
  184. GFXShaderConstBufferRef mShaderConsts;
  185. GFXShaderConstHandle *mModelViewProjSC;
  186. GFXShaderConstHandle *mMiscSC; // Camera height, cam height squared, scale and scale over depth.
  187. GFXShaderConstHandle *mSphereRadiiSC; // Inner and out radius, and inner and outer radius squared.
  188. GFXShaderConstHandle *mScatteringCoefficientsSC; // Rayleigh sun brightness, mie sun brightness and 4 * PI * coefficients.
  189. GFXShaderConstHandle *mCamPosSC;
  190. GFXShaderConstHandle *mLightDirSC;
  191. GFXShaderConstHandle *mSunDirSC;
  192. GFXShaderConstHandle *mNightColorSC;
  193. GFXShaderConstHandle *mInverseWavelengthSC;
  194. GFXShaderConstHandle *mNightInterpolantAndExposureSC;
  195. GFXShaderConstHandle *mUseCubemapSC;
  196. F32 mColorizeAmt;
  197. LinearColorF mColorize;
  198. GFXShaderConstHandle *mColorizeSC;
  199. };
  200. #endif // _SCATTERSKY_H_