| 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 | * ViewDependentShadow codes Copyright (C) 2008 Wojciech Lewandowski |
|---|
| 14 | * Thanks to to my company http://www.ai.com.pl for allowing me free this work. |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | #ifndef OSGSHADOW_STANDARDSHADOWMAP |
|---|
| 18 | #define OSGSHADOW_STANDARDSHADOWMAP 1 |
|---|
| 19 | |
|---|
| 20 | #include <osgShadow/DebugShadowMap> |
|---|
| 21 | |
|---|
| 22 | namespace osgShadow { |
|---|
| 23 | |
|---|
| 24 | class OSGSHADOW_EXPORT StandardShadowMap : public DebugShadowMap |
|---|
| 25 | { |
|---|
| 26 | public : |
|---|
| 27 | /** Convenient typedef used in definition of ViewData struct and methods */ |
|---|
| 28 | typedef StandardShadowMap ThisClass; |
|---|
| 29 | /** Convenient typedef used in definition of ViewData struct and methods */ |
|---|
| 30 | typedef DebugShadowMap BaseClass; |
|---|
| 31 | |
|---|
| 32 | /** Classic OSG constructor */ |
|---|
| 33 | StandardShadowMap(); |
|---|
| 34 | |
|---|
| 35 | /** Classic OSG cloning constructor */ |
|---|
| 36 | StandardShadowMap(const StandardShadowMap& ssm, |
|---|
| 37 | const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); |
|---|
| 38 | |
|---|
| 39 | /** Declaration of standard OSG object methods */ |
|---|
| 40 | META_Object( osgShadow, StandardShadowMap ); |
|---|
| 41 | |
|---|
| 42 | void setBaseTextureUnit( unsigned int unit ) |
|---|
| 43 | { _baseTextureUnit = unit; dirty(); } |
|---|
| 44 | |
|---|
| 45 | unsigned int getBaseTextureUnit( void ) const |
|---|
| 46 | { return _baseTextureUnit; } |
|---|
| 47 | |
|---|
| 48 | void setShadowTextureUnit( unsigned int unit ) |
|---|
| 49 | { _shadowTextureUnit = unit; dirty(); } |
|---|
| 50 | |
|---|
| 51 | unsigned int getShadowTextureUnit( void ) const |
|---|
| 52 | { return _shadowTextureUnit; } |
|---|
| 53 | |
|---|
| 54 | // Texture Indices are changed by search and replace on shader source |
|---|
| 55 | // Carefully order these calls when changing both base and shadow indices |
|---|
| 56 | // In worst case when intend to swap indices |
|---|
| 57 | // one will have to call these methods more than once |
|---|
| 58 | // with one extra pass to change index to unused value to avoid |
|---|
| 59 | // unwanted superfluous replace: |
|---|
| 60 | // |
|---|
| 61 | // Example: Imagine we want to swap base(0) and shadow(1) indices: |
|---|
| 62 | // We have to do an extra step to make sure both do not end up as 1 |
|---|
| 63 | // |
|---|
| 64 | // // initialy change base to something else than 1 |
|---|
| 65 | // setBaseTextureCoordIndex( 100 ); |
|---|
| 66 | // // now search and replace all gl_TexCord[1] to gl_TexCord[0] |
|---|
| 67 | // setShadowTextureCoordIndex( 0 ); |
|---|
| 68 | // // finally change base from 100 to 0 |
|---|
| 69 | // setBaseTextureCoordIndex( 1 ); |
|---|
| 70 | |
|---|
| 71 | void setBaseTextureCoordIndex( unsigned int index ) |
|---|
| 72 | { updateTextureCoordIndices( _baseTextureCoordIndex, index ); |
|---|
| 73 | _baseTextureCoordIndex = index; } |
|---|
| 74 | |
|---|
| 75 | unsigned int getBaseTextureCoordIndex( void ) const |
|---|
| 76 | { return _baseTextureCoordIndex; } |
|---|
| 77 | |
|---|
| 78 | // Texture Indices are changed by search and replace on shader source |
|---|
| 79 | // Look at the comment above setBaseTextureCoordIndex |
|---|
| 80 | |
|---|
| 81 | void setShadowTextureCoordIndex( unsigned int index ) |
|---|
| 82 | { updateTextureCoordIndices( _shadowTextureCoordIndex, index ); |
|---|
| 83 | _shadowTextureCoordIndex = index; } |
|---|
| 84 | |
|---|
| 85 | unsigned int getShadowTextureCoordIndex( void ) const |
|---|
| 86 | { return _shadowTextureCoordIndex; } |
|---|
| 87 | |
|---|
| 88 | void setTextureSize( const osg::Vec2s& textureSize ) |
|---|
| 89 | { _textureSize = textureSize; dirty(); } |
|---|
| 90 | |
|---|
| 91 | const osg::Vec2s& getTextureSize() const |
|---|
| 92 | { return _textureSize; } |
|---|
| 93 | |
|---|
| 94 | void setLight( osg::Light* light ) |
|---|
| 95 | { _light = light; } |
|---|
| 96 | |
|---|
| 97 | osg::Light* getLight( void ) |
|---|
| 98 | { return _light.get(); } |
|---|
| 99 | |
|---|
| 100 | const osg::Light* getLight( void ) const |
|---|
| 101 | { return _light.get(); } |
|---|
| 102 | |
|---|
| 103 | osg::Shader * getShadowVertexShader() |
|---|
| 104 | { return _shadowVertexShader.get(); } |
|---|
| 105 | |
|---|
| 106 | osg::Shader * getShadowFragmentShader() |
|---|
| 107 | { return _shadowFragmentShader.get(); } |
|---|
| 108 | |
|---|
| 109 | osg::Shader * getMainVertexShader( ) |
|---|
| 110 | { return _mainVertexShader.get(); } |
|---|
| 111 | |
|---|
| 112 | osg::Shader * getMainFragmentShader( ) |
|---|
| 113 | { return _mainFragmentShader.get(); } |
|---|
| 114 | |
|---|
| 115 | void setShadowVertexShader( osg::Shader * shader ) |
|---|
| 116 | { _shadowVertexShader = shader; } |
|---|
| 117 | |
|---|
| 118 | void setShadowFragmentShader( osg::Shader * shader ) |
|---|
| 119 | { _shadowFragmentShader = shader; } |
|---|
| 120 | |
|---|
| 121 | void setMainVertexShader( osg::Shader * shader ) |
|---|
| 122 | { _mainVertexShader = shader; } |
|---|
| 123 | |
|---|
| 124 | void setMainFragmentShader( osg::Shader * shader ) |
|---|
| 125 | { _mainFragmentShader = shader; } |
|---|
| 126 | |
|---|
| 127 | protected: |
|---|
| 128 | /** Classic protected OSG destructor */ |
|---|
| 129 | virtual ~StandardShadowMap(void); |
|---|
| 130 | |
|---|
| 131 | virtual void updateTextureCoordIndices |
|---|
| 132 | ( unsigned int baseTexCoordIndex, unsigned int shadowTexCoordIndex ); |
|---|
| 133 | |
|---|
| 134 | virtual void searchAndReplaceShaderSource |
|---|
| 135 | ( osg::Shader*, std::string fromString, std::string toString ); |
|---|
| 136 | |
|---|
| 137 | osg::ref_ptr< osg::Shader > _mainVertexShader; |
|---|
| 138 | osg::ref_ptr< osg::Shader > _mainFragmentShader; |
|---|
| 139 | osg::ref_ptr< osg::Shader > _shadowVertexShader; |
|---|
| 140 | osg::ref_ptr< osg::Shader > _shadowFragmentShader; |
|---|
| 141 | |
|---|
| 142 | osg::ref_ptr< osg::Light > _light; |
|---|
| 143 | float _polygonOffsetFactor; |
|---|
| 144 | float _polygonOffsetUnits; |
|---|
| 145 | osg::Vec2s _textureSize; |
|---|
| 146 | unsigned int _baseTextureUnit; |
|---|
| 147 | unsigned int _shadowTextureUnit; |
|---|
| 148 | unsigned int _baseTextureCoordIndex; |
|---|
| 149 | unsigned int _shadowTextureCoordIndex; |
|---|
| 150 | |
|---|
| 151 | struct OSGSHADOW_EXPORT ViewData: public BaseClass::ViewData |
|---|
| 152 | { |
|---|
| 153 | osg::ref_ptr< osg::Light > * _lightPtr; |
|---|
| 154 | unsigned int * _baseTextureUnitPtr; |
|---|
| 155 | unsigned int * _shadowTextureUnitPtr; |
|---|
| 156 | |
|---|
| 157 | // ShadowMap texture is defined by base DebugShadowMap |
|---|
| 158 | // osg::ref_ptr<osg::Texture2D> _texture; |
|---|
| 159 | |
|---|
| 160 | // ShadowMap camera is defined by base DebugShadowMap |
|---|
| 161 | // osg::ref_ptr<osg::Camera> _camera; |
|---|
| 162 | |
|---|
| 163 | osg::ref_ptr<osg::TexGen> _texgen; |
|---|
| 164 | osg::ref_ptr<osg::StateSet> _stateset; |
|---|
| 165 | |
|---|
| 166 | virtual void init( ThisClass * st, osgUtil::CullVisitor * cv ); |
|---|
| 167 | |
|---|
| 168 | virtual void cull( ); |
|---|
| 169 | |
|---|
| 170 | virtual void aimShadowCastingCamera( |
|---|
| 171 | const osg::BoundingSphere &bounds, |
|---|
| 172 | const osg::Light *light, |
|---|
| 173 | const osg::Vec4 &worldLightPos, |
|---|
| 174 | const osg::Vec3 &worldLightDir, |
|---|
| 175 | const osg::Vec3 &worldLightUp = osg::Vec3(0,1,0) ); |
|---|
| 176 | |
|---|
| 177 | virtual void cullShadowReceivingScene( ); |
|---|
| 178 | |
|---|
| 179 | virtual void cullShadowCastingScene( ); |
|---|
| 180 | |
|---|
| 181 | virtual void addShadowReceivingTexGen( ); |
|---|
| 182 | |
|---|
| 183 | virtual const osg::Light* selectLight( osg::Vec4 &viewLightPos, |
|---|
| 184 | osg::Vec3 &viewLightDir ); |
|---|
| 185 | |
|---|
| 186 | virtual void aimShadowCastingCamera( const osg::Light *light, |
|---|
| 187 | const osg::Vec4 &worldLightPos, |
|---|
| 188 | const osg::Vec3 &worldLightDir, |
|---|
| 189 | const osg::Vec3 &worldLightUp |
|---|
| 190 | = osg::Vec3(0,1,0) ); |
|---|
| 191 | }; |
|---|
| 192 | |
|---|
| 193 | friend struct ViewData; |
|---|
| 194 | |
|---|
| 195 | META_ViewDependentShadowTechniqueData( ThisClass, ThisClass::ViewData ) |
|---|
| 196 | }; |
|---|
| 197 | |
|---|
| 198 | } // namespace osgShadow |
|---|
| 199 | |
|---|
| 200 | #endif |
|---|