| 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 | #ifndef OSGSHADOW_SHADOWEMAP |
|---|
| 15 | #define OSGSHADOW_SHADOWEMAP 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/Camera> |
|---|
| 18 | #include <osg/Material> |
|---|
| 19 | #include <osg/MatrixTransform> |
|---|
| 20 | #include <osg/LightSource> |
|---|
| 21 | |
|---|
| 22 | #include <osgShadow/ShadowTechnique> |
|---|
| 23 | |
|---|
| 24 | namespace osgShadow { |
|---|
| 25 | |
|---|
| 26 | /** ShadowedTexture provides an implementation of shadow textures.*/ |
|---|
| 27 | class OSGSHADOW_EXPORT ShadowMap : public ShadowTechnique |
|---|
| 28 | { |
|---|
| 29 | public : |
|---|
| 30 | ShadowMap(); |
|---|
| 31 | |
|---|
| 32 | ShadowMap(const ShadowMap& es, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); |
|---|
| 33 | |
|---|
| 34 | META_Object(osgShadow, ShadowMap); |
|---|
| 35 | |
|---|
| 36 | /** Set the texture unit that the shadow texture will be applied on.*/ |
|---|
| 37 | void setTextureUnit(unsigned int unit); |
|---|
| 38 | |
|---|
| 39 | /** Get the texture unit that the shadow texture will be applied on.*/ |
|---|
| 40 | unsigned int getTextureUnit() const { return _shadowTextureUnit; } |
|---|
| 41 | |
|---|
| 42 | /** set the polygon offset used initially */ |
|---|
| 43 | void setPolygonOffset(const osg::Vec2& polyOffset); |
|---|
| 44 | |
|---|
| 45 | /** get the used polygon offset */ |
|---|
| 46 | const osg::Vec2& getPolygonOffset() const { return _polyOffset; } |
|---|
| 47 | |
|---|
| 48 | /** Set the values for the ambient bias the shader will use.*/ |
|---|
| 49 | void setAmbientBias(const osg::Vec2& ambientBias ); |
|---|
| 50 | |
|---|
| 51 | /** Get the values that are used for the ambient bias in the shader.*/ |
|---|
| 52 | const osg::Vec2& getAmbientBias() const { return _ambientBias; } |
|---|
| 53 | |
|---|
| 54 | /** set the size in pixels x / y for the shadow texture.*/ |
|---|
| 55 | void setTextureSize(const osg::Vec2s& textureSize); |
|---|
| 56 | |
|---|
| 57 | /** Get the values that are used for the ambient bias in the shader.*/ |
|---|
| 58 | const osg::Vec2s& getTextureSize() const { return _textureSize; } |
|---|
| 59 | |
|---|
| 60 | /** Set the Light that will cast shadows */ |
|---|
| 61 | void setLight(osg::Light* light); |
|---|
| 62 | void setLight(osg::LightSource* ls); |
|---|
| 63 | |
|---|
| 64 | typedef std::vector< osg::ref_ptr<osg::Uniform> > UniformList; |
|---|
| 65 | |
|---|
| 66 | typedef std::vector< osg::ref_ptr<osg::Shader> > ShaderList; |
|---|
| 67 | |
|---|
| 68 | /** Add a shader to internal list, will be used instead of the default ones */ |
|---|
| 69 | inline void addShader(osg::Shader* shader) { _shaderList.push_back(shader); } |
|---|
| 70 | |
|---|
| 71 | /** Reset internal shader list */ |
|---|
| 72 | inline void clearShaderList() { _shaderList.clear(); } |
|---|
| 73 | |
|---|
| 74 | /** initialize the ShadowedScene and local cached data structures.*/ |
|---|
| 75 | virtual void init(); |
|---|
| 76 | |
|---|
| 77 | /** run the update traversal of the ShadowedScene and update any loca chached data structures.*/ |
|---|
| 78 | virtual void update(osg::NodeVisitor& nv); |
|---|
| 79 | |
|---|
| 80 | /** run the cull traversal of the ShadowedScene and set up the rendering for this ShadowTechnique.*/ |
|---|
| 81 | virtual void cull(osgUtil::CullVisitor& cv); |
|---|
| 82 | |
|---|
| 83 | /** Clean scene graph from any shadow technique specific nodes, state and drawables.*/ |
|---|
| 84 | virtual void cleanSceneGraph(); |
|---|
| 85 | |
|---|
| 86 | // debug methods |
|---|
| 87 | |
|---|
| 88 | osg::ref_ptr<osg::Camera> makeDebugHUD(); |
|---|
| 89 | |
|---|
| 90 | protected: |
|---|
| 91 | virtual ~ShadowMap(void) {}; |
|---|
| 92 | |
|---|
| 93 | /** Create the managed Uniforms */ |
|---|
| 94 | virtual void createUniforms(); |
|---|
| 95 | |
|---|
| 96 | virtual void createShaders(); |
|---|
| 97 | |
|---|
| 98 | // forward declare, interface and implementation provided in ShadowMap.cpp |
|---|
| 99 | class DrawableDrawWithDepthShadowComparisonOffCallback; |
|---|
| 100 | |
|---|
| 101 | osg::ref_ptr<osg::Camera> _camera; |
|---|
| 102 | osg::ref_ptr<osg::TexGen> _texgen; |
|---|
| 103 | osg::ref_ptr<osg::Texture2D> _texture; |
|---|
| 104 | osg::ref_ptr<osg::StateSet> _stateset; |
|---|
| 105 | osg::ref_ptr<osg::Program> _program; |
|---|
| 106 | osg::ref_ptr<osg::Light> _light; |
|---|
| 107 | |
|---|
| 108 | osg::ref_ptr<osg::LightSource> _ls; |
|---|
| 109 | |
|---|
| 110 | osg::ref_ptr<osg::Uniform> _ambientBiasUniform; |
|---|
| 111 | UniformList _uniformList; |
|---|
| 112 | ShaderList _shaderList; |
|---|
| 113 | unsigned int _baseTextureUnit; |
|---|
| 114 | unsigned int _shadowTextureUnit; |
|---|
| 115 | osg::Vec2 _polyOffset; |
|---|
| 116 | osg::Vec2 _ambientBias; |
|---|
| 117 | osg::Vec2s _textureSize; |
|---|
| 118 | |
|---|
| 119 | }; |
|---|
| 120 | |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | #endif |
|---|