| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | #include <osgShadow/MinimalShadowMap> |
|---|
| 18 | #include <osgShadow/ConvexPolyhedron> |
|---|
| 19 | #include <osg/MatrixTransform> |
|---|
| 20 | #include <osgShadow/ShadowedScene> |
|---|
| 21 | #include <osg/ComputeBoundsVisitor> |
|---|
| 22 | |
|---|
| 23 | using namespace osgShadow; |
|---|
| 24 | |
|---|
| 25 | #define PRINT_SHADOW_TEXEL_TO_PIXEL_ERROR 0 |
|---|
| 26 | |
|---|
| 27 | MinimalShadowMap::MinimalShadowMap(): |
|---|
| 28 | BaseClass(), |
|---|
| 29 | _maxFarPlane( FLT_MAX ), |
|---|
| 30 | _minLightMargin( 0 ), |
|---|
| 31 | _shadowReceivingCoarseBoundAccuracy( BOUNDING_BOX ) |
|---|
| 32 | { |
|---|
| 33 | |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | MinimalShadowMap::MinimalShadowMap |
|---|
| 37 | (const MinimalShadowMap& copy, const osg::CopyOp& copyop) : |
|---|
| 38 | BaseClass(copy,copyop), |
|---|
| 39 | _maxFarPlane( copy._maxFarPlane ), |
|---|
| 40 | _minLightMargin( copy._minLightMargin ), |
|---|
| 41 | _shadowReceivingCoarseBoundAccuracy( copy._shadowReceivingCoarseBoundAccuracy ) |
|---|
| 42 | { |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | MinimalShadowMap::~MinimalShadowMap() |
|---|
| 46 | { |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | osg::BoundingBox MinimalShadowMap::ViewData::computeShadowReceivingCoarseBounds() |
|---|
| 50 | { |
|---|
| 51 | |
|---|
| 52 | ShadowReceivingCoarseBoundAccuracy accuracy = DEFAULT_ACCURACY; |
|---|
| 53 | |
|---|
| 54 | MinimalShadowMap * msm = dynamic_cast< MinimalShadowMap* >( _st.get() ); |
|---|
| 55 | if( msm ) accuracy = msm->getShadowReceivingCoarseBoundAccuracy(); |
|---|
| 56 | |
|---|
| 57 | if( accuracy == MinimalShadowMap::EMPTY_BOX ) |
|---|
| 58 | { |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | return osg::BoundingBox(); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | if( accuracy == MinimalShadowMap::BOUNDING_SPHERE ) |
|---|
| 71 | { |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | osg::Camera * camera = _cv->getRenderStage()->getCamera(); |
|---|
| 75 | osg::Matrix m = camera->getViewMatrix() * _clampedProjection; |
|---|
| 76 | |
|---|
| 77 | ConvexPolyhedron frustum; |
|---|
| 78 | frustum.setToUnitFrustum(); |
|---|
| 79 | frustum.transform( osg::Matrix::inverse( m ), m ); |
|---|
| 80 | |
|---|
| 81 | osg::BoundingSphere bs =_st->getShadowedScene()->getBound(); |
|---|
| 82 | osg::BoundingBox bb; |
|---|
| 83 | bb.expandBy( bs ); |
|---|
| 84 | osg::Polytope box; |
|---|
| 85 | box.setToBoundingBox( bb ); |
|---|
| 86 | |
|---|
| 87 | frustum.cut( box ); |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | box.transform( |
|---|
| 92 | osg::Matrix::translate( -bs.center() ) * |
|---|
| 93 | osg::Matrix::rotate( osg::PI_4, 0, 0, 1 ) * |
|---|
| 94 | osg::Matrix::rotate( osg::PI_4, 1, 1, 0 ) * |
|---|
| 95 | osg::Matrix::translate( bs.center() ) ); |
|---|
| 96 | frustum.cut( box ); |
|---|
| 97 | |
|---|
| 98 | return frustum.computeBoundingBox( ); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | if( accuracy == MinimalShadowMap::BOUNDING_BOX ) |
|---|
| 102 | { |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | osg::ComputeBoundsVisitor cbbv(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN); |
|---|
| 108 | cbbv.setTraversalMask(_st->getShadowedScene()->getCastsShadowTraversalMask()); |
|---|
| 109 | _st->getShadowedScene()->osg::Group::traverse(cbbv); |
|---|
| 110 | |
|---|
| 111 | return cbbv.getBoundingBox(); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | return osg::BoundingBox(); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | void MinimalShadowMap::ViewData::aimShadowCastingCamera |
|---|
| 118 | ( const osg::Light *light, const osg::Vec4 &lightPos, |
|---|
| 119 | const osg::Vec3 &lightDir, const osg::Vec3 &lightUp ) |
|---|
| 120 | { |
|---|
| 121 | osg::BoundingBox bb = computeScenePolytopeBounds(); |
|---|
| 122 | if( !bb.valid() ) { |
|---|
| 123 | bb.expandBy( osg::BoundingSphere( _cv->getEyePoint(), 1 ) ); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | osg::Vec3 up = lightUp; |
|---|
| 127 | |
|---|
| 128 | if( up.length2() <= 0 ) |
|---|
| 129 | { |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | #if 0 |
|---|
| 135 | osg::Matrix m = osg::Matrix::inverse( *cv.getModelViewMatrix() ); |
|---|
| 136 | osg::Vec3 camFw( -m( 2, 0 ), -m( 2, 1 ), -m( 2, 2 ) ); |
|---|
| 137 | camFw.normalize(); |
|---|
| 138 | |
|---|
| 139 | osg::Vec3 camUp( m( 1, 0 ), m( 1, 1 ), m( 1, 2 ) ); |
|---|
| 140 | camUp.normalize(); |
|---|
| 141 | |
|---|
| 142 | up = camUp * ( camFw * lightDir ) - camFw * ( camUp * lightDir ); |
|---|
| 143 | up.normalize(); |
|---|
| 144 | #else |
|---|
| 145 | osg::Matrix m = osg::Matrix::inverse( *_cv->getModelViewMatrix() ); |
|---|
| 146 | |
|---|
| 147 | up.set( -m( 2, 0 ), -m( 2, 1 ), -m( 2, 2 ) ); |
|---|
| 148 | #endif |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | BaseClass::ViewData::aimShadowCastingCamera |
|---|
| 152 | ( bb, light, lightPos, lightDir, up ); |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | osg::Matrix mvp = _camera->getViewMatrix() * _camera->getProjectionMatrix(); |
|---|
| 159 | cutScenePolytope( osg::Matrix::inverse( mvp ), mvp ); |
|---|
| 160 | |
|---|
| 161 | MinimalShadowMap::ViewData::frameShadowCastingCamera |
|---|
| 162 | ( _cv->getRenderStage()->getCamera(), _camera.get(), 0 ); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | void MinimalShadowMap::ViewData::frameShadowCastingCamera |
|---|
| 166 | ( const osg::Camera* cameraMain, osg::Camera* cameraShadow, int pass ) |
|---|
| 167 | { |
|---|
| 168 | osg::Matrix mvp = |
|---|
| 169 | cameraShadow->getViewMatrix() * cameraShadow->getProjectionMatrix(); |
|---|
| 170 | |
|---|
| 171 | ConvexPolyhedron polytope = _sceneReceivingShadowPolytope; |
|---|
| 172 | std::vector<osg::Vec3d> points = _sceneReceivingShadowPolytopePoints; |
|---|
| 173 | |
|---|
| 174 | osg::BoundingBox bb = computeScenePolytopeBounds( mvp ); |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | if( bb.valid() && *_minLightMarginPtr > 0 ) { |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | osg::Matrix transform = osg::Matrix::inverse( mvp ); |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | osg::Vec3d normal = |
|---|
| 188 | osg::Vec3d(0,0,-1) * transform - osg::Vec3d(0,0,1) * transform; |
|---|
| 189 | |
|---|
| 190 | normal.normalize(); |
|---|
| 191 | _sceneReceivingShadowPolytope.extrude( normal * *_minLightMarginPtr ); |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | if ( pass == 0 ) |
|---|
| 201 | { |
|---|
| 202 | osg::Polytope lightFrustum; |
|---|
| 203 | lightFrustum.setToUnitFrustum(); |
|---|
| 204 | lightFrustum.transformProvidingInverse( mvp ); |
|---|
| 205 | _sceneReceivingShadowPolytope.cut( lightFrustum ); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | _sceneReceivingShadowPolytopePoints.clear(); |
|---|
| 209 | _sceneReceivingShadowPolytope.getPoints |
|---|
| 210 | ( _sceneReceivingShadowPolytopePoints ); |
|---|
| 211 | |
|---|
| 212 | bb = computeScenePolytopeBounds( mvp ); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | setDebugPolytope( "extended", |
|---|
| 216 | _sceneReceivingShadowPolytope, osg::Vec4( 1, 0.5, 0, 1 ), osg::Vec4( 1, 0.5, 0, 0.1 ) ); |
|---|
| 217 | |
|---|
| 218 | _sceneReceivingShadowPolytope = polytope; |
|---|
| 219 | _sceneReceivingShadowPolytopePoints = points; |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | if( bb.valid() ) |
|---|
| 225 | trimProjection( cameraShadow->getProjectionMatrix(), bb, 1|2|4|8|16|32 ); |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | setDebugPolytope( "scene", _sceneReceivingShadowPolytope, osg::Vec4(0,1,0,1) ); |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | #if PRINT_SHADOW_TEXEL_TO_PIXEL_ERROR |
|---|
| 232 | if( pass == 1 ) |
|---|
| 233 | displayShadowTexelToPixelErrors |
|---|
| 234 | ( cameraMain, cameraShadow, &_sceneReceivingShadowPolytope ); |
|---|
| 235 | #endif |
|---|
| 236 | |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | void MinimalShadowMap::ViewData::cullShadowReceivingScene( ) |
|---|
| 240 | { |
|---|
| 241 | BaseClass::ViewData::cullShadowReceivingScene( ); |
|---|
| 242 | |
|---|
| 243 | _clampedProjection = *_cv->getProjectionMatrix(); |
|---|
| 244 | |
|---|
| 245 | if( _cv->getComputeNearFarMode() ) { |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | _cv->computeNearPlane(); |
|---|
| 256 | |
|---|
| 257 | osgUtil::CullVisitor::value_type n = _cv->getCalculatedNearPlane(); |
|---|
| 258 | osgUtil::CullVisitor::value_type f = _cv->getCalculatedFarPlane(); |
|---|
| 259 | |
|---|
| 260 | if( n < f ) |
|---|
| 261 | _cv->clampProjectionMatrix( _clampedProjection, n, f ); |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | if( 0 < *_maxFarPlanePtr ) |
|---|
| 267 | clampProjection( _clampedProjection, 0.f, *_maxFarPlanePtr ); |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | osg::BoundingBox bb = computeShadowReceivingCoarseBounds( ); |
|---|
| 271 | if( bb.valid() ) |
|---|
| 272 | _sceneReceivingShadowPolytope.setToBoundingBox( bb ); |
|---|
| 273 | else |
|---|
| 274 | _sceneReceivingShadowPolytope.clear(); |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | osg::Matrix mvp = *_cv->getModelViewMatrix() * _clampedProjection; |
|---|
| 282 | |
|---|
| 283 | cutScenePolytope( osg::Matrix::inverse( mvp ), mvp ); |
|---|
| 284 | |
|---|
| 285 | setDebugPolytope |
|---|
| 286 | ( "frustum", _sceneReceivingShadowPolytope, osg::Vec4(1,0,1,1)); |
|---|
| 287 | |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | void MinimalShadowMap::ViewData::init( ThisClass *st, osgUtil::CullVisitor *cv ) |
|---|
| 291 | { |
|---|
| 292 | BaseClass::ViewData::init( st, cv ); |
|---|
| 293 | |
|---|
| 294 | _modellingSpaceToWorldPtr = &st->_modellingSpaceToWorld; |
|---|
| 295 | _minLightMarginPtr = &st->_minLightMargin; |
|---|
| 296 | _maxFarPlanePtr = &st->_maxFarPlane; |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | void MinimalShadowMap::ViewData::cutScenePolytope |
|---|
| 300 | ( const osg::Matrix & transform, |
|---|
| 301 | const osg::Matrix & inverse, |
|---|
| 302 | const osg::BoundingBox & bb ) |
|---|
| 303 | { |
|---|
| 304 | _sceneReceivingShadowPolytopePoints.clear(); |
|---|
| 305 | |
|---|
| 306 | if( bb.valid() ) { |
|---|
| 307 | osg::Polytope polytope; |
|---|
| 308 | polytope.setToBoundingBox( bb ); |
|---|
| 309 | polytope.transformProvidingInverse( inverse ); |
|---|
| 310 | _sceneReceivingShadowPolytope.cut( polytope ); |
|---|
| 311 | _sceneReceivingShadowPolytope.getPoints |
|---|
| 312 | ( _sceneReceivingShadowPolytopePoints ); |
|---|
| 313 | } else |
|---|
| 314 | _sceneReceivingShadowPolytope.clear(); |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | osg::BoundingBox |
|---|
| 318 | MinimalShadowMap::ViewData::computeScenePolytopeBounds( const osg::Matrix & m ) |
|---|
| 319 | { |
|---|
| 320 | osg::BoundingBox bb; |
|---|
| 321 | |
|---|
| 322 | if( &m ) |
|---|
| 323 | for( unsigned i = 0; i < _sceneReceivingShadowPolytopePoints.size(); ++i ) |
|---|
| 324 | bb.expandBy( _sceneReceivingShadowPolytopePoints[i] * m ); |
|---|
| 325 | else |
|---|
| 326 | for( unsigned i = 0; i < _sceneReceivingShadowPolytopePoints.size(); ++i ) |
|---|
| 327 | bb.expandBy( _sceneReceivingShadowPolytopePoints[i] ); |
|---|
| 328 | |
|---|
| 329 | return bb; |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | void MinimalShadowMap::ViewData::trimProjection |
|---|
| 337 | ( osg::Matrixd & projectionMatrix, osg::BoundingBox bb, unsigned int trimMask ) |
|---|
| 338 | { |
|---|
| 339 | #if 1 |
|---|
| 340 | if( !bb.valid() || !( trimMask & (1|2|4|8|16|32) ) ) return; |
|---|
| 341 | double l = -1, r = 1, b = -1, t = 1, n = 1, f = -1; |
|---|
| 342 | |
|---|
| 343 | #if 0 |
|---|
| 344 | |
|---|
| 345 | for( int i = 0; i < 3; i ++ ) { |
|---|
| 346 | if( bb._min[i] < -1 ) bb._min[i] = -1; |
|---|
| 347 | if( bb._max[i] > 1 ) bb._max[i] = 1; |
|---|
| 348 | } |
|---|
| 349 | #endif |
|---|
| 350 | |
|---|
| 351 | if( trimMask & 1 ) l = bb._min[0]; |
|---|
| 352 | if( trimMask & 2 ) r = bb._max[0]; |
|---|
| 353 | if( trimMask & 4 ) b = bb._min[1]; |
|---|
| 354 | if( trimMask & 8 ) t = bb._max[1]; |
|---|
| 355 | if( trimMask & 16 ) n = -bb._min[2]; |
|---|
| 356 | if( trimMask & 32 ) f = -bb._max[2]; |
|---|
| 357 | |
|---|
| 358 | projectionMatrix.postMult( osg::Matrix::ortho( l,r,b,t,n,f ) ); |
|---|
| 359 | #else |
|---|
| 360 | if( !bb.valid() || !( trimMask & (1|2|4|8|16|32) ) ) return; |
|---|
| 361 | double l, r, t, b, n, f; |
|---|
| 362 | bool ortho = projectionMatrix.getOrtho( l, r, b, t, n, f ); |
|---|
| 363 | if( !ortho && !projectionMatrix.getFrustum( l, r, b, t, n, f ) ) |
|---|
| 364 | return; |
|---|
| 365 | |
|---|
| 366 | |
|---|
| 367 | for( int i = 0; i < 3; i ++ ) { |
|---|
| 368 | if( bb._min[i] < -1 ) bb._min[i] = -1; |
|---|
| 369 | if( bb._max[i] > 1 ) bb._max[i] = 1; |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | osg::Matrix projectionToView = osg::Matrix::inverse( projectionMatrix ); |
|---|
| 373 | |
|---|
| 374 | osg::Vec3 min = |
|---|
| 375 | osg::Vec3( bb._min[0], bb._min[1], bb._min[2] ) * projectionToView; |
|---|
| 376 | |
|---|
| 377 | osg::Vec3 max = |
|---|
| 378 | osg::Vec3( bb._max[0], bb._max[1], bb._max[2] ) * projectionToView; |
|---|
| 379 | |
|---|
| 380 | if( trimMask & 16 ) { |
|---|
| 381 | if( !ortho ) { |
|---|
| 382 | l *= -min[2] / n; |
|---|
| 383 | r *= -min[2] / n; |
|---|
| 384 | b *= -min[2] / n; |
|---|
| 385 | t *= -min[2] / n; |
|---|
| 386 | } |
|---|
| 387 | n = -min[2]; |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | if( trimMask & 32 ) |
|---|
| 391 | f = -max[2]; |
|---|
| 392 | |
|---|
| 393 | if( !ortho ) { |
|---|
| 394 | min[0] *= -n / min[2]; |
|---|
| 395 | min[1] *= -n / min[2]; |
|---|
| 396 | max[0] *= -n / max[2]; |
|---|
| 397 | max[1] *= -n / max[2]; |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | if( l < r ) { |
|---|
| 401 | if( l < min[0] && ( trimMask & 1 ) ) l = min[0]; |
|---|
| 402 | if( r > max[0] && ( trimMask & 2 ) ) r = max[0]; |
|---|
| 403 | } else { |
|---|
| 404 | if( l > min[0] && ( trimMask & 1 ) ) l = min[0]; |
|---|
| 405 | if( r < max[0] && ( trimMask & 2 ) ) r = max[0]; |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | if( b < t ) { |
|---|
| 409 | if( b < min[1] && ( trimMask & 4 ) ) b = min[1]; |
|---|
| 410 | if( t > max[1] && ( trimMask & 8 ) ) t = max[1]; |
|---|
| 411 | } else { |
|---|
| 412 | if( b > min[1] && ( trimMask & 4 ) ) b = min[1]; |
|---|
| 413 | if( t < max[1] && ( trimMask & 8 ) ) t = max[1]; |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | if( ortho ) |
|---|
| 417 | projectionMatrix.makeOrtho( l, r, b, t, n, f ); |
|---|
| 418 | else |
|---|
| 419 | projectionMatrix.makeFrustum( l, r, b, t, n, f ); |
|---|
| 420 | #endif |
|---|
| 421 | } |
|---|
| 422 | |
|---|
| 423 | void MinimalShadowMap::ViewData::clampProjection |
|---|
| 424 | ( osg::Matrixd & projection, float new_near, float new_far ) |
|---|
| 425 | { |
|---|
| 426 | double r, l, t, b, n, f; |
|---|
| 427 | bool perspective = projection.getFrustum( l, r, b, t, n, f ); |
|---|
| 428 | if( !perspective && !projection.getOrtho( l, r, b, t, n, f ) ) |
|---|
| 429 | { |
|---|
| 430 | |
|---|
| 431 | osg::notify( osg::WARN ) |
|---|
| 432 | << "MinimalShadowMap::clampProjectionFarPlane failed - non standard matrix" |
|---|
| 433 | << std::endl; |
|---|
| 434 | |
|---|
| 435 | } else if( n < new_near || new_far < f ) { |
|---|
| 436 | |
|---|
| 437 | if( n < new_near && new_near < f ) { |
|---|
| 438 | if( perspective ) { |
|---|
| 439 | l *= new_near / n; |
|---|
| 440 | r *= new_near / n; |
|---|
| 441 | b *= new_near / n; |
|---|
| 442 | t *= new_near / n; |
|---|
| 443 | } |
|---|
| 444 | n = new_near; |
|---|
| 445 | } |
|---|
| 446 | |
|---|
| 447 | if( n < new_far && new_far < f ) { |
|---|
| 448 | f = new_far; |
|---|
| 449 | } |
|---|
| 450 | |
|---|
| 451 | if( perspective ) |
|---|
| 452 | projection.makeFrustum( l, r, b, t, n, f ); |
|---|
| 453 | else |
|---|
| 454 | projection.makeOrtho( l, r, b, t, n, f ); |
|---|
| 455 | } |
|---|
| 456 | } |
|---|
| 457 | |
|---|
| 458 | |
|---|
| 459 | |
|---|
| 460 | |
|---|
| 461 | |
|---|
| 462 | |
|---|
| 463 | |
|---|
| 464 | |
|---|
| 465 | void MinimalShadowMap::ViewData::extendProjection |
|---|
| 466 | ( osg::Matrixd & projection, osg::Viewport * viewport, const osg::Vec2& margin ) |
|---|
| 467 | { |
|---|
| 468 | double l,r,b,t,n,f; |
|---|
| 469 | |
|---|
| 470 | |
|---|
| 471 | |
|---|
| 472 | bool frustum = projection.getFrustum( l,r,b,t,n,f ); |
|---|
| 473 | |
|---|
| 474 | if( !frustum && !projection.getOrtho( l,r,b,t,n,f ) ) { |
|---|
| 475 | osg::notify( osg::WARN ) |
|---|
| 476 | << " Awkward projection matrix. ComputeExtendedProjection failed" |
|---|
| 477 | << std::endl; |
|---|
| 478 | return; |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | osg::Matrix window = viewport->computeWindowMatrix(); |
|---|
| 482 | |
|---|
| 483 | osg::Vec3 vMin( viewport->x() - margin.x(), |
|---|
| 484 | viewport->y() - margin.y(), |
|---|
| 485 | 0.0 ); |
|---|
| 486 | |
|---|
| 487 | osg::Vec3 vMax( viewport->width() + margin.x() * 2 + vMin.x(), |
|---|
| 488 | viewport->height() + margin.y() * 2 + vMin.y(), |
|---|
| 489 | 0.0 ); |
|---|
| 490 | |
|---|
| 491 | osg::Matrix inversePW = osg::Matrix::inverse( projection * window ); |
|---|
| 492 | |
|---|
| 493 | vMin = vMin * inversePW; |
|---|
| 494 | vMax = vMax * inversePW; |
|---|
| 495 | |
|---|
| 496 | l = vMin.x(); |
|---|
| 497 | r = vMax.x(); |
|---|
| 498 | b = vMin.y(); |
|---|
| 499 | t = vMax.y(); |
|---|
| 500 | |
|---|
| 501 | if( frustum ) |
|---|
| 502 | projection.makeFrustum( l,r,b,t,n,f ); |
|---|
| 503 | else |
|---|
| 504 | projection.makeOrtho( l,r,b,t,n,f ); |
|---|
| 505 | } |
|---|