BVB Source Codes

CRYENGINE Show SkyLightNishita.h Source code

Return Download CRYENGINE: download SkyLightNishita.h Source code - Download CRYENGINE Source code - Type:.h
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. /*************************************************************************
  4.    -------------------------------------------------------------------------
  5.    $Id$
  6.    $DateTime$
  7.  
  8.    -------------------------------------------------------------------------
  9.    History:
  10.    - 09:05:2005   11:08 : Created by Carsten Wenzel
  11.  
  12. *************************************************************************/
  13.  
  14. #ifndef _SKY_LIGHT_NISHITA_H_
  15. #define _SKY_LIGHT_NISHITA_H_
  16.  
  17. #pragma once
  18.  
  19. #include <vector>
  20.  
  21. class CSkyLightManager;
  22.  
  23. class CSkyLightNishita : public Cry3DEngineBase
  24. {
  25. private:
  26.  
  27.         // size of lookup tables
  28.         static const uint32 cOLUT_HeightSteps = 32;
  29.         static const uint32 cOLUT_AngularSteps = 256;
  30.  
  31.         // definition of optical depth LUT for mie/rayleigh scattering
  32.         struct CRY_ALIGN(8) SOpticalDepthLUTEntry
  33.         {
  34.                 f32 mie;
  35.                 f32 rayleigh;
  36.  
  37.                 void GetMemoryUsage(ICrySizer* pSizer) const {}
  38.  
  39.                 AUTO_STRUCT_INFO;
  40.         };
  41.  
  42.         // definition of optical scale LUT for mie/rayleigh scattering
  43.         struct SOpticalScaleLUTEntry
  44.         {
  45.                 f32  atmosphereLayerHeight;
  46.                 f32  mie;
  47.                 f32  rayleigh;
  48.  
  49.                 void GetMemoryUsage(ICrySizer* pSizer) const {}
  50.  
  51.                 AUTO_STRUCT_INFO;
  52.         };
  53.  
  54.         // definition lookup table entry for phase function
  55.         struct CRY_ALIGN(8) SPhaseLUTEntry
  56.         {
  57.                 f32 mie;
  58.                 f32 rayleigh;
  59.         };
  60.  
  61.         // definition of lookup tables
  62.         typedef std::vector<SOpticalDepthLUTEntry> OpticalDepthLUT;
  63.         typedef std::vector<SOpticalScaleLUTEntry> OpticalScaleLUT;
  64.  
  65.         static const uint32 cPLUT_AngularSteps = 256;
  66.         //replace std::vector by a custom class with static storage
  67.         class PhaseLUT
  68.         {
  69.                 CRY_ALIGN(128) SPhaseLUTEntry m_LUT[cPLUT_AngularSteps];
  70.                 size_t m_Size;
  71.         public:
  72.                 PhaseLUT() : m_Size(0) {}
  73.                 SPhaseLUTEntry&       operator[](size_t index)         { return m_LUT[index]; }
  74.                 const SPhaseLUTEntry& operator[](size_t index) const   { return m_LUT[index]; }
  75.                 void                  resize(size_t size)              { m_Size = size; };
  76.                 void                  reserve(size_t)                  {}
  77.                 void                  push_back(SPhaseLUTEntry& entry) { m_LUT[m_Size++] = entry; }
  78.                 size_t                size() const                     { return m_Size; }
  79.         };
  80.  
  81. public:
  82.         CSkyLightNishita();
  83.         ~CSkyLightNishita();
  84.  
  85.         // set sky dome conditions
  86.         void SetAtmosphericConditions(const Vec3& sunIntensity, const f32 Km, const f32 Kr, const f32 g);
  87.         void SetRGBWaveLengths(const Vec3& rgbWaveLengths);
  88.         void SetSunDirection(const Vec3& sunDir);
  89.  
  90.         // compute sky colors
  91.         void  ComputeSkyColor(const Vec3& skyDir, Vec3* pInScattering, Vec3* pInScatteringMieNoPremul, Vec3* pInScatteringRayleighNoPremul, Vec3* pInScatteringRayleigh) const;
  92.  
  93.         void  SetInScatteringIntegralStepSize(int32 stepSize);
  94.         int32 GetInScatteringIntegralStepSize() const;
  95.  
  96.         // constants for final pixel shader processing, if "no pre-multiplied in-scattering " colors are to be processed in a pixel shader
  97.         Vec4 GetPartialMieInScatteringConst() const;
  98.         Vec4 GetPartialRayleighInScatteringConst() const;
  99.         Vec3 GetSunDirection() const;
  100.         Vec4 GetPhaseFunctionConsts() const;
  101.  
  102.         void GetMemoryUsage(ICrySizer* pSizer) const
  103.         {
  104.                 pSizer->AddObject(this, sizeof(*this));
  105.                 pSizer->AddObject(m_opticalDepthLUT);
  106.                 pSizer->AddObject(m_opticalScaleLUT);
  107.         }
  108. private:
  109.         // mapping helpers
  110.         f64  MapIndexToHeight(uint32 index) const;
  111.         f64  MapIndexToCosVertAngle(uint32 index) const;
  112.         f32  MapIndexToCosPhaseAngle(uint32 index) const;
  113.  
  114.         void MapCosPhaseAngleToIndex(const f32 cosPhaseAngle, uint32& index, f32& indexFrc) const;
  115.  
  116.         // optical lookup table access helpers
  117.         uint32 OpticalLUTIndex(uint32 heightIndex, uint32 cosVertAngleIndex) const;
  118.  
  119.         // computes lookup tables for optical depth, etc.
  120.         void ComputeOpticalLUTs();
  121.  
  122.         // computes lookup table for phase function
  123.         void ComputePhaseLUT();
  124.  
  125.         // computes optical depth (helpers for ComputeOpticalLUTs())
  126.         f64 IntegrateOpticalDepth(const Vec3d& start, const Vec3d& end,
  127.                                   const f64& avgDensityHeightInv, const f64& error) const;
  128.  
  129.         bool ComputeOpticalDepth(const Vec3d& cameraLookDir, const f64& cameraHeight, const f64& avgDensityHeightInv, float& depth) const;
  130.  
  131.         // does a bilinearily filtered lookup into the optical depth LUT
  132.         // SOpticalDepthLUTEntry* is passed to save address resolve operations
  133.         ILINE SOpticalDepthLUTEntry LookupBilerpedOpticalDepthLUTEntry(const SOpticalDepthLUTEntry* const __restrict cpOptDepthLUT,
  134.                                                                        uint32 heightIndex, const f32 cosVertAngle) const;
  135.  
  136.         // does a bilinearily filtered lookup into the phase LUT
  137.         SPhaseLUTEntry LookupBilerpedPhaseLUTEntry(const f32 cosPhaseAngle) const;
  138.  
  139.         // computes in-scattering
  140.         void SamplePartialInScatteringAtHeight(const SOpticalScaleLUTEntry& osAtHeight,
  141.                                                const f32 outScatteringConstMie, const Vec3& outScatteringConstRayleigh, const SOpticalDepthLUTEntry& odAtHeightSky,
  142.                                                const SOpticalDepthLUTEntry& odAtViewerSky, const SOpticalDepthLUTEntry& odAtHeightSun,
  143.                                                Vec3& partialInScatteringMie, Vec3& partialInScatteringRayleigh) const;
  144.  
  145.         void ComputeInScatteringNoPremul(const f32 outScatteringConstMie, const Vec3& outScatteringConstRayleigh, const Vec3& skyDir,
  146.                                          Vec3& inScatteringMieNoPremul, Vec3& inScatteringRayleighNoPremul) const;
  147.  
  148.         // serialization of optical LUTs
  149.         bool                   LoadOpticalLUTs();
  150.         void                   SaveOpticalLUTs() const;
  151.  
  152.         const OpticalScaleLUT& GetOpticalScaleLUT() const;
  153.  
  154. private:
  155.         // lookup tables
  156.         OpticalDepthLUT m_opticalDepthLUT;
  157.         OpticalScaleLUT m_opticalScaleLUT;
  158.         PhaseLUT        m_phaseLUT;
  159.  
  160.         // mie scattering constant
  161.         f32 m_Km;
  162.  
  163.         // rayleigh scattering constant
  164.         f32 m_Kr;
  165.  
  166.         // sun intensity
  167.         Vec3 m_sunIntensity;
  168.  
  169.         // mie scattering asymmetry factor (g is always 0.0 for rayleigh scattering)
  170.         f32 m_g;
  171.  
  172.         // wavelengths for r, g, and b to the -4th used for mie rayleigh scattering
  173.         Vec3 m_invRGBWaveLength4;
  174.  
  175.         // direction towards the sun
  176.         Vec3 m_sunDir;
  177.  
  178.         // step size for solving in-scattering integral
  179.         int32 m_inScatteringStepSize;
  180.  
  181.         friend class CSkyLightManager;
  182. };
  183.  
  184. ILINE void CSkyLightNishita::SetRGBWaveLengths(const Vec3& rgbWaveLengths)
  185. {
  186.         assert(380.0f <= rgbWaveLengths.x && 780.0f >= rgbWaveLengths.x);
  187.         assert(380.0f <= rgbWaveLengths.y && 780.0f >= rgbWaveLengths.y);
  188.         assert(380.0f <= rgbWaveLengths.z && 780.0f >= rgbWaveLengths.z);
  189.  
  190.         m_invRGBWaveLength4.x = powf(rgbWaveLengths.x * 1e-3f, -4.0f);
  191.         m_invRGBWaveLength4.y = powf(rgbWaveLengths.y * 1e-3f, -4.0f);
  192.         m_invRGBWaveLength4.z = powf(rgbWaveLengths.z * 1e-3f, -4.0f);
  193. }
  194.  
  195. ILINE void CSkyLightNishita::SetSunDirection(const Vec3& sunDir)
  196. {
  197.         assert(sunDir.GetLengthSquared() > 0.0f);
  198.         m_sunDir = sunDir;
  199.         m_sunDir.Normalize();
  200. }
  201.  
  202. ILINE void CSkyLightNishita::SetAtmosphericConditions(const Vec3& sunIntensity,
  203.                                                       const f32 Km, const f32 Kr, const f32 g)
  204. {
  205.         m_sunIntensity = sunIntensity;
  206.         m_Km = Km;
  207.         m_Kr = Kr;
  208.  
  209.         // update g only if it changed as phase lut needs to be rebuilt
  210.         float newg(clamp_tpl(g, -0.9995f, 0.9995f));
  211.         if (fabsf(m_g - newg) > 1e-6f)
  212.         {
  213.                 m_g = newg;
  214.                 ComputePhaseLUT();
  215.         }
  216. }
  217.  
  218. ILINE const CSkyLightNishita::OpticalScaleLUT& CSkyLightNishita::GetOpticalScaleLUT() const
  219. {
  220.         return m_opticalScaleLUT;
  221. }
  222.  
  223. #endif // #ifndef _SKY_LIGHT_NISHITA_H_
  224.  
downloadSkyLightNishita.h Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top