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