scatterSky.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. struct SphereVertex
  92. {
  93. Point3F pos;
  94. };
  95. Vector<SphereVertex> tmpVertices;
  96. Vector<F32> vertsVec;
  97. struct FinalVertexData
  98. {
  99. Point3F pos;
  100. };
  101. Vector<FinalVertexData> finalVertData;
  102. void addVertex(Point3F vert);
  103. void BuildFinalVert();
  104. void clearVectors();
  105. protected:
  106. void _render( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  107. void _debugRender( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  108. void _renderMoon( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  109. void _initVBIB();
  110. bool _initShader();
  111. void _initMoon();
  112. void _initCurves();
  113. F32 _getRayleighPhase( F32 fCos2 );
  114. F32 _getMiePhase( F32 fCos, F32 fCos2, F32 g, F32 g2 );
  115. F32 _vernierScale( F32 fCos );
  116. void _generateSkyPoints();
  117. void _getColor( const Point3F &pos, LinearColorF *outColor );
  118. void _getFogColor( LinearColorF *outColor );
  119. void _getAmbientColor( LinearColorF *outColor );
  120. void _getSunColor( LinearColorF *outColor );
  121. void _interpolateColors();
  122. void _conformLights();
  123. void _updateTimeOfDay( TimeOfDay *timeofDay, F32 time );
  124. // static protected field set methods
  125. static bool ptSetElevation( void *object, const char *index, const char *data );
  126. static bool ptSetAzimuth( void *object, const char *index, const char *data );
  127. // SimObject.
  128. virtual void _onSelected();
  129. virtual void _onUnselected();
  130. protected:
  131. static const F32 smEarthRadius;
  132. static const F32 smAtmosphereRadius;
  133. static const F32 smViewerHeight;
  134. #define CURVE_COUNT 5
  135. FloatCurve mCurves[CURVE_COUNT];
  136. U32 mVertCount;
  137. U32 mPrimCount;
  138. F32 mRayleighScattering;
  139. F32 mRayleighScattering4PI;
  140. F32 mSunSize;
  141. F32 mMieScattering;
  142. F32 mMieScattering4PI;
  143. F32 mSkyBrightness;
  144. F32 mMiePhaseAssymetry;
  145. F32 mOuterRadius;
  146. F32 mScale;
  147. LinearColorF mWavelength;
  148. F32 mWavelength4[3];
  149. F32 mRayleighScaleDepth;
  150. F32 mMieScaleDepth;
  151. F32 mSphereInnerRadius;
  152. F32 mSphereOuterRadius;
  153. F32 mExposure;
  154. F32 mNightInterpolant;
  155. F32 mZOffset;
  156. VectorF mLightDir;
  157. VectorF mSunDir;
  158. F32 mSunAzimuth;
  159. F32 mSunElevation;
  160. F32 mMoonAzimuth;
  161. F32 mMoonElevation;
  162. F32 mTimeOfDay;
  163. F32 mBrightness;
  164. LinearColorF mNightColor;
  165. LinearColorF mNightFogColor;
  166. LinearColorF mAmbientColor; ///< Not a field
  167. LinearColorF mSunColor; ///< Not a field
  168. LinearColorF mFogColor; ///< Not a field
  169. LinearColorF mAmbientScale;
  170. LinearColorF mSunScale;
  171. LinearColorF mFogScale;
  172. LightInfo *mLight;
  173. bool mCastShadows;
  174. S32 mStaticRefreshFreq;
  175. S32 mDynamicRefreshFreq;
  176. bool mDirty;
  177. LightFlareData *mFlareData;
  178. LightFlareState mFlareState;
  179. F32 mFlareScale;
  180. bool mMoonEnabled;
  181. DECLARE_MATERIALASSET(ScatterSky, MoonMat);
  182. DECLARE_ASSET_NET_SETGET(ScatterSky, MoonMat, UpdateMask);
  183. BaseMatInstance *mMoonMatInst;
  184. F32 mMoonScale;
  185. LinearColorF mMoonTint;
  186. VectorF mMoonLightDir;
  187. CubemapData *mNightCubemap;
  188. StringTableEntry mNightCubemapName;
  189. bool mUseNightCubemap;
  190. MatrixSet *mMatrixSet;
  191. Vector<Point3F> mSkyPoints;
  192. // Prim buffer, vertex buffer and shader for rendering.
  193. GFXPrimitiveBufferHandle mPrimBuffer;
  194. GFXVertexBufferHandle<GFXVertexP> mVB;
  195. GFXShaderRef mShader;
  196. GFXStateBlockRef mStateBlock;
  197. // Shared shader constant blocks
  198. GFXShaderConstBufferRef mShaderConsts;
  199. GFXShaderConstHandle *mModelViewProjSC;
  200. GFXShaderConstHandle *mMiscSC; // Camera height, cam height squared, scale and scale over depth.
  201. GFXShaderConstHandle *mSphereRadiiSC; // Inner and out radius, and inner and outer radius squared.
  202. GFXShaderConstHandle *mScatteringCoefficientsSC; // Rayleigh sun brightness, mie sun brightness and 4 * PI * coefficients.
  203. GFXShaderConstHandle *mCamPosSC;
  204. GFXShaderConstHandle *mLightDirSC;
  205. GFXShaderConstHandle *mSunDirSC;
  206. GFXShaderConstHandle *mNightColorSC;
  207. GFXShaderConstHandle *mInverseWavelengthSC;
  208. GFXShaderConstHandle *mNightInterpolantAndExposureSC;
  209. GFXShaderConstHandle *mUseCubemapSC;
  210. F32 mColorizeAmt;
  211. LinearColorF mColorize;
  212. GFXShaderConstHandle *mColorizeSC;
  213. };
  214. #endif // _SCATTERSKY_H_