| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osgShadow/ViewDependentShadowMap> |
|---|
| 15 | #include <osgShadow/ShadowedScene> |
|---|
| 16 | #include <osg/CullFace> |
|---|
| 17 | #include <osg/Geode> |
|---|
| 18 | #include <osg/io_utils> |
|---|
| 19 | |
|---|
| 20 | #include <sstream> |
|---|
| 21 | |
|---|
| 22 | using namespace osgShadow; |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | #if 0 |
|---|
| 28 | static const char fragmentShaderSource_withBaseTexture[] = |
|---|
| 29 | "uniform sampler2D baseTexture; \n" |
|---|
| 30 | "uniform sampler2DShadow shadowTexture; \n" |
|---|
| 31 | " \n" |
|---|
| 32 | "void main(void) \n" |
|---|
| 33 | "{ \n" |
|---|
| 34 | " vec4 colorAmbientEmissive = gl_FrontLightModelProduct.sceneColor; \n" |
|---|
| 35 | " vec4 color = texture2D( baseTexture, gl_TexCoord[0].xy ); \n" |
|---|
| 36 | " color *= mix( colorAmbientEmissive, gl_Color, shadow2DProj( shadowTexture, gl_TexCoord[1] ).r ); \n" |
|---|
| 37 | " gl_FragColor = color; \n" |
|---|
| 38 | "} \n"; |
|---|
| 39 | #else |
|---|
| 40 | static const char fragmentShaderSource_withBaseTexture[] = |
|---|
| 41 | "uniform sampler2D baseTexture; \n" |
|---|
| 42 | "uniform int baseTextureUnit; \n" |
|---|
| 43 | "uniform sampler2DShadow shadowTexture0; \n" |
|---|
| 44 | "uniform int shadowTextureUnit0; \n" |
|---|
| 45 | " \n" |
|---|
| 46 | "void main(void) \n" |
|---|
| 47 | "{ \n" |
|---|
| 48 | " vec4 colorAmbientEmissive = gl_FrontLightModelProduct.sceneColor; \n" |
|---|
| 49 | " vec4 color = texture2D( baseTexture, gl_TexCoord[baseTextureUnit].xy ); \n" |
|---|
| 50 | " color *= mix( colorAmbientEmissive, gl_Color, shadow2DProj( shadowTexture0, gl_TexCoord[shadowTextureUnit0] ).r ); \n" |
|---|
| 51 | " gl_FragColor = color; \n" |
|---|
| 52 | "} \n"; |
|---|
| 53 | |
|---|
| 54 | static const char fragmentShaderSource_withBaseTexture_twoShadowMaps[] = |
|---|
| 55 | "uniform sampler2D baseTexture; \n" |
|---|
| 56 | "uniform int baseTextureUnit; \n" |
|---|
| 57 | "uniform sampler2DShadow shadowTexture0; \n" |
|---|
| 58 | "uniform int shadowTextureUnit0; \n" |
|---|
| 59 | "uniform sampler2DShadow shadowTexture1; \n" |
|---|
| 60 | "uniform int shadowTextureUnit1; \n" |
|---|
| 61 | " \n" |
|---|
| 62 | "void main(void) \n" |
|---|
| 63 | "{ \n" |
|---|
| 64 | " vec4 colorAmbientEmissive = gl_FrontLightModelProduct.sceneColor; \n" |
|---|
| 65 | " vec4 color = texture2D( baseTexture, gl_TexCoord[baseTextureUnit].xy ); \n" |
|---|
| 66 | " float shadow0 = shadow2DProj( shadowTexture0, gl_TexCoord[shadowTextureUnit0] ).r; \n" |
|---|
| 67 | " float shadow1 = shadow2DProj( shadowTexture1, gl_TexCoord[shadowTextureUnit1] ).r; \n" |
|---|
| 68 | " color *= mix( colorAmbientEmissive, gl_Color, shadow0*shadow1 ); \n" |
|---|
| 69 | " gl_FragColor = color; \n" |
|---|
| 70 | "} \n"; |
|---|
| 71 | #endif |
|---|
| 72 | |
|---|
| 73 | template<class T> |
|---|
| 74 | class RenderLeafTraverser : public T |
|---|
| 75 | { |
|---|
| 76 | public: |
|---|
| 77 | |
|---|
| 78 | RenderLeafTraverser() |
|---|
| 79 | { |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | void traverse(const osgUtil::RenderStage* rs) |
|---|
| 83 | { |
|---|
| 84 | traverse(static_cast<const osgUtil::RenderBin*>(rs)); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | void traverse(const osgUtil::RenderBin* renderBin) |
|---|
| 88 | { |
|---|
| 89 | const osgUtil::RenderBin::RenderBinList& rbl = renderBin->getRenderBinList(); |
|---|
| 90 | for(osgUtil::RenderBin::RenderBinList::const_iterator itr = rbl.begin(); |
|---|
| 91 | itr != rbl.end(); |
|---|
| 92 | ++itr) |
|---|
| 93 | { |
|---|
| 94 | traverse(itr->second.get()); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | const osgUtil::RenderBin::RenderLeafList& rll = renderBin->getRenderLeafList(); |
|---|
| 98 | for(osgUtil::RenderBin::RenderLeafList::const_iterator itr = rll.begin(); |
|---|
| 99 | itr != rll.end(); |
|---|
| 100 | ++itr) |
|---|
| 101 | { |
|---|
| 102 | handle(*itr); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | const osgUtil::RenderBin::StateGraphList& rgl = renderBin->getStateGraphList(); |
|---|
| 106 | for(osgUtil::RenderBin::StateGraphList::const_iterator itr = rgl.begin(); |
|---|
| 107 | itr != rgl.end(); |
|---|
| 108 | ++itr) |
|---|
| 109 | { |
|---|
| 110 | traverse(*itr); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | void traverse(const osgUtil::StateGraph* stateGraph) |
|---|
| 116 | { |
|---|
| 117 | const osgUtil::StateGraph::ChildList& cl = stateGraph->_children; |
|---|
| 118 | for(osgUtil::StateGraph::ChildList::const_iterator itr = cl.begin(); |
|---|
| 119 | itr != cl.end(); |
|---|
| 120 | ++itr) |
|---|
| 121 | { |
|---|
| 122 | traverse(itr->second.get()); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | const osgUtil::StateGraph::LeafList& ll = stateGraph->_leaves; |
|---|
| 126 | for(osgUtil::StateGraph::LeafList::const_iterator itr = ll.begin(); |
|---|
| 127 | itr != ll.end(); |
|---|
| 128 | ++itr) |
|---|
| 129 | { |
|---|
| 130 | handle(itr->get()); |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | inline void handle(const osgUtil::RenderLeaf* renderLeaf) |
|---|
| 135 | { |
|---|
| 136 | this->operator()(renderLeaf); |
|---|
| 137 | } |
|---|
| 138 | }; |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | class VDSMCameraCullCallback : public osg::NodeCallback |
|---|
| 145 | { |
|---|
| 146 | public: |
|---|
| 147 | |
|---|
| 148 | VDSMCameraCullCallback(ViewDependentShadowMap* vdsm, osg::Polytope& polytope); |
|---|
| 149 | |
|---|
| 150 | virtual void operator()(osg::Node*, osg::NodeVisitor* nv); |
|---|
| 151 | |
|---|
| 152 | osg::RefMatrix* getProjectionMatrix() { return _projectionMatrix.get(); } |
|---|
| 153 | osgUtil::RenderStage* getRenderStage() { return _renderStage.get(); } |
|---|
| 154 | |
|---|
| 155 | protected: |
|---|
| 156 | |
|---|
| 157 | ViewDependentShadowMap* _vdsm; |
|---|
| 158 | osg::ref_ptr<osg::RefMatrix> _projectionMatrix; |
|---|
| 159 | osg::ref_ptr<osgUtil::RenderStage> _renderStage; |
|---|
| 160 | osg::Polytope _polytope; |
|---|
| 161 | }; |
|---|
| 162 | |
|---|
| 163 | VDSMCameraCullCallback::VDSMCameraCullCallback(ViewDependentShadowMap* vdsm, osg::Polytope& polytope): |
|---|
| 164 | _vdsm(vdsm), |
|---|
| 165 | _polytope(polytope) |
|---|
| 166 | { |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | void VDSMCameraCullCallback::operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 170 | { |
|---|
| 171 | osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv); |
|---|
| 172 | osg::Camera* camera = dynamic_cast<osg::Camera*>(node); |
|---|
| 173 | OSG_INFO<<"VDSMCameraCullCallback::operator()(osg::Node* "<<camera<<", osg::NodeVisitor* "<<cv<<")"<<std::endl; |
|---|
| 174 | |
|---|
| 175 | #if 1 |
|---|
| 176 | if (!_polytope.empty()) |
|---|
| 177 | { |
|---|
| 178 | OSG_INFO<<"Pushing custom Polytope"<<std::endl; |
|---|
| 179 | |
|---|
| 180 | osg::CullingSet& cs = cv->getProjectionCullingStack().back(); |
|---|
| 181 | |
|---|
| 182 | cs.setFrustum(_polytope); |
|---|
| 183 | |
|---|
| 184 | cv->pushCullingSet(); |
|---|
| 185 | } |
|---|
| 186 | #endif |
|---|
| 187 | if (_vdsm->getShadowedScene()) |
|---|
| 188 | { |
|---|
| 189 | _vdsm->getShadowedScene()->osg::Group::traverse(*nv); |
|---|
| 190 | } |
|---|
| 191 | #if 1 |
|---|
| 192 | if (!_polytope.empty()) |
|---|
| 193 | { |
|---|
| 194 | OSG_INFO<<"Popping custom Polytope"<<std::endl; |
|---|
| 195 | cv->popCullingSet(); |
|---|
| 196 | } |
|---|
| 197 | #endif |
|---|
| 198 | |
|---|
| 199 | _renderStage = cv->getCurrentRenderBin()->getStage(); |
|---|
| 200 | |
|---|
| 201 | OSG_INFO<<"VDSM second : _renderStage = "<<_renderStage<<std::endl; |
|---|
| 202 | |
|---|
| 203 | if (cv->getComputeNearFarMode() != osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR) |
|---|
| 204 | { |
|---|
| 205 | |
|---|
| 206 | cv->computeNearPlane(); |
|---|
| 207 | |
|---|
| 208 | osg::Matrixd projection = *(cv->getProjectionMatrix()); |
|---|
| 209 | |
|---|
| 210 | OSG_INFO<<"RTT Projection matrix "<<projection<<std::endl; |
|---|
| 211 | |
|---|
| 212 | osg::Matrix::value_type left, right, bottom, top, zNear, zFar; |
|---|
| 213 | osg::Matrix::value_type epsilon = 1e-6; |
|---|
| 214 | if (fabs(projection(0,3))<epsilon && fabs(projection(1,3))<epsilon && fabs(projection(2,3))<epsilon ) |
|---|
| 215 | { |
|---|
| 216 | projection.getOrtho(left, right, |
|---|
| 217 | bottom, top, |
|---|
| 218 | zNear, zFar); |
|---|
| 219 | |
|---|
| 220 | OSG_INFO<<"Ortho zNear="<<zNear<<", zFar="<<zFar<<std::endl; |
|---|
| 221 | } |
|---|
| 222 | else |
|---|
| 223 | { |
|---|
| 224 | projection.getFrustum(left, right, |
|---|
| 225 | bottom, top, |
|---|
| 226 | zNear, zFar); |
|---|
| 227 | |
|---|
| 228 | OSG_INFO<<"Frustum zNear="<<zNear<<", zFar="<<zFar<<std::endl; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | OSG_INFO<<"Calculated zNear = "<<cv->getCalculatedNearPlane()<<", zFar = "<<cv->getCalculatedFarPlane()<<std::endl; |
|---|
| 232 | |
|---|
| 233 | zNear = osg::maximum(zNear, cv->getCalculatedNearPlane()); |
|---|
| 234 | zFar = osg::minimum(zFar, cv->getCalculatedFarPlane()); |
|---|
| 235 | |
|---|
| 236 | cv->setCalculatedNearPlane(zNear); |
|---|
| 237 | cv->setCalculatedFarPlane(zFar); |
|---|
| 238 | |
|---|
| 239 | cv->clampProjectionMatrix(projection, zNear, zFar); |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | OSG_INFO<<"RTT Projection matrix after clamping "<<projection<<std::endl; |
|---|
| 243 | |
|---|
| 244 | camera->setProjectionMatrix(projection); |
|---|
| 245 | |
|---|
| 246 | _projectionMatrix = cv->getProjectionMatrix(); |
|---|
| 247 | } |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | class ComputeLightSpaceBounds : public osg::NodeVisitor, public osg::CullStack |
|---|
| 252 | { |
|---|
| 253 | public: |
|---|
| 254 | ComputeLightSpaceBounds(osg::Viewport* viewport, const osg::Matrixd& projectionMatrix, osg::Matrixd& viewMatrix): |
|---|
| 255 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN) |
|---|
| 256 | { |
|---|
| 257 | setCullingMode(osg::CullSettings::VIEW_FRUSTUM_CULLING); |
|---|
| 258 | |
|---|
| 259 | pushViewport(viewport); |
|---|
| 260 | pushProjectionMatrix(new osg::RefMatrix(projectionMatrix)); |
|---|
| 261 | pushModelViewMatrix(new osg::RefMatrix(viewMatrix),osg::Transform::ABSOLUTE_RF); |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | void apply(osg::Node& node) |
|---|
| 265 | { |
|---|
| 266 | if (isCulled(node)) return; |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | pushCurrentMask(); |
|---|
| 270 | |
|---|
| 271 | traverse(node); |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | popCurrentMask(); |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | void apply(osg::Geode& node) |
|---|
| 278 | { |
|---|
| 279 | if (isCulled(node)) return; |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | pushCurrentMask(); |
|---|
| 283 | |
|---|
| 284 | for(unsigned int i=0; i<node.getNumDrawables();++i) |
|---|
| 285 | { |
|---|
| 286 | if (node.getDrawable(i)) |
|---|
| 287 | { |
|---|
| 288 | updateBound(node.getDrawable(i)->getBound()); |
|---|
| 289 | } |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | popCurrentMask(); |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | void apply(osg::Billboard&) |
|---|
| 297 | { |
|---|
| 298 | OSG_INFO<<"Warning Billboards not yet supported"<<std::endl; |
|---|
| 299 | return; |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | void apply(osg::Projection&) |
|---|
| 303 | { |
|---|
| 304 | |
|---|
| 305 | return; |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | void apply(osg::Transform& transform) |
|---|
| 309 | { |
|---|
| 310 | if (isCulled(transform)) return; |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | pushCurrentMask(); |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | if (transform.getReferenceFrame()==osg::Transform::RELATIVE_RF) |
|---|
| 317 | { |
|---|
| 318 | osg::ref_ptr<osg::RefMatrix> matrix = new osg::RefMatrix(*getModelViewMatrix()); |
|---|
| 319 | transform.computeLocalToWorldMatrix(*matrix,this); |
|---|
| 320 | pushModelViewMatrix(matrix.get(), transform.getReferenceFrame()); |
|---|
| 321 | |
|---|
| 322 | traverse(transform); |
|---|
| 323 | |
|---|
| 324 | popModelViewMatrix(); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | popCurrentMask(); |
|---|
| 329 | |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | void apply(osg::Camera&) |
|---|
| 333 | { |
|---|
| 334 | |
|---|
| 335 | return; |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | void updateBound(const osg::BoundingBox& bb) |
|---|
| 339 | { |
|---|
| 340 | if (!bb.valid()) return; |
|---|
| 341 | |
|---|
| 342 | const osg::Matrix& matrix = *getModelViewMatrix() * *getProjectionMatrix(); |
|---|
| 343 | |
|---|
| 344 | update(bb.corner(0) * matrix); |
|---|
| 345 | update(bb.corner(1) * matrix); |
|---|
| 346 | update(bb.corner(2) * matrix); |
|---|
| 347 | update(bb.corner(3) * matrix); |
|---|
| 348 | update(bb.corner(4) * matrix); |
|---|
| 349 | update(bb.corner(5) * matrix); |
|---|
| 350 | update(bb.corner(6) * matrix); |
|---|
| 351 | update(bb.corner(7) * matrix); |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | void update(const osg::Vec3& v) |
|---|
| 355 | { |
|---|
| 356 | if (v.z()<-1.0f) |
|---|
| 357 | { |
|---|
| 358 | |
|---|
| 359 | return; |
|---|
| 360 | } |
|---|
| 361 | float x = v.x(); |
|---|
| 362 | if (x<-1.0f) x=-1.0f; |
|---|
| 363 | if (x>1.0f) x=1.0f; |
|---|
| 364 | float y = v.y(); |
|---|
| 365 | if (y<-1.0f) y=-1.0f; |
|---|
| 366 | if (y>1.0f) y=1.0f; |
|---|
| 367 | _bb.expandBy(osg::Vec3(x,y,v.z())); |
|---|
| 368 | } |
|---|
| 369 | |
|---|
| 370 | osg::BoundingBox _bb; |
|---|
| 371 | }; |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | ViewDependentShadowMap::LightData::LightData(ViewDependentShadowMap::ViewDependentData* vdd): |
|---|
| 378 | _viewDependentData(vdd), |
|---|
| 379 | directionalLight(false) |
|---|
| 380 | { |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | void ViewDependentShadowMap::LightData::setLightData(osg::RefMatrix* lm, const osg::Light* l, const osg::Matrixd& modelViewMatrix) |
|---|
| 384 | { |
|---|
| 385 | lightMatrix = lm; |
|---|
| 386 | light = l; |
|---|
| 387 | |
|---|
| 388 | lightPos = light->getPosition(); |
|---|
| 389 | directionalLight = (light->getPosition().w()== 0.0); |
|---|
| 390 | if (directionalLight) |
|---|
| 391 | { |
|---|
| 392 | lightPos3.set(0.0, 0.0, 0.0); |
|---|
| 393 | lightDir.set(-lightPos.x(), -lightPos.y(), -lightPos.z()); |
|---|
| 394 | lightDir.normalize(); |
|---|
| 395 | OSG_INFO<<" Directional light, lightPos="<<lightPos<<", lightDir="<<lightDir<<std::endl; |
|---|
| 396 | if (lightMatrix.valid()) |
|---|
| 397 | { |
|---|
| 398 | OSG_INFO<<" Light matrix "<<*lightMatrix<<std::endl; |
|---|
| 399 | osg::Matrix lightToLocalMatrix(*lightMatrix * osg::Matrix::inverse(modelViewMatrix) ); |
|---|
| 400 | lightDir = osg::Matrix::transform3x3( lightDir, lightToLocalMatrix ); |
|---|
| 401 | lightDir.normalize(); |
|---|
| 402 | OSG_INFO<<" new LightDir ="<<lightDir<<std::endl; |
|---|
| 403 | } |
|---|
| 404 | } |
|---|
| 405 | else |
|---|
| 406 | { |
|---|
| 407 | OSG_INFO<<" Positional light, lightPos="<<lightPos<<std::endl; |
|---|
| 408 | lightDir = light->getDirection(); |
|---|
| 409 | lightDir.normalize(); |
|---|
| 410 | if (lightMatrix.valid()) |
|---|
| 411 | { |
|---|
| 412 | OSG_INFO<<" Light matrix "<<*lightMatrix<<std::endl; |
|---|
| 413 | osg::Matrix lightToLocalMatrix(*lightMatrix * osg::Matrix::inverse(modelViewMatrix) ); |
|---|
| 414 | lightPos = lightPos * lightToLocalMatrix; |
|---|
| 415 | lightDir = osg::Matrix::transform3x3( lightDir, lightToLocalMatrix ); |
|---|
| 416 | lightDir.normalize(); |
|---|
| 417 | OSG_INFO<<" new LightPos ="<<lightPos<<std::endl; |
|---|
| 418 | OSG_INFO<<" new LightDir ="<<lightDir<<std::endl; |
|---|
| 419 | } |
|---|
| 420 | lightPos3.set(lightPos.x()/lightPos.w(), lightPos.y()/lightPos.w(), lightPos.z()/lightPos.w()); |
|---|
| 421 | } |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | |
|---|
| 428 | ViewDependentShadowMap::ShadowData::ShadowData(ViewDependentShadowMap::ViewDependentData* vdd): |
|---|
| 429 | _viewDependentData(vdd), |
|---|
| 430 | _textureUnit(0) |
|---|
| 431 | { |
|---|
| 432 | |
|---|
| 433 | const ShadowSettings* settings = vdd->getViewDependentShadowMap()->getShadowedScene()->getShadowSettings(); |
|---|
| 434 | |
|---|
| 435 | bool debug = settings->getDebugDraw(); |
|---|
| 436 | |
|---|
| 437 | |
|---|
| 438 | _texgen = new osg::TexGen; |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | _texture = new osg::Texture2D; |
|---|
| 442 | |
|---|
| 443 | osg::Vec2s textureSize = debug ? osg::Vec2s(512,512) : settings->getTextureSize(); |
|---|
| 444 | _texture->setTextureSize(textureSize.x(), textureSize.y()); |
|---|
| 445 | |
|---|
| 446 | if (debug) |
|---|
| 447 | { |
|---|
| 448 | _texture->setInternalFormat(GL_RGB); |
|---|
| 449 | } |
|---|
| 450 | else |
|---|
| 451 | { |
|---|
| 452 | _texture->setInternalFormat(GL_DEPTH_COMPONENT); |
|---|
| 453 | _texture->setShadowComparison(true); |
|---|
| 454 | _texture->setShadowTextureMode(osg::Texture2D::LUMINANCE); |
|---|
| 455 | } |
|---|
| 456 | |
|---|
| 457 | _texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR); |
|---|
| 458 | _texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR); |
|---|
| 459 | |
|---|
| 460 | |
|---|
| 461 | _texture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP_TO_BORDER); |
|---|
| 462 | _texture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP_TO_BORDER); |
|---|
| 463 | _texture->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 464 | |
|---|
| 465 | |
|---|
| 466 | |
|---|
| 467 | _camera = new osg::Camera; |
|---|
| 468 | _camera->setName("ShadowCamera"); |
|---|
| 469 | _camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF_INHERIT_VIEWPOINT); |
|---|
| 470 | |
|---|
| 471 | |
|---|
| 472 | _camera->setClearColor(osg::Vec4(0.0f,0.0f,0.0f,0.0f)); |
|---|
| 473 | |
|---|
| 474 | _camera->setComputeNearFarMode(osg::Camera::COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES); |
|---|
| 475 | |
|---|
| 476 | |
|---|
| 477 | |
|---|
| 478 | _camera->setCullingMode(_camera->getCullingMode() & ~osg::CullSettings::SMALL_FEATURE_CULLING); |
|---|
| 479 | |
|---|
| 480 | |
|---|
| 481 | _camera->setViewport(0,0,textureSize.x(),textureSize.y()); |
|---|
| 482 | |
|---|
| 483 | |
|---|
| 484 | if (debug) |
|---|
| 485 | { |
|---|
| 486 | |
|---|
| 487 | _camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | _camera->setRenderOrder(osg::Camera::POST_RENDER); |
|---|
| 491 | |
|---|
| 492 | |
|---|
| 493 | |
|---|
| 494 | _camera->attach(osg::Camera::COLOR_BUFFER, _texture.get()); |
|---|
| 495 | } |
|---|
| 496 | else |
|---|
| 497 | { |
|---|
| 498 | |
|---|
| 499 | _camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); |
|---|
| 500 | |
|---|
| 501 | |
|---|
| 502 | _camera->setRenderOrder(osg::Camera::PRE_RENDER); |
|---|
| 503 | |
|---|
| 504 | |
|---|
| 505 | _camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); |
|---|
| 506 | |
|---|
| 507 | |
|---|
| 508 | _camera->attach(osg::Camera::DEPTH_BUFFER, _texture.get()); |
|---|
| 509 | |
|---|
| 510 | } |
|---|
| 511 | } |
|---|
| 512 | |
|---|
| 513 | void ViewDependentShadowMap::ShadowData::releaseGLObjects(osg::State* state) const |
|---|
| 514 | { |
|---|
| 515 | OSG_INFO<<"ViewDependentShadowMap::ShadowData::releaseGLObjects"<<std::endl; |
|---|
| 516 | _texture->releaseGLObjects(state); |
|---|
| 517 | _camera->releaseGLObjects(state); |
|---|
| 518 | } |
|---|
| 519 | |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | |
|---|
| 523 | |
|---|
| 524 | ViewDependentShadowMap::Frustum::Frustum(osgUtil::CullVisitor* cv, double minZNear, double maxZFar): |
|---|
| 525 | corners(8), |
|---|
| 526 | faces(6), |
|---|
| 527 | edges(12) |
|---|
| 528 | { |
|---|
| 529 | projectionMatrix = *(cv->getProjectionMatrix()); |
|---|
| 530 | modelViewMatrix = *(cv->getModelViewMatrix()); |
|---|
| 531 | |
|---|
| 532 | OSG_INFO<<"Projection matrix "<<projectionMatrix<<std::endl; |
|---|
| 533 | |
|---|
| 534 | if (cv->getComputeNearFarMode()!=osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR) |
|---|
| 535 | { |
|---|
| 536 | osg::Matrix::value_type zNear = osg::maximum<osg::Matrix::value_type>(cv->getCalculatedNearPlane(),minZNear); |
|---|
| 537 | osg::Matrix::value_type zFar = osg::minimum<osg::Matrix::value_type>(cv->getCalculatedFarPlane(),maxZFar); |
|---|
| 538 | |
|---|
| 539 | cv->clampProjectionMatrix(projectionMatrix, zNear, zFar); |
|---|
| 540 | |
|---|
| 541 | OSG_INFO<<"zNear = "<<zNear<<", zFar = "<<zFar<<std::endl; |
|---|
| 542 | OSG_INFO<<"Projection matrix after clamping "<<projectionMatrix<<std::endl; |
|---|
| 543 | } |
|---|
| 544 | |
|---|
| 545 | corners[0].set(-1.0,-1.0,-1.0); |
|---|
| 546 | corners[1].set(1.0,-1.0,-1.0); |
|---|
| 547 | corners[2].set(1.0,-1.0,1.0); |
|---|
| 548 | corners[3].set(-1.0,-1.0,1.0); |
|---|
| 549 | corners[4].set(-1.0,1.0,-1.0); |
|---|
| 550 | corners[5].set(1.0,1.0,-1.0); |
|---|
| 551 | corners[6].set(1.0,1.0,1.0); |
|---|
| 552 | corners[7].set(-1.0,1.0,1.0); |
|---|
| 553 | |
|---|
| 554 | osg::Matrixd clipToWorld; |
|---|
| 555 | clipToWorld.invert(modelViewMatrix * projectionMatrix); |
|---|
| 556 | |
|---|
| 557 | |
|---|
| 558 | for(Vertices::iterator itr = corners.begin(); |
|---|
| 559 | itr != corners.end(); |
|---|
| 560 | ++itr) |
|---|
| 561 | { |
|---|
| 562 | *itr = (*itr) * clipToWorld; |
|---|
| 563 | |
|---|
| 564 | OSG_INFO<<" corner "<<*itr<<std::endl; |
|---|
| 565 | } |
|---|
| 566 | |
|---|
| 567 | |
|---|
| 568 | eye = osg::Vec3d(0.0,0.0,0.0) * osg::Matrix::inverse(modelViewMatrix); |
|---|
| 569 | |
|---|
| 570 | |
|---|
| 571 | centerNearPlane = (corners[0]+corners[1]+corners[5]+corners[4])*0.25; |
|---|
| 572 | centerFarPlane = (corners[3]+corners[2]+corners[6]+corners[7])*0.25; |
|---|
| 573 | center = (centerNearPlane+centerFarPlane)*0.5; |
|---|
| 574 | frustumCenterLine = centerFarPlane-centerNearPlane; |
|---|
| 575 | frustumCenterLine.normalize(); |
|---|
| 576 | |
|---|
| 577 | OSG_INFO<<" center "<<center<<std::endl; |
|---|
| 578 | |
|---|
| 579 | faces[0].push_back(0); |
|---|
| 580 | faces[0].push_back(3); |
|---|
| 581 | faces[0].push_back(7); |
|---|
| 582 | faces[0].push_back(4); |
|---|
| 583 | |
|---|
| 584 | faces[1].push_back(1); |
|---|
| 585 | faces[1].push_back(5); |
|---|
| 586 | faces[1].push_back(6); |
|---|
| 587 | faces[1].push_back(2); |
|---|
| 588 | |
|---|
| 589 | faces[2].push_back(0); |
|---|
| 590 | faces[2].push_back(1); |
|---|
| 591 | faces[2].push_back(2); |
|---|
| 592 | faces[2].push_back(3); |
|---|
| 593 | |
|---|
| 594 | faces[3].push_back(4); |
|---|
| 595 | faces[3].push_back(7); |
|---|
| 596 | faces[3].push_back(6); |
|---|
| 597 | faces[3].push_back(5); |
|---|
| 598 | |
|---|
| 599 | faces[4].push_back(0); |
|---|
| 600 | faces[4].push_back(4); |
|---|
| 601 | faces[4].push_back(5); |
|---|
| 602 | faces[4].push_back(1); |
|---|
| 603 | |
|---|
| 604 | faces[5].push_back(2); |
|---|
| 605 | faces[5].push_back(6); |
|---|
| 606 | faces[5].push_back(7); |
|---|
| 607 | faces[5].push_back(3); |
|---|
| 608 | |
|---|
| 609 | edges[0].push_back(0); edges[0].push_back(1); |
|---|
| 610 | edges[0].push_back(2); edges[0].push_back(4); |
|---|
| 611 | |
|---|
| 612 | edges[1].push_back(1); edges[1].push_back(2); |
|---|
| 613 | edges[1].push_back(2); edges[1].push_back(1); |
|---|
| 614 | |
|---|
| 615 | edges[2].push_back(2); edges[2].push_back(3); |
|---|
| 616 | edges[2].push_back(2); edges[2].push_back(5); |
|---|
| 617 | |
|---|
| 618 | edges[3].push_back(3); edges[3].push_back(0); |
|---|
| 619 | edges[3].push_back(2); edges[3].push_back(0); |
|---|
| 620 | |
|---|
| 621 | |
|---|
| 622 | edges[4].push_back(0); edges[4].push_back(4); |
|---|
| 623 | edges[4].push_back(0); edges[4].push_back(4); |
|---|
| 624 | |
|---|
| 625 | edges[5].push_back(1); edges[5].push_back(5); |
|---|
| 626 | edges[5].push_back(4); edges[5].push_back(1); |
|---|
| 627 | |
|---|
| 628 | edges[6].push_back(2); edges[6].push_back(6); |
|---|
| 629 | edges[6].push_back(1); edges[6].push_back(5); |
|---|
| 630 | |
|---|
| 631 | edges[7].push_back(3); edges[7].push_back(7); |
|---|
| 632 | edges[7].push_back(5); edges[7].push_back(0); |
|---|
| 633 | |
|---|
| 634 | |
|---|
| 635 | edges[8].push_back(4); edges[8].push_back(5); |
|---|
| 636 | edges[8].push_back(3); edges[8].push_back(4); |
|---|
| 637 | |
|---|
| 638 | edges[9].push_back(5); edges[9].push_back(6); |
|---|
| 639 | edges[9].push_back(3); edges[9].push_back(1); |
|---|
| 640 | |
|---|
| 641 | edges[10].push_back(6);edges[10].push_back(7); |
|---|
| 642 | edges[10].push_back(3);edges[10].push_back(5); |
|---|
| 643 | |
|---|
| 644 | edges[11].push_back(7); edges[11].push_back(4); |
|---|
| 645 | edges[11].push_back(3); edges[11].push_back(0); |
|---|
| 646 | } |
|---|
| 647 | |
|---|
| 648 | |
|---|
| 649 | |
|---|
| 650 | |
|---|
| 651 | |
|---|
| 652 | |
|---|
| 653 | ViewDependentShadowMap::ViewDependentData::ViewDependentData(ViewDependentShadowMap* vdsm): |
|---|
| 654 | _viewDependentShadowMap(vdsm) |
|---|
| 655 | { |
|---|
| 656 | OSG_INFO<<"ViewDependentData::ViewDependentData()"<<this<<std::endl; |
|---|
| 657 | _stateset = new osg::StateSet; |
|---|
| 658 | } |
|---|
| 659 | |
|---|
| 660 | void ViewDependentShadowMap::ViewDependentData::releaseGLObjects(osg::State* state) const |
|---|
| 661 | { |
|---|
| 662 | for(ShadowDataList::const_iterator itr = _shadowDataList.begin(); |
|---|
| 663 | itr != _shadowDataList.end(); |
|---|
| 664 | ++itr) |
|---|
| 665 | { |
|---|
| 666 | (*itr)->releaseGLObjects(state); |
|---|
| 667 | } |
|---|
| 668 | } |
|---|
| 669 | |
|---|
| 670 | |
|---|
| 671 | |
|---|
| 672 | |
|---|
| 673 | |
|---|
| 674 | ViewDependentShadowMap::ViewDependentShadowMap(): |
|---|
| 675 | ShadowTechnique() |
|---|
| 676 | { |
|---|
| 677 | _shadowRecievingPlaceholderStateSet = new osg::StateSet; |
|---|
| 678 | } |
|---|
| 679 | |
|---|
| 680 | ViewDependentShadowMap::ViewDependentShadowMap(const ViewDependentShadowMap& vdsm, const osg::CopyOp& copyop): |
|---|
| 681 | ShadowTechnique(vdsm,copyop) |
|---|
| 682 | { |
|---|
| 683 | _shadowRecievingPlaceholderStateSet = new osg::StateSet; |
|---|
| 684 | } |
|---|
| 685 | |
|---|
| 686 | ViewDependentShadowMap::~ViewDependentShadowMap() |
|---|
| 687 | { |
|---|
| 688 | } |
|---|
| 689 | |
|---|
| 690 | |
|---|
| 691 | void ViewDependentShadowMap::init() |
|---|
| 692 | { |
|---|
| 693 | if (!_shadowedScene) return; |
|---|
| 694 | |
|---|
| 695 | OSG_INFO<<"ViewDependentShadowMap::init()"<<std::endl; |
|---|
| 696 | |
|---|
| 697 | createShaders(); |
|---|
| 698 | |
|---|
| 699 | _dirty = false; |
|---|
| 700 | } |
|---|
| 701 | |
|---|
| 702 | void ViewDependentShadowMap::cleanSceneGraph() |
|---|
| 703 | { |
|---|
| 704 | OSG_INFO<<"ViewDependentShadowMap::cleanSceneGraph()"<<std::endl; |
|---|
| 705 | } |
|---|
| 706 | |
|---|
| 707 | ViewDependentShadowMap::ViewDependentData* ViewDependentShadowMap::createViewDependentData(osgUtil::CullVisitor* cv) |
|---|
| 708 | { |
|---|
| 709 | return new ViewDependentData(this); |
|---|
| 710 | } |
|---|
| 711 | |
|---|
| 712 | ViewDependentShadowMap::ViewDependentData* ViewDependentShadowMap::getViewDependentData(osgUtil::CullVisitor* cv) |
|---|
| 713 | { |
|---|
| 714 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_viewDependentDataMapMutex); |
|---|
| 715 | ViewDependentDataMap::iterator itr = _viewDependentDataMap.find(cv); |
|---|
| 716 | if (itr!=_viewDependentDataMap.end()) return itr->second.get(); |
|---|
| 717 | |
|---|
| 718 | osg::ref_ptr<ViewDependentData> vdd = createViewDependentData(cv); |
|---|
| 719 | _viewDependentDataMap[cv] = vdd; |
|---|
| 720 | return vdd.release(); |
|---|
| 721 | } |
|---|
| 722 | |
|---|
| 723 | void ViewDependentShadowMap::update(osg::NodeVisitor& nv) |
|---|
| 724 | { |
|---|
| 725 | OSG_INFO<<"ViewDependentShadowMap::update(osg::NodeVisitor& "<<&nv<<")"<<std::endl; |
|---|
| 726 | _shadowedScene->osg::Group::traverse(nv); |
|---|
| 727 | } |
|---|
| 728 | |
|---|
| 729 | void ViewDependentShadowMap::cull(osgUtil::CullVisitor& cv) |
|---|
| 730 | { |
|---|
| 731 | OSG_INFO<<std::endl<<std::endl<<"ViewDependentShadowMap::cull(osg::CullVisitor&"<<&cv<<")"<<std::endl; |
|---|
| 732 | |
|---|
| 733 | if (!_shadowCastingStateSet) |
|---|
| 734 | { |
|---|
| 735 | OSG_INFO<<"Warning, init() has not yet been called so ShadowCastingStateSet has not been setup yet, unable to create shadows."<<std::endl; |
|---|
| 736 | _shadowedScene->osg::Group::traverse(cv); |
|---|
| 737 | return; |
|---|
| 738 | } |
|---|
| 739 | |
|---|
| 740 | ViewDependentData* vdd = getViewDependentData(&cv); |
|---|
| 741 | |
|---|
| 742 | if (!vdd) |
|---|
| 743 | { |
|---|
| 744 | OSG_INFO<<"Warning, now ViewDependentData created, unable to create shadows."<<std::endl; |
|---|
| 745 | _shadowedScene->osg::Group::traverse(cv); |
|---|
| 746 | return; |
|---|
| 747 | } |
|---|
| 748 | |
|---|
| 749 | OSG_INFO<<"cv->getProjectionMatrix()="<<*cv.getProjectionMatrix()<<std::endl; |
|---|
| 750 | |
|---|
| 751 | osg::CullSettings::ComputeNearFarMode cachedNearFarMode = cv.getComputeNearFarMode(); |
|---|
| 752 | |
|---|
| 753 | osg::RefMatrix& viewProjectionMatrix = *cv.getProjectionMatrix(); |
|---|
| 754 | |
|---|
| 755 | |
|---|
| 756 | bool orthographicViewFrustum = viewProjectionMatrix(0,3)==0.0 && |
|---|
| 757 | viewProjectionMatrix(1,3)==0.0 && |
|---|
| 758 | viewProjectionMatrix(2,3)==0.0; |
|---|
| 759 | |
|---|
| 760 | double minZNear = 0.0; |
|---|
| 761 | double maxZFar = DBL_MAX; |
|---|
| 762 | |
|---|
| 763 | if (cachedNearFarMode==osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR) |
|---|
| 764 | { |
|---|
| 765 | double left, right, top, bottom; |
|---|
| 766 | if (orthographicViewFrustum) |
|---|
| 767 | { |
|---|
| 768 | viewProjectionMatrix.getOrtho(left, right, bottom, top, minZNear, maxZFar); |
|---|
| 769 | } |
|---|
| 770 | else |
|---|
| 771 | { |
|---|
| 772 | viewProjectionMatrix.getFrustum(left, right, bottom, top, minZNear, maxZFar); |
|---|
| 773 | } |
|---|
| 774 | OSG_INFO<<"minZNear="<<minZNear<<", maxZFar="<<maxZFar<<std::endl; |
|---|
| 775 | } |
|---|
| 776 | |
|---|
| 777 | |
|---|
| 778 | cv.setComputeNearFarMode(osg::CullSettings::COMPUTE_NEAR_FAR_USING_PRIMITIVES); |
|---|
| 779 | |
|---|
| 780 | |
|---|
| 781 | |
|---|
| 782 | cv.pushStateSet( _shadowRecievingPlaceholderStateSet.get() ); |
|---|
| 783 | |
|---|
| 784 | osg::ref_ptr<osgUtil::StateGraph> decoratorStateGraph = cv.getCurrentStateGraph(); |
|---|
| 785 | |
|---|
| 786 | cullShadowReceivingScene(&cv); |
|---|
| 787 | |
|---|
| 788 | cv.popStateSet(); |
|---|
| 789 | |
|---|
| 790 | if (cv.getComputeNearFarMode()!=osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR) |
|---|
| 791 | { |
|---|
| 792 | OSG_INFO<<"Just done main subgraph traversak"<<std::endl; |
|---|
| 793 | |
|---|
| 794 | |
|---|
| 795 | cv.computeNearPlane(); |
|---|
| 796 | } |
|---|
| 797 | |
|---|
| 798 | Frustum frustum(&cv, minZNear, maxZFar); |
|---|
| 799 | |
|---|
| 800 | |
|---|
| 801 | cv.setComputeNearFarMode(cachedNearFarMode); |
|---|
| 802 | |
|---|
| 803 | OSG_INFO<<"frustum.eye="<<frustum.eye<<", frustum.centerNearPlane, "<<frustum.centerNearPlane<<" distance = "<<(frustum.eye-frustum.centerNearPlane).length()<<std::endl; |
|---|
| 804 | |
|---|
| 805 | |
|---|
| 806 | |
|---|
| 807 | |
|---|
| 808 | selectActiveLights(&cv, vdd); |
|---|
| 809 | |
|---|
| 810 | ShadowSettings* settings = getShadowedScene()->getShadowSettings(); |
|---|
| 811 | |
|---|
| 812 | unsigned int pos_x = 0; |
|---|
| 813 | unsigned int textureUnit = settings->getBaseShadowTextureUnit(); |
|---|
| 814 | unsigned int numValidShadows = 0; |
|---|
| 815 | |
|---|
| 816 | ShadowDataList& sdl = vdd->getShadowDataList(); |
|---|
| 817 | ShadowDataList previous_sdl; |
|---|
| 818 | previous_sdl.swap(sdl); |
|---|
| 819 | |
|---|
| 820 | unsigned int numShadowMapsPerLight = settings->getNumShadowMapsPerLight(); |
|---|
| 821 | if (numShadowMapsPerLight>2) |
|---|
| 822 | { |
|---|
| 823 | OSG_NOTICE<<"numShadowMapsPerLight of "<<numShadowMapsPerLight<<" is greater than maximum supported, falling back to 2."<<std::endl; |
|---|
| 824 | numShadowMapsPerLight = 2; |
|---|
| 825 | } |
|---|
| 826 | |
|---|
| 827 | LightDataList& pll = vdd->getLightDataList(); |
|---|
| 828 | for(LightDataList::iterator itr = pll.begin(); |
|---|
| 829 | itr != pll.end(); |
|---|
| 830 | ++itr) |
|---|
| 831 | { |
|---|
| 832 | |
|---|
| 833 | |
|---|
| 834 | |
|---|
| 835 | LightData& pl = **itr; |
|---|
| 836 | |
|---|
| 837 | |
|---|
| 838 | |
|---|
| 839 | osg::Polytope polytope = computeLightViewFrustumPolytope(frustum, pl); |
|---|
| 840 | |
|---|
| 841 | |
|---|
| 842 | if (polytope.empty()) |
|---|
| 843 | { |
|---|
| 844 | OSG_NOTICE<<"Polytope empty no shadow to render"<<std::endl; |
|---|
| 845 | continue; |
|---|
| 846 | } |
|---|
| 847 | |
|---|
| 848 | |
|---|
| 849 | |
|---|
| 850 | osg::Matrixd projectionMatrix; |
|---|
| 851 | osg::Matrixd viewMatrix; |
|---|
| 852 | if (!computeShadowCameraSettings(frustum, pl, projectionMatrix, viewMatrix)) |
|---|
| 853 | { |
|---|
| 854 | OSG_NOTICE<<"No valid Camera settings, no shadow to render"<<std::endl; |
|---|
| 855 | continue; |
|---|
| 856 | } |
|---|
| 857 | |
|---|
| 858 | |
|---|
| 859 | |
|---|
| 860 | if ( _shadowedScene->getCastsShadowTraversalMask()!=0xffffffff) |
|---|
| 861 | { |
|---|
| 862 | |
|---|
| 863 | |
|---|
| 864 | osg::ref_ptr<osg::Viewport> viewport = new osg::Viewport(0,0,2048,2048); |
|---|
| 865 | ComputeLightSpaceBounds clsb(viewport.get(), projectionMatrix, viewMatrix); |
|---|
| 866 | clsb.setTraversalMask(_shadowedScene->getCastsShadowTraversalMask()); |
|---|
| 867 | |
|---|
| 868 | osg::Matrixd invertModelView; |
|---|
| 869 | invertModelView.invert(viewMatrix); |
|---|
| 870 | osg::Polytope local_polytope(polytope); |
|---|
| 871 | local_polytope.transformProvidingInverse(invertModelView); |
|---|
| 872 | |
|---|
| 873 | osg::CullingSet& cs = clsb.getProjectionCullingStack().back(); |
|---|
| 874 | cs.setFrustum(local_polytope); |
|---|
| 875 | clsb.pushCullingSet(); |
|---|
| 876 | |
|---|
| 877 | _shadowedScene->accept(clsb); |
|---|
| 878 | |
|---|
| 879 | |
|---|
| 880 | |
|---|
| 881 | |
|---|
| 882 | if (clsb._bb.xMin()>-1.0f || clsb._bb.xMax()<1.0f || clsb._bb.yMin()>-1.0f || clsb._bb.yMax()<1.0f) |
|---|
| 883 | { |
|---|
| 884 | |
|---|
| 885 | |
|---|
| 886 | #if 1 |
|---|
| 887 | double xMid = (clsb._bb.xMin()+clsb._bb.xMax())*0.5f; |
|---|
| 888 | double xRange = clsb._bb.xMax()-clsb._bb.xMin(); |
|---|
| 889 | #else |
|---|
| 890 | double xMid = 0.0; |
|---|
| 891 | double xRange = 2.0; |
|---|
| 892 | #endif |
|---|
| 893 | double yMid = (clsb._bb.yMin()+clsb._bb.yMax())*0.5f; |
|---|
| 894 | double yRange = (clsb._bb.yMax()-clsb._bb.yMin()); |
|---|
| 895 | |
|---|
| 896 | |
|---|
| 897 | |
|---|
| 898 | projectionMatrix = |
|---|
| 899 | projectionMatrix * |
|---|
| 900 | osg::Matrixd::translate(osg::Vec3d(-xMid,-yMid,0.0)) * |
|---|
| 901 | osg::Matrixd::scale(osg::Vec3d(2.0/xRange, 2.0/yRange,1.0)); |
|---|
| 902 | |
|---|
| 903 | } |
|---|
| 904 | |
|---|
| 905 | } |
|---|
| 906 | |
|---|
| 907 | double splitPoint = 0.0; |
|---|
| 908 | |
|---|
| 909 | if (numShadowMapsPerLight>1) |
|---|
| 910 | { |
|---|
| 911 | osg::Vec3d eye_v = frustum.eye * viewMatrix; |
|---|
| 912 | osg::Vec3d center_v = frustum.center * viewMatrix; |
|---|
| 913 | osg::Vec3d viewdir_v = center_v-eye_v; viewdir_v.normalize(); |
|---|
| 914 | osg::Vec3d lightdir(0.0,0.0,-1.0); |
|---|
| 915 | |
|---|
| 916 | double dotProduct_v = lightdir * viewdir_v; |
|---|
| 917 | double angle = acosf(dotProduct_v); |
|---|
| 918 | |
|---|
| 919 | osg::Vec3d eye_ls = eye_v * projectionMatrix; |
|---|
| 920 | |
|---|
| 921 | OSG_INFO<<"Angle between view vector and eye "<<osg::RadiansToDegrees(angle)<<std::endl; |
|---|
| 922 | OSG_INFO<<"eye_ls="<<eye_ls<<std::endl; |
|---|
| 923 | |
|---|
| 924 | if (eye_ls.y()>=-1.0 && eye_ls.y()<=1.0) |
|---|
| 925 | { |
|---|
| 926 | OSG_INFO<<"Eye point inside light space clip region "<<std::endl; |
|---|
| 927 | splitPoint = 0.0; |
|---|
| 928 | } |
|---|
| 929 | else |
|---|
| 930 | { |
|---|
| 931 | double n = -1.0-eye_ls.y(); |
|---|
| 932 | double f = 1.0-eye_ls.y(); |
|---|
| 933 | double sqrt_nf = sqrt(n*f); |
|---|
| 934 | double mid = eye_ls.y()+sqrt_nf; |
|---|
| 935 | double ratioOfMidToUseForSplit = 0.8; |
|---|
| 936 | splitPoint = mid * ratioOfMidToUseForSplit; |
|---|
| 937 | |
|---|
| 938 | OSG_INFO<<" n="<<n<<", f="<<f<<", sqrt_nf="<<sqrt_nf<<" mid="<<mid<<std::endl; |
|---|
| 939 | } |
|---|
| 940 | } |
|---|
| 941 | |
|---|
| 942 | |
|---|
| 943 | for (unsigned int sm_i=0; sm_i<numShadowMapsPerLight; ++sm_i) |
|---|
| 944 | { |
|---|
| 945 | osg::ref_ptr<ShadowData> sd; |
|---|
| 946 | |
|---|
| 947 | if (previous_sdl.empty()) |
|---|
| 948 | { |
|---|
| 949 | OSG_INFO<<"Create new ShadowData"<<std::endl; |
|---|
| 950 | sd = new ShadowData(vdd); |
|---|
| 951 | } |
|---|
| 952 | else |
|---|
| 953 | { |
|---|
| 954 | OSG_INFO<<"Taking ShadowData from from of previous_sdl"<<std::endl; |
|---|
| 955 | sd = previous_sdl.front(); |
|---|
| 956 | previous_sdl.erase(previous_sdl.begin()); |
|---|
| 957 | } |
|---|
| 958 | |
|---|
| 959 | osg::ref_ptr<osg::Camera> camera = sd->_camera; |
|---|
| 960 | |
|---|
| 961 | camera->setProjectionMatrix(projectionMatrix); |
|---|
| 962 | camera->setViewMatrix(viewMatrix); |
|---|
| 963 | |
|---|
| 964 | if (settings->getDebugDraw()) |
|---|
| 965 | { |
|---|
| 966 | camera->getViewport()->x() = pos_x; |
|---|
| 967 | pos_x += camera->getViewport()->width() + 40; |
|---|
| 968 | } |
|---|
| 969 | |
|---|
| 970 | |
|---|
| 971 | osg::Matrixd invertModelView; |
|---|
| 972 | invertModelView.invert(camera->getViewMatrix()); |
|---|
| 973 | |
|---|
| 974 | osg::Polytope local_polytope(polytope); |
|---|
| 975 | local_polytope.transformProvidingInverse(invertModelView); |
|---|
| 976 | |
|---|
| 977 | |
|---|
| 978 | if (numShadowMapsPerLight>1) |
|---|
| 979 | { |
|---|
| 980 | |
|---|
| 981 | #if 0 |
|---|
| 982 | double r_start = (sm_i==0) ? -1.0 : (double(sm_i)/double(numShadowMapsPerLight)*2.0-1.0); |
|---|
| 983 | double r_end = (sm_i+1==numShadowMapsPerLight) ? 1.0 : (double(sm_i+1)/double(numShadowMapsPerLight)*2.0-1.0); |
|---|
| 984 | #endif |
|---|
| 985 | |
|---|
| 986 | |
|---|
| 987 | double r_start = (sm_i==0) ? -1.0 : splitPoint; |
|---|
| 988 | double r_end = (sm_i+1==numShadowMapsPerLight) ? 1.0 : splitPoint; |
|---|
| 989 | |
|---|
| 990 | |
|---|
| 991 | |
|---|
| 992 | if (sm_i+1<numShadowMapsPerLight) r_end+=0.01; |
|---|
| 993 | |
|---|
| 994 | |
|---|
| 995 | if (sm_i>0) |
|---|
| 996 | { |
|---|
| 997 | |
|---|
| 998 | |
|---|
| 999 | |
|---|
| 1000 | osg::Plane plane(0.0,1.0,0.0,-r_start); |
|---|
| 1001 | |
|---|
| 1002 | |
|---|
| 1003 | plane.transformProvidingInverse(projectionMatrix); |
|---|
| 1004 | local_polytope.getPlaneList().push_back(plane); |
|---|
| 1005 | |
|---|
| 1006 | |
|---|
| 1007 | |
|---|
| 1008 | } |
|---|
| 1009 | |
|---|
| 1010 | if (sm_i+1<numShadowMapsPerLight) |
|---|
| 1011 | { |
|---|
| 1012 | |
|---|
| 1013 | |
|---|
| 1014 | |
|---|
| 1015 | osg::Plane plane(0.0,-1.0,0.0,r_end); |
|---|
| 1016 | |
|---|
| 1017 | |
|---|
| 1018 | plane.transformProvidingInverse(projectionMatrix); |
|---|
| 1019 | local_polytope.getPlaneList().push_back(plane); |
|---|
| 1020 | |
|---|
| 1021 | |
|---|
| 1022 | } |
|---|
| 1023 | |
|---|
| 1024 | local_polytope.setupMask(); |
|---|
| 1025 | |
|---|
| 1026 | |
|---|
| 1027 | |
|---|
| 1028 | |
|---|
| 1029 | |
|---|
| 1030 | double mid_r = (r_start+r_end)*0.5; |
|---|
| 1031 | double range_r = (r_end-r_start); |
|---|
| 1032 | |
|---|
| 1033 | |
|---|
| 1034 | |
|---|
| 1035 | camera->setProjectionMatrix( |
|---|
| 1036 | camera->getProjectionMatrix() * |
|---|
| 1037 | osg::Matrixd::translate(osg::Vec3d(0.0,-mid_r,0.0)) * |
|---|
| 1038 | osg::Matrixd::scale(osg::Vec3d(1.0,2.0/range_r,1.0))); |
|---|
| 1039 | |
|---|
| 1040 | } |
|---|
| 1041 | |
|---|
| 1042 | |
|---|
| 1043 | osg::ref_ptr<VDSMCameraCullCallback> vdsmCallback = new VDSMCameraCullCallback(this, local_polytope); |
|---|
| 1044 | camera->setCullCallback(vdsmCallback.get()); |
|---|
| 1045 | |
|---|
| 1046 | |
|---|
| 1047 | |
|---|
| 1048 | |
|---|
| 1049 | cv.pushStateSet(_shadowCastingStateSet.get()); |
|---|
| 1050 | |
|---|
| 1051 | cullShadowCastingScene(&cv, camera.get()); |
|---|
| 1052 | |
|---|
| 1053 | cv.popStateSet(); |
|---|
| 1054 | |
|---|
| 1055 | if (!orthographicViewFrustum && settings->getShadowMapProjectionHint()==ShadowSettings::PERSPECTIVE_SHADOW_MAP) |
|---|
| 1056 | { |
|---|
| 1057 | adjustPerspectiveShadowMapCameraSettings(vdsmCallback->getRenderStage(), frustum, pl, camera.get()); |
|---|
| 1058 | if (vdsmCallback->getProjectionMatrix()) |
|---|
| 1059 | { |
|---|
| 1060 | vdsmCallback->getProjectionMatrix()->set(camera->getProjectionMatrix()); |
|---|
| 1061 | } |
|---|
| 1062 | } |
|---|
| 1063 | |
|---|
| 1064 | |
|---|
| 1065 | |
|---|
| 1066 | assignTexGenSettings(&cv, camera.get(), textureUnit, sd->_texgen.get()); |
|---|
| 1067 | |
|---|
| 1068 | |
|---|
| 1069 | pl.textureUnits.push_back(textureUnit); |
|---|
| 1070 | |
|---|
| 1071 | |
|---|
| 1072 | sd->_textureUnit = textureUnit; |
|---|
| 1073 | |
|---|
| 1074 | if (textureUnit >= 8) |
|---|
| 1075 | { |
|---|
| 1076 | OSG_NOTICE<<"Shadow texture unit is invalid for texgen, will not be used."<<std::endl; |
|---|
| 1077 | } |
|---|
| 1078 | else |
|---|
| 1079 | { |
|---|
| 1080 | sdl.push_back(sd); |
|---|
| 1081 | } |
|---|
| 1082 | |
|---|
| 1083 | |
|---|
| 1084 | ++textureUnit; |
|---|
| 1085 | ++numValidShadows ; |
|---|
| 1086 | } |
|---|
| 1087 | } |
|---|
| 1088 | |
|---|
| 1089 | if (numValidShadows>0) |
|---|
| 1090 | { |
|---|
| 1091 | decoratorStateGraph->setStateSet(selectStateSetForRenderingShadow(*vdd)); |
|---|
| 1092 | } |
|---|
| 1093 | |
|---|
| 1094 | |
|---|
| 1095 | } |
|---|
| 1096 | |
|---|
| 1097 | bool ViewDependentShadowMap::selectActiveLights(osgUtil::CullVisitor* cv, ViewDependentData* vdd) const |
|---|
| 1098 | { |
|---|
| 1099 | OSG_INFO<<"selectActiveLights"<<std::endl; |
|---|
| 1100 | |
|---|
| 1101 | LightDataList& pll = vdd->getLightDataList(); |
|---|
| 1102 | |
|---|
| 1103 | LightDataList previous_ldl; |
|---|
| 1104 | previous_ldl.swap(pll); |
|---|
| 1105 | |
|---|
| 1106 | |
|---|
| 1107 | osgUtil::RenderStage * rs = cv->getCurrentRenderBin()->getStage(); |
|---|
| 1108 | |
|---|
| 1109 | OSG_INFO<<"selectActiveLights osgUtil::RenderStage="<<rs<<std::endl; |
|---|
| 1110 | |
|---|
| 1111 | osg::Matrixd modelViewMatrix = *(cv->getModelViewMatrix()); |
|---|
| 1112 | |
|---|
| 1113 | osgUtil::PositionalStateContainer::AttrMatrixList& aml = |
|---|
| 1114 | rs->getPositionalStateContainer()->getAttrMatrixList(); |
|---|
| 1115 | |
|---|
| 1116 | |
|---|
| 1117 | const ShadowSettings* settings = getShadowedScene()->getShadowSettings(); |
|---|
| 1118 | |
|---|
| 1119 | for(osgUtil::PositionalStateContainer::AttrMatrixList::reverse_iterator itr = aml.rbegin(); |
|---|
| 1120 | itr != aml.rend(); |
|---|
| 1121 | ++itr) |
|---|
| 1122 | { |
|---|
| 1123 | const osg::Light* light = dynamic_cast<const osg::Light*>(itr->first.get()); |
|---|
| 1124 | if (light && light->getLightNum() >= 0) |
|---|
| 1125 | { |
|---|
| 1126 | |
|---|
| 1127 | if (settings && settings->getLightNum()>=0 && light->getLightNum()!=settings->getLightNum()) continue; |
|---|
| 1128 | |
|---|
| 1129 | LightDataList::iterator pll_itr = pll.begin(); |
|---|
| 1130 | for(; pll_itr != pll.end(); ++pll_itr) |
|---|
| 1131 | { |
|---|
| 1132 | if ((*pll_itr)->light->getLightNum()==light->getLightNum()) break; |
|---|
| 1133 | } |
|---|
| 1134 | |
|---|
| 1135 | if (pll_itr==pll.end()) |
|---|
| 1136 | { |
|---|
| 1137 | OSG_INFO<<"Light num "<<light->getLightNum()<<std::endl; |
|---|
| 1138 | LightData* ld = new LightData(vdd); |
|---|
| 1139 | ld->setLightData(itr->second.get(), light, modelViewMatrix); |
|---|
| 1140 | pll.push_back(ld); |
|---|
| 1141 | } |
|---|
| 1142 | else |
|---|
| 1143 | { |
|---|
| 1144 | OSG_INFO<<"Light num "<<light->getLightNum()<<" already used, ignore light"<<std::endl; |
|---|
| 1145 | } |
|---|
| 1146 | } |
|---|
| 1147 | } |
|---|
| 1148 | |
|---|
| 1149 | return !pll.empty(); |
|---|
| 1150 | } |
|---|
| 1151 | |
|---|
| 1152 | void ViewDependentShadowMap::createShaders() |
|---|
| 1153 | { |
|---|
| 1154 | OSG_INFO<<"ViewDependentShadowMap::createShaders()"<<std::endl; |
|---|
| 1155 | |
|---|
| 1156 | unsigned int _baseTextureUnit = 0; |
|---|
| 1157 | |
|---|
| 1158 | _shadowCastingStateSet = new osg::StateSet; |
|---|
| 1159 | |
|---|
| 1160 | ShadowSettings* settings = getShadowedScene()->getShadowSettings(); |
|---|
| 1161 | |
|---|
| 1162 | if (!settings->getDebugDraw()) |
|---|
| 1163 | { |
|---|
| 1164 | |
|---|
| 1165 | |
|---|
| 1166 | |
|---|
| 1167 | |
|---|
| 1168 | |
|---|
| 1169 | |
|---|
| 1170 | |
|---|
| 1171 | |
|---|
| 1172 | _shadowCastingStateSet->setAttribute( new osg::CullFace( osg::CullFace::FRONT ), |
|---|
| 1173 | osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE ); |
|---|
| 1174 | |
|---|
| 1175 | |
|---|
| 1176 | |
|---|
| 1177 | |
|---|
| 1178 | _shadowCastingStateSet->setMode( GL_CULL_FACE, osg::StateAttribute::OFF ); |
|---|
| 1179 | } |
|---|
| 1180 | |
|---|
| 1181 | #if 1 |
|---|
| 1182 | float factor = 1.1; |
|---|
| 1183 | float units = 4.0; |
|---|
| 1184 | #else |
|---|
| 1185 | float factor = -1.1; |
|---|
| 1186 | float units = -4.0; |
|---|
| 1187 | #endif |
|---|
| 1188 | _polygonOffset = new osg::PolygonOffset(factor, units); |
|---|
| 1189 | _shadowCastingStateSet->setAttribute(_polygonOffset.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| 1190 | _shadowCastingStateSet->setMode(GL_POLYGON_OFFSET_FILL, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| 1191 | |
|---|
| 1192 | |
|---|
| 1193 | _uniforms.clear(); |
|---|
| 1194 | osg::ref_ptr<osg::Uniform> baseTextureSampler = new osg::Uniform("baseTexture",(int)_baseTextureUnit); |
|---|
| 1195 | _uniforms.push_back(baseTextureSampler.get()); |
|---|
| 1196 | |
|---|
| 1197 | osg::ref_ptr<osg::Uniform> baseTextureUnit = new osg::Uniform("baseTextureUnit",(int)_baseTextureUnit); |
|---|
| 1198 | _uniforms.push_back(baseTextureUnit.get()); |
|---|
| 1199 | |
|---|
| 1200 | for(unsigned int sm_i=0; sm_i<settings->getNumShadowMapsPerLight(); ++sm_i) |
|---|
| 1201 | { |
|---|
| 1202 | { |
|---|
| 1203 | std::stringstream sstr; |
|---|
| 1204 | sstr<<"shadowTexture"<<sm_i; |
|---|
| 1205 | osg::ref_ptr<osg::Uniform> shadowTextureSampler = new osg::Uniform(sstr.str().c_str(),(int)(settings->getBaseShadowTextureUnit()+sm_i)); |
|---|
| 1206 | _uniforms.push_back(shadowTextureSampler.get()); |
|---|
| 1207 | } |
|---|
| 1208 | |
|---|
| 1209 | { |
|---|
| 1210 | std::stringstream sstr; |
|---|
| 1211 | sstr<<"shadowTextureUnit"<<sm_i; |
|---|
| 1212 | osg::ref_ptr<osg::Uniform> shadowTextureUnit = new osg::Uniform(sstr.str().c_str(),(int)(settings->getBaseShadowTextureUnit()+sm_i)); |
|---|
| 1213 | _uniforms.push_back(shadowTextureUnit.get()); |
|---|
| 1214 | } |
|---|
| 1215 | } |
|---|
| 1216 | |
|---|
| 1217 | switch(settings->getShaderHint()) |
|---|
| 1218 | { |
|---|
| 1219 | case(ShadowSettings::NO_SHADERS): |
|---|
| 1220 | { |
|---|
| 1221 | OSG_INFO<<"No shaders provided by, user must supply own shaders"<<std::endl; |
|---|
| 1222 | break; |
|---|
| 1223 | } |
|---|
| 1224 | case(ShadowSettings::PROVIDE_VERTEX_AND_FRAGMENT_SHADER): |
|---|
| 1225 | case(ShadowSettings::PROVIDE_FRAGMENT_SHADER): |
|---|
| 1226 | { |
|---|
| 1227 | _program = new osg::Program; |
|---|
| 1228 | |
|---|
| 1229 | |
|---|
| 1230 | if (settings->getNumShadowMapsPerLight()==2) |
|---|
| 1231 | { |
|---|
| 1232 | _program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource_withBaseTexture_twoShadowMaps)); |
|---|
| 1233 | } |
|---|
| 1234 | else |
|---|
| 1235 | { |
|---|
| 1236 | _program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource_withBaseTexture)); |
|---|
| 1237 | } |
|---|
| 1238 | |
|---|
| 1239 | break; |
|---|
| 1240 | } |
|---|
| 1241 | } |
|---|
| 1242 | |
|---|
| 1243 | { |
|---|
| 1244 | osg::ref_ptr<osg::Image> image = new osg::Image; |
|---|
| 1245 | image->allocateImage( 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE ); |
|---|
| 1246 | *(osg::Vec4ub*)image->data() = osg::Vec4ub( 0xFF, 0xFF, 0xFF, 0xFF ); |
|---|
| 1247 | |
|---|
| 1248 | _fallbackBaseTexture = new osg::Texture2D(image.get()); |
|---|
| 1249 | _fallbackBaseTexture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::REPEAT); |
|---|
| 1250 | _fallbackBaseTexture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::REPEAT); |
|---|
| 1251 | _fallbackBaseTexture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::NEAREST); |
|---|
| 1252 | _fallbackBaseTexture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::NEAREST); |
|---|
| 1253 | |
|---|
| 1254 | _fallbackShadowMapTexture = new osg::Texture2D(image.get()); |
|---|
| 1255 | _fallbackShadowMapTexture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::REPEAT); |
|---|
| 1256 | _fallbackShadowMapTexture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::REPEAT); |
|---|
| 1257 | _fallbackShadowMapTexture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::NEAREST); |
|---|
| 1258 | _fallbackShadowMapTexture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::NEAREST); |
|---|
| 1259 | |
|---|
| 1260 | } |
|---|
| 1261 | } |
|---|
| 1262 | |
|---|
| 1263 | osg::Polytope ViewDependentShadowMap::computeLightViewFrustumPolytope(Frustum& frustum, LightData& positionedLight) |
|---|
| 1264 | { |
|---|
| 1265 | OSG_INFO<<"computeLightViewFrustumPolytope()"<<std::endl; |
|---|
| 1266 | |
|---|
| 1267 | osg::Polytope polytope; |
|---|
| 1268 | polytope.setToUnitFrustum(); |
|---|
| 1269 | |
|---|
| 1270 | polytope.transformProvidingInverse( frustum.projectionMatrix ); |
|---|
| 1271 | polytope.transformProvidingInverse( frustum.modelViewMatrix ); |
|---|
| 1272 | |
|---|
| 1273 | osg::Polytope lightVolumePolytope; |
|---|
| 1274 | |
|---|
| 1275 | if (positionedLight.directionalLight) |
|---|
| 1276 | { |
|---|
| 1277 | osg::Polytope::PlaneList& planes = polytope.getPlaneList(); |
|---|
| 1278 | osg::Polytope::ClippingMask selector_mask = 0x1; |
|---|
| 1279 | osg::Polytope::ClippingMask result_mask = 0x0; |
|---|
| 1280 | for(unsigned int i=0; i<planes.size(); ++i, selector_mask <<= 1) |
|---|
| 1281 | { |
|---|
| 1282 | OSG_INFO<<" plane "<<planes[i]<<" planes["<<i<<"].dotProductNormal(lightDir)="<<planes[i].dotProductNormal(positionedLight.lightDir); |
|---|
| 1283 | if (planes[i].dotProductNormal(positionedLight.lightDir)>=0.0) |
|---|
| 1284 | { |
|---|
| 1285 | OSG_INFO<<" Need remove side "<<i<<std::endl; |
|---|
| 1286 | } |
|---|
| 1287 | else |
|---|
| 1288 | { |
|---|
| 1289 | OSG_INFO<<std::endl; |
|---|
| 1290 | lightVolumePolytope.add(planes[i]); |
|---|
| 1291 | result_mask = result_mask | selector_mask; |
|---|
| 1292 | } |
|---|
| 1293 | } |
|---|
| 1294 | |
|---|
| 1295 | OSG_INFO<<" planes.size() = "<<planes.size()<<std::endl; |
|---|
| 1296 | OSG_INFO<<" planes.getResultMask() = "<<polytope.getResultMask()<<std::endl; |
|---|
| 1297 | OSG_INFO<<" resultMask = "<<result_mask<<std::endl; |
|---|
| 1298 | polytope.setResultMask(result_mask); |
|---|
| 1299 | } |
|---|
| 1300 | else |
|---|
| 1301 | { |
|---|
| 1302 | const osg::Polytope::PlaneList& planes = polytope.getPlaneList(); |
|---|
| 1303 | osg::Polytope::ClippingMask selector_mask = 0x1; |
|---|
| 1304 | osg::Polytope::ClippingMask result_mask = 0x0; |
|---|
| 1305 | for(unsigned int i=0; i<planes.size(); ++i, selector_mask <<= 1) |
|---|
| 1306 | { |
|---|
| 1307 | |
|---|
| 1308 | double d = planes[i].distance(positionedLight.lightPos3); |
|---|
| 1309 | OSG_INFO<<" plane "<<planes[i]<<" planes["<<i<<"].distance(lightPos3)="<<d; |
|---|
| 1310 | if (d<0.0) |
|---|
| 1311 | { |
|---|
| 1312 | OSG_INFO<<" Need remove side "<<i<<std::endl; |
|---|
| 1313 | } |
|---|
| 1314 | else |
|---|
| 1315 | { |
|---|
| 1316 | OSG_INFO<<std::endl; |
|---|
| 1317 | lightVolumePolytope.add(planes[i]); |
|---|
| 1318 | result_mask = result_mask | selector_mask; |
|---|
| 1319 | } |
|---|
| 1320 | } |
|---|
| 1321 | OSG_INFO<<" planes.size() = "<<planes.size()<<std::endl; |
|---|
| 1322 | OSG_INFO<<" planes.getResultMask() = "<<polytope.getResultMask()<<std::endl; |
|---|
| 1323 | OSG_INFO<<" resultMask = "<<result_mask<<std::endl; |
|---|
| 1324 | polytope.setResultMask(result_mask); |
|---|
| 1325 | } |
|---|
| 1326 | |
|---|
| 1327 | OSG_INFO<<"Which frustum edges are active?"<<std::endl; |
|---|
| 1328 | for(unsigned int i=0; i<12; ++i) |
|---|
| 1329 | { |
|---|
| 1330 | Frustum::Indices& indices = frustum.edges[i]; |
|---|
| 1331 | |
|---|
| 1332 | unsigned int corner_a = indices[0]; |
|---|
| 1333 | unsigned int corner_b = indices[1]; |
|---|
| 1334 | unsigned int face_a = indices[2]; |
|---|
| 1335 | unsigned int face_b = indices[3]; |
|---|
| 1336 | bool face_a_active = (polytope.getResultMask()&(0x1<<face_a))!=0; |
|---|
| 1337 | bool face_b_active = (polytope.getResultMask()&(0x1<<face_b))!=0; |
|---|
| 1338 | unsigned int numActive = 0; |
|---|
| 1339 | if (face_a_active) ++numActive; |
|---|
| 1340 | if (face_b_active) ++numActive; |
|---|
| 1341 | if (numActive==1) |
|---|
| 1342 | { |
|---|
| 1343 | |
|---|
| 1344 | osg::Plane boundaryPlane; |
|---|
| 1345 | |
|---|
| 1346 | if (positionedLight.directionalLight) |
|---|
| 1347 | { |
|---|
| 1348 | osg::Vec3d normal = (frustum.corners[corner_b]-frustum.corners[corner_a])^positionedLight.lightDir; |
|---|
| 1349 | normal.normalize(); |
|---|
| 1350 | boundaryPlane.set(normal, frustum.corners[corner_a]); |
|---|
| 1351 | } |
|---|
| 1352 | else |
|---|
| 1353 | { |
|---|
| 1354 | boundaryPlane.set(positionedLight.lightPos3, frustum.corners[corner_a], frustum.corners[corner_b]); |
|---|
| 1355 | } |
|---|
| 1356 | |
|---|
| 1357 | OSG_INFO<<"Boundary Edge "<<i<<", corner_a="<<corner_a<<", corner_b="<<corner_b<<", face_a_active="<<face_a_active<<", face_b_active="<<face_b_active; |
|---|
| 1358 | if (boundaryPlane.distance(frustum.center)<0.0) |
|---|
| 1359 | { |
|---|
| 1360 | boundaryPlane.flip(); |
|---|
| 1361 | OSG_INFO<<", flipped boundary edge "<<boundaryPlane<<std::endl; |
|---|
| 1362 | } |
|---|
| 1363 | else |
|---|
| 1364 | { |
|---|
| 1365 | OSG_INFO<<", no need to flip boundary edge "<<boundaryPlane<<std::endl; |
|---|
| 1366 | } |
|---|
| 1367 | lightVolumePolytope.add(boundaryPlane); |
|---|
| 1368 | } |
|---|
| 1369 | else OSG_INFO<<"Internal Edge "<<i<<", corner_a="<<corner_a<<", corner_b="<<corner_b<<", face_a_active="<<face_a_active<<", face_b_active="<<face_b_active<<std::endl; |
|---|
| 1370 | } |
|---|
| 1371 | |
|---|
| 1372 | |
|---|
| 1373 | const osg::Polytope::PlaneList& planes = lightVolumePolytope.getPlaneList(); |
|---|
| 1374 | for(unsigned int i=0; i<planes.size(); ++i) |
|---|
| 1375 | { |
|---|
| 1376 | OSG_INFO<<" plane "<<planes[i]<<" "<<((lightVolumePolytope.getResultMask() & (0x1<<i))?"on":"off")<<std::endl; |
|---|
| 1377 | } |
|---|
| 1378 | |
|---|
| 1379 | return lightVolumePolytope; |
|---|
| 1380 | } |
|---|
| 1381 | |
|---|
| 1382 | bool ViewDependentShadowMap::computeShadowCameraSettings(Frustum& frustum, LightData& positionedLight, osg::Matrixd& projectionMatrix, osg::Matrixd& viewMatrix) |
|---|
| 1383 | { |
|---|
| 1384 | OSG_INFO<<"standardShadowMapCameraSettings()"<<std::endl; |
|---|
| 1385 | |
|---|
| 1386 | osg::Vec3d lightSide; |
|---|
| 1387 | |
|---|
| 1388 | const ShadowSettings* settings = getShadowedScene()->getShadowSettings(); |
|---|
| 1389 | |
|---|
| 1390 | double dotProduct_v = positionedLight.lightDir * frustum.frustumCenterLine; |
|---|
| 1391 | double gamma_v = acos(dotProduct_v); |
|---|
| 1392 | if (gamma_v<osg::DegreesToRadians(settings->getPerspectiveShadowMapCutOffAngle()) || gamma_v>osg::DegreesToRadians(180.0-settings->getPerspectiveShadowMapCutOffAngle())) |
|---|
| 1393 | { |
|---|
| 1394 | OSG_INFO<<"View direction and Light direction below tolerance"<<std::endl; |
|---|
| 1395 | osg::Vec3d viewSide = osg::Matrixd::transform3x3(frustum.modelViewMatrix, osg::Vec3d(1.0,0.0,0.0)); |
|---|
| 1396 | lightSide = positionedLight.lightDir ^ (viewSide ^ positionedLight.lightDir); |
|---|
| 1397 | lightSide.normalize(); |
|---|
| 1398 | } |
|---|
| 1399 | else |
|---|
| 1400 | { |
|---|
| 1401 | lightSide = positionedLight.lightDir ^ frustum.frustumCenterLine; |
|---|
| 1402 | lightSide.normalize(); |
|---|
| 1403 | } |
|---|
| 1404 | |
|---|
| 1405 | osg::Vec3d lightUp = lightSide ^ positionedLight.lightDir; |
|---|
| 1406 | |
|---|
| 1407 | #if 0 |
|---|
| 1408 | OSG_NOTICE<<"positionedLight.lightDir="<<positionedLight.lightDir<<std::endl; |
|---|
| 1409 | OSG_NOTICE<<"lightSide="<<lightSide<<std::endl; |
|---|
| 1410 | OSG_NOTICE<<"lightUp="<<lightUp<<std::endl; |
|---|
| 1411 | #endif |
|---|
| 1412 | |
|---|
| 1413 | |
|---|
| 1414 | if (positionedLight.directionalLight) |
|---|
| 1415 | { |
|---|
| 1416 | double xMin=0.0, xMax=0.0; |
|---|
| 1417 | double yMin=0.0, yMax=0.0; |
|---|
| 1418 | double zMin=0.0, zMax=0.0; |
|---|
| 1419 | |
|---|
| 1420 | for(Frustum::Vertices::iterator itr = frustum.corners.begin(); |
|---|
| 1421 | itr != frustum.corners.end(); |
|---|
| 1422 | ++itr) |
|---|
| 1423 | { |
|---|
| 1424 | osg::Vec3d cornerDelta(*itr - frustum.center); |
|---|
| 1425 | osg::Vec3d cornerInLightCoords(cornerDelta*lightSide, |
|---|
| 1426 | cornerDelta*lightUp, |
|---|
| 1427 | cornerDelta*positionedLight.lightDir); |
|---|
| 1428 | |
|---|
| 1429 | OSG_INFO<<" corner ="<<*itr<<" in lightcoords "<<cornerInLightCoords<<std::endl; |
|---|
| 1430 | |
|---|
| 1431 | xMin = osg::minimum( xMin, cornerInLightCoords.x()); |
|---|
| 1432 | xMax = osg::maximum( xMax, cornerInLightCoords.x()); |
|---|
| 1433 | yMin = osg::minimum( yMin, cornerInLightCoords.y()); |
|---|
| 1434 | yMax = osg::maximum( yMax, cornerInLightCoords.y()); |
|---|
| 1435 | zMin = osg::minimum( zMin, cornerInLightCoords.z()); |
|---|
| 1436 | zMax = osg::maximum( zMax, cornerInLightCoords.z()); |
|---|
| 1437 | } |
|---|
| 1438 | |
|---|
| 1439 | OSG_INFO<<"before bs xMin="<<xMin<<", xMax="<<xMax<<", yMin="<<yMin<<", yMax="<<yMax<<", zMin="<<zMin<<", zMax="<<zMax<<std::endl; |
|---|
| 1440 | |
|---|
| 1441 | osg::BoundingSphere bs = _shadowedScene->getBound(); |
|---|
| 1442 | osg::Vec3d modelCenterRelativeFrustumCenter(bs.center()-frustum.center); |
|---|
| 1443 | osg::Vec3d modelCenterInLightCoords(modelCenterRelativeFrustumCenter*lightSide, |
|---|
| 1444 | modelCenterRelativeFrustumCenter*lightUp, |
|---|
| 1445 | modelCenterRelativeFrustumCenter*positionedLight.lightDir); |
|---|
| 1446 | |
|---|
| 1447 | OSG_INFO<<"modelCenterInLight="<<modelCenterInLightCoords<<" radius="<<bs.radius()<<std::endl; |
|---|
| 1448 | double radius(bs.radius()); |
|---|
| 1449 | |
|---|
| 1450 | xMin = osg::maximum(xMin, modelCenterInLightCoords.x()-radius); |
|---|
| 1451 | xMax = osg::minimum(xMax, modelCenterInLightCoords.x()+radius); |
|---|
| 1452 | yMin = osg::maximum(yMin, modelCenterInLightCoords.y()-radius); |
|---|
| 1453 | yMax = osg::minimum(yMax, modelCenterInLightCoords.y()+radius); |
|---|
| 1454 | zMin = modelCenterInLightCoords.z()-radius; |
|---|
| 1455 | zMax = osg::minimum(zMax, modelCenterInLightCoords.z()+radius); |
|---|
| 1456 | |
|---|
| 1457 | OSG_INFO<<"after bs xMin="<<xMin<<", xMax="<<xMax<<", yMin="<<yMin<<", yMax="<<yMax<<", zMin="<<zMin<<", zMax="<<zMax<<std::endl; |
|---|
| 1458 | |
|---|
| 1459 | if (xMin>=xMax || yMin>=yMax || zMin>=zMax) |
|---|
| 1460 | { |
|---|
| 1461 | OSG_INFO<<"Warning nothing available to create shadows"<<zMax<<std::endl; |
|---|
| 1462 | return false; |
|---|
| 1463 | } |
|---|
| 1464 | else |
|---|
| 1465 | { |
|---|
| 1466 | projectionMatrix.makeOrtho(xMin,xMax, yMin, yMax,0.0,zMax-zMin); |
|---|
| 1467 | viewMatrix.makeLookAt(frustum.center+positionedLight.lightDir*zMin, frustum.center+positionedLight.lightDir*zMax, lightUp); |
|---|
| 1468 | } |
|---|
| 1469 | } |
|---|
| 1470 | else |
|---|
| 1471 | { |
|---|
| 1472 | double zMax=-DBL_MAX; |
|---|
| 1473 | |
|---|
| 1474 | OSG_INFO<<"lightDir = "<<positionedLight.lightDir<<std::endl; |
|---|
| 1475 | OSG_INFO<<"lightPos3 = "<<positionedLight.lightPos3<<std::endl; |
|---|
| 1476 | for(Frustum::Vertices::iterator itr = frustum.corners.begin(); |
|---|
| 1477 | itr != frustum.corners.end(); |
|---|
| 1478 | ++itr) |
|---|
| 1479 | { |
|---|
| 1480 | osg::Vec3d cornerDelta(*itr - positionedLight.lightPos3); |
|---|
| 1481 | osg::Vec3d cornerInLightCoords(cornerDelta*lightSide, |
|---|
| 1482 | cornerDelta*lightUp, |
|---|
| 1483 | cornerDelta*positionedLight.lightDir); |
|---|
| 1484 | |
|---|
| 1485 | OSG_INFO<<" cornerInLightCoords= "<<cornerInLightCoords<<std::endl; |
|---|
| 1486 | |
|---|
| 1487 | zMax = osg::maximum( zMax, cornerInLightCoords.z()); |
|---|
| 1488 | } |
|---|
| 1489 | |
|---|
| 1490 | OSG_INFO<<"zMax = "<<zMax<<std::endl; |
|---|
| 1491 | |
|---|
| 1492 | if (zMax<0.0) |
|---|
| 1493 | { |
|---|
| 1494 | |
|---|
| 1495 | return false; |
|---|
| 1496 | } |
|---|
| 1497 | |
|---|
| 1498 | double minRatio = 0.0001; |
|---|
| 1499 | double zMin=zMax*minRatio; |
|---|
| 1500 | |
|---|
| 1501 | double fov = positionedLight.light->getSpotCutoff() * 2.0; |
|---|
| 1502 | if(fov < 180.0) |
|---|
| 1503 | { |
|---|
| 1504 | projectionMatrix.makePerspective(fov, 1.0, zMin, zMax); |
|---|
| 1505 | viewMatrix.makeLookAt(positionedLight.lightPos3, |
|---|
| 1506 | positionedLight.lightPos3+positionedLight.lightDir, lightUp); |
|---|
| 1507 | } |
|---|
| 1508 | else |
|---|
| 1509 | { |
|---|
| 1510 | double fovMAX = 160.0f; |
|---|
| 1511 | fov = 0.0; |
|---|
| 1512 | |
|---|
| 1513 | |
|---|
| 1514 | for(Frustum::Vertices::iterator itr = frustum.corners.begin(); |
|---|
| 1515 | itr != frustum.corners.end(); |
|---|
| 1516 | ++itr) |
|---|
| 1517 | { |
|---|
| 1518 | osg::Vec3d cornerDelta(*itr - positionedLight.lightPos3); |
|---|
| 1519 | double length = cornerDelta.length(); |
|---|
| 1520 | |
|---|
| 1521 | if (length==0.0) fov = osg::minimum(fov, 180.0); |
|---|
| 1522 | else |
|---|
| 1523 | { |
|---|
| 1524 | double dotProduct = cornerDelta*positionedLight.lightDir; |
|---|
| 1525 | double angle = 2.0*osg::RadiansToDegrees( acos(dotProduct/length) ); |
|---|
| 1526 | fov = osg::maximum(fov, angle); |
|---|
| 1527 | } |
|---|
| 1528 | } |
|---|
| 1529 | |
|---|
| 1530 | OSG_INFO<<"Computed fov = "<<fov<<std::endl; |
|---|
| 1531 | |
|---|
| 1532 | if (fov>fovMAX) |
|---|
| 1533 | { |
|---|
| 1534 | OSG_INFO<<"Clampping fov = "<<fov<<std::endl; |
|---|
| 1535 | fov=fovMAX; |
|---|
| 1536 | } |
|---|
| 1537 | |
|---|
| 1538 | projectionMatrix.makePerspective(fov, 1.0, zMin, zMax); |
|---|
| 1539 | viewMatrix.makeLookAt(positionedLight.lightPos3, |
|---|
| 1540 | positionedLight.lightPos3+positionedLight.lightDir, lightUp); |
|---|
| 1541 | |
|---|
| 1542 | } |
|---|
| 1543 | } |
|---|
| 1544 | return true; |
|---|
| 1545 | } |
|---|
| 1546 | |
|---|
| 1547 | struct ConvexHull |
|---|
| 1548 | { |
|---|
| 1549 | typedef std::vector<osg::Vec3d> Vertices; |
|---|
| 1550 | typedef std::pair< osg::Vec3d, osg::Vec3d > Edge; |
|---|
| 1551 | typedef std::list< Edge > Edges; |
|---|
| 1552 | |
|---|
| 1553 | Edges _edges; |
|---|
| 1554 | |
|---|
| 1555 | bool valid() const { return !_edges.empty(); } |
|---|
| 1556 | |
|---|
| 1557 | void setToFrustum(ViewDependentShadowMap::Frustum& frustum) |
|---|
| 1558 | { |
|---|
| 1559 | _edges.push_back( Edge(frustum.corners[0],frustum.corners[1]) ); |
|---|
| 1560 | _edges.push_back( Edge(frustum.corners[1],frustum.corners[2]) ); |
|---|
| 1561 | _edges.push_back( Edge(frustum.corners[2],frustum.corners[3]) ); |
|---|
| 1562 | _edges.push_back( Edge(frustum.corners[3],frustum.corners[0]) ); |
|---|
| 1563 | |
|---|
| 1564 | _edges.push_back( Edge(frustum.corners[4],frustum.corners[5]) ); |
|---|
| 1565 | _edges.push_back( Edge(frustum.corners[5],frustum.corners[6]) ); |
|---|
| 1566 | _edges.push_back( Edge(frustum.corners[6],frustum.corners[7]) ); |
|---|
| 1567 | _edges.push_back( Edge(frustum.corners[7],frustum.corners[4]) ); |
|---|
| 1568 | |
|---|
| 1569 | _edges.push_back( Edge(frustum.corners[0],frustum.corners[4]) ); |
|---|
| 1570 | _edges.push_back( Edge(frustum.corners[1],frustum.corners[5]) ); |
|---|
| 1571 | _edges.push_back( Edge(frustum.corners[2],frustum.corners[6]) ); |
|---|
| 1572 | _edges.push_back( Edge(frustum.corners[3],frustum.corners[7]) ); |
|---|
| 1573 | } |
|---|
| 1574 | |
|---|
| 1575 | void transform(const osg::Matrixd& m) |
|---|
| 1576 | { |
|---|
| 1577 | for(Edges::iterator itr = _edges.begin(); |
|---|
| 1578 | itr != _edges.end(); |
|---|
| 1579 | ++itr) |
|---|
| 1580 | { |
|---|
| 1581 | itr->first = itr->first * m; |
|---|
| 1582 | itr->second = itr->second * m; |
|---|
| 1583 | } |
|---|
| 1584 | } |
|---|
| 1585 | |
|---|
| 1586 | void clip(const osg::Plane& plane) |
|---|
| 1587 | { |
|---|
| 1588 | Vertices intersections; |
|---|
| 1589 | |
|---|
| 1590 | |
|---|
| 1591 | for(Edges::iterator itr = _edges.begin(); |
|---|
| 1592 | itr != _edges.end(); |
|---|
| 1593 | ) |
|---|
| 1594 | { |
|---|
| 1595 | double d0 = plane.distance(itr->first); |
|---|
| 1596 | double d1 = plane.distance(itr->second); |
|---|
| 1597 | if (d0<0.0 && d1<0.0) |
|---|
| 1598 | { |
|---|
| 1599 | |
|---|
| 1600 | Edges::iterator to_delete_itr = itr; |
|---|
| 1601 | ++itr; |
|---|
| 1602 | _edges.erase(to_delete_itr); |
|---|
| 1603 | } |
|---|
| 1604 | else if (d0>=0.0 && d1>=0.0) |
|---|
| 1605 | { |
|---|
| 1606 | |
|---|
| 1607 | ++itr; |
|---|
| 1608 | } |
|---|
| 1609 | else |
|---|
| 1610 | { |
|---|
| 1611 | osg::Vec3d& v0 = itr->first; |
|---|
| 1612 | osg::Vec3d& v1 = itr->second; |
|---|
| 1613 | osg::Vec3d intersection = v0 - (v1-v0)*(d0/(d1-d0)); |
|---|
| 1614 | intersections.push_back(intersection); |
|---|
| 1615 | |
|---|
| 1616 | if (d0<0.0) |
|---|
| 1617 | { |
|---|
| 1618 | |
|---|
| 1619 | v0 = intersection; |
|---|
| 1620 | } |
|---|
| 1621 | else |
|---|
| 1622 | { |
|---|
| 1623 | |
|---|
| 1624 | v1 = intersection; |
|---|
| 1625 | } |
|---|
| 1626 | |
|---|
| 1627 | ++itr; |
|---|
| 1628 | } |
|---|
| 1629 | } |
|---|
| 1630 | |
|---|
| 1631 | |
|---|
| 1632 | if (intersections.size() < 2) |
|---|
| 1633 | { |
|---|
| 1634 | return; |
|---|
| 1635 | } |
|---|
| 1636 | |
|---|
| 1637 | if (intersections.size() == 2) |
|---|
| 1638 | { |
|---|
| 1639 | _edges.push_back( Edge(intersections[0], intersections[1]) ); |
|---|
| 1640 | return; |
|---|
| 1641 | } |
|---|
| 1642 | |
|---|
| 1643 | if (intersections.size() == 3) |
|---|
| 1644 | { |
|---|
| 1645 | _edges.push_back( Edge(intersections[0], intersections[1]) ); |
|---|
| 1646 | _edges.push_back( Edge(intersections[1], intersections[2]) ); |
|---|
| 1647 | _edges.push_back( Edge(intersections[2], intersections[0]) ); |
|---|
| 1648 | return; |
|---|
| 1649 | } |
|---|
| 1650 | |
|---|
| 1651 | |
|---|
| 1652 | |
|---|
| 1653 | |
|---|
| 1654 | osg::Vec3d normal(plane.getNormal()); |
|---|
| 1655 | |
|---|
| 1656 | osg::Vec3d side_x = osg::Vec3d(1.0,0.0,0.0) ^ normal; |
|---|
| 1657 | osg::Vec3d side_y = osg::Vec3d(0.0,1.0,0.0) ^ normal; |
|---|
| 1658 | osg::Vec3d side = (side_x.length2()>=side_y.length2()) ? side_x : side_y; |
|---|
| 1659 | side.normalize(); |
|---|
| 1660 | |
|---|
| 1661 | osg::Vec3d up = side ^ normal; |
|---|
| 1662 | up.normalize(); |
|---|
| 1663 | |
|---|
| 1664 | osg::Vec3d center; |
|---|
| 1665 | for(Vertices::iterator itr = intersections.begin(); |
|---|
| 1666 | itr != intersections.end(); |
|---|
| 1667 | ++itr) |
|---|
| 1668 | { |
|---|
| 1669 | center += *itr; |
|---|
| 1670 | } |
|---|
| 1671 | |
|---|
| 1672 | center /= double(intersections.size()); |
|---|
| 1673 | |
|---|
| 1674 | typedef std::map<double, osg::Vec3d> VertexMap; |
|---|
| 1675 | VertexMap vertexMap; |
|---|
| 1676 | for(Vertices::iterator itr = intersections.begin(); |
|---|
| 1677 | itr != intersections.end(); |
|---|
| 1678 | ++itr) |
|---|
| 1679 | { |
|---|
| 1680 | osg::Vec3d dv = (*itr-center); |
|---|
| 1681 | double h = dv * side; |
|---|
| 1682 | double v = dv * up; |
|---|
| 1683 | double angle = atan2(h,v); |
|---|
| 1684 | |
|---|
| 1685 | vertexMap[angle] = *itr; |
|---|
| 1686 | } |
|---|
| 1687 | |
|---|
| 1688 | osg::Vec3d previous_v = vertexMap.rbegin()->second; |
|---|
| 1689 | for(VertexMap::iterator itr = vertexMap.begin(); |
|---|
| 1690 | itr != vertexMap.end(); |
|---|
| 1691 | ++itr) |
|---|
| 1692 | { |
|---|
| 1693 | _edges.push_back(Edge(previous_v, itr->second)); |
|---|
| 1694 | previous_v = itr->second; |
|---|
| 1695 | } |
|---|
| 1696 | |
|---|
| 1697 | |
|---|
| 1698 | } |
|---|
| 1699 | |
|---|
| 1700 | void clip(const osg::Polytope& polytope) |
|---|
| 1701 | { |
|---|
| 1702 | const osg::Polytope::PlaneList& planes = polytope.getPlaneList(); |
|---|
| 1703 | for(osg::Polytope::PlaneList::const_iterator itr = planes.begin(); |
|---|
| 1704 | itr != planes.end(); |
|---|
| 1705 | ++itr) |
|---|
| 1706 | { |
|---|
| 1707 | clip(*itr); |
|---|
| 1708 | } |
|---|
| 1709 | } |
|---|
| 1710 | |
|---|
| 1711 | double min(unsigned int index) const |
|---|
| 1712 | { |
|---|
| 1713 | double m = DBL_MAX; |
|---|
| 1714 | for(Edges::const_iterator itr = _edges.begin(); |
|---|
| 1715 | itr != _edges.end(); |
|---|
| 1716 | ++itr) |
|---|
| 1717 | { |
|---|
| 1718 | const Edge& edge = *itr; |
|---|
| 1719 | if (edge.first[index]<m) m = edge.first[index]; |
|---|
| 1720 | if (edge.second[index]<m) m = edge.second[index]; |
|---|
| 1721 | } |
|---|
| 1722 | return m; |
|---|
| 1723 | } |
|---|
| 1724 | |
|---|
| 1725 | double max(unsigned int index) const |
|---|
| 1726 | { |
|---|
| 1727 | double m = -DBL_MAX; |
|---|
| 1728 | for(Edges::const_iterator itr = _edges.begin(); |
|---|
| 1729 | itr != _edges.end(); |
|---|
| 1730 | ++itr) |
|---|
| 1731 | { |
|---|
| 1732 | const Edge& edge = *itr; |
|---|
| 1733 | if (edge.first[index]>m) m = edge.first[index]; |
|---|
| 1734 | if (edge.second[index]>m) m = edge.second[index]; |
|---|
| 1735 | } |
|---|
| 1736 | return m; |
|---|
| 1737 | } |
|---|
| 1738 | |
|---|
| 1739 | double minRatio(const osg::Vec3d& eye, unsigned int index) const |
|---|
| 1740 | { |
|---|
| 1741 | double m = DBL_MAX; |
|---|
| 1742 | osg::Vec3d delta; |
|---|
| 1743 | double ratio; |
|---|
| 1744 | for(Edges::const_iterator itr = _edges.begin(); |
|---|
| 1745 | itr != _edges.end(); |
|---|
| 1746 | ++itr) |
|---|
| 1747 | { |
|---|
| 1748 | const Edge& edge = *itr; |
|---|
| 1749 | |
|---|
| 1750 | delta = edge.first-eye; |
|---|
| 1751 | ratio = delta[index]/delta[1]; |
|---|
| 1752 | if (ratio<m) m = ratio; |
|---|
| 1753 | |
|---|
| 1754 | delta = edge.second-eye; |
|---|
| 1755 | ratio = delta[index]/delta[1]; |
|---|
| 1756 | if (ratio<m) m = ratio; |
|---|
| 1757 | } |
|---|
| 1758 | return m; |
|---|
| 1759 | } |
|---|
| 1760 | |
|---|
| 1761 | double maxRatio(const osg::Vec3d& eye, unsigned int index) const |
|---|
| 1762 | { |
|---|
| 1763 | double m = -DBL_MAX; |
|---|
| 1764 | osg::Vec3d delta; |
|---|
| 1765 | double ratio; |
|---|
| 1766 | for(Edges::const_iterator itr = _edges.begin(); |
|---|
| 1767 | itr != _edges.end(); |
|---|
| 1768 | ++itr) |
|---|
| 1769 | { |
|---|
| 1770 | const Edge& edge = *itr; |
|---|
| 1771 | |
|---|
| 1772 | delta = edge.first-eye; |
|---|
| 1773 | ratio = delta[index]/delta[1]; |
|---|
| 1774 | if (ratio>m) m = ratio; |
|---|
| 1775 | |
|---|
| 1776 | delta = edge.second-eye; |
|---|
| 1777 | ratio = delta[index]/delta[1]; |
|---|
| 1778 | if (ratio>m) m = ratio; |
|---|
| 1779 | } |
|---|
| 1780 | return m; |
|---|
| 1781 | } |
|---|
| 1782 | |
|---|
| 1783 | void output(std::ostream& out) |
|---|
| 1784 | { |
|---|
| 1785 | out<<"ConvexHull"<<std::endl; |
|---|
| 1786 | for(Edges::const_iterator itr = _edges.begin(); |
|---|
| 1787 | itr != _edges.end(); |
|---|
| 1788 | ++itr) |
|---|
| 1789 | { |
|---|
| 1790 | const Edge& edge = *itr; |
|---|
| 1791 | out<<" edge ("<<edge.first<<") ("<<edge.second<<")"<<std::endl; |
|---|
| 1792 | } |
|---|
| 1793 | } |
|---|
| 1794 | }; |
|---|
| 1795 | |
|---|
| 1796 | |
|---|
| 1797 | struct RenderLeafBounds |
|---|
| 1798 | { |
|---|
| 1799 | RenderLeafBounds(): |
|---|
| 1800 | computeRatios(false), |
|---|
| 1801 | numRenderLeaf(0), |
|---|
| 1802 | n(0.0), |
|---|
| 1803 | previous_modelview(0), |
|---|
| 1804 | clip_min_x(-1.0), clip_max_x(1.0), |
|---|
| 1805 | clip_min_y(-1.0), clip_max_y(1.0), |
|---|
| 1806 | clip_min_z(-1.0), clip_max_z(1.0), |
|---|
| 1807 | clip_min_x_ratio(-DBL_MAX), clip_max_x_ratio(DBL_MAX), |
|---|
| 1808 | clip_min_z_ratio(-DBL_MAX), clip_max_z_ratio(DBL_MAX), |
|---|
| 1809 | min_x_ratio(DBL_MAX), max_x_ratio(-DBL_MAX), |
|---|
| 1810 | min_z_ratio(DBL_MAX), max_z_ratio(-DBL_MAX), |
|---|
| 1811 | min_x(1.0), max_x(-1.0), |
|---|
| 1812 | min_y(1.0), max_y(-1.0), |
|---|
| 1813 | min_z(1.0), max_z(-1.0) |
|---|
| 1814 | { |
|---|
| 1815 | |
|---|
| 1816 | } |
|---|
| 1817 | |
|---|
| 1818 | void set(const osg::Matrixd& p) |
|---|
| 1819 | { |
|---|
| 1820 | computeRatios = false; |
|---|
| 1821 | light_p = p; |
|---|
| 1822 | clip_min_x = -DBL_MAX; clip_max_x = DBL_MAX; |
|---|
| 1823 | clip_min_y = -DBL_MAX; clip_max_y = DBL_MAX; |
|---|
| 1824 | clip_min_z = -DBL_MAX; clip_max_z = DBL_MAX; |
|---|
| 1825 | min_x = DBL_MAX; max_x = -DBL_MAX; |
|---|
| 1826 | min_y = DBL_MAX; max_y = -DBL_MAX; |
|---|
| 1827 | min_z = DBL_MAX; max_z = -DBL_MAX; |
|---|
| 1828 | } |
|---|
| 1829 | |
|---|
| 1830 | void set(const osg::Matrixd& p, osg::Vec3d& e_ls, double nr) |
|---|
| 1831 | { |
|---|
| 1832 | computeRatios = true; |
|---|
| 1833 | light_p = p; |
|---|
| 1834 | eye_ls = e_ls; |
|---|
| 1835 | n = nr; |
|---|
| 1836 | } |
|---|
| 1837 | |
|---|
| 1838 | void operator() (const osgUtil::RenderLeaf* renderLeaf) |
|---|
| 1839 | { |
|---|
| 1840 | ++numRenderLeaf; |
|---|
| 1841 | |
|---|
| 1842 | if (renderLeaf->_modelview.get()!=previous_modelview) |
|---|
| 1843 | { |
|---|
| 1844 | previous_modelview = renderLeaf->_modelview.get(); |
|---|
| 1845 | if (previous_modelview) |
|---|
| 1846 | { |
|---|
| 1847 | light_mvp.mult(*renderLeaf->_modelview, light_p); |
|---|
| 1848 | } |
|---|
| 1849 | else |
|---|
| 1850 | { |
|---|
| 1851 | |
|---|
| 1852 | |
|---|
| 1853 | light_mvp = light_p; |
|---|
| 1854 | } |
|---|
| 1855 | |
|---|
| 1856 | } |
|---|
| 1857 | else |
|---|
| 1858 | { |
|---|
| 1859 | |
|---|
| 1860 | } |
|---|
| 1861 | |
|---|
| 1862 | const osg::BoundingBox& bb = renderLeaf->_drawable->getBound(); |
|---|
| 1863 | if (bb.valid()) |
|---|
| 1864 | { |
|---|
| 1865 | |
|---|
| 1866 | handle(osg::Vec3d(bb.xMin(),bb.yMin(),bb.zMin())); |
|---|
| 1867 | handle(osg::Vec3d(bb.xMax(),bb.yMin(),bb.zMin())); |
|---|
| 1868 | handle(osg::Vec3d(bb.xMin(),bb.yMax(),bb.zMin())); |
|---|
| 1869 | handle(osg::Vec3d(bb.xMax(),bb.yMax(),bb.zMin())); |
|---|
| 1870 | handle(osg::Vec3d(bb.xMin(),bb.yMin(),bb.zMax())); |
|---|
| 1871 | handle(osg::Vec3d(bb.xMax(),bb.yMin(),bb.zMax())); |
|---|
| 1872 | handle(osg::Vec3d(bb.xMin(),bb.yMax(),bb.zMax())); |
|---|
| 1873 | handle(osg::Vec3d(bb.xMax(),bb.yMax(),bb.zMax())); |
|---|
| 1874 | } |
|---|
| 1875 | else |
|---|
| 1876 | { |
|---|
| 1877 | OSG_INFO<<"bb invalid"<<std::endl; |
|---|
| 1878 | } |
|---|
| 1879 | } |
|---|
| 1880 | |
|---|
| 1881 | void handle(const osg::Vec3d& v) |
|---|
| 1882 | { |
|---|
| 1883 | osg::Vec3d ls = v * light_mvp; |
|---|
| 1884 | |
|---|
| 1885 | |
|---|
| 1886 | |
|---|
| 1887 | if (computeRatios) |
|---|
| 1888 | { |
|---|
| 1889 | osg::Vec3d delta = ls-eye_ls; |
|---|
| 1890 | |
|---|
| 1891 | double x_ratio, z_ratio; |
|---|
| 1892 | if (delta.y()>n) |
|---|
| 1893 | { |
|---|
| 1894 | x_ratio = delta.x()/delta.y(); |
|---|
| 1895 | z_ratio = delta.z()/delta.y(); |
|---|
| 1896 | } |
|---|
| 1897 | else |
|---|
| 1898 | { |
|---|
| 1899 | x_ratio = delta.x()/n; |
|---|
| 1900 | z_ratio = delta.z()/n; |
|---|
| 1901 | } |
|---|
| 1902 | |
|---|
| 1903 | if (x_ratio<min_x_ratio) min_x_ratio = x_ratio; |
|---|
| 1904 | if (x_ratio>max_x_ratio) max_x_ratio = x_ratio; |
|---|
| 1905 | if (z_ratio<min_z_ratio) min_z_ratio = z_ratio; |
|---|
| 1906 | if (z_ratio>max_z_ratio) max_z_ratio = z_ratio; |
|---|
| 1907 | } |
|---|
| 1908 | |
|---|
| 1909 | |
|---|
| 1910 | if (ls.x()<clip_min_x) ls.x()=clip_min_x; |
|---|
| 1911 | if (ls.x()>clip_max_x) ls.x()=clip_max_x; |
|---|
| 1912 | if (ls.y()<clip_min_y) ls.y()=clip_min_y; |
|---|
| 1913 | if (ls.y()>clip_max_y) ls.y()=clip_max_y; |
|---|
| 1914 | if (ls.z()<clip_min_z) ls.z()=clip_min_z; |
|---|
| 1915 | if (ls.z()>clip_max_z) ls.z()=clip_max_z; |
|---|
| 1916 | |
|---|
| 1917 | |
|---|
| 1918 | if (ls.x()<min_x) min_x=ls.x(); |
|---|
| 1919 | if (ls.x()>max_x) max_x=ls.x(); |
|---|
| 1920 | if (ls.y()<min_y) min_y=ls.y(); |
|---|
| 1921 | if (ls.y()>max_y) max_y=ls.y(); |
|---|
| 1922 | if (ls.z()<min_z) { min_z=ls.z(); } |
|---|
| 1923 | if (ls.z()>max_z) { max_z=ls.z(); } |
|---|
| 1924 | |
|---|
| 1925 | |
|---|
| 1926 | |
|---|
| 1927 | } |
|---|
| 1928 | |
|---|
| 1929 | bool computeRatios; |
|---|
| 1930 | |
|---|
| 1931 | unsigned int numRenderLeaf; |
|---|
| 1932 | |
|---|
| 1933 | osg::Matrixd light_p; |
|---|
| 1934 | osg::Vec3d eye_ls; |
|---|
| 1935 | double n; |
|---|
| 1936 | |
|---|
| 1937 | osg::Matrixd light_mvp; |
|---|
| 1938 | osg::RefMatrix* previous_modelview; |
|---|
| 1939 | unsigned int numLeaves; |
|---|
| 1940 | |
|---|
| 1941 | double clip_min_x, clip_max_x; |
|---|
| 1942 | double clip_min_y, clip_max_y; |
|---|
| 1943 | double clip_min_z, clip_max_z; |
|---|
| 1944 | |
|---|
| 1945 | double clip_min_x_ratio, clip_max_x_ratio; |
|---|
| 1946 | double clip_min_z_ratio, clip_max_z_ratio; |
|---|
| 1947 | |
|---|
| 1948 | double min_x_ratio, max_x_ratio; |
|---|
| 1949 | double min_z_ratio, max_z_ratio; |
|---|
| 1950 | double min_x, max_x; |
|---|
| 1951 | double min_y, max_y; |
|---|
| 1952 | double min_z, max_z; |
|---|
| 1953 | }; |
|---|
| 1954 | |
|---|
| 1955 | bool ViewDependentShadowMap::adjustPerspectiveShadowMapCameraSettings(osgUtil::RenderStage* renderStage, Frustum& frustum, LightData& positionedLight, osg::Camera* camera) |
|---|
| 1956 | { |
|---|
| 1957 | const ShadowSettings* settings = getShadowedScene()->getShadowSettings(); |
|---|
| 1958 | |
|---|
| 1959 | |
|---|
| 1960 | |
|---|
| 1961 | |
|---|
| 1962 | osg::Matrixd light_p = camera->getProjectionMatrix(); |
|---|
| 1963 | osg::Matrixd light_v = camera->getViewMatrix(); |
|---|
| 1964 | osg::Matrixd light_vp = light_v * light_p; |
|---|
| 1965 | osg::Vec3d lightdir(0.0,0.0,-1.0); |
|---|
| 1966 | |
|---|
| 1967 | |
|---|
| 1968 | bool orthographicLightSpaceProjection = light_p(0,3)==0.0 && light_p(1,3)==0.0 && light_p(2,3)==0.0; |
|---|
| 1969 | |
|---|
| 1970 | if (!orthographicLightSpaceProjection) |
|---|
| 1971 | { |
|---|
| 1972 | OSG_INFO<<"perspective light space projection not yet supported."<<std::endl; |
|---|
| 1973 | return false; |
|---|
| 1974 | } |
|---|
| 1975 | |
|---|
| 1976 | |
|---|
| 1977 | |
|---|
| 1978 | |
|---|
| 1979 | |
|---|
| 1980 | ConvexHull convexHull; |
|---|
| 1981 | convexHull.setToFrustum(frustum); |
|---|
| 1982 | |
|---|
| 1983 | #if 0 |
|---|
| 1984 | OSG_NOTICE<<"ws ConvexHull xMin="<<convexHull.min(0)<<", xMax="<<convexHull.max(0)<<std::endl; |
|---|
| 1985 | OSG_NOTICE<<"ws ConvexHull yMin="<<convexHull.min(1)<<", yMax="<<convexHull.max(1)<<std::endl; |
|---|
| 1986 | OSG_NOTICE<<"ws ConvexHull zMin="<<convexHull.min(2)<<", zMax="<<convexHull.max(2)<<std::endl; |
|---|
| 1987 | |
|---|
| 1988 | convexHull.output(osg::notify(osg::NOTICE)); |
|---|
| 1989 | #endif |
|---|
| 1990 | |
|---|
| 1991 | convexHull.transform(light_vp); |
|---|
| 1992 | |
|---|
| 1993 | #if 0 |
|---|
| 1994 | convexHull.output(osg::notify(osg::NOTICE)); |
|---|
| 1995 | |
|---|
| 1996 | OSG_NOTICE<<"ls ConvexHull xMin="<<convexHull.min(0)<<", xMax="<<convexHull.max(0)<<std::endl; |
|---|
| 1997 | OSG_NOTICE<<"ls ConvexHull yMin="<<convexHull.min(1)<<", yMax="<<convexHull.max(1)<<std::endl; |
|---|
| 1998 | OSG_NOTICE<<"ls ConvexHull zMin="<<convexHull.min(2)<<", zMax="<<convexHull.max(2)<<std::endl; |
|---|
| 1999 | #endif |
|---|
| 2000 | |
|---|
| 2001 | #if 0 |
|---|
| 2002 | |
|---|
| 2003 | { |
|---|
| 2004 | convexHull.clip(osg::Plane(0.0,0.0,1,1.0)); |
|---|
| 2005 | convexHull.clip(osg::Plane(0.0,0.0,-1,1.0)); |
|---|
| 2006 | } |
|---|
| 2007 | #endif |
|---|
| 2008 | |
|---|
| 2009 | #if 1 |
|---|
| 2010 | if (renderStage) |
|---|
| 2011 | { |
|---|
| 2012 | #if 1 |
|---|
| 2013 | osg::ElapsedTime timer; |
|---|
| 2014 | #endif |
|---|
| 2015 | |
|---|
| 2016 | RenderLeafTraverser<RenderLeafBounds> rli; |
|---|
| 2017 | rli.set(light_p); |
|---|
| 2018 | rli.traverse(renderStage); |
|---|
| 2019 | |
|---|
| 2020 | if (rli.numRenderLeaf==0) |
|---|
| 2021 | { |
|---|
| 2022 | return false; |
|---|
| 2023 | } |
|---|
| 2024 | #if 0 |
|---|
| 2025 | OSG_NOTICE<<"New Time for RenderLeafTraverser "<<timer.elapsedTime_m()<<"ms, number of render leaves "<<rli.numRenderLeaf<<std::endl; |
|---|
| 2026 | OSG_NOTICE<<" scene bounds min_x="<<rli.min_x<<", max_x="<<rli.max_x<<std::endl; |
|---|
| 2027 | OSG_NOTICE<<" scene bounds min_y="<<rli.min_y<<", max_y="<<rli.max_y<<std::endl; |
|---|
| 2028 | OSG_NOTICE<<" scene bounds min_z="<<rli.min_z<<", max_z="<<rli.max_z<<std::endl; |
|---|
| 2029 | #endif |
|---|
| 2030 | |
|---|
| 2031 | #if 0 |
|---|
| 2032 | double widest_x = osg::maximum(fabs(rli.min_x), fabs(rli.max_x)); |
|---|
| 2033 | double widest_y = osg::maximum(fabs(rli.min_y), fabs(rli.max_y)); |
|---|
| 2034 | double widest_z = osg::maximum(fabs(rli.min_z), fabs(rli.max_z)); |
|---|
| 2035 | #endif |
|---|
| 2036 | |
|---|
| 2037 | #if 1 |
|---|
| 2038 | #if 1 |
|---|
| 2039 | convexHull.clip(osg::Plane(1.0,0.0,0.0,-rli.min_x)); |
|---|
| 2040 | convexHull.clip(osg::Plane(-1.0,0.0,0.0,rli.max_x)); |
|---|
| 2041 | #else |
|---|
| 2042 | convexHull.clip(osg::Plane(1.0,0.0,0.0,widest_x)); |
|---|
| 2043 | convexHull.clip(osg::Plane(-1.0,0.0,0.0,widest_x)); |
|---|
| 2044 | #endif |
|---|
| 2045 | #if 1 |
|---|
| 2046 | convexHull.clip(osg::Plane(0.0,1.0,0.0,-rli.min_y)); |
|---|
| 2047 | convexHull.clip(osg::Plane(0.0,-1.0,0.0,rli.max_y)); |
|---|
| 2048 | #endif |
|---|
| 2049 | #endif |
|---|
| 2050 | |
|---|
| 2051 | #if 0 |
|---|
| 2052 | convexHull.clip(osg::Plane(0.0,0.0,1.0,-rli.min_z)); |
|---|
| 2053 | convexHull.clip(osg::Plane(0.0,0.0,-1.0,rli.max_z)); |
|---|
| 2054 | #else |
|---|
| 2055 | convexHull.clip(osg::Plane(0.0,0.0,1.0,1.0)); |
|---|
| 2056 | convexHull.clip(osg::Plane(0.0,0.0,-1.0,1.0)); |
|---|
| 2057 | #endif |
|---|
| 2058 | |
|---|
| 2059 | #if 0 |
|---|
| 2060 | OSG_NOTICE<<"widest_x = "<<widest_x<<std::endl; |
|---|
| 2061 | OSG_NOTICE<<"widest_y = "<<widest_y<<std::endl; |
|---|
| 2062 | OSG_NOTICE<<"widest_z = "<<widest_z<<std::endl; |
|---|
| 2063 | #endif |
|---|
| 2064 | } |
|---|
| 2065 | #endif |
|---|
| 2066 | |
|---|
| 2067 | #if 0 |
|---|
| 2068 | convexHull.output(osg::notify(osg::NOTICE)); |
|---|
| 2069 | |
|---|
| 2070 | OSG_NOTICE<<"after clipped ls ConvexHull xMin="<<convexHull.min(0)<<", xMax="<<convexHull.max(0)<<std::endl; |
|---|
| 2071 | OSG_NOTICE<<"after clipped ls ConvexHull yMin="<<convexHull.min(1)<<", yMax="<<convexHull.max(1)<<std::endl; |
|---|
| 2072 | OSG_NOTICE<<"after clipped ls ConvexHull zMin="<<convexHull.min(2)<<", zMax="<<convexHull.max(2)<<std::endl; |
|---|
| 2073 | #endif |
|---|
| 2074 | |
|---|
| 2075 | double xMin=-1.0, xMax=1.0; |
|---|
| 2076 | double yMin=-1.0, yMax=1.0; |
|---|
| 2077 | double zMin=-1.0, zMax=1.0; |
|---|
| 2078 | |
|---|
| 2079 | if (convexHull.valid()) |
|---|
| 2080 | { |
|---|
| 2081 | double widest_x = osg::maximum(fabs(convexHull.min(0)), fabs(convexHull.max(0))); |
|---|
| 2082 | xMin = osg::maximum(-1.0,-widest_x); |
|---|
| 2083 | xMax = osg::minimum(1.0,widest_x); |
|---|
| 2084 | yMin = osg::maximum(-1.0,convexHull.min(1)); |
|---|
| 2085 | yMax = osg::minimum(1.0,convexHull.max(1)); |
|---|
| 2086 | } |
|---|
| 2087 | else |
|---|
| 2088 | { |
|---|
| 2089 | |
|---|
| 2090 | convexHull.setToFrustum(frustum); |
|---|
| 2091 | convexHull.transform(light_vp); |
|---|
| 2092 | } |
|---|
| 2093 | |
|---|
| 2094 | #if 0 |
|---|
| 2095 | OSG_NOTICE<<"xMin = "<<xMin<<", \txMax = "<<xMax<<std::endl; |
|---|
| 2096 | OSG_NOTICE<<"yMin = "<<yMin<<", \tyMax = "<<yMax<<std::endl; |
|---|
| 2097 | OSG_NOTICE<<"zMin = "<<zMin<<", \tzMax = "<<zMax<<std::endl; |
|---|
| 2098 | #endif |
|---|
| 2099 | |
|---|
| 2100 | #if 1 |
|---|
| 2101 | |
|---|
| 2102 | zMin = -1.0; |
|---|
| 2103 | if (xMin!=-1.0 || yMin!=-1.0 || zMin!=-1.0 || |
|---|
| 2104 | xMax!=1.0 || yMax!=1.0 || zMax!=1.0) |
|---|
| 2105 | { |
|---|
| 2106 | osg::Matrix m; |
|---|
| 2107 | m.makeTranslate(osg::Vec3d(-0.5*(xMax+xMin), |
|---|
| 2108 | -0.5*(yMax+yMin), |
|---|
| 2109 | -0.5*(zMax+zMin))); |
|---|
| 2110 | |
|---|
| 2111 | m.postMultScale(osg::Vec3d(2.0/(xMax-xMin), |
|---|
| 2112 | 2.0/(yMax-yMin), |
|---|
| 2113 | 2.0/(zMax-zMin))); |
|---|
| 2114 | |
|---|
| 2115 | convexHull.transform(m); |
|---|
| 2116 | light_p.postMult(m); |
|---|
| 2117 | light_vp = light_v * light_p; |
|---|
| 2118 | |
|---|
| 2119 | #if 0 |
|---|
| 2120 | OSG_NOTICE<<"Adjusting projection matrix "<<m<<std::endl; |
|---|
| 2121 | convexHull.output(osg::notify(osg::NOTICE)); |
|---|
| 2122 | #endif |
|---|
| 2123 | camera->setProjectionMatrix(light_p); |
|---|
| 2124 | } |
|---|
| 2125 | |
|---|
| 2126 | #endif |
|---|
| 2127 | |
|---|
| 2128 | osg::Vec3d eye_v = frustum.eye * light_v; |
|---|
| 2129 | |
|---|
| 2130 | osg::Vec3d center_v = frustum.center * light_v; |
|---|
| 2131 | osg::Vec3d viewdir_v = center_v-eye_v; viewdir_v.normalize(); |
|---|
| 2132 | |
|---|
| 2133 | double dotProduct_v = lightdir * viewdir_v; |
|---|
| 2134 | double gamma_v = acos(dotProduct_v); |
|---|
| 2135 | if (gamma_v<osg::DegreesToRadians(settings->getPerspectiveShadowMapCutOffAngle()) || gamma_v>osg::DegreesToRadians(180-settings->getPerspectiveShadowMapCutOffAngle())) |
|---|
| 2136 | { |
|---|
| 2137 | |
|---|
| 2138 | return true; |
|---|
| 2139 | } |
|---|
| 2140 | |
|---|
| 2141 | |
|---|
| 2142 | |
|---|
| 2143 | |
|---|
| 2144 | |
|---|
| 2145 | osg::Vec3d eye_ls = frustum.eye * light_vp; |
|---|
| 2146 | #if 0 |
|---|
| 2147 | if (eye_ls.y()>-1.0) |
|---|
| 2148 | { |
|---|
| 2149 | OSG_NOTICE<<"Eye point within light space - use standard shadow map."<<std::endl; |
|---|
| 2150 | return true; |
|---|
| 2151 | } |
|---|
| 2152 | #endif |
|---|
| 2153 | |
|---|
| 2154 | |
|---|
| 2155 | |
|---|
| 2156 | osg::Vec3d center_ls = frustum.center * light_vp; |
|---|
| 2157 | osg::Vec3d viewdir_ls = center_ls-eye_ls; viewdir_ls.normalize(); |
|---|
| 2158 | |
|---|
| 2159 | osg::Vec3d side = lightdir ^ viewdir_ls; side.normalize(); |
|---|
| 2160 | osg::Vec3d up = side ^ lightdir; |
|---|
| 2161 | |
|---|
| 2162 | double d = 2.0; |
|---|
| 2163 | |
|---|
| 2164 | double alpha = osg::DegreesToRadians(30.0); |
|---|
| 2165 | double n = tan(alpha)*tan(osg::PI_2-gamma_v)*tan(osg::PI_2-gamma_v); |
|---|
| 2166 | |
|---|
| 2167 | |
|---|
| 2168 | |
|---|
| 2169 | double min_n = osg::maximum(-1.0-eye_ls.y(), settings->getMinimumShadowMapNearFarRatio()); |
|---|
| 2170 | if (n<min_n) |
|---|
| 2171 | { |
|---|
| 2172 | |
|---|
| 2173 | n=min_n; |
|---|
| 2174 | } |
|---|
| 2175 | |
|---|
| 2176 | |
|---|
| 2177 | |
|---|
| 2178 | |
|---|
| 2179 | |
|---|
| 2180 | |
|---|
| 2181 | |
|---|
| 2182 | double f = n+d; |
|---|
| 2183 | |
|---|
| 2184 | double a = (f+n)/(f-n); |
|---|
| 2185 | double b = -2.0*f*n/(f-n); |
|---|
| 2186 | |
|---|
| 2187 | osg::Vec3d virtual_eye(0.0,-1.0-n, eye_ls.z()); |
|---|
| 2188 | |
|---|
| 2189 | osg::Matrixd lightView; |
|---|
| 2190 | lightView.makeLookAt(virtual_eye, virtual_eye+lightdir, up); |
|---|
| 2191 | |
|---|
| 2192 | #if 0 |
|---|
| 2193 | OSG_NOTICE<<"n = "<<n<<", f="<<f<<std::endl; |
|---|
| 2194 | OSG_NOTICE<<"eye_ls = "<<eye_ls<<", virtual_eye="<<virtual_eye<<std::endl; |
|---|
| 2195 | OSG_NOTICE<<"frustum.eyes="<<frustum.eye<<std::endl; |
|---|
| 2196 | #endif |
|---|
| 2197 | |
|---|
| 2198 | double min_x_ratio = 0.0; |
|---|
| 2199 | double max_x_ratio = 0.0; |
|---|
| 2200 | double min_z_ratio = FLT_MAX; |
|---|
| 2201 | double max_z_ratio = -FLT_MAX; |
|---|
| 2202 | |
|---|
| 2203 | min_x_ratio = convexHull.valid() ? convexHull.minRatio(virtual_eye,0) : -DBL_MAX; |
|---|
| 2204 | max_x_ratio = convexHull.valid() ? convexHull.maxRatio(virtual_eye,0) : DBL_MAX; |
|---|
| 2205 | |
|---|
| 2206 | |
|---|
| 2207 | |
|---|
| 2208 | #if 0 |
|---|
| 2209 | OSG_NOTICE<<"convexHull min_x_ratio = "<<min_x_ratio<<std::endl; |
|---|
| 2210 | OSG_NOTICE<<"convexHull max_x_ratio = "<<max_x_ratio<<std::endl; |
|---|
| 2211 | OSG_NOTICE<<"convexHull min_z_ratio = "<<min_z_ratio<<std::endl; |
|---|
| 2212 | OSG_NOTICE<<"convexHull max_z_ratio = "<<max_z_ratio<<std::endl; |
|---|
| 2213 | #endif |
|---|
| 2214 | |
|---|
| 2215 | #if 1 |
|---|
| 2216 | if (renderStage) |
|---|
| 2217 | { |
|---|
| 2218 | #if 1 |
|---|
| 2219 | osg::ElapsedTime timer; |
|---|
| 2220 | #endif |
|---|
| 2221 | |
|---|
| 2222 | RenderLeafTraverser<RenderLeafBounds> rli; |
|---|
| 2223 | rli.set(light_p, virtual_eye, n); |
|---|
| 2224 | rli.traverse(renderStage); |
|---|
| 2225 | |
|---|
| 2226 | if (rli.numRenderLeaf==0) |
|---|
| 2227 | { |
|---|
| 2228 | return false; |
|---|
| 2229 | } |
|---|
| 2230 | |
|---|
| 2231 | #if 0 |
|---|
| 2232 | OSG_NOTICE<<"Time for RenderLeafTraverser "<<timer.elapsedTime_m()<<"ms, number of render leaves "<<rli.numRenderLeaf<<std::endl; |
|---|
| 2233 | OSG_NOTICE<<"scene bounds min_x="<<rli.min_x<<", max_x="<<rli.max_x<<std::endl; |
|---|
| 2234 | OSG_NOTICE<<"scene bounds min_y="<<rli.min_y<<", max_y="<<rli.max_y<<std::endl; |
|---|
| 2235 | OSG_NOTICE<<"scene bounds min_z="<<rli.min_z<<", max_z="<<rli.max_z<<std::endl; |
|---|
| 2236 | OSG_NOTICE<<"min_x_ratio="<<rli.min_x_ratio<<", max_x_ratio="<<rli.max_x_ratio<<std::endl; |
|---|
| 2237 | OSG_NOTICE<<"min_z_ratio="<<rli.min_z_ratio<<", max_z_ratio="<<rli.max_z_ratio<<std::endl; |
|---|
| 2238 | #endif |
|---|
| 2239 | if (rli.min_x_ratio>min_x_ratio) min_x_ratio = rli.min_x_ratio; |
|---|
| 2240 | if (rli.max_x_ratio<max_x_ratio) max_x_ratio = rli.max_x_ratio; |
|---|
| 2241 | |
|---|
| 2242 | min_z_ratio = rli.min_z_ratio; |
|---|
| 2243 | max_z_ratio = rli.max_z_ratio; |
|---|
| 2244 | } |
|---|
| 2245 | #endif |
|---|
| 2246 | double best_x_ratio = osg::maximum(fabs(min_x_ratio),fabs(max_x_ratio)); |
|---|
| 2247 | double best_z_ratio = osg::maximum(fabs(min_z_ratio),fabs(max_z_ratio)); |
|---|
| 2248 | |
|---|
| 2249 | |
|---|
| 2250 | #if 0 |
|---|
| 2251 | OSG_NOTICE<<"min_x_ratio = "<<min_x_ratio<<std::endl; |
|---|
| 2252 | OSG_NOTICE<<"max_x_ratio = "<<max_x_ratio<<std::endl; |
|---|
| 2253 | OSG_NOTICE<<"best_x_ratio = "<<best_x_ratio<<std::endl; |
|---|
| 2254 | OSG_NOTICE<<"min_z_ratio = "<<min_z_ratio<<std::endl; |
|---|
| 2255 | OSG_NOTICE<<"max_z_ratio = "<<max_z_ratio<<std::endl; |
|---|
| 2256 | OSG_NOTICE<<"best_z_ratio = "<<best_z_ratio<<std::endl; |
|---|
| 2257 | #endif |
|---|
| 2258 | |
|---|
| 2259 | |
|---|
| 2260 | |
|---|
| 2261 | osg::Matrixd lightPerspective( 1.0/best_x_ratio, 0.0, 0.0, 0.0, |
|---|
| 2262 | 0.0, a, 0.0, 1.0, |
|---|
| 2263 | 0.0, 0.0, 1.0/best_z_ratio, 0.0, |
|---|
| 2264 | 0.0, b, 0.0, 0.0 ); |
|---|
| 2265 | osg::Matrixd light_persp = light_p * lightView * lightPerspective; |
|---|
| 2266 | |
|---|
| 2267 | #if 0 |
|---|
| 2268 | OSG_NOTICE<<"light_p = "<<light_p<<std::endl; |
|---|
| 2269 | OSG_NOTICE<<"lightView = "<<lightView<<std::endl; |
|---|
| 2270 | OSG_NOTICE<<"lightPerspective = "<<lightPerspective<<std::endl; |
|---|
| 2271 | OSG_NOTICE<<"light_persp result = "<<light_persp<<std::endl; |
|---|
| 2272 | #endif |
|---|
| 2273 | camera->setProjectionMatrix(light_persp); |
|---|
| 2274 | |
|---|
| 2275 | return true; |
|---|
| 2276 | } |
|---|
| 2277 | |
|---|
| 2278 | bool ViewDependentShadowMap::assignTexGenSettings(osgUtil::CullVisitor* cv, osg::Camera* camera, unsigned int textureUnit, osg::TexGen* texgen) |
|---|
| 2279 | { |
|---|
| 2280 | OSG_INFO<<"assignTexGenSettings() textureUnit="<<textureUnit<<" texgen="<<texgen<<std::endl; |
|---|
| 2281 | |
|---|
| 2282 | texgen->setMode(osg::TexGen::EYE_LINEAR); |
|---|
| 2283 | |
|---|
| 2284 | |
|---|
| 2285 | |
|---|
| 2286 | |
|---|
| 2287 | texgen->setPlanesFromMatrix( camera->getProjectionMatrix() * |
|---|
| 2288 | osg::Matrix::translate(1.0,1.0,1.0) * |
|---|
| 2289 | osg::Matrix::scale(0.5,0.5,0.5) ); |
|---|
| 2290 | |
|---|
| 2291 | |
|---|
| 2292 | osg::ref_ptr<osg::RefMatrix> refMatrix = |
|---|
| 2293 | new osg::RefMatrix( camera->getInverseViewMatrix() * (*(cv->getModelViewMatrix())) ); |
|---|
| 2294 | |
|---|
| 2295 | cv->getRenderStage()->getPositionalStateContainer()->addPositionedTextureAttribute( textureUnit, refMatrix.get(), texgen ); |
|---|
| 2296 | |
|---|
| 2297 | |
|---|
| 2298 | return true; |
|---|
| 2299 | } |
|---|
| 2300 | |
|---|
| 2301 | void ViewDependentShadowMap::cullShadowReceivingScene(osgUtil::CullVisitor* cv) const |
|---|
| 2302 | { |
|---|
| 2303 | OSG_INFO<<"cullShadowReceivingScene()"<<std::endl; |
|---|
| 2304 | |
|---|
| 2305 | #if 0 |
|---|
| 2306 | |
|---|
| 2307 | unsigned int traversalMask = cv->getTraversalMask(); |
|---|
| 2308 | |
|---|
| 2309 | cv->setTraversalMask( traversalMask & _shadowedScene->getReceivesShadowTraversalMask() ); |
|---|
| 2310 | #endif |
|---|
| 2311 | |
|---|
| 2312 | _shadowedScene->osg::Group::traverse(*cv); |
|---|
| 2313 | |
|---|
| 2314 | #if 0 |
|---|
| 2315 | cv->setTraversalMask( traversalMask ); |
|---|
| 2316 | #endif |
|---|
| 2317 | |
|---|
| 2318 | return; |
|---|
| 2319 | } |
|---|
| 2320 | |
|---|
| 2321 | void ViewDependentShadowMap::cullShadowCastingScene(osgUtil::CullVisitor* cv, osg::Camera* camera) const |
|---|
| 2322 | { |
|---|
| 2323 | OSG_INFO<<"cullShadowCastingScene()"<<std::endl; |
|---|
| 2324 | |
|---|
| 2325 | |
|---|
| 2326 | unsigned int traversalMask = cv->getTraversalMask(); |
|---|
| 2327 | |
|---|
| 2328 | cv->setTraversalMask( traversalMask & _shadowedScene->getCastsShadowTraversalMask() ); |
|---|
| 2329 | |
|---|
| 2330 | if (camera) camera->accept(*cv); |
|---|
| 2331 | |
|---|
| 2332 | cv->setTraversalMask( traversalMask ); |
|---|
| 2333 | |
|---|
| 2334 | return; |
|---|
| 2335 | } |
|---|
| 2336 | |
|---|
| 2337 | osg::StateSet* ViewDependentShadowMap::selectStateSetForRenderingShadow(ViewDependentData& vdd) const |
|---|
| 2338 | { |
|---|
| 2339 | OSG_INFO<<" selectStateSetForRenderingShadow() "<<vdd.getStateSet()<<std::endl; |
|---|
| 2340 | |
|---|
| 2341 | osg::ref_ptr<osg::StateSet> stateset = vdd.getStateSet(); |
|---|
| 2342 | |
|---|
| 2343 | vdd.getStateSet()->clear(); |
|---|
| 2344 | |
|---|
| 2345 | vdd.getStateSet()->setTextureAttributeAndModes(0, _fallbackBaseTexture.get(), osg::StateAttribute::ON); |
|---|
| 2346 | |
|---|
| 2347 | for(Uniforms::const_iterator itr=_uniforms.begin(); |
|---|
| 2348 | itr!=_uniforms.end(); |
|---|
| 2349 | ++itr) |
|---|
| 2350 | { |
|---|
| 2351 | OSG_INFO<<"addUniform("<<(*itr)->getName()<<")"<<std::endl; |
|---|
| 2352 | stateset->addUniform(itr->get()); |
|---|
| 2353 | } |
|---|
| 2354 | |
|---|
| 2355 | if (_program.valid()) |
|---|
| 2356 | { |
|---|
| 2357 | stateset->setAttribute(_program.get()); |
|---|
| 2358 | } |
|---|
| 2359 | |
|---|
| 2360 | LightDataList& pll = vdd.getLightDataList(); |
|---|
| 2361 | for(LightDataList::iterator itr = pll.begin(); |
|---|
| 2362 | itr != pll.end(); |
|---|
| 2363 | ++itr) |
|---|
| 2364 | { |
|---|
| 2365 | |
|---|
| 2366 | |
|---|
| 2367 | |
|---|
| 2368 | LightData& pl = (**itr); |
|---|
| 2369 | |
|---|
| 2370 | |
|---|
| 2371 | if (pl.textureUnits.empty()) continue; |
|---|
| 2372 | |
|---|
| 2373 | for(LightData::ActiveTextureUnits::iterator atu_itr = pl.textureUnits.begin(); |
|---|
| 2374 | atu_itr != pl.textureUnits.end(); |
|---|
| 2375 | ++atu_itr) |
|---|
| 2376 | { |
|---|
| 2377 | OSG_INFO<<" Need to assign state for "<<*atu_itr<<std::endl; |
|---|
| 2378 | } |
|---|
| 2379 | |
|---|
| 2380 | } |
|---|
| 2381 | |
|---|
| 2382 | const ShadowSettings* settings = getShadowedScene()->getShadowSettings(); |
|---|
| 2383 | unsigned int shadowMapModeValue = settings->getUseOverrideForShadowMapTexture() ? |
|---|
| 2384 | osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE : |
|---|
| 2385 | osg::StateAttribute::ON; |
|---|
| 2386 | |
|---|
| 2387 | |
|---|
| 2388 | ShadowDataList& sdl = vdd.getShadowDataList(); |
|---|
| 2389 | for(ShadowDataList::iterator itr = sdl.begin(); |
|---|
| 2390 | itr != sdl.end(); |
|---|
| 2391 | ++itr) |
|---|
| 2392 | { |
|---|
| 2393 | |
|---|
| 2394 | |
|---|
| 2395 | |
|---|
| 2396 | ShadowData& sd = (**itr); |
|---|
| 2397 | |
|---|
| 2398 | OSG_INFO<<" ShadowData for "<<sd._textureUnit<<std::endl; |
|---|
| 2399 | |
|---|
| 2400 | stateset->setTextureAttributeAndModes(sd._textureUnit, sd._texture.get(), shadowMapModeValue); |
|---|
| 2401 | |
|---|
| 2402 | stateset->setTextureMode(sd._textureUnit,GL_TEXTURE_GEN_S,osg::StateAttribute::ON); |
|---|
| 2403 | stateset->setTextureMode(sd._textureUnit,GL_TEXTURE_GEN_T,osg::StateAttribute::ON); |
|---|
| 2404 | stateset->setTextureMode(sd._textureUnit,GL_TEXTURE_GEN_R,osg::StateAttribute::ON); |
|---|
| 2405 | stateset->setTextureMode(sd._textureUnit,GL_TEXTURE_GEN_Q,osg::StateAttribute::ON); |
|---|
| 2406 | } |
|---|
| 2407 | |
|---|
| 2408 | return vdd.getStateSet(); |
|---|
| 2409 | } |
|---|
| 2410 | |
|---|
| 2411 | void ViewDependentShadowMap::resizeGLObjectBuffers(unsigned int maxSize) |
|---|
| 2412 | { |
|---|
| 2413 | |
|---|
| 2414 | } |
|---|
| 2415 | |
|---|
| 2416 | void ViewDependentShadowMap::releaseGLObjects(osg::State* state) const |
|---|
| 2417 | { |
|---|
| 2418 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_viewDependentDataMapMutex); |
|---|
| 2419 | for(ViewDependentDataMap::const_iterator itr = _viewDependentDataMap.begin(); |
|---|
| 2420 | itr != _viewDependentDataMap.end(); |
|---|
| 2421 | ++itr) |
|---|
| 2422 | { |
|---|
| 2423 | ViewDependentData* vdd = itr->second.get(); |
|---|
| 2424 | if (vdd) |
|---|
| 2425 | { |
|---|
| 2426 | vdd->releaseGLObjects(state); |
|---|
| 2427 | } |
|---|
| 2428 | } |
|---|
| 2429 | } |
|---|