| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | /* ParallelSplitShadowMap written by Adrian Egli |
|---|
| 15 | * |
|---|
| 16 | * this version has still a bug in mutli-thread application (flickering problem) |
|---|
| 17 | * to avoid the flickering problem try osgShadow --pssm --SingleThreaded your_scene.ive |
|---|
| 18 | * |
|---|
| 19 | * The Parallel Split Shadow Map only supports directional light for simulating the shadow. |
|---|
| 20 | * It's one of the most robust algorithm for huge terrain sun light's shadow simulation, if |
|---|
| 21 | * you need to shadow a terrain, or another huge scene, you should use Parallel Split Shadow Map |
|---|
| 22 | * or at least test it against your scene. Have fun. |
|---|
| 23 | * |
|---|
| 24 | */ |
|---|
| 25 | |
|---|
| 26 | #ifndef OSGSHADOW_ParallelSplitShadowMap |
|---|
| 27 | #define OSGSHADOW_ParallelSplitShadowMap 1 |
|---|
| 28 | |
|---|
| 29 | #include <osg/Camera> |
|---|
| 30 | #include <osg/Material> |
|---|
| 31 | #include <osg/Depth> |
|---|
| 32 | #include <osg/ClipPlane> |
|---|
| 33 | |
|---|
| 34 | #include <osgShadow/ShadowTechnique> |
|---|
| 35 | |
|---|
| 36 | namespace osgShadow { |
|---|
| 37 | |
|---|
| 38 | class OSGSHADOW_EXPORT ParallelSplitShadowMap : public ShadowTechnique |
|---|
| 39 | { |
|---|
| 40 | public: |
|---|
| 41 | ParallelSplitShadowMap(osg::Geode** debugGroup=NULL, int icountplanes=3); |
|---|
| 42 | |
|---|
| 43 | ParallelSplitShadowMap(const ParallelSplitShadowMap& es, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); |
|---|
| 44 | |
|---|
| 45 | META_Object(osgShadow, ParallelSplitShadowMap); |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | /** Initialize the ShadowedScene and local cached data structures.*/ |
|---|
| 49 | virtual void init(); |
|---|
| 50 | |
|---|
| 51 | /** Run the update traversal of the ShadowedScene and update any loca chached data structures.*/ |
|---|
| 52 | virtual void update(osg::NodeVisitor& nv); |
|---|
| 53 | |
|---|
| 54 | /** Run the cull traversal of the ShadowedScene and set up the rendering for this ShadowTechnique.*/ |
|---|
| 55 | virtual void cull(osgUtil::CullVisitor& cv); |
|---|
| 56 | |
|---|
| 57 | /** Clean scene graph from any shadow technique specific nodes, state and drawables.*/ |
|---|
| 58 | virtual void cleanSceneGraph(); |
|---|
| 59 | |
|---|
| 60 | /** Switch on the debug coloring in GLSL (only the first 3 texture/splits showed for visualisation */ |
|---|
| 61 | inline void setDebugColorOn() { _debug_color_in_GLSL = true; } |
|---|
| 62 | |
|---|
| 63 | /** Set the polygon offset osg::Vec2f(factor,unit) */ |
|---|
| 64 | inline void setPolygonOffset(const osg::Vec2f& p) { _polgyonOffset = p;_user_polgyonOffset_set=true;} |
|---|
| 65 | |
|---|
| 66 | /** Get the polygon offset osg::Vec2f(factor,unit) */ |
|---|
| 67 | inline const osg::Vec2f& getPolygonOffset() const { return _polgyonOffset;} |
|---|
| 68 | |
|---|
| 69 | /** Set the texture resolution */ |
|---|
| 70 | inline void setTextureResolution(unsigned int resolution) { _resolution = resolution; } |
|---|
| 71 | |
|---|
| 72 | /** Get the texture resolution */ |
|---|
| 73 | inline unsigned int getTextureResolution() const { return _resolution; } |
|---|
| 74 | |
|---|
| 75 | /** Set the max far distance */ |
|---|
| 76 | inline void setMaxFarDistance(double farDist) { _setMaxFarDistance = farDist; _isSetMaxFarDistance = true; } |
|---|
| 77 | |
|---|
| 78 | /** Get the max far distance */ |
|---|
| 79 | inline double getMaxFarDistance() const { return _setMaxFarDistance; } |
|---|
| 80 | |
|---|
| 81 | /** Set the factor for moving the virtual camera behind the real camera*/ |
|---|
| 82 | inline void setMoveVCamBehindRCamFactor(double distFactor ) { _move_vcam_behind_rcam_factor = distFactor; } |
|---|
| 83 | |
|---|
| 84 | /** Get the factor for moving the virtual camera behind the real camera*/ |
|---|
| 85 | inline double getMoveVCamBehindRCamFactor() const { return _move_vcam_behind_rcam_factor; } |
|---|
| 86 | |
|---|
| 87 | /** Set min near distance for splits */ |
|---|
| 88 | inline void setMinNearDistanceForSplits(double nd){ _split_min_near_dist=nd; } |
|---|
| 89 | |
|---|
| 90 | /** Get min near distance for splits */ |
|---|
| 91 | inline double getMinNearDistanceForSplits() const { return _split_min_near_dist; } |
|---|
| 92 | |
|---|
| 93 | /** set a user defined light for shadow simulation (sun light, ... ) |
|---|
| 94 | * when this light get passed to pssm, the scene's light are no longer collected |
|---|
| 95 | * and simulated. just this user passed light, it needs to be a directional light. |
|---|
| 96 | */ |
|---|
| 97 | inline void setUserLight(osg::Light* light) { _userLight = light; } |
|---|
| 98 | |
|---|
| 99 | /** get the user defined light for shadow simulation */ |
|---|
| 100 | inline const osg::Light* getUserLight() const { return _userLight.get(); } |
|---|
| 101 | |
|---|
| 102 | /** Set the values for the ambient bias the shader will use.*/ |
|---|
| 103 | void setAmbientBias(const osg::Vec2& ambientBias ); |
|---|
| 104 | |
|---|
| 105 | /** Get the values for the ambient bias the shader will use.*/ |
|---|
| 106 | const osg::Vec2& getAmbientBias() const { return _ambientBias; } |
|---|
| 107 | |
|---|
| 108 | /** |
|---|
| 109 | * you can overwrite the fragment shader if you like to modify it yourself, own fragment shader can be used |
|---|
| 110 | */ |
|---|
| 111 | class OSGSHADOW_EXPORT FragmentShaderGenerator : public osg::Referenced { |
|---|
| 112 | public: |
|---|
| 113 | /** |
|---|
| 114 | * generate the GLSL fragement shader |
|---|
| 115 | */ |
|---|
| 116 | virtual std::string generateGLSL_FragmentShader_BaseTex(bool debug, unsigned int splitCount,double textureRes, bool filtered, unsigned int nbrSplits,unsigned int textureOffset); |
|---|
| 117 | }; |
|---|
| 118 | |
|---|
| 119 | /** set fragment shader generator */ |
|---|
| 120 | inline void setFragmentShaderGenerator(FragmentShaderGenerator* fsw) { _FragmentShaderGenerator = fsw;} |
|---|
| 121 | |
|---|
| 122 | /** enable / disable shadow filtering */ |
|---|
| 123 | inline void enableShadowGLSLFiltering(bool filtering = true) { _GLSL_shadow_filtered = filtering; } |
|---|
| 124 | |
|---|
| 125 | enum SplitCalcMode { |
|---|
| 126 | SPLIT_LINEAR, |
|---|
| 127 | SPLIT_EXP |
|---|
| 128 | }; |
|---|
| 129 | |
|---|
| 130 | /** set split calculation mode */ |
|---|
| 131 | inline void setSplitCalculationMode(SplitCalcMode scm=SPLIT_EXP) { _SplitCalcMode = scm; } |
|---|
| 132 | |
|---|
| 133 | /** get split calculation mode */ |
|---|
| 134 | inline SplitCalcMode getSplitCalculationMode() const { return _SplitCalcMode; } |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | protected : |
|---|
| 138 | |
|---|
| 139 | virtual ~ParallelSplitShadowMap() {} |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | struct PSSMShadowSplitTexture { |
|---|
| 143 | // RTT |
|---|
| 144 | osg::ref_ptr<osg::Camera> _camera; |
|---|
| 145 | osg::ref_ptr<osg::TexGen> _texgen; |
|---|
| 146 | osg::ref_ptr<osg::Texture2D> _texture; |
|---|
| 147 | osg::ref_ptr<osg::StateSet> _stateset; |
|---|
| 148 | unsigned int _textureUnit; |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | double _split_far; |
|---|
| 152 | |
|---|
| 153 | osg::ref_ptr<osg::Camera> _debug_camera; |
|---|
| 154 | osg::ref_ptr<osg::Texture2D> _debug_texture; |
|---|
| 155 | osg::ref_ptr<osg::StateSet> _debug_stateset; |
|---|
| 156 | unsigned int _debug_textureUnit; |
|---|
| 157 | |
|---|
| 158 | // Light (SUN) |
|---|
| 159 | osg::Vec3d _lightCameraSource; |
|---|
| 160 | osg::Vec3d _lightCameraTarget; |
|---|
| 161 | osg::Vec3d _frustumSplitCenter; |
|---|
| 162 | osg::Vec3d _lightDirection; |
|---|
| 163 | double _lightNear; |
|---|
| 164 | double _lightFar; |
|---|
| 165 | |
|---|
| 166 | osg::Matrix _cameraView; |
|---|
| 167 | osg::Matrix _cameraProj; |
|---|
| 168 | |
|---|
| 169 | unsigned int _splitID; |
|---|
| 170 | unsigned int _resolution; |
|---|
| 171 | |
|---|
| 172 | osg::Uniform* _farDistanceSplit; |
|---|
| 173 | }; |
|---|
| 174 | |
|---|
| 175 | typedef std::map<unsigned int,PSSMShadowSplitTexture> PSSMShadowSplitTextureMap; |
|---|
| 176 | PSSMShadowSplitTextureMap _PSSMShadowSplitTextureMap; |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | private: |
|---|
| 180 | void calculateFrustumCorners(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners); |
|---|
| 181 | void calculateLightInitialPosition(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners); |
|---|
| 182 | void calculateLightNearFarFormFrustum(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners); |
|---|
| 183 | void calculateLightViewProjectionFormFrustum(PSSMShadowSplitTexture &pssmShadowSplitTexture,osg::Vec3d *frustumCorners); |
|---|
| 184 | |
|---|
| 185 | osg::Geode** _displayTexturesGroupingNode; |
|---|
| 186 | |
|---|
| 187 | unsigned int _textureUnitOffset; |
|---|
| 188 | |
|---|
| 189 | unsigned int _number_of_splits; |
|---|
| 190 | |
|---|
| 191 | bool _debug_color_in_GLSL; |
|---|
| 192 | |
|---|
| 193 | osg::Vec2 _polgyonOffset; |
|---|
| 194 | bool _user_polgyonOffset_set; |
|---|
| 195 | |
|---|
| 196 | unsigned int _resolution; |
|---|
| 197 | |
|---|
| 198 | double _setMaxFarDistance; |
|---|
| 199 | bool _isSetMaxFarDistance; |
|---|
| 200 | |
|---|
| 201 | double _split_min_near_dist; |
|---|
| 202 | |
|---|
| 203 | double _move_vcam_behind_rcam_factor; |
|---|
| 204 | |
|---|
| 205 | osg::ref_ptr<osg::Light> _userLight; |
|---|
| 206 | osg::ref_ptr<FragmentShaderGenerator> _FragmentShaderGenerator; |
|---|
| 207 | |
|---|
| 208 | bool _GLSL_shadow_filtered; |
|---|
| 209 | SplitCalcMode _SplitCalcMode; |
|---|
| 210 | |
|---|
| 211 | osg::Uniform* _ambientBiasUniform; |
|---|
| 212 | osg::Vec2 _ambientBias; |
|---|
| 213 | |
|---|
| 214 | }; |
|---|
| 215 | } |
|---|
| 216 | #endif |
|---|