scatterSky.h 6.7 KB

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