2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2008-10-06 16:53:54 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* 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
|
2008-10-06 16:53:54 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2008-10-06 16:53:54 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2008-10-06 16:53:54 +08:00
|
|
|
* 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 {
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
class OSGSHADOW_EXPORT MinimalShadowMap : public StandardShadowMap
|
2008-10-06 16:53:54 +08:00
|
|
|
{
|
|
|
|
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(
|
2012-03-22 01:36:20 +08:00
|
|
|
const MinimalShadowMap& msm,
|
2008-10-06 16:53:54 +08:00
|
|
|
const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
|
|
|
|
|
|
|
|
/** Declaration of standard OSG object methods */
|
2011-02-24 00:45:44 +08:00
|
|
|
META_Object( osgShadow, MinimalShadowMap );
|
2008-10-06 16:53:54 +08:00
|
|
|
|
|
|
|
void setModellingSpaceToWorldTransform( const osg::Matrix & modellingSpaceToWorld )
|
|
|
|
{ _modellingSpaceToWorld = modellingSpaceToWorld; }
|
|
|
|
|
2010-04-20 18:29:04 +08:00
|
|
|
const osg::Matrix & getModellingSpaceToWorldTransform( void ) const
|
2008-10-06 16:53:54 +08:00
|
|
|
{ return _modellingSpaceToWorld; }
|
|
|
|
|
2010-04-20 18:29:04 +08:00
|
|
|
float getMaxFarPlane( ) const
|
2008-10-06 16:53:54 +08:00
|
|
|
{ return _maxFarPlane; }
|
|
|
|
|
|
|
|
void setMaxFarPlane( float maxFarPlane )
|
|
|
|
{ _maxFarPlane = maxFarPlane; }
|
|
|
|
|
2010-04-20 18:29:04 +08:00
|
|
|
float getMinLightMargin( ) const
|
2008-10-06 16:53:54 +08:00
|
|
|
{ return _minLightMargin; }
|
|
|
|
|
|
|
|
void setMinLightMargin( float minLightMargin )
|
|
|
|
{ _minLightMargin = minLightMargin; }
|
|
|
|
|
|
|
|
enum ShadowReceivingCoarseBoundAccuracy {
|
|
|
|
EMPTY_BOX,
|
|
|
|
BOUNDING_SPHERE,
|
|
|
|
BOUNDING_BOX,
|
2008-10-07 20:31:42 +08:00
|
|
|
DEFAULT_ACCURACY = BOUNDING_BOX
|
2008-10-06 16:53:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void setShadowReceivingCoarseBoundAccuracy
|
|
|
|
( ShadowReceivingCoarseBoundAccuracy accuracy )
|
|
|
|
{ _shadowReceivingCoarseBoundAccuracy = accuracy; }
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
ShadowReceivingCoarseBoundAccuracy
|
2010-04-20 18:29:04 +08:00
|
|
|
getShadowReceivingCoarseBoundAccuracy() const
|
2008-10-06 16:53:54 +08:00
|
|
|
{ return _shadowReceivingCoarseBoundAccuracy; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/** Classic protected OSG destructor */
|
|
|
|
virtual ~MinimalShadowMap(void);
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
protected:
|
2008-10-06 16:53:54 +08:00
|
|
|
// Matrix modellingSpaceToWorld and its inverse
|
|
|
|
// are used to define Modelling Space where shadowed scene drawables
|
|
|
|
// have minimal (smallest possible extent) bounding boxes.
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
// Computing visible shadow range in this space
|
2008-10-06 16:53:54 +08:00
|
|
|
// allows for optimal use of ShadowMap resolution.
|
|
|
|
|
|
|
|
// By default it is set to identity ie computations are in world space.
|
2012-03-22 01:36:20 +08:00
|
|
|
// But it should be set to ElipsoidModel::localToWorld
|
2008-10-06 16:53:54 +08:00
|
|
|
// when scene objects are put on earth ellipsoid surface.
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
// Other scenarios are also possible for example when models are
|
2008-10-06 16:53:54 +08:00
|
|
|
// built in XZY space which would require identity matrix with swapped colums
|
|
|
|
|
|
|
|
osg::Matrix _modellingSpaceToWorld;
|
|
|
|
float _maxFarPlane;
|
2012-03-22 01:36:20 +08:00
|
|
|
float _minLightMargin;
|
|
|
|
ShadowReceivingCoarseBoundAccuracy _shadowReceivingCoarseBoundAccuracy;
|
2008-10-06 16:53:54 +08:00
|
|
|
|
2008-11-06 22:04:26 +08:00
|
|
|
struct OSGSHADOW_EXPORT ViewData: public BaseClass::ViewData
|
2008-10-06 16:53:54 +08:00
|
|
|
{
|
|
|
|
osg::Matrix *_modellingSpaceToWorldPtr;
|
|
|
|
float *_maxFarPlanePtr;
|
|
|
|
float *_minLightMarginPtr;
|
2010-11-24 18:09:04 +08:00
|
|
|
int _frameShadowCastingCameraPasses;
|
2008-10-06 16:53:54 +08:00
|
|
|
|
2008-10-06 22:18:27 +08:00
|
|
|
ConvexPolyhedron _sceneReceivingShadowPolytope;
|
2008-10-06 16:53:54 +08:00
|
|
|
std::vector< osg::Vec3d > _sceneReceivingShadowPolytopePoints;
|
|
|
|
|
2008-10-06 22:18:27 +08:00
|
|
|
osg::Matrixd _clampedProjection;
|
2008-10-06 16:53:54 +08:00
|
|
|
|
|
|
|
virtual void init( ThisClass * st, osgUtil::CullVisitor * cv );
|
|
|
|
|
|
|
|
virtual osg::BoundingBox computeShadowReceivingCoarseBounds( );
|
|
|
|
|
|
|
|
virtual void cullShadowReceivingScene( );
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
virtual void aimShadowCastingCamera(
|
2010-03-06 00:17:12 +08:00
|
|
|
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) );
|
|
|
|
|
2008-10-06 16:53:54 +08:00
|
|
|
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 );
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
void cutScenePolytope( const osg::Matrix & matrix,
|
2008-10-06 16:53:54 +08:00
|
|
|
const osg::Matrix & inverse,
|
2012-03-22 01:36:20 +08:00
|
|
|
const osg::BoundingBox &bb =
|
2008-10-06 16:53:54 +08:00
|
|
|
osg::BoundingBox(-1,-1,-1,1,1,1) );
|
|
|
|
|
2015-04-29 00:12:57 +08:00
|
|
|
osg::BoundingBox computeScenePolytopeBounds();
|
|
|
|
osg::BoundingBox computeScenePolytopeBounds(const osg::Matrix& m);
|
2008-10-06 16:53:54 +08:00
|
|
|
|
|
|
|
// 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
|
2008-10-06 22:18:27 +08:00
|
|
|
( osg::Matrixd & projection, osg::BoundingBox subrange,
|
2008-10-06 16:53:54 +08:00
|
|
|
unsigned int trimMask = (1|2|4|8|16|32)
|
|
|
|
/*1=left|2=right|4=bottom|8=top|16=near|32=far*/);
|
|
|
|
|
|
|
|
static void clampProjection
|
2008-10-06 22:18:27 +08:00
|
|
|
( osg::Matrixd & projection, float n = 0, float f = FLT_MAX );
|
2008-10-06 16:53:54 +08:00
|
|
|
|
|
|
|
static void extendProjection
|
2008-10-06 22:18:27 +08:00
|
|
|
( osg::Matrixd & projection, osg::Viewport * viewport, const osg::Vec2& margin );
|
2008-10-06 16:53:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
META_ViewDependentShadowTechniqueData( ThisClass, ThisClass::ViewData )
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace osgShadow
|
|
|
|
|
|
|
|
#endif
|