| 1 | #include <osg/ArgumentParser> |
|---|
| 2 | |
|---|
| 3 | #include <osg/LightModel> |
|---|
| 4 | #include <osg/Depth> |
|---|
| 5 | #include <osg/BlendFunc> |
|---|
| 6 | #include <osg/Camera> |
|---|
| 7 | #include <osg/Stencil> |
|---|
| 8 | #include <osg/StencilTwoSided> |
|---|
| 9 | #include <osg/CullFace> |
|---|
| 10 | #include <osg/Geometry> |
|---|
| 11 | #include <osg/Switch> |
|---|
| 12 | |
|---|
| 13 | #include <osgGA/TrackballManipulator> |
|---|
| 14 | #include <osgGA/FlightManipulator> |
|---|
| 15 | #include <osgGA/DriveManipulator> |
|---|
| 16 | #include <osgGA/KeySwitchMatrixManipulator> |
|---|
| 17 | #include <osgGA/AnimationPathManipulator> |
|---|
| 18 | #include <osgGA/TerrainManipulator> |
|---|
| 19 | #include <osgGA/AnimationPathManipulator> |
|---|
| 20 | #include <osgGA/StateSetManipulator> |
|---|
| 21 | |
|---|
| 22 | #include <osgViewer/Viewer> |
|---|
| 23 | #include <osgViewer/StatsHandler> |
|---|
| 24 | |
|---|
| 25 | #include <osgShadow/OccluderGeometry> |
|---|
| 26 | |
|---|
| 27 | #include <osgDB/ReadFile> |
|---|
| 28 | #include <osgDB/WriteFile> |
|---|
| 29 | |
|---|
| 30 | #include <iostream> |
|---|
| 31 | |
|---|
| 32 | class ComputeBoundingBoxVisitor : public osg::NodeVisitor |
|---|
| 33 | { |
|---|
| 34 | public: |
|---|
| 35 | ComputeBoundingBoxVisitor(): |
|---|
| 36 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) |
|---|
| 37 | { |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | virtual void reset() |
|---|
| 41 | { |
|---|
| 42 | _matrixStack.clear(); |
|---|
| 43 | _bb.init(); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | osg::BoundingBox& getBoundingBox() { return _bb; } |
|---|
| 47 | |
|---|
| 48 | void getPolytope(osg::Polytope& polytope, float margin=0.1) const |
|---|
| 49 | { |
|---|
| 50 | float delta = _bb.radius()*margin; |
|---|
| 51 | polytope.add( osg::Plane(0.0, 0.0, 1.0, -(_bb.zMin()-delta)) ); |
|---|
| 52 | polytope.add( osg::Plane(0.0, 0.0, -1.0, (_bb.zMax()+delta)) ); |
|---|
| 53 | |
|---|
| 54 | polytope.add( osg::Plane(1.0, 0.0, 0.0, -(_bb.xMin()-delta)) ); |
|---|
| 55 | polytope.add( osg::Plane(-1.0, 0.0, 0.0, (_bb.xMax()+delta)) ); |
|---|
| 56 | |
|---|
| 57 | polytope.add( osg::Plane(0.0, 1.0, 0.0, -(_bb.yMin()-delta)) ); |
|---|
| 58 | polytope.add( osg::Plane(0.0, -1.0, 0.0, (_bb.yMax()+delta)) ); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | void getBase(osg::Polytope& polytope, float margin=0.1) const |
|---|
| 62 | { |
|---|
| 63 | float delta = _bb.radius()*margin; |
|---|
| 64 | polytope.add( osg::Plane(0.0, 0.0, 1.0, -(_bb.zMin()-delta)) ); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | void apply(osg::Node& node) |
|---|
| 68 | { |
|---|
| 69 | traverse(node); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | void apply(osg::Transform& transform) |
|---|
| 73 | { |
|---|
| 74 | osg::Matrix matrix; |
|---|
| 75 | if (!_matrixStack.empty()) matrix = _matrixStack.back(); |
|---|
| 76 | |
|---|
| 77 | transform.computeLocalToWorldMatrix(matrix,this); |
|---|
| 78 | |
|---|
| 79 | pushMatrix(matrix); |
|---|
| 80 | |
|---|
| 81 | traverse(transform); |
|---|
| 82 | |
|---|
| 83 | popMatrix(); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | void apply(osg::Geode& geode) |
|---|
| 87 | { |
|---|
| 88 | for(unsigned int i=0; i<geode.getNumDrawables(); ++i) |
|---|
| 89 | { |
|---|
| 90 | apply(geode.getDrawable(i)); |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | void pushMatrix(osg::Matrix& matrix) |
|---|
| 95 | { |
|---|
| 96 | _matrixStack.push_back(matrix); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | void popMatrix() |
|---|
| 100 | { |
|---|
| 101 | _matrixStack.pop_back(); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | void apply(osg::Drawable* drawable) |
|---|
| 105 | { |
|---|
| 106 | if (_matrixStack.empty()) _bb.expandBy(drawable->getBound()); |
|---|
| 107 | else |
|---|
| 108 | { |
|---|
| 109 | osg::Matrix& matrix = _matrixStack.back(); |
|---|
| 110 | const osg::BoundingBox& dbb = drawable->getBound(); |
|---|
| 111 | if (dbb.valid()) |
|---|
| 112 | { |
|---|
| 113 | _bb.expandBy(dbb.corner(0) * matrix); |
|---|
| 114 | _bb.expandBy(dbb.corner(1) * matrix); |
|---|
| 115 | _bb.expandBy(dbb.corner(2) * matrix); |
|---|
| 116 | _bb.expandBy(dbb.corner(3) * matrix); |
|---|
| 117 | _bb.expandBy(dbb.corner(4) * matrix); |
|---|
| 118 | _bb.expandBy(dbb.corner(5) * matrix); |
|---|
| 119 | _bb.expandBy(dbb.corner(6) * matrix); |
|---|
| 120 | _bb.expandBy(dbb.corner(7) * matrix); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | protected: |
|---|
| 126 | |
|---|
| 127 | typedef std::vector<osg::Matrix> MatrixStack; |
|---|
| 128 | |
|---|
| 129 | MatrixStack _matrixStack; |
|---|
| 130 | osg::BoundingBox _bb; |
|---|
| 131 | }; |
|---|
| 132 | |
|---|
| 133 | enum Faces |
|---|
| 134 | { |
|---|
| 135 | FRONT_FACE = 1, |
|---|
| 136 | BACK_FACE = 2, |
|---|
| 137 | LEFT_FACE = 4, |
|---|
| 138 | RIGHT_FACE = 8, |
|---|
| 139 | TOP_FACE = 16, |
|---|
| 140 | BOTTOM_FACE = 32 |
|---|
| 141 | }; |
|---|
| 142 | |
|---|
| 143 | osg::Node* createCube(unsigned int mask) |
|---|
| 144 | { |
|---|
| 145 | osg::Geode* geode = new osg::Geode; |
|---|
| 146 | |
|---|
| 147 | osg::Geometry* geometry = new osg::Geometry; |
|---|
| 148 | geode->addDrawable(geometry); |
|---|
| 149 | |
|---|
| 150 | osg::Vec3Array* vertices = new osg::Vec3Array; |
|---|
| 151 | geometry->setVertexArray(vertices); |
|---|
| 152 | |
|---|
| 153 | osg::Vec3Array* normals = new osg::Vec3Array; |
|---|
| 154 | geometry->setNormalArray(normals); |
|---|
| 155 | geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 156 | |
|---|
| 157 | osg::Vec4Array* colours = new osg::Vec4Array; |
|---|
| 158 | geometry->setColorArray(colours); |
|---|
| 159 | geometry->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 160 | colours->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | osg::Vec3 origin(0.0f,0.0f,0.0f); |
|---|
| 164 | osg::Vec3 dx(2.0f,0.0f,0.0f); |
|---|
| 165 | osg::Vec3 dy(0.0f,1.0f,0.0f); |
|---|
| 166 | osg::Vec3 dz(0.0f,0.0f,1.0f); |
|---|
| 167 | |
|---|
| 168 | osg::Vec3 px(1.0f,0.0,0.0f); |
|---|
| 169 | osg::Vec3 nx(-1.0f,0.0,0.0f); |
|---|
| 170 | osg::Vec3 py(0.0f,1.0f,0.0f); |
|---|
| 171 | osg::Vec3 ny(0.0f,-1.0f,0.0f); |
|---|
| 172 | osg::Vec3 pz(0.0f,0.0f,1.0f); |
|---|
| 173 | osg::Vec3 nz(0.0f,0.0f,-1.0f); |
|---|
| 174 | |
|---|
| 175 | if (mask & FRONT_FACE) |
|---|
| 176 | { |
|---|
| 177 | |
|---|
| 178 | vertices->push_back(origin); |
|---|
| 179 | vertices->push_back(origin+dx); |
|---|
| 180 | vertices->push_back(origin+dx+dz); |
|---|
| 181 | vertices->push_back(origin+dz); |
|---|
| 182 | normals->push_back(ny); |
|---|
| 183 | normals->push_back(ny); |
|---|
| 184 | normals->push_back(ny); |
|---|
| 185 | normals->push_back(ny); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | if (mask & BACK_FACE) |
|---|
| 189 | { |
|---|
| 190 | |
|---|
| 191 | vertices->push_back(origin+dy); |
|---|
| 192 | vertices->push_back(origin+dy+dz); |
|---|
| 193 | vertices->push_back(origin+dy+dx+dz); |
|---|
| 194 | vertices->push_back(origin+dy+dx); |
|---|
| 195 | normals->push_back(py); |
|---|
| 196 | normals->push_back(py); |
|---|
| 197 | normals->push_back(py); |
|---|
| 198 | normals->push_back(py); |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | if (mask & LEFT_FACE) |
|---|
| 202 | { |
|---|
| 203 | |
|---|
| 204 | vertices->push_back(origin+dy); |
|---|
| 205 | vertices->push_back(origin); |
|---|
| 206 | vertices->push_back(origin+dz); |
|---|
| 207 | vertices->push_back(origin+dy+dz); |
|---|
| 208 | normals->push_back(nx); |
|---|
| 209 | normals->push_back(nx); |
|---|
| 210 | normals->push_back(nx); |
|---|
| 211 | normals->push_back(nx); |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | if (mask & RIGHT_FACE) |
|---|
| 215 | { |
|---|
| 216 | |
|---|
| 217 | vertices->push_back(origin+dx+dy); |
|---|
| 218 | vertices->push_back(origin+dx+dy+dz); |
|---|
| 219 | vertices->push_back(origin+dx+dz); |
|---|
| 220 | vertices->push_back(origin+dx); |
|---|
| 221 | normals->push_back(px); |
|---|
| 222 | normals->push_back(px); |
|---|
| 223 | normals->push_back(px); |
|---|
| 224 | normals->push_back(px); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | if (mask & TOP_FACE) |
|---|
| 228 | { |
|---|
| 229 | |
|---|
| 230 | vertices->push_back(origin+dz); |
|---|
| 231 | vertices->push_back(origin+dz+dx); |
|---|
| 232 | vertices->push_back(origin+dz+dx+dy); |
|---|
| 233 | vertices->push_back(origin+dz+dy); |
|---|
| 234 | normals->push_back(pz); |
|---|
| 235 | normals->push_back(pz); |
|---|
| 236 | normals->push_back(pz); |
|---|
| 237 | normals->push_back(pz); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | if (mask & BOTTOM_FACE) |
|---|
| 241 | { |
|---|
| 242 | |
|---|
| 243 | vertices->push_back(origin); |
|---|
| 244 | vertices->push_back(origin+dy); |
|---|
| 245 | vertices->push_back(origin+dx+dy); |
|---|
| 246 | vertices->push_back(origin+dx); |
|---|
| 247 | normals->push_back(nz); |
|---|
| 248 | normals->push_back(nz); |
|---|
| 249 | normals->push_back(nz); |
|---|
| 250 | normals->push_back(nz); |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | geometry->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, vertices->size())); |
|---|
| 254 | |
|---|
| 255 | return geode; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | class SwitchHandler : public osgGA::GUIEventHandler |
|---|
| 259 | { |
|---|
| 260 | public: |
|---|
| 261 | |
|---|
| 262 | SwitchHandler(): |
|---|
| 263 | _childNum(0) {} |
|---|
| 264 | |
|---|
| 265 | virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& , osg::Object* object, osg::NodeVisitor* ) |
|---|
| 266 | { |
|---|
| 267 | osg::Switch* sw = dynamic_cast<osg::Switch*>(object); |
|---|
| 268 | if (!sw) return false; |
|---|
| 269 | |
|---|
| 270 | if (ea.getHandled()) return false; |
|---|
| 271 | |
|---|
| 272 | switch(ea.getEventType()) |
|---|
| 273 | { |
|---|
| 274 | case(osgGA::GUIEventAdapter::KEYDOWN): |
|---|
| 275 | { |
|---|
| 276 | if (ea.getKey()=='n') |
|---|
| 277 | { |
|---|
| 278 | ++_childNum; |
|---|
| 279 | if (_childNum >= sw->getNumChildren()) _childNum = 0; |
|---|
| 280 | |
|---|
| 281 | sw->setSingleChildOn(_childNum); |
|---|
| 282 | return true; |
|---|
| 283 | } |
|---|
| 284 | break; |
|---|
| 285 | } |
|---|
| 286 | default: |
|---|
| 287 | break; |
|---|
| 288 | } |
|---|
| 289 | return false; |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | protected: |
|---|
| 293 | |
|---|
| 294 | virtual ~SwitchHandler() {} |
|---|
| 295 | unsigned int _childNum; |
|---|
| 296 | |
|---|
| 297 | }; |
|---|
| 298 | |
|---|
| 299 | osg::Node* createTestModel() |
|---|
| 300 | { |
|---|
| 301 | osg::Switch* sw = new osg::Switch; |
|---|
| 302 | sw->setEventCallback(new SwitchHandler); |
|---|
| 303 | |
|---|
| 304 | sw->addChild(createCube(FRONT_FACE), true); |
|---|
| 305 | sw->addChild(createCube(FRONT_FACE | BACK_FACE), false); |
|---|
| 306 | sw->addChild(createCube(FRONT_FACE | BACK_FACE | LEFT_FACE), false); |
|---|
| 307 | sw->addChild(createCube(FRONT_FACE | BACK_FACE | LEFT_FACE | RIGHT_FACE), false); |
|---|
| 308 | sw->addChild(createCube(FRONT_FACE | BACK_FACE | LEFT_FACE | RIGHT_FACE | TOP_FACE), false); |
|---|
| 309 | sw->addChild(createCube(FRONT_FACE | BACK_FACE | LEFT_FACE | RIGHT_FACE | TOP_FACE | BOTTOM_FACE), false); |
|---|
| 310 | |
|---|
| 311 | return sw; |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | class ShadowCallback : public osg::NodeCallback |
|---|
| 317 | { |
|---|
| 318 | public: |
|---|
| 319 | |
|---|
| 320 | osg::ref_ptr<osg::Light> _ambientLight; |
|---|
| 321 | osg::ref_ptr<osg::Light> _diffuseLight; |
|---|
| 322 | |
|---|
| 323 | osg::ref_ptr<osg::StateSet> _ss1; |
|---|
| 324 | osg::ref_ptr<osg::StateSet> _mainShadowStateSet; |
|---|
| 325 | osg::ref_ptr<osg::StateSet> _shadowVolumeStateSet; |
|---|
| 326 | osg::ref_ptr<osg::StateSet> _shadowedSceneStateSet; |
|---|
| 327 | |
|---|
| 328 | ShadowCallback(osgShadow::ShadowVolumeGeometry::DrawMode drawMode) |
|---|
| 329 | { |
|---|
| 330 | |
|---|
| 331 | { |
|---|
| 332 | _ss1 = new osg::StateSet; |
|---|
| 333 | |
|---|
| 334 | #if 0 |
|---|
| 335 | osg::LightModel* lm1 = new osg::LightModel; |
|---|
| 336 | lm1->setAmbientIntensity(ambient); |
|---|
| 337 | _ss1->setAttribute(lm1); |
|---|
| 338 | |
|---|
| 339 | osg::Light* light1 = new osg::Light; |
|---|
| 340 | light1->setAmbient(ambient); |
|---|
| 341 | light1->setDiffuse(zero_colour); |
|---|
| 342 | light1->setPosition(_lightpos); |
|---|
| 343 | _ss1->setAttributeAndModes(light1, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| 344 | #endif |
|---|
| 345 | |
|---|
| 346 | _ss1->setMode(GL_LIGHTING, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | { |
|---|
| 350 | _mainShadowStateSet = new osg::StateSet; |
|---|
| 351 | |
|---|
| 352 | osg::Depth* depth = new osg::Depth; |
|---|
| 353 | depth->setWriteMask(false); |
|---|
| 354 | depth->setFunction(osg::Depth::LEQUAL); |
|---|
| 355 | _mainShadowStateSet->setAttribute(depth); |
|---|
| 356 | _mainShadowStateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | { |
|---|
| 360 | _shadowVolumeStateSet = new osg::StateSet; |
|---|
| 361 | |
|---|
| 362 | osg::Depth* depth = new osg::Depth; |
|---|
| 363 | depth->setWriteMask(false); |
|---|
| 364 | depth->setFunction(osg::Depth::LEQUAL); |
|---|
| 365 | _shadowVolumeStateSet->setAttribute(depth); |
|---|
| 366 | _shadowVolumeStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE); |
|---|
| 367 | _shadowVolumeStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON); |
|---|
| 368 | |
|---|
| 369 | |
|---|
| 370 | if (drawMode == osgShadow::ShadowVolumeGeometry::STENCIL_TWO_SIDED) |
|---|
| 371 | { |
|---|
| 372 | osg::StencilTwoSided* stencil = new osg::StencilTwoSided; |
|---|
| 373 | stencil->setFunction(osg::StencilTwoSided::BACK, osg::StencilTwoSided::ALWAYS,0,~0u); |
|---|
| 374 | stencil->setOperation(osg::StencilTwoSided::BACK, osg::StencilTwoSided::KEEP, osg::StencilTwoSided::KEEP, osg::StencilTwoSided::DECR_WRAP); |
|---|
| 375 | stencil->setFunction(osg::StencilTwoSided::FRONT, osg::StencilTwoSided::ALWAYS,0,~0u); |
|---|
| 376 | stencil->setOperation(osg::StencilTwoSided::FRONT, osg::StencilTwoSided::KEEP, osg::StencilTwoSided::KEEP, osg::StencilTwoSided::INCR_WRAP); |
|---|
| 377 | |
|---|
| 378 | osg::ColorMask* colourMask = new osg::ColorMask(false, false, false, false); |
|---|
| 379 | |
|---|
| 380 | _shadowVolumeStateSet->setAttributeAndModes(stencil,osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| 381 | _shadowVolumeStateSet->setAttribute(colourMask, osg::StateAttribute::OVERRIDE); |
|---|
| 382 | _shadowVolumeStateSet->setMode(GL_CULL_FACE,osg::StateAttribute::OFF); |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | } |
|---|
| 386 | else |
|---|
| 387 | { |
|---|
| 388 | osg::Stencil* stencil = new osg::Stencil; |
|---|
| 389 | stencil->setFunction(osg::Stencil::ALWAYS,0,~0u); |
|---|
| 390 | stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::KEEP); |
|---|
| 391 | |
|---|
| 392 | osg::ColorMask* colourMask = new osg::ColorMask(false, false, false, false); |
|---|
| 393 | |
|---|
| 394 | _shadowVolumeStateSet->setAttributeAndModes(stencil,osg::StateAttribute::ON); |
|---|
| 395 | _shadowVolumeStateSet->setAttribute(colourMask); |
|---|
| 396 | _shadowVolumeStateSet->setMode(GL_CULL_FACE,osg::StateAttribute::ON); |
|---|
| 397 | } |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | |
|---|
| 401 | { |
|---|
| 402 | _shadowedSceneStateSet = new osg::StateSet; |
|---|
| 403 | |
|---|
| 404 | osg::Depth* depth = new osg::Depth; |
|---|
| 405 | depth->setWriteMask(false); |
|---|
| 406 | depth->setFunction(osg::Depth::LEQUAL); |
|---|
| 407 | _shadowedSceneStateSet->setAttribute(depth); |
|---|
| 408 | _shadowedSceneStateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON); |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | #if 0 |
|---|
| 412 | osg::LightModel* lm1 = new osg::LightModel; |
|---|
| 413 | lm1->setAmbientIntensity(zero_colour); |
|---|
| 414 | _shadowedSceneStateSet->setAttribute(lm1); |
|---|
| 415 | |
|---|
| 416 | osg::Light* light = new osg::Light; |
|---|
| 417 | light->setAmbient(zero_colour); |
|---|
| 418 | light->setDiffuse(diffuse); |
|---|
| 419 | light->setPosition(_lightpos); |
|---|
| 420 | |
|---|
| 421 | _shadowedSceneStateSet->setMode(GL_LIGHT0, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| 422 | _shadowedSceneStateSet->setAttribute(light); |
|---|
| 423 | #endif |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | osg::Stencil* stencil = new osg::Stencil; |
|---|
| 427 | stencil->setFunction(osg::Stencil::EQUAL,0,~0u); |
|---|
| 428 | stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::KEEP); |
|---|
| 429 | _shadowedSceneStateSet->setAttributeAndModes(stencil,osg::StateAttribute::ON); |
|---|
| 430 | |
|---|
| 431 | osg::BlendFunc* blend = new osg::BlendFunc; |
|---|
| 432 | blend->setFunction(osg::BlendFunc::ONE, osg::BlendFunc::ONE); |
|---|
| 433 | _shadowedSceneStateSet->setAttributeAndModes(blend, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| 434 | |
|---|
| 435 | } |
|---|
| 436 | |
|---|
| 437 | _ss1->setThreadSafeRefUnref(true); |
|---|
| 438 | _mainShadowStateSet->setThreadSafeRefUnref(true); |
|---|
| 439 | _shadowVolumeStateSet->setThreadSafeRefUnref(true); |
|---|
| 440 | _shadowedSceneStateSet->setThreadSafeRefUnref(true); |
|---|
| 441 | |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 445 | { |
|---|
| 446 | osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv); |
|---|
| 447 | if (!cv) |
|---|
| 448 | { |
|---|
| 449 | traverse(node,nv); |
|---|
| 450 | return; |
|---|
| 451 | } |
|---|
| 452 | |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | |
|---|
| 456 | osg::ref_ptr<osgUtil::RenderBin> original_bin = cv->getCurrentRenderBin(); |
|---|
| 457 | |
|---|
| 458 | osg::ref_ptr<osgUtil::RenderBin> new_bin = original_bin->find_or_insert(0,"RenderBin"); |
|---|
| 459 | |
|---|
| 460 | cv->setCurrentRenderBin(new_bin.get()); |
|---|
| 461 | |
|---|
| 462 | traverse(node,nv); |
|---|
| 463 | |
|---|
| 464 | cv->setCurrentRenderBin(original_bin.get()); |
|---|
| 465 | |
|---|
| 466 | osgUtil::RenderBin::RenderBinList::iterator itr = new_bin->getRenderBinList().find(1000); |
|---|
| 467 | osg::ref_ptr<osgUtil::RenderBin> shadowVolumeBin; |
|---|
| 468 | if (itr != new_bin->getRenderBinList().end()) |
|---|
| 469 | { |
|---|
| 470 | shadowVolumeBin = itr->second; |
|---|
| 471 | |
|---|
| 472 | if (shadowVolumeBin.valid()) |
|---|
| 473 | { |
|---|
| 474 | |
|---|
| 475 | new_bin->getRenderBinList().erase(itr); |
|---|
| 476 | } |
|---|
| 477 | } |
|---|
| 478 | |
|---|
| 479 | if (shadowVolumeBin.valid()) |
|---|
| 480 | { |
|---|
| 481 | original_bin->setStateSet(_ss1.get()); |
|---|
| 482 | |
|---|
| 483 | osgUtil::RenderStage* orig_rs = cv->getRenderStage(); |
|---|
| 484 | osgUtil::RenderStage* new_rs = new osgUtil::RenderStage; |
|---|
| 485 | orig_rs->addPostRenderStage(new_rs); |
|---|
| 486 | |
|---|
| 487 | new_rs->setViewport(orig_rs->getViewport()); |
|---|
| 488 | new_rs->setClearColor(orig_rs->getClearColor()); |
|---|
| 489 | new_rs->setClearMask(GL_STENCIL_BUFFER_BIT); |
|---|
| 490 | new_rs->setDrawBuffer(orig_rs->getDrawBuffer()); |
|---|
| 491 | new_rs->setReadBuffer(orig_rs->getReadBuffer()); |
|---|
| 492 | new_rs->setColorMask(orig_rs->getColorMask()); |
|---|
| 493 | |
|---|
| 494 | new_rs->setPositionalStateContainer(orig_rs->getPositionalStateContainer()); |
|---|
| 495 | |
|---|
| 496 | if (shadowVolumeBin.valid()) |
|---|
| 497 | { |
|---|
| 498 | |
|---|
| 499 | new_rs->getRenderBinList()[0] = shadowVolumeBin; |
|---|
| 500 | shadowVolumeBin->setStateSet(_shadowVolumeStateSet.get()); |
|---|
| 501 | |
|---|
| 502 | osg::ref_ptr<osgUtil::RenderBin> nested_bin = new_rs->find_or_insert(1,"RenderBin"); |
|---|
| 503 | nested_bin->getRenderBinList()[0] = new_bin; |
|---|
| 504 | nested_bin->setStateSet(_shadowedSceneStateSet.get()); |
|---|
| 505 | } |
|---|
| 506 | } |
|---|
| 507 | } |
|---|
| 508 | }; |
|---|
| 509 | |
|---|
| 510 | |
|---|
| 511 | |
|---|
| 512 | int main(int argc, char** argv) |
|---|
| 513 | { |
|---|
| 514 | |
|---|
| 515 | osg::ArgumentParser arguments(&argc, argv); |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName() + " is the example which demonstrates using of GL_ARB_shadow extension implemented in osg::Texture class"); |
|---|
| 519 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()); |
|---|
| 520 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help", "Display this information"); |
|---|
| 521 | arguments.getApplicationUsage()->addCommandLineOption("--positionalLight", "Use a positional light."); |
|---|
| 522 | arguments.getApplicationUsage()->addCommandLineOption("--directionalLight", "Use a direction light."); |
|---|
| 523 | arguments.getApplicationUsage()->addCommandLineOption("--addOccluderToScene", "Add the occluders geometry."); |
|---|
| 524 | arguments.getApplicationUsage()->addCommandLineOption("--noUpdate", "Disable the updating the of light source."); |
|---|
| 525 | arguments.getApplicationUsage()->addCommandLineOption("--base", "Add a base geometry to test shadows."); |
|---|
| 526 | arguments.getApplicationUsage()->addCommandLineOption("--noShadow", "Disable the shadows."); |
|---|
| 527 | arguments.getApplicationUsage()->addCommandLineOption("--two-sided", "Use two-sided stencil extension for shadow volumes."); |
|---|
| 528 | arguments.getApplicationUsage()->addCommandLineOption("--two-pass", "Use two-pass stencil for shadow volumes."); |
|---|
| 529 | |
|---|
| 530 | |
|---|
| 531 | osg::DisplaySettings::instance()->setMinimumNumStencilBits(8); |
|---|
| 532 | |
|---|
| 533 | |
|---|
| 534 | osgViewer::Viewer viewer; |
|---|
| 535 | |
|---|
| 536 | |
|---|
| 537 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 538 | { |
|---|
| 539 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 540 | return 1; |
|---|
| 541 | } |
|---|
| 542 | |
|---|
| 543 | bool postionalLight = false; |
|---|
| 544 | while (arguments.read("--positionalLight")) postionalLight = true; |
|---|
| 545 | while (arguments.read("--directionalLight")) postionalLight = false; |
|---|
| 546 | |
|---|
| 547 | bool addOccluderToScene = false; |
|---|
| 548 | while (arguments.read("--addOccluderToScene")) addOccluderToScene = true; |
|---|
| 549 | |
|---|
| 550 | bool updateLightPosition = true; |
|---|
| 551 | while (arguments.read("--noUpdate")) updateLightPosition = false; |
|---|
| 552 | |
|---|
| 553 | bool createBase = false; |
|---|
| 554 | while (arguments.read("--base")) createBase = true; |
|---|
| 555 | |
|---|
| 556 | bool doShadow = true; |
|---|
| 557 | while (arguments.read("--noShadow")) doShadow = false; |
|---|
| 558 | |
|---|
| 559 | bool cullCallback = false; |
|---|
| 560 | while (arguments.read("-c")) cullCallback = true; |
|---|
| 561 | |
|---|
| 562 | int screenNum = -1; |
|---|
| 563 | while (arguments.read("--screen", screenNum)) viewer.setUpViewOnSingleScreen(screenNum); |
|---|
| 564 | |
|---|
| 565 | osgShadow::ShadowVolumeGeometry::DrawMode drawMode = osgShadow::ShadowVolumeGeometry::STENCIL_TWO_SIDED; |
|---|
| 566 | while (arguments.read("--two-sided")) drawMode = osgShadow::ShadowVolumeGeometry::STENCIL_TWO_SIDED; |
|---|
| 567 | while (arguments.read("--two-pass")) drawMode = osgShadow::ShadowVolumeGeometry::STENCIL_TWO_PASS; |
|---|
| 568 | |
|---|
| 569 | |
|---|
| 570 | |
|---|
| 571 | { |
|---|
| 572 | osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; |
|---|
| 573 | |
|---|
| 574 | keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() ); |
|---|
| 575 | keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() ); |
|---|
| 576 | keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() ); |
|---|
| 577 | keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() ); |
|---|
| 578 | |
|---|
| 579 | std::string pathfile; |
|---|
| 580 | char keyForAnimationPath = '5'; |
|---|
| 581 | while (arguments.read("-p",pathfile)) |
|---|
| 582 | { |
|---|
| 583 | osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile); |
|---|
| 584 | if (apm || !apm->valid()) |
|---|
| 585 | { |
|---|
| 586 | unsigned int num = keyswitchManipulator->getNumMatrixManipulators(); |
|---|
| 587 | keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm ); |
|---|
| 588 | keyswitchManipulator->selectMatrixManipulator(num); |
|---|
| 589 | ++keyForAnimationPath; |
|---|
| 590 | } |
|---|
| 591 | } |
|---|
| 592 | |
|---|
| 593 | viewer.setCameraManipulator( keyswitchManipulator.get() ); |
|---|
| 594 | } |
|---|
| 595 | |
|---|
| 596 | |
|---|
| 597 | |
|---|
| 598 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 599 | |
|---|
| 600 | |
|---|
| 601 | viewer.addEventHandler( new osgViewer::StatsHandler() ); |
|---|
| 602 | |
|---|
| 603 | |
|---|
| 604 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 605 | |
|---|
| 606 | |
|---|
| 607 | if (arguments.errors()) |
|---|
| 608 | { |
|---|
| 609 | arguments.writeErrorMessages(std::cout); |
|---|
| 610 | return 1; |
|---|
| 611 | } |
|---|
| 612 | |
|---|
| 613 | |
|---|
| 614 | osg::ref_ptr<osg::Node> model = osgDB::readNodeFiles(arguments); |
|---|
| 615 | if (!model) |
|---|
| 616 | { |
|---|
| 617 | model = createTestModel(); |
|---|
| 618 | } |
|---|
| 619 | |
|---|
| 620 | |
|---|
| 621 | ComputeBoundingBoxVisitor cbbv; |
|---|
| 622 | model->accept(cbbv); |
|---|
| 623 | osg::BoundingBox bb = cbbv.getBoundingBox(); |
|---|
| 624 | |
|---|
| 625 | if (createBase) |
|---|
| 626 | { |
|---|
| 627 | osg::ref_ptr<osg::Group> newGroup = new osg::Group; |
|---|
| 628 | newGroup->addChild(model.get()); |
|---|
| 629 | |
|---|
| 630 | osg::Geode* geode = new osg::Geode; |
|---|
| 631 | |
|---|
| 632 | osg::Vec3 widthVec(bb.radius(), 0.0f, 0.0f); |
|---|
| 633 | osg::Vec3 depthVec(0.0f, bb.radius(), 0.0f); |
|---|
| 634 | osg::Vec3 centerBase( (bb.xMin()+bb.xMax())*0.5f, (bb.yMin()+bb.yMax())*0.5f, bb.zMin()-bb.radius()*0.1f ); |
|---|
| 635 | |
|---|
| 636 | geode->addDrawable( osg::createTexturedQuadGeometry( centerBase-widthVec*1.5f-depthVec*1.5f, |
|---|
| 637 | widthVec*3.0f, depthVec*3.0f) ); |
|---|
| 638 | newGroup->addChild(geode); |
|---|
| 639 | |
|---|
| 640 | model = newGroup.get(); |
|---|
| 641 | } |
|---|
| 642 | |
|---|
| 643 | |
|---|
| 644 | cbbv.reset(); |
|---|
| 645 | model->accept(cbbv); |
|---|
| 646 | bb = cbbv.getBoundingBox(); |
|---|
| 647 | |
|---|
| 648 | osg::ref_ptr<osg::Group> group = new osg::Group; |
|---|
| 649 | |
|---|
| 650 | |
|---|
| 651 | osg::ref_ptr<osgShadow::OccluderGeometry> occluder = new osgShadow::OccluderGeometry; |
|---|
| 652 | occluder->computeOccluderGeometry(model.get()); |
|---|
| 653 | |
|---|
| 654 | cbbv.getBase(occluder->getBoundingPolytope(),0.001); |
|---|
| 655 | |
|---|
| 656 | if (addOccluderToScene) |
|---|
| 657 | { |
|---|
| 658 | osg::ref_ptr<osg::Geode> geode = new osg::Geode; |
|---|
| 659 | geode->addDrawable(occluder.get()); |
|---|
| 660 | group->addChild(geode.get()); |
|---|
| 661 | } |
|---|
| 662 | |
|---|
| 663 | osg::ref_ptr<osgShadow::ShadowVolumeGeometry> shadowVolume = new osgShadow::ShadowVolumeGeometry; |
|---|
| 664 | |
|---|
| 665 | shadowVolume->setUseDisplayList(!updateLightPosition); |
|---|
| 666 | |
|---|
| 667 | osg::Vec4 lightpos; |
|---|
| 668 | |
|---|
| 669 | if (postionalLight) |
|---|
| 670 | { |
|---|
| 671 | lightpos.set(bb.center().x(), bb.center().y(), bb.zMax() + bb.radius() ,1.0f); |
|---|
| 672 | } |
|---|
| 673 | else |
|---|
| 674 | { |
|---|
| 675 | lightpos.set(0.5f,0.25f,0.8f,0.0f); |
|---|
| 676 | } |
|---|
| 677 | |
|---|
| 678 | |
|---|
| 679 | |
|---|
| 680 | osg::ref_ptr<osg::Light> light = new osg::Light; |
|---|
| 681 | light->setPosition(lightpos); |
|---|
| 682 | |
|---|
| 683 | if (!doShadow) |
|---|
| 684 | { |
|---|
| 685 | group->addChild(model.get()); |
|---|
| 686 | |
|---|
| 687 | osg::ref_ptr<osg::Geode> geode = new osg::Geode; |
|---|
| 688 | occluder->computeShadowVolumeGeometry(lightpos, *shadowVolume); |
|---|
| 689 | geode->addDrawable(shadowVolume.get()); |
|---|
| 690 | group->addChild(geode.get()); |
|---|
| 691 | |
|---|
| 692 | osg::StateSet* ss = geode->getOrCreateStateSet(); |
|---|
| 693 | ss->setAttributeAndModes(new osg::CullFace(osg::CullFace::BACK), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| 694 | |
|---|
| 695 | } |
|---|
| 696 | else if (cullCallback) |
|---|
| 697 | { |
|---|
| 698 | |
|---|
| 699 | int shadowVolumeBin = 1000; |
|---|
| 700 | |
|---|
| 701 | group->setCullCallback(new ShadowCallback(drawMode)); |
|---|
| 702 | |
|---|
| 703 | group->addChild(model.get()); |
|---|
| 704 | |
|---|
| 705 | osg::ref_ptr<osg::LightSource> ls = new osg::LightSource; |
|---|
| 706 | ls->setLight(light.get()); |
|---|
| 707 | group->addChild(ls.get()); |
|---|
| 708 | |
|---|
| 709 | { |
|---|
| 710 | osg::ref_ptr<osg::Geode> geode = new osg::Geode; |
|---|
| 711 | group->addChild(geode.get()); |
|---|
| 712 | |
|---|
| 713 | occluder->computeShadowVolumeGeometry(lightpos, *shadowVolume); |
|---|
| 714 | shadowVolume->setDrawMode(drawMode); |
|---|
| 715 | |
|---|
| 716 | if (drawMode == osgShadow::ShadowVolumeGeometry::STENCIL_TWO_SIDED) |
|---|
| 717 | { |
|---|
| 718 | osg::notify(osg::NOTICE)<<"STENCIL_TWO_SIDED seleteced"<<std::endl; |
|---|
| 719 | |
|---|
| 720 | osg::StateSet* ss_sv1 = geode->getOrCreateStateSet(); |
|---|
| 721 | ss_sv1->setRenderBinDetails(shadowVolumeBin, "RenderBin"); |
|---|
| 722 | geode->addDrawable(shadowVolume.get()); |
|---|
| 723 | } |
|---|
| 724 | else |
|---|
| 725 | { |
|---|
| 726 | osg::notify(osg::NOTICE)<<"STENCIL_TWO_PASSES seleteced"<<std::endl; |
|---|
| 727 | |
|---|
| 728 | osg::StateSet* ss_sv1 = geode->getOrCreateStateSet(); |
|---|
| 729 | ss_sv1->setRenderBinDetails(shadowVolumeBin, "RenderBin"); |
|---|
| 730 | geode->addDrawable(shadowVolume.get()); |
|---|
| 731 | } |
|---|
| 732 | |
|---|
| 733 | } |
|---|
| 734 | |
|---|
| 735 | } |
|---|
| 736 | else |
|---|
| 737 | { |
|---|
| 738 | osg::Vec4 ambient(0.2,0.2,0.2,1.0); |
|---|
| 739 | osg::Vec4 diffuse(0.8,0.8,0.8,1.0); |
|---|
| 740 | osg::Vec4 zero_colour(0.0,0.0,0.0,1.0); |
|---|
| 741 | |
|---|
| 742 | |
|---|
| 743 | { |
|---|
| 744 | |
|---|
| 745 | osg::Group* first_model_group = new osg::Group; |
|---|
| 746 | first_model_group->addChild(model.get()); |
|---|
| 747 | group->addChild(first_model_group); |
|---|
| 748 | |
|---|
| 749 | osg::StateSet* ss1 = first_model_group->getOrCreateStateSet(); |
|---|
| 750 | |
|---|
| 751 | osg::LightModel* lm1 = new osg::LightModel; |
|---|
| 752 | lm1->setAmbientIntensity(ambient); |
|---|
| 753 | ss1->setAttribute(lm1); |
|---|
| 754 | |
|---|
| 755 | osg::Light* light1 = new osg::Light; |
|---|
| 756 | light1->setAmbient(ambient); |
|---|
| 757 | light1->setDiffuse(zero_colour); |
|---|
| 758 | light1->setPosition(lightpos); |
|---|
| 759 | ss1->setAttributeAndModes(light1, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| 760 | ss1->setMode(GL_LIGHTING, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| 761 | |
|---|
| 762 | } |
|---|
| 763 | |
|---|
| 764 | |
|---|
| 765 | { |
|---|
| 766 | |
|---|
| 767 | osg::Camera* camera = new osg::Camera; |
|---|
| 768 | camera->setRenderOrder(osg::Camera::POST_RENDER); |
|---|
| 769 | camera->setClearMask(GL_STENCIL_BUFFER_BIT); |
|---|
| 770 | group->addChild(camera); |
|---|
| 771 | |
|---|
| 772 | osg::StateSet* ss_camera = camera->getOrCreateStateSet(); |
|---|
| 773 | |
|---|
| 774 | osg::Depth* depth = new osg::Depth; |
|---|
| 775 | depth->setWriteMask(false); |
|---|
| 776 | depth->setFunction(osg::Depth::LEQUAL); |
|---|
| 777 | ss_camera->setAttribute(depth); |
|---|
| 778 | |
|---|
| 779 | { |
|---|
| 780 | osg::ref_ptr<osg::Geode> geode = new osg::Geode; |
|---|
| 781 | occluder->computeShadowVolumeGeometry(lightpos, *shadowVolume); |
|---|
| 782 | shadowVolume->setDrawMode(drawMode); |
|---|
| 783 | |
|---|
| 784 | |
|---|
| 785 | if (drawMode == osgShadow::ShadowVolumeGeometry::STENCIL_TWO_SIDED) |
|---|
| 786 | { |
|---|
| 787 | osg::notify(osg::NOTICE)<<"STENCIL_TWO_SIDED seleteced"<<std::endl; |
|---|
| 788 | |
|---|
| 789 | osg::StencilTwoSided* stencil = new osg::StencilTwoSided; |
|---|
| 790 | stencil->setFunction(osg::StencilTwoSided::BACK, osg::StencilTwoSided::ALWAYS,0,~0u); |
|---|
| 791 | stencil->setOperation(osg::StencilTwoSided::BACK, osg::StencilTwoSided::KEEP, osg::StencilTwoSided::KEEP, osg::StencilTwoSided::DECR_WRAP); |
|---|
| 792 | stencil->setFunction(osg::StencilTwoSided::FRONT, osg::StencilTwoSided::ALWAYS,0,~0u); |
|---|
| 793 | stencil->setOperation(osg::StencilTwoSided::FRONT, osg::StencilTwoSided::KEEP, osg::StencilTwoSided::KEEP, osg::StencilTwoSided::INCR_WRAP); |
|---|
| 794 | |
|---|
| 795 | |
|---|
| 796 | osg::ColorMask* colourMask = new osg::ColorMask(false, false, false, false); |
|---|
| 797 | |
|---|
| 798 | osg::StateSet* ss_sv1 = geode->getOrCreateStateSet(); |
|---|
| 799 | ss_sv1->setRenderBinDetails(0, "RenderBin"); |
|---|
| 800 | ss_sv1->setAttributeAndModes(stencil,osg::StateAttribute::ON); |
|---|
| 801 | ss_sv1->setAttribute(colourMask); |
|---|
| 802 | ss_sv1->setMode(GL_CULL_FACE,osg::StateAttribute::OFF); |
|---|
| 803 | |
|---|
| 804 | geode->addDrawable(shadowVolume.get()); |
|---|
| 805 | |
|---|
| 806 | camera->addChild(geode.get()); |
|---|
| 807 | |
|---|
| 808 | } |
|---|
| 809 | else |
|---|
| 810 | { |
|---|
| 811 | osg::notify(osg::NOTICE)<<"STENCIL_TWO_PASSES seleteced"<<std::endl; |
|---|
| 812 | |
|---|
| 813 | osg::Stencil* stencil = new osg::Stencil; |
|---|
| 814 | stencil->setFunction(osg::Stencil::ALWAYS,0,~0u); |
|---|
| 815 | stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::KEEP); |
|---|
| 816 | |
|---|
| 817 | osg::ColorMask* colourMask = new osg::ColorMask(false, false, false, false); |
|---|
| 818 | |
|---|
| 819 | osg::StateSet* ss_sv1 = geode->getOrCreateStateSet(); |
|---|
| 820 | ss_sv1->setRenderBinDetails(0, "RenderBin"); |
|---|
| 821 | ss_sv1->setAttributeAndModes(stencil,osg::StateAttribute::ON); |
|---|
| 822 | ss_sv1->setAttribute(colourMask); |
|---|
| 823 | ss_sv1->setMode(GL_CULL_FACE,osg::StateAttribute::ON); |
|---|
| 824 | |
|---|
| 825 | geode->addDrawable(shadowVolume.get()); |
|---|
| 826 | |
|---|
| 827 | camera->addChild(geode.get()); |
|---|
| 828 | } |
|---|
| 829 | |
|---|
| 830 | } |
|---|
| 831 | |
|---|
| 832 | |
|---|
| 833 | |
|---|
| 834 | { |
|---|
| 835 | osg::Group* second_model_group = new osg::Group; |
|---|
| 836 | second_model_group->addChild(model.get()); |
|---|
| 837 | |
|---|
| 838 | |
|---|
| 839 | osg::StateSet* ss1 = second_model_group->getOrCreateStateSet(); |
|---|
| 840 | ss1->setRenderBinDetails(5, "RenderBin"); |
|---|
| 841 | |
|---|
| 842 | osg::LightModel* lm1 = new osg::LightModel; |
|---|
| 843 | lm1->setAmbientIntensity(zero_colour); |
|---|
| 844 | ss1->setAttribute(lm1); |
|---|
| 845 | |
|---|
| 846 | |
|---|
| 847 | osg::LightSource* lightsource = new osg::LightSource; |
|---|
| 848 | lightsource->setLight(light.get()); |
|---|
| 849 | light->setAmbient(zero_colour); |
|---|
| 850 | light->setDiffuse(diffuse); |
|---|
| 851 | light->setPosition(lightpos); |
|---|
| 852 | second_model_group->addChild(lightsource); |
|---|
| 853 | |
|---|
| 854 | ss1->setMode(GL_LIGHT0, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| 855 | |
|---|
| 856 | |
|---|
| 857 | osg::Stencil* stencil = new osg::Stencil; |
|---|
| 858 | stencil->setFunction(osg::Stencil::EQUAL,0,~0u); |
|---|
| 859 | stencil->setOperation(osg::Stencil::KEEP, osg::Stencil::KEEP, osg::Stencil::KEEP); |
|---|
| 860 | ss1->setAttributeAndModes(stencil,osg::StateAttribute::ON); |
|---|
| 861 | |
|---|
| 862 | osg::BlendFunc* blend = new osg::BlendFunc; |
|---|
| 863 | blend->setFunction(osg::BlendFunc::ONE, osg::BlendFunc::ONE); |
|---|
| 864 | ss1->setAttributeAndModes(blend, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| 865 | ss1->setMode(GL_LIGHTING, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); |
|---|
| 866 | |
|---|
| 867 | camera->addChild(second_model_group); |
|---|
| 868 | } |
|---|
| 869 | |
|---|
| 870 | } |
|---|
| 871 | |
|---|
| 872 | } |
|---|
| 873 | |
|---|
| 874 | |
|---|
| 875 | viewer.setSceneData(group.get()); |
|---|
| 876 | |
|---|
| 877 | |
|---|
| 878 | viewer.realize(); |
|---|
| 879 | |
|---|
| 880 | |
|---|
| 881 | |
|---|
| 882 | while (!viewer.done()) |
|---|
| 883 | { |
|---|
| 884 | if (updateLightPosition) |
|---|
| 885 | { |
|---|
| 886 | float t = viewer.getFrameStamp()->getSimulationTime(); |
|---|
| 887 | if (postionalLight) |
|---|
| 888 | { |
|---|
| 889 | lightpos.set(bb.center().x()+sinf(t)*bb.radius(), bb.center().y() + cosf(t)*bb.radius(), bb.zMax() + bb.radius() ,1.0f); |
|---|
| 890 | } |
|---|
| 891 | else |
|---|
| 892 | { |
|---|
| 893 | lightpos.set(sinf(t),cosf(t),0.8f,0.0f); |
|---|
| 894 | } |
|---|
| 895 | light->setPosition(lightpos); |
|---|
| 896 | occluder->computeShadowVolumeGeometry(lightpos, *shadowVolume); |
|---|
| 897 | } |
|---|
| 898 | |
|---|
| 899 | viewer.frame(); |
|---|
| 900 | } |
|---|
| 901 | |
|---|
| 902 | return 0; |
|---|
| 903 | } |
|---|