| 243 | | |
| 244 | | osg::Group* second_model_group = new osg::Group; |
| 245 | | second_model_group->addChild(model.get()); |
| 246 | | group->addChild(second_model_group); |
| 247 | | |
| 248 | | osg::StateSet* ss1 = second_model_group->getOrCreateStateSet(); |
| 249 | | ss1->setRenderBinDetails(1, "RenderBin"); |
| 250 | | |
| 251 | | osg::LightModel* lm1 = new osg::LightModel; |
| 252 | | lm1->setAmbientIntensity(zero_colour); |
| 253 | | ss1->setAttribute(lm1); |
| 254 | | |
| 255 | | |
| 256 | | osg::LightSource* lightsource = new osg::LightSource; |
| 257 | | lightsource->setLight(light.get()); |
| 258 | | light->setAmbient(zero_colour); |
| 259 | | light->setDiffuse(diffuse); |
| 260 | | light->setPosition(lightpos); |
| 261 | | second_model_group->addChild(lightsource); |
| 262 | | |
| 263 | | ss1->setMode(GL_LIGHT0, osg::StateAttribute::ON); |
| 264 | | |
| | 253 | // use a camera here just to implement a seperate post rendering stage. |
| | 254 | osg::Camera* camera = new osg::Camera; |
| | 255 | camera->setRenderOrder(osg::Camera::POST_RENDER); |
| | 256 | camera->setClearMask(GL_STENCIL_BUFFER_BIT); |
| | 257 | group->addChild(camera); |
| | 258 | |
| | 259 | osg::StateSet* ss_camera = camera->getOrCreateStateSet(); |
| | 260 | |
| 268 | | ss1->setAttribute(depth); |
| | 264 | ss_camera->setAttribute(depth); |
| | 265 | |
| | 266 | { |
| | 267 | osg::ref_ptr<osg::Geode> geode = new osg::Geode; |
| | 268 | occluder->comptueShadowVolumeGeometry(lightpos, *shadowVolume); |
| | 269 | geode->addDrawable(shadowVolume.get()); |
| | 270 | |
| | 271 | // switch off the writing to the color bit planes. |
| | 272 | osg::ColorMask* colourMask = new osg::ColorMask; |
| | 273 | colourMask->setMask(false,false,false,false); |
| | 274 | |
| | 275 | osg::Stencil* stencil = new osg::Stencil; |
| | 276 | stencil->setFunction(osg::Stencil::ALWAYS,0,~0u); |
| | 277 | stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::INCR); |
| | 278 | |
| | 279 | osg::StateSet* ss_sv1 = geode->getOrCreateStateSet(); |
| | 280 | ss_sv1->setRenderBinDetails(0, "RenderBin"); |
| | 281 | ss_sv1->setAttributeAndModes(stencil,osg::StateAttribute::ON); |
| | 282 | ss_sv1->setAttribute(colourMask); |
| | 283 | |
| | 284 | ss_sv1->setAttributeAndModes(new osg::CullFace(osg::CullFace::BACK), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
| | 285 | |
| | 286 | camera->addChild(geode.get()); |
| | 287 | } |
| 270 | | osg::BlendFunc* blend = new osg::BlendFunc; |
| 271 | | blend->setFunction(osg::BlendFunc::ONE, osg::BlendFunc::ONE); |
| 272 | | ss1->setAttributeAndModes(blend, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
| 273 | | |
| | 289 | if (true) |
| | 290 | { |
| | 291 | osg::ref_ptr<osg::Geode> geode = new osg::Geode; |
| | 292 | occluder->comptueShadowVolumeGeometry(lightpos, *shadowVolume); |
| | 293 | geode->addDrawable(shadowVolume.get()); |
| | 294 | |
| | 295 | // switch off the writing to the color bit planes. |
| | 296 | osg::ColorMask* colourMask = new osg::ColorMask; |
| | 297 | colourMask->setMask(false,false,false,false); |
| | 298 | |
| | 299 | osg::Stencil* stencil = new osg::Stencil; |
| | 300 | stencil->setFunction(osg::Stencil::ALWAYS,0,~0u); |
| | 301 | stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::DECR); |
| | 302 | |
| | 303 | osg::StateSet* ss_sv1 = geode->getOrCreateStateSet(); |
| | 304 | ss_sv1->setRenderBinDetails(1, "RenderBin"); |
| | 305 | ss_sv1->setAttributeAndModes(stencil,osg::StateAttribute::ON); |
| | 306 | ss_sv1->setAttribute(colourMask); |
| | 307 | |
| | 308 | ss_sv1->setAttributeAndModes(new osg::CullFace(osg::CullFace::FRONT), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
| | 309 | |
| | 310 | camera->addChild(geode.get()); |
| | 311 | } |
| | 312 | |
| | 313 | { |
| | 314 | osg::Group* second_model_group = new osg::Group; |
| | 315 | second_model_group->addChild(model.get()); |
| | 316 | |
| | 317 | |
| | 318 | osg::StateSet* ss1 = second_model_group->getOrCreateStateSet(); |
| | 319 | ss1->setRenderBinDetails(5, "RenderBin"); |
| | 320 | |
| | 321 | osg::LightModel* lm1 = new osg::LightModel; |
| | 322 | lm1->setAmbientIntensity(zero_colour); |
| | 323 | ss1->setAttribute(lm1); |
| | 324 | |
| | 325 | |
| | 326 | osg::LightSource* lightsource = new osg::LightSource; |
| | 327 | lightsource->setLight(light.get()); |
| | 328 | light->setAmbient(zero_colour); |
| | 329 | light->setDiffuse(diffuse); |
| | 330 | light->setPosition(lightpos); |
| | 331 | second_model_group->addChild(lightsource); |
| | 332 | |
| | 333 | ss1->setMode(GL_LIGHT0, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
| | 334 | |
| | 335 | // set up the stencil ops so that only operator on this mirrors stencil value. |
| | 336 | osg::Stencil* stencil = new osg::Stencil; |
| | 337 | stencil->setFunction(osg::Stencil::EQUAL,0,~0u); |
| | 338 | stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::KEEP); |
| | 339 | ss1->setAttributeAndModes(stencil,osg::StateAttribute::ON); |
| | 340 | |
| | 341 | |
| | 342 | osg::BlendFunc* blend = new osg::BlendFunc; |
| | 343 | blend->setFunction(osg::BlendFunc::ONE, osg::BlendFunc::ONE); |
| | 344 | ss1->setAttributeAndModes(blend, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
| | 345 | ss1->setMode(GL_LIGHTING, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
| | 346 | |
| | 347 | camera->addChild(second_model_group); |
| | 348 | } |
| | 349 | |