MinimalShadowMap 6.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
 *
 * This library is open source and may be redistributed and/or modified under  
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * OpenSceneGraph Public License for more details.
 *
 * ViewDependentShadow codes Copyright (C) 2008 Wojciech Lewandowski
 * Thanks to to my company http://www.ai.com.pl for allowing me free this work.
*/

#ifndef OSGSHADOW_MINIMALSHADOWMAP
#define OSGSHADOW_MINIMALSHADOWMAP 1

#include <osgShadow/StandardShadowMap>

namespace osgShadow {

class OSGSHADOW_EXPORT MinimalShadowMap : public StandardShadowMap 
{
    public :
        /** Convenient typedef used in definition of ViewData struct and methods */
        typedef MinimalShadowMap  ThisClass;
        /** Convenient typedef used in definition of ViewData struct and methods */
        typedef StandardShadowMap BaseClass;

        /** Classic OSG constructor */
        MinimalShadowMap();

        /** Classic OSG cloning constructor */
        MinimalShadowMap(
            const MinimalShadowMap& msm, 
            const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);

        /** Declaration of standard OSG object methods */
        META_Object( osgShadow, MinimalShadowMap );

        void setModellingSpaceToWorldTransform( const osg::Matrix & modellingSpaceToWorld )
            { _modellingSpaceToWorld = modellingSpaceToWorld; }

        const osg::Matrix & getModellingSpaceToWorldTransform( void ) const
            { return _modellingSpaceToWorld; }

        float getMaxFarPlane( ) const
            { return _maxFarPlane; }

        void setMaxFarPlane( float maxFarPlane )
            { _maxFarPlane = maxFarPlane; }

        float getMinLightMargin( ) const
            { return _minLightMargin; }

        void setMinLightMargin( float minLightMargin )
            { _minLightMargin = minLightMargin; }

        enum ShadowReceivingCoarseBoundAccuracy {
            EMPTY_BOX,
            BOUNDING_SPHERE,
            BOUNDING_BOX,
            DEFAULT_ACCURACY = BOUNDING_BOX
        };

        void  setShadowReceivingCoarseBoundAccuracy
                ( ShadowReceivingCoarseBoundAccuracy accuracy )
                    { _shadowReceivingCoarseBoundAccuracy = accuracy; }

        ShadowReceivingCoarseBoundAccuracy 
                getShadowReceivingCoarseBoundAccuracy() const
                    { return _shadowReceivingCoarseBoundAccuracy; }

    protected:
        /** Classic protected OSG destructor */
        virtual ~MinimalShadowMap(void);

    protected:         
        // Matrix modellingSpaceToWorld and its inverse
        // are used to define Modelling Space where shadowed scene drawables
        // have minimal (smallest possible extent) bounding boxes.

        // Computing visible shadow range in this space 
        // allows for optimal use of ShadowMap resolution.

        // By default it is set to identity ie computations are in world space.
        // But it should be set to ElipsoidModel::localToWorld 
        // when scene objects are put on earth ellipsoid surface.

        // Other scenarios are also possible for example when models are 
        // built in XZY space which would require identity matrix with swapped colums

        osg::Matrix                        _modellingSpaceToWorld;
        float                              _maxFarPlane;
        float                              _minLightMargin; 
        ShadowReceivingCoarseBoundAccuracy _shadowReceivingCoarseBoundAccuracy;        

        struct OSGSHADOW_EXPORT ViewData: public BaseClass::ViewData
        {
            osg::Matrix                      *_modellingSpaceToWorldPtr;
            float                            *_maxFarPlanePtr;
            float                            *_minLightMarginPtr;
            int                               _frameShadowCastingCameraPasses;

            ConvexPolyhedron                 _sceneReceivingShadowPolytope;
            std::vector< osg::Vec3d >        _sceneReceivingShadowPolytopePoints;

            osg::Matrixd                     _clampedProjection;

            virtual void init( ThisClass * st, osgUtil::CullVisitor * cv );

            virtual osg::BoundingBox computeShadowReceivingCoarseBounds( );

            virtual void cullShadowReceivingScene( );

            virtual void aimShadowCastingCamera( 
                const osg::BoundingSphere &bounds,
                const osg::Light *light,
                const osg::Vec4 &worldLightPos,
                const osg::Vec3 &worldLightDir,
                const osg::Vec3 &worldLightUp = osg::Vec3(0,1,0) );

            virtual void aimShadowCastingCamera( const osg::Light *light,
                const osg::Vec4 &worldLightPos,
                const osg::Vec3 &worldLightDir,
                const osg::Vec3 &worldLightUp
                = osg::Vec3(0,1,0) );

            virtual void frameShadowCastingCamera
                ( const osg::Camera* cameraMain, osg::Camera* cameraShadow, int pass = 1 );

            void cutScenePolytope( const osg::Matrix & matrix, 
                const osg::Matrix & inverse,
                const osg::BoundingBox &bb = 
                osg::BoundingBox(-1,-1,-1,1,1,1) );

            osg::BoundingBox computeScenePolytopeBounds
                ( const osg::Matrix & m = *(osg::Matrix*)(NULL) );    

            // Utility methods for adjusting projection matrices

            // Modify projection matrix so that some output subrange
            // is remapped to whole clip space (-1..1,-1..1,-1..1).
            // Bit mask can be used to limit remaping to selected bounds only.
            static void trimProjection
                ( osg::Matrixd & projection, osg::BoundingBox subrange,
                unsigned int trimMask = (1|2|4|8|16|32)
                /*1=left|2=right|4=bottom|8=top|16=near|32=far*/);

            static void clampProjection
                ( osg::Matrixd & projection, float n = 0, float f = FLT_MAX );

            static void extendProjection
                ( osg::Matrixd & projection, osg::Viewport * viewport, const osg::Vec2& margin );
        };

        META_ViewDependentShadowTechniqueData( ThisClass, ThisClass::ViewData )
};

} // namespace osgShadow

#endif