| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osg/ArgumentParser> |
|---|
| 20 | #include <osg/ComputeBoundsVisitor> |
|---|
| 21 | #include <osg/Texture2D> |
|---|
| 22 | #include <osg/ShapeDrawable> |
|---|
| 23 | #include <osg/MatrixTransform> |
|---|
| 24 | #include <osg/Geometry> |
|---|
| 25 | |
|---|
| 26 | #include <osgGA/TrackballManipulator> |
|---|
| 27 | #include <osgGA/FlightManipulator> |
|---|
| 28 | #include <osgGA/DriveManipulator> |
|---|
| 29 | #include <osgGA/KeySwitchMatrixManipulator> |
|---|
| 30 | #include <osgGA/AnimationPathManipulator> |
|---|
| 31 | #include <osgGA/TerrainManipulator> |
|---|
| 32 | #include <osgGA/AnimationPathManipulator> |
|---|
| 33 | #include <osgGA/StateSetManipulator> |
|---|
| 34 | |
|---|
| 35 | #include <osgViewer/Viewer> |
|---|
| 36 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 37 | |
|---|
| 38 | #include <osgShadow/ShadowedScene> |
|---|
| 39 | #include <osgShadow/ShadowVolume> |
|---|
| 40 | #include <osgShadow/ShadowTexture> |
|---|
| 41 | #include <osgShadow/ShadowMap> |
|---|
| 42 | #include <osgShadow/SoftShadowMap> |
|---|
| 43 | #include <osgShadow/ParallelSplitShadowMap> |
|---|
| 44 | #include <osgShadow/LightSpacePerspectiveShadowMap> |
|---|
| 45 | #include <osgShadow/StandardShadowMap> |
|---|
| 46 | |
|---|
| 47 | #include <osgDB/ReadFile> |
|---|
| 48 | #include <osgDB/WriteFile> |
|---|
| 49 | |
|---|
| 50 | #include <iostream> |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | #include "terrain_coords.h" |
|---|
| 55 | |
|---|
| 56 | #include "IslandScene.h" |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | class ChangeFOVHandler : public osgGA::GUIEventHandler |
|---|
| 60 | { |
|---|
| 61 | public: |
|---|
| 62 | ChangeFOVHandler(osg::Camera* camera) |
|---|
| 63 | : _camera(camera) |
|---|
| 64 | { |
|---|
| 65 | double fovy, aspectRatio, zNear, zFar; |
|---|
| 66 | _camera->getProjectionMatrix().getPerspective(fovy, aspectRatio, zNear, zFar); |
|---|
| 67 | std::cout << "FOV is " << fovy << std::endl; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) |
|---|
| 72 | { |
|---|
| 73 | if (ea.getEventType() == osgGA::GUIEventAdapter::KEYUP) |
|---|
| 74 | { |
|---|
| 75 | if (ea.getKey() == '-' || ea.getKey() == '=' || ea.getKey() == '0') |
|---|
| 76 | { |
|---|
| 77 | double fovy, aspectRatio, zNear, zFar; |
|---|
| 78 | _camera->getProjectionMatrix().getPerspective(fovy, aspectRatio, zNear, zFar); |
|---|
| 79 | |
|---|
| 80 | if (ea.getKey() == '-') |
|---|
| 81 | { |
|---|
| 82 | fovy -= 5.0; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | if (ea.getKey() == '=') |
|---|
| 86 | { |
|---|
| 87 | fovy += 5.0; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | if (ea.getKey() == '0') |
|---|
| 91 | { |
|---|
| 92 | fovy = 45.0; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | std::cout << "Setting FOV to " << fovy << std::endl; |
|---|
| 96 | _camera->getProjectionMatrix().makePerspective(fovy, aspectRatio, zNear, zFar); |
|---|
| 97 | |
|---|
| 98 | return true; |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | return false; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | osg::ref_ptr<osg::Camera> _camera; |
|---|
| 106 | }; |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | class DumpShadowVolumesHandler : public osgGA::GUIEventHandler |
|---|
| 110 | { |
|---|
| 111 | public: |
|---|
| 112 | DumpShadowVolumesHandler( ) |
|---|
| 113 | { |
|---|
| 114 | set( false ); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | bool get() { return _value; } |
|---|
| 118 | void set( bool value ) { _value = value; } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) |
|---|
| 122 | { |
|---|
| 123 | if (ea.getEventType() == osgGA::GUIEventAdapter::KEYUP) |
|---|
| 124 | { |
|---|
| 125 | if (ea.getKey() == 'D' ) |
|---|
| 126 | { |
|---|
| 127 | set( true ); |
|---|
| 128 | return true; |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | return false; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | bool _value; |
|---|
| 136 | }; |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | static int ReceivesShadowTraversalMask = 0x1; |
|---|
| 140 | static int CastsShadowTraversalMask = 0x2; |
|---|
| 141 | |
|---|
| 142 | namespace ModelOne |
|---|
| 143 | { |
|---|
| 144 | |
|---|
| 145 | enum Faces |
|---|
| 146 | { |
|---|
| 147 | FRONT_FACE = 1, |
|---|
| 148 | BACK_FACE = 2, |
|---|
| 149 | LEFT_FACE = 4, |
|---|
| 150 | RIGHT_FACE = 8, |
|---|
| 151 | TOP_FACE = 16, |
|---|
| 152 | BOTTOM_FACE = 32 |
|---|
| 153 | }; |
|---|
| 154 | |
|---|
| 155 | osg::Node* createCube(unsigned int mask) |
|---|
| 156 | { |
|---|
| 157 | osg::Geode* geode = new osg::Geode; |
|---|
| 158 | |
|---|
| 159 | osg::Geometry* geometry = new osg::Geometry; |
|---|
| 160 | geode->addDrawable(geometry); |
|---|
| 161 | |
|---|
| 162 | osg::Vec3Array* vertices = new osg::Vec3Array; |
|---|
| 163 | geometry->setVertexArray(vertices); |
|---|
| 164 | |
|---|
| 165 | osg::Vec3Array* normals = new osg::Vec3Array; |
|---|
| 166 | geometry->setNormalArray(normals); |
|---|
| 167 | geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 168 | |
|---|
| 169 | osg::Vec4Array* colours = new osg::Vec4Array; |
|---|
| 170 | geometry->setColorArray(colours); |
|---|
| 171 | geometry->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 172 | colours->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | osg::Vec3 origin(0.0f,0.0f,0.0f); |
|---|
| 176 | osg::Vec3 dx(2.0f,0.0f,0.0f); |
|---|
| 177 | osg::Vec3 dy(0.0f,1.0f,0.0f); |
|---|
| 178 | osg::Vec3 dz(0.0f,0.0f,1.0f); |
|---|
| 179 | |
|---|
| 180 | osg::Vec3 px(1.0f,0.0,0.0f); |
|---|
| 181 | osg::Vec3 nx(-1.0f,0.0,0.0f); |
|---|
| 182 | osg::Vec3 py(0.0f,1.0f,0.0f); |
|---|
| 183 | osg::Vec3 ny(0.0f,-1.0f,0.0f); |
|---|
| 184 | osg::Vec3 pz(0.0f,0.0f,1.0f); |
|---|
| 185 | osg::Vec3 nz(0.0f,0.0f,-1.0f); |
|---|
| 186 | |
|---|
| 187 | if (mask & FRONT_FACE) |
|---|
| 188 | { |
|---|
| 189 | |
|---|
| 190 | vertices->push_back(origin); |
|---|
| 191 | vertices->push_back(origin+dx); |
|---|
| 192 | vertices->push_back(origin+dx+dz); |
|---|
| 193 | vertices->push_back(origin+dz); |
|---|
| 194 | normals->push_back(ny); |
|---|
| 195 | normals->push_back(ny); |
|---|
| 196 | normals->push_back(ny); |
|---|
| 197 | normals->push_back(ny); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | if (mask & BACK_FACE) |
|---|
| 201 | { |
|---|
| 202 | |
|---|
| 203 | vertices->push_back(origin+dy); |
|---|
| 204 | vertices->push_back(origin+dy+dz); |
|---|
| 205 | vertices->push_back(origin+dy+dx+dz); |
|---|
| 206 | vertices->push_back(origin+dy+dx); |
|---|
| 207 | normals->push_back(py); |
|---|
| 208 | normals->push_back(py); |
|---|
| 209 | normals->push_back(py); |
|---|
| 210 | normals->push_back(py); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | if (mask & LEFT_FACE) |
|---|
| 214 | { |
|---|
| 215 | |
|---|
| 216 | vertices->push_back(origin+dy); |
|---|
| 217 | vertices->push_back(origin); |
|---|
| 218 | vertices->push_back(origin+dz); |
|---|
| 219 | vertices->push_back(origin+dy+dz); |
|---|
| 220 | normals->push_back(nx); |
|---|
| 221 | normals->push_back(nx); |
|---|
| 222 | normals->push_back(nx); |
|---|
| 223 | normals->push_back(nx); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | if (mask & RIGHT_FACE) |
|---|
| 227 | { |
|---|
| 228 | |
|---|
| 229 | vertices->push_back(origin+dx+dy); |
|---|
| 230 | vertices->push_back(origin+dx+dy+dz); |
|---|
| 231 | vertices->push_back(origin+dx+dz); |
|---|
| 232 | vertices->push_back(origin+dx); |
|---|
| 233 | normals->push_back(px); |
|---|
| 234 | normals->push_back(px); |
|---|
| 235 | normals->push_back(px); |
|---|
| 236 | normals->push_back(px); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | if (mask & TOP_FACE) |
|---|
| 240 | { |
|---|
| 241 | |
|---|
| 242 | vertices->push_back(origin+dz); |
|---|
| 243 | vertices->push_back(origin+dz+dx); |
|---|
| 244 | vertices->push_back(origin+dz+dx+dy); |
|---|
| 245 | vertices->push_back(origin+dz+dy); |
|---|
| 246 | normals->push_back(pz); |
|---|
| 247 | normals->push_back(pz); |
|---|
| 248 | normals->push_back(pz); |
|---|
| 249 | normals->push_back(pz); |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | if (mask & BOTTOM_FACE) |
|---|
| 253 | { |
|---|
| 254 | |
|---|
| 255 | vertices->push_back(origin); |
|---|
| 256 | vertices->push_back(origin+dy); |
|---|
| 257 | vertices->push_back(origin+dx+dy); |
|---|
| 258 | vertices->push_back(origin+dx); |
|---|
| 259 | normals->push_back(nz); |
|---|
| 260 | normals->push_back(nz); |
|---|
| 261 | normals->push_back(nz); |
|---|
| 262 | normals->push_back(nz); |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | geometry->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, vertices->size())); |
|---|
| 266 | |
|---|
| 267 | return geode; |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | class SwitchHandler : public osgGA::GUIEventHandler |
|---|
| 271 | { |
|---|
| 272 | public: |
|---|
| 273 | |
|---|
| 274 | SwitchHandler(): |
|---|
| 275 | _childNum(0) {} |
|---|
| 276 | |
|---|
| 277 | virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& , osg::Object* object, osg::NodeVisitor* ) |
|---|
| 278 | { |
|---|
| 279 | osg::Switch* sw = dynamic_cast<osg::Switch*>(object); |
|---|
| 280 | if (!sw) return false; |
|---|
| 281 | |
|---|
| 282 | if (ea.getHandled()) return false; |
|---|
| 283 | |
|---|
| 284 | switch(ea.getEventType()) |
|---|
| 285 | { |
|---|
| 286 | case(osgGA::GUIEventAdapter::KEYDOWN): |
|---|
| 287 | { |
|---|
| 288 | if (ea.getKey()=='n') |
|---|
| 289 | { |
|---|
| 290 | ++_childNum; |
|---|
| 291 | if (_childNum >= sw->getNumChildren()) _childNum = 0; |
|---|
| 292 | |
|---|
| 293 | sw->setSingleChildOn(_childNum); |
|---|
| 294 | return true; |
|---|
| 295 | } |
|---|
| 296 | break; |
|---|
| 297 | } |
|---|
| 298 | default: |
|---|
| 299 | break; |
|---|
| 300 | } |
|---|
| 301 | return false; |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | protected: |
|---|
| 305 | |
|---|
| 306 | virtual ~SwitchHandler() {} |
|---|
| 307 | unsigned int _childNum; |
|---|
| 308 | |
|---|
| 309 | }; |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | osg::Node* createModel(osg::ArgumentParser& ) |
|---|
| 313 | { |
|---|
| 314 | osg::Switch* sw = new osg::Switch; |
|---|
| 315 | sw->setEventCallback(new ModelOne::SwitchHandler); |
|---|
| 316 | |
|---|
| 317 | sw->addChild(ModelOne::createCube(ModelOne::FRONT_FACE), true); |
|---|
| 318 | sw->addChild(ModelOne::createCube(ModelOne::FRONT_FACE | ModelOne::BACK_FACE), false); |
|---|
| 319 | sw->addChild(ModelOne::createCube(ModelOne::FRONT_FACE | ModelOne::BACK_FACE | ModelOne::LEFT_FACE), false); |
|---|
| 320 | sw->addChild(ModelOne::createCube(ModelOne::FRONT_FACE | ModelOne::BACK_FACE | ModelOne::LEFT_FACE | ModelOne::RIGHT_FACE), false); |
|---|
| 321 | sw->addChild(ModelOne::createCube(ModelOne::FRONT_FACE | ModelOne::BACK_FACE | ModelOne::LEFT_FACE | ModelOne::RIGHT_FACE | ModelOne::TOP_FACE), false); |
|---|
| 322 | sw->addChild(ModelOne::createCube(ModelOne::FRONT_FACE | ModelOne::BACK_FACE | ModelOne::LEFT_FACE | ModelOne::RIGHT_FACE | ModelOne::TOP_FACE | ModelOne::BOTTOM_FACE), false); |
|---|
| 323 | |
|---|
| 324 | return sw; |
|---|
| 325 | } |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | namespace ModelTwo |
|---|
| 329 | { |
|---|
| 330 | osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime) |
|---|
| 331 | { |
|---|
| 332 | |
|---|
| 333 | osg::AnimationPath* animationPath = new osg::AnimationPath; |
|---|
| 334 | animationPath->setLoopMode(osg::AnimationPath::LOOP); |
|---|
| 335 | |
|---|
| 336 | int numSamples = 40; |
|---|
| 337 | float yaw = 0.0f; |
|---|
| 338 | float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f); |
|---|
| 339 | float roll = osg::inDegrees(30.0f); |
|---|
| 340 | |
|---|
| 341 | double time=0.0f; |
|---|
| 342 | double time_delta = looptime/(double)numSamples; |
|---|
| 343 | for(int i=0;i<numSamples;++i) |
|---|
| 344 | { |
|---|
| 345 | osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f)); |
|---|
| 346 | osg::Quat rotation(osg::Quat(roll,osg::Vec3(0.0,1.0,0.0))*osg::Quat(-(yaw+osg::inDegrees(90.0f)),osg::Vec3(0.0,0.0,1.0))); |
|---|
| 347 | |
|---|
| 348 | animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation)); |
|---|
| 349 | |
|---|
| 350 | yaw += yaw_delta; |
|---|
| 351 | time += time_delta; |
|---|
| 352 | |
|---|
| 353 | } |
|---|
| 354 | return animationPath; |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | osg::Node* createBase(const osg::Vec3& center,float radius) |
|---|
| 358 | { |
|---|
| 359 | |
|---|
| 360 | osg::Geode* geode = new osg::Geode; |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | osg::StateSet* stateset = new osg::StateSet(); |
|---|
| 364 | osg::Image* image = osgDB::readImageFile("Images/lz.rgb"); |
|---|
| 365 | if (image) |
|---|
| 366 | { |
|---|
| 367 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 368 | texture->setImage(image); |
|---|
| 369 | stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | geode->setStateSet( stateset ); |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | osg::HeightField* grid = new osg::HeightField; |
|---|
| 376 | grid->allocate(38,39); |
|---|
| 377 | grid->setOrigin(center+osg::Vec3(-radius,-radius,0.0f)); |
|---|
| 378 | grid->setXInterval(radius*2.0f/(float)(38-1)); |
|---|
| 379 | grid->setYInterval(radius*2.0f/(float)(39-1)); |
|---|
| 380 | |
|---|
| 381 | float minHeight = FLT_MAX; |
|---|
| 382 | float maxHeight = -FLT_MAX; |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | unsigned int r; |
|---|
| 386 | for(r=0;r<39;++r) |
|---|
| 387 | { |
|---|
| 388 | for(unsigned int c=0;c<38;++c) |
|---|
| 389 | { |
|---|
| 390 | float h = vertex[r+c*39][2]; |
|---|
| 391 | if (h>maxHeight) maxHeight=h; |
|---|
| 392 | if (h<minHeight) minHeight=h; |
|---|
| 393 | } |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | float hieghtScale = radius*0.5f/(maxHeight-minHeight); |
|---|
| 397 | float hieghtOffset = -(minHeight+maxHeight)*0.5f; |
|---|
| 398 | |
|---|
| 399 | for(r=0;r<39;++r) |
|---|
| 400 | { |
|---|
| 401 | for(unsigned int c=0;c<38;++c) |
|---|
| 402 | { |
|---|
| 403 | float h = vertex[r+c*39][2]; |
|---|
| 404 | grid->setHeight(c,r,(h+hieghtOffset)*hieghtScale); |
|---|
| 405 | } |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | geode->addDrawable(new osg::ShapeDrawable(grid)); |
|---|
| 409 | |
|---|
| 410 | osg::Group* group = new osg::Group; |
|---|
| 411 | group->addChild(geode); |
|---|
| 412 | |
|---|
| 413 | return group; |
|---|
| 414 | |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | osg::Node* createMovingModel(const osg::Vec3& center, float radius) |
|---|
| 418 | { |
|---|
| 419 | float animationLength = 10.0f; |
|---|
| 420 | |
|---|
| 421 | osg::AnimationPath* animationPath = createAnimationPath(center,radius,animationLength); |
|---|
| 422 | |
|---|
| 423 | osg::Group* model = new osg::Group; |
|---|
| 424 | |
|---|
| 425 | osg::Node* cessna = osgDB::readNodeFile("cessna.osg"); |
|---|
| 426 | if (cessna) |
|---|
| 427 | { |
|---|
| 428 | const osg::BoundingSphere& bs = cessna->getBound(); |
|---|
| 429 | |
|---|
| 430 | float size = radius/bs.radius()*0.3f; |
|---|
| 431 | osg::MatrixTransform* positioned = new osg::MatrixTransform; |
|---|
| 432 | positioned->setDataVariance(osg::Object::STATIC); |
|---|
| 433 | positioned->setMatrix(osg::Matrix::translate(-bs.center())* |
|---|
| 434 | osg::Matrix::scale(size,size,size)* |
|---|
| 435 | osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,2.0f)); |
|---|
| 436 | |
|---|
| 437 | positioned->addChild(cessna); |
|---|
| 438 | |
|---|
| 439 | osg::MatrixTransform* xform = new osg::MatrixTransform; |
|---|
| 440 | xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,2.0)); |
|---|
| 441 | xform->addChild(positioned); |
|---|
| 442 | |
|---|
| 443 | model->addChild(xform); |
|---|
| 444 | } |
|---|
| 445 | |
|---|
| 446 | return model; |
|---|
| 447 | } |
|---|
| 448 | |
|---|
| 449 | osg::Node* createModel(osg::ArgumentParser& ) |
|---|
| 450 | { |
|---|
| 451 | osg::Vec3 center(0.0f,0.0f,0.0f); |
|---|
| 452 | float radius = 100.0f; |
|---|
| 453 | osg::Vec3 lightPosition(center+osg::Vec3(0.0f,0.0f,radius)); |
|---|
| 454 | |
|---|
| 455 | |
|---|
| 456 | osg::Node* shadower = createMovingModel(center,radius*0.5f); |
|---|
| 457 | shadower->setNodeMask(CastsShadowTraversalMask); |
|---|
| 458 | |
|---|
| 459 | |
|---|
| 460 | osg::Node* shadowed = createBase(center-osg::Vec3(0.0f,0.0f,radius*0.25),radius); |
|---|
| 461 | shadowed->setNodeMask(ReceivesShadowTraversalMask); |
|---|
| 462 | |
|---|
| 463 | osg::Group* group = new osg::Group; |
|---|
| 464 | |
|---|
| 465 | group->addChild(shadowed); |
|---|
| 466 | group->addChild(shadower); |
|---|
| 467 | |
|---|
| 468 | return group; |
|---|
| 469 | } |
|---|
| 470 | } |
|---|
| 471 | |
|---|
| 472 | namespace ModelThree |
|---|
| 473 | { |
|---|
| 474 | osg::Group* createModel(osg::ArgumentParser& arguments) |
|---|
| 475 | { |
|---|
| 476 | osg::Group* scene = new osg::Group; |
|---|
| 477 | |
|---|
| 478 | osg::ref_ptr<osg::Geode> geode_1 = new osg::Geode; |
|---|
| 479 | scene->addChild(geode_1.get()); |
|---|
| 480 | |
|---|
| 481 | osg::ref_ptr<osg::Geode> geode_2 = new osg::Geode; |
|---|
| 482 | osg::ref_ptr<osg::MatrixTransform> transform_2 = new osg::MatrixTransform; |
|---|
| 483 | transform_2->addChild(geode_2.get()); |
|---|
| 484 | |
|---|
| 485 | scene->addChild(transform_2.get()); |
|---|
| 486 | |
|---|
| 487 | osg::ref_ptr<osg::Geode> geode_3 = new osg::Geode; |
|---|
| 488 | osg::ref_ptr<osg::MatrixTransform> transform_3 = new osg::MatrixTransform; |
|---|
| 489 | transform_3->addChild(geode_3.get()); |
|---|
| 490 | |
|---|
| 491 | scene->addChild(transform_3.get()); |
|---|
| 492 | |
|---|
| 493 | const float radius = 0.8f; |
|---|
| 494 | const float height = 1.0f; |
|---|
| 495 | osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints; |
|---|
| 496 | hints->setDetailRatio(2.0f); |
|---|
| 497 | osg::ref_ptr<osg::ShapeDrawable> shape; |
|---|
| 498 | |
|---|
| 499 | shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f, 0.0f, -2.0f), 10, 10.0f, 0.1f), hints.get()); |
|---|
| 500 | shape->setColor(osg::Vec4(0.5f, 0.5f, 0.7f, 1.0f)); |
|---|
| 501 | geode_1->addDrawable(shape.get()); |
|---|
| 502 | |
|---|
| 503 | shape = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f, 0.0f, 0.0f), radius * 2), hints.get()); |
|---|
| 504 | shape->setColor(osg::Vec4(0.8f, 0.8f, 0.8f, 1.0f)); |
|---|
| 505 | geode_1->addDrawable(shape.get()); |
|---|
| 506 | |
|---|
| 507 | shape = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(-3.0f, 0.0f, 0.0f), radius), hints.get()); |
|---|
| 508 | shape->setColor(osg::Vec4(0.6f, 0.8f, 0.8f, 1.0f)); |
|---|
| 509 | geode_2->addDrawable(shape.get()); |
|---|
| 510 | |
|---|
| 511 | shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(3.0f, 0.0f, 0.0f), 2 * radius), hints.get()); |
|---|
| 512 | shape->setColor(osg::Vec4(0.4f, 0.9f, 0.3f, 1.0f)); |
|---|
| 513 | geode_2->addDrawable(shape.get()); |
|---|
| 514 | |
|---|
| 515 | shape = new osg::ShapeDrawable(new osg::Cone(osg::Vec3(0.0f, -3.0f, 0.0f), radius, height), hints.get()); |
|---|
| 516 | shape->setColor(osg::Vec4(0.2f, 0.5f, 0.7f, 1.0f)); |
|---|
| 517 | geode_2->addDrawable(shape.get()); |
|---|
| 518 | |
|---|
| 519 | shape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(0.0f, 3.0f, 0.0f), radius, height), hints.get()); |
|---|
| 520 | shape->setColor(osg::Vec4(1.0f, 0.3f, 0.3f, 1.0f)); |
|---|
| 521 | geode_2->addDrawable(shape.get()); |
|---|
| 522 | |
|---|
| 523 | shape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f, 0.0f, 3.0f), 2.0f, 2.0f, 0.1f), hints.get()); |
|---|
| 524 | shape->setColor(osg::Vec4(0.8f, 0.8f, 0.4f, 1.0f)); |
|---|
| 525 | geode_3->addDrawable(shape.get()); |
|---|
| 526 | |
|---|
| 527 | |
|---|
| 528 | osg::ref_ptr<osg::Material> matirial = new osg::Material; |
|---|
| 529 | matirial->setColorMode(osg::Material::DIFFUSE); |
|---|
| 530 | matirial->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1)); |
|---|
| 531 | matirial->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 1, 1, 1)); |
|---|
| 532 | matirial->setShininess(osg::Material::FRONT_AND_BACK, 64.0f); |
|---|
| 533 | scene->getOrCreateStateSet()->setAttributeAndModes(matirial.get(), osg::StateAttribute::ON); |
|---|
| 534 | |
|---|
| 535 | bool withBaseTexture = true; |
|---|
| 536 | while(arguments.read("--with-base-texture")) { withBaseTexture = true; } |
|---|
| 537 | while(arguments.read("--no-base-texture")) { withBaseTexture = false; } |
|---|
| 538 | |
|---|
| 539 | if (withBaseTexture) |
|---|
| 540 | { |
|---|
| 541 | scene->getOrCreateStateSet()->setTextureAttributeAndModes( 0, new osg::Texture2D(osgDB::readImageFile("Images/lz.rgb")), osg::StateAttribute::ON); |
|---|
| 542 | } |
|---|
| 543 | |
|---|
| 544 | return scene; |
|---|
| 545 | } |
|---|
| 546 | |
|---|
| 547 | } |
|---|
| 548 | |
|---|
| 549 | |
|---|
| 550 | osg::Node* createTestModel(osg::ArgumentParser& arguments) |
|---|
| 551 | { |
|---|
| 552 | if (arguments.read("-1")) |
|---|
| 553 | { |
|---|
| 554 | return ModelOne::createModel(arguments); |
|---|
| 555 | } |
|---|
| 556 | else if (arguments.read("-2")) |
|---|
| 557 | { |
|---|
| 558 | return ModelTwo::createModel(arguments); |
|---|
| 559 | } |
|---|
| 560 | else if (arguments.read("-4")) |
|---|
| 561 | { |
|---|
| 562 | return ModelFour::createModel(arguments); |
|---|
| 563 | } |
|---|
| 564 | else |
|---|
| 565 | { |
|---|
| 566 | return ModelThree::createModel(arguments); |
|---|
| 567 | } |
|---|
| 568 | |
|---|
| 569 | } |
|---|
| 570 | |
|---|
| 571 | struct MyUpdateSlaveCallback : public osg::View::Slave::UpdateSlaveCallback |
|---|
| 572 | { |
|---|
| 573 | MyUpdateSlaveCallback(double nr, double fr):_zNear(nr),_zFar(fr) {} |
|---|
| 574 | |
|---|
| 575 | virtual void updateSlave(osg::View& view, osg::View::Slave& slave) |
|---|
| 576 | { |
|---|
| 577 | slave.updateSlaveImplementation(view); |
|---|
| 578 | osg::Camera* camera = slave._camera.get(); |
|---|
| 579 | |
|---|
| 580 | |
|---|
| 581 | |
|---|
| 582 | if (camera->getProjectionMatrix()(0,3)==0.0 && |
|---|
| 583 | camera->getProjectionMatrix()(1,3)==0.0 && |
|---|
| 584 | camera->getProjectionMatrix()(2,3)==0.0) |
|---|
| 585 | { |
|---|
| 586 | |
|---|
| 587 | double left, right, bottom, top, zNear, zFar; |
|---|
| 588 | camera->getProjectionMatrixAsOrtho(left, right, bottom, top, zNear, zFar); |
|---|
| 589 | camera->setProjectionMatrixAsOrtho(left, right, bottom, top, _zNear, _zFar); |
|---|
| 590 | } |
|---|
| 591 | else |
|---|
| 592 | { |
|---|
| 593 | double left, right, bottom, top, zNear, zFar; |
|---|
| 594 | camera->getProjectionMatrixAsFrustum(left, right, bottom, top, zNear, zFar); |
|---|
| 595 | |
|---|
| 596 | double nr = _zNear / zNear; |
|---|
| 597 | camera->setProjectionMatrixAsFrustum(left * nr, right * nr, bottom * nr, top * nr, _zNear, _zFar); |
|---|
| 598 | } |
|---|
| 599 | } |
|---|
| 600 | |
|---|
| 601 | double _zNear, _zFar; |
|---|
| 602 | }; |
|---|
| 603 | |
|---|
| 604 | void setUpViewForDepthPartion(osgViewer::Viewer& viewer, double partitionPosition) |
|---|
| 605 | { |
|---|
| 606 | OSG_NOTICE<<"setUpViewForDepthPartion(Viewer, "<<partitionPosition<<")"<<std::endl; |
|---|
| 607 | |
|---|
| 608 | osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); |
|---|
| 609 | if (!wsi) |
|---|
| 610 | { |
|---|
| 611 | OSG_NOTICE<<"View::setUpViewAcrossAllScreens() : Error, no WindowSystemInterface available, cannot create windows."<<std::endl; |
|---|
| 612 | return; |
|---|
| 613 | } |
|---|
| 614 | |
|---|
| 615 | |
|---|
| 616 | osg::GraphicsContext::ScreenIdentifier si(0); |
|---|
| 617 | si.readDISPLAY(); |
|---|
| 618 | |
|---|
| 619 | |
|---|
| 620 | if (si.displayNum<0) si.displayNum = 0; |
|---|
| 621 | if (si.screenNum<0) si.screenNum = 0; |
|---|
| 622 | |
|---|
| 623 | unsigned int width = 1280, height = 1024; |
|---|
| 624 | wsi->getScreenResolution(si, width, height); |
|---|
| 625 | |
|---|
| 626 | |
|---|
| 627 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
|---|
| 628 | traits->screenNum = 0; |
|---|
| 629 | traits->x = 0; |
|---|
| 630 | traits->y = 0; |
|---|
| 631 | traits->width = width; |
|---|
| 632 | traits->height = height; |
|---|
| 633 | traits->windowDecoration = false; |
|---|
| 634 | traits->doubleBuffer = true; |
|---|
| 635 | traits->sharedContext = 0; |
|---|
| 636 | |
|---|
| 637 | osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); |
|---|
| 638 | if (gc.valid()) |
|---|
| 639 | { |
|---|
| 640 | osg::notify(osg::INFO)<<" GraphicsWindow has been created successfully."<<std::endl; |
|---|
| 641 | } |
|---|
| 642 | else |
|---|
| 643 | { |
|---|
| 644 | osg::notify(osg::NOTICE)<<" GraphicsWindow has not been created successfully."<<std::endl; |
|---|
| 645 | } |
|---|
| 646 | |
|---|
| 647 | double zNear = 0.5; |
|---|
| 648 | double zMid = partitionPosition; |
|---|
| 649 | double zFar = 200.0; |
|---|
| 650 | |
|---|
| 651 | |
|---|
| 652 | { |
|---|
| 653 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 654 | camera->setGraphicsContext(gc); |
|---|
| 655 | camera->setViewport(new osg::Viewport(0,0, width, height)); |
|---|
| 656 | |
|---|
| 657 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 658 | camera->setDrawBuffer(buffer); |
|---|
| 659 | camera->setReadBuffer(buffer); |
|---|
| 660 | |
|---|
| 661 | double scale_z = 1.0; |
|---|
| 662 | double translate_z = 0.0; |
|---|
| 663 | |
|---|
| 664 | camera->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR); |
|---|
| 665 | camera->setCullingMode(osg::Camera::ENABLE_ALL_CULLING); |
|---|
| 666 | |
|---|
| 667 | viewer.addSlave(camera.get(), osg::Matrix::scale(1.0, 1.0, scale_z)*osg::Matrix::translate(0.0, 0.0, translate_z), osg::Matrix() ); |
|---|
| 668 | |
|---|
| 669 | osg::View::Slave& slave = viewer.getSlave(viewer.getNumSlaves()-1); |
|---|
| 670 | slave._updateSlaveCallback = new MyUpdateSlaveCallback(zMid, zFar); |
|---|
| 671 | } |
|---|
| 672 | |
|---|
| 673 | |
|---|
| 674 | { |
|---|
| 675 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 676 | camera->setGraphicsContext(gc); |
|---|
| 677 | camera->setViewport(new osg::Viewport(0,0, width, height)); |
|---|
| 678 | |
|---|
| 679 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 680 | camera->setDrawBuffer(buffer); |
|---|
| 681 | camera->setReadBuffer(buffer); |
|---|
| 682 | |
|---|
| 683 | double scale_z = 1.0; |
|---|
| 684 | double translate_z = 0.0; |
|---|
| 685 | |
|---|
| 686 | camera->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR); |
|---|
| 687 | camera->setCullingMode(osg::Camera::ENABLE_ALL_CULLING); |
|---|
| 688 | camera->setClearMask(GL_DEPTH_BUFFER_BIT); |
|---|
| 689 | |
|---|
| 690 | viewer.addSlave(camera.get(), osg::Matrix::scale(1.0, 1.0, scale_z)*osg::Matrix::translate(0.0, 0.0, translate_z), osg::Matrix() ); |
|---|
| 691 | |
|---|
| 692 | osg::View::Slave& slave = viewer.getSlave(viewer.getNumSlaves()-1); |
|---|
| 693 | slave._updateSlaveCallback = new MyUpdateSlaveCallback(zNear, zMid); |
|---|
| 694 | } |
|---|
| 695 | } |
|---|
| 696 | |
|---|
| 697 | |
|---|
| 698 | int main(int argc, char** argv) |
|---|
| 699 | { |
|---|
| 700 | |
|---|
| 701 | osg::ArgumentParser arguments(&argc, argv); |
|---|
| 702 | |
|---|
| 703 | |
|---|
| 704 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName() + " is the example which demonstrates using of GL_ARB_shadow extension implemented in osg::Texture class"); |
|---|
| 705 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()); |
|---|
| 706 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help", "Display this information"); |
|---|
| 707 | arguments.getApplicationUsage()->addCommandLineOption("--positionalLight", "Use a positional light."); |
|---|
| 708 | arguments.getApplicationUsage()->addCommandLineOption("--directionalLight", "Use a direction light."); |
|---|
| 709 | arguments.getApplicationUsage()->addCommandLineOption("--noUpdate", "Disable the updating the of light source."); |
|---|
| 710 | |
|---|
| 711 | arguments.getApplicationUsage()->addCommandLineOption("--castsShadowMask", "Override default castsShadowMask (default - 0x2)"); |
|---|
| 712 | arguments.getApplicationUsage()->addCommandLineOption("--receivesShadowMask", "Override default receivesShadowMask (default - 0x1)"); |
|---|
| 713 | |
|---|
| 714 | arguments.getApplicationUsage()->addCommandLineOption("--base", "Add a base geometry to test shadows."); |
|---|
| 715 | arguments.getApplicationUsage()->addCommandLineOption("--sv", "Select ShadowVolume implementation."); |
|---|
| 716 | arguments.getApplicationUsage()->addCommandLineOption("--ssm", "Select SoftShadowMap implementation."); |
|---|
| 717 | arguments.getApplicationUsage()->addCommandLineOption("--sm", "Select ShadowMap implementation."); |
|---|
| 718 | |
|---|
| 719 | arguments.getApplicationUsage()->addCommandLineOption("--pssm", "Select ParallelSplitShadowMap implementation."); |
|---|
| 720 | arguments.getApplicationUsage()->addCommandLineOption("--mapcount", "ParallelSplitShadowMap texture count."); |
|---|
| 721 | arguments.getApplicationUsage()->addCommandLineOption("--mapres", "ParallelSplitShadowMap texture resolution."); |
|---|
| 722 | arguments.getApplicationUsage()->addCommandLineOption("--debug-color", "ParallelSplitShadowMap display debugging color (only the first 3 maps are color r=0,g=1,b=2."); |
|---|
| 723 | arguments.getApplicationUsage()->addCommandLineOption("--minNearSplit", "ParallelSplitShadowMap shadow map near offset."); |
|---|
| 724 | arguments.getApplicationUsage()->addCommandLineOption("--maxFarDist", "ParallelSplitShadowMap max far distance to shadow."); |
|---|
| 725 | arguments.getApplicationUsage()->addCommandLineOption("--moveVCamFactor", "ParallelSplitShadowMap move the virtual frustum behind the real camera, (also back ground object can cast shadow)."); |
|---|
| 726 | arguments.getApplicationUsage()->addCommandLineOption("--PolyOffset-Factor", "ParallelSplitShadowMap set PolygonOffset factor."); |
|---|
| 727 | arguments.getApplicationUsage()->addCommandLineOption("--PolyOffset-Unit", "ParallelSplitShadowMap set PolygonOffset unit."); |
|---|
| 728 | |
|---|
| 729 | arguments.getApplicationUsage()->addCommandLineOption("--lispsm", "Select LightSpacePerspectiveShadowMap implementation."); |
|---|
| 730 | arguments.getApplicationUsage()->addCommandLineOption("--msm", "Select MinimalShadowMap implementation."); |
|---|
| 731 | arguments.getApplicationUsage()->addCommandLineOption("--ViewBounds", "MSM, LiSPSM & optimize shadow for view frustum (weakest option)"); |
|---|
| 732 | arguments.getApplicationUsage()->addCommandLineOption("--CullBounds", "MSM, LiSPSM & optimize shadow for bounds of culled objects in view frustum (better option)."); |
|---|
| 733 | arguments.getApplicationUsage()->addCommandLineOption("--DrawBounds", "MSM, LiSPSM & optimize shadow for bounds of predrawn pixels in view frustum (best & default)."); |
|---|
| 734 | arguments.getApplicationUsage()->addCommandLineOption("--mapres", "MSM, LiSPSM & texture resolution."); |
|---|
| 735 | arguments.getApplicationUsage()->addCommandLineOption("--maxFarDist", "MSM, LiSPSM & max far distance to shadow."); |
|---|
| 736 | arguments.getApplicationUsage()->addCommandLineOption("--moveVCamFactor", "MSM, LiSPSM & move the virtual frustum behind the real camera, (also back ground object can cast shadow)."); |
|---|
| 737 | arguments.getApplicationUsage()->addCommandLineOption("--minLightMargin", "MSM, LiSPSM t& the same as --moveVCamFactor."); |
|---|
| 738 | |
|---|
| 739 | arguments.getApplicationUsage()->addCommandLineOption("-1", "Use test model one."); |
|---|
| 740 | arguments.getApplicationUsage()->addCommandLineOption("-2", "Use test model two."); |
|---|
| 741 | arguments.getApplicationUsage()->addCommandLineOption("-3", "Use test model three (default)."); |
|---|
| 742 | arguments.getApplicationUsage()->addCommandLineOption("-4", "Use test model four - island scene."); |
|---|
| 743 | arguments.getApplicationUsage()->addCommandLineOption("--two-sided", "Use two-sided stencil extension for shadow volumes."); |
|---|
| 744 | arguments.getApplicationUsage()->addCommandLineOption("--two-pass", "Use two-pass stencil for shadow volumes."); |
|---|
| 745 | |
|---|
| 746 | |
|---|
| 747 | osgViewer::Viewer viewer(arguments); |
|---|
| 748 | |
|---|
| 749 | |
|---|
| 750 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 751 | { |
|---|
| 752 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 753 | return 1; |
|---|
| 754 | } |
|---|
| 755 | |
|---|
| 756 | double partitionPosition = 5.0; |
|---|
| 757 | if (arguments.read("--depth-partition",partitionPosition) || arguments.read("--dp")) |
|---|
| 758 | { |
|---|
| 759 | setUpViewForDepthPartion(viewer,partitionPosition); |
|---|
| 760 | } |
|---|
| 761 | |
|---|
| 762 | float fov = 0.0; |
|---|
| 763 | while (arguments.read("--fov",fov)) {} |
|---|
| 764 | |
|---|
| 765 | osg::Vec4 lightpos(0.0,0.0,1,0.0); |
|---|
| 766 | while (arguments.read("--positionalLight")) { lightpos.set(0.5,0.5,1.5,1.0); } |
|---|
| 767 | while (arguments.read("--directionalLight")) { lightpos.set(0.0,0.0,1,0.0); } |
|---|
| 768 | |
|---|
| 769 | while ( arguments.read("--light-pos", lightpos.x(), lightpos.y(), lightpos.z(), lightpos.w())) {} |
|---|
| 770 | while ( arguments.read("--light-pos", lightpos.x(), lightpos.y(), lightpos.z())) { lightpos.w()=1.0; } |
|---|
| 771 | while ( arguments.read("--light-dir", lightpos.x(), lightpos.y(), lightpos.z())) { lightpos.w()=0.0; } |
|---|
| 772 | |
|---|
| 773 | |
|---|
| 774 | while (arguments.read("--castsShadowMask", CastsShadowTraversalMask )); |
|---|
| 775 | while (arguments.read("--receivesShadowMask", ReceivesShadowTraversalMask )); |
|---|
| 776 | |
|---|
| 777 | bool updateLightPosition = true; |
|---|
| 778 | while (arguments.read("--noUpdate")) updateLightPosition = false; |
|---|
| 779 | |
|---|
| 780 | |
|---|
| 781 | { |
|---|
| 782 | osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; |
|---|
| 783 | |
|---|
| 784 | keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() ); |
|---|
| 785 | keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() ); |
|---|
| 786 | keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() ); |
|---|
| 787 | keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() ); |
|---|
| 788 | |
|---|
| 789 | std::string pathfile; |
|---|
| 790 | char keyForAnimationPath = '5'; |
|---|
| 791 | while (arguments.read("-p",pathfile)) |
|---|
| 792 | { |
|---|
| 793 | osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile); |
|---|
| 794 | if (apm || !apm->valid()) |
|---|
| 795 | { |
|---|
| 796 | unsigned int num = keyswitchManipulator->getNumMatrixManipulators(); |
|---|
| 797 | keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm ); |
|---|
| 798 | keyswitchManipulator->selectMatrixManipulator(num); |
|---|
| 799 | ++keyForAnimationPath; |
|---|
| 800 | } |
|---|
| 801 | } |
|---|
| 802 | |
|---|
| 803 | viewer.setCameraManipulator( keyswitchManipulator.get() ); |
|---|
| 804 | } |
|---|
| 805 | |
|---|
| 806 | |
|---|
| 807 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 808 | |
|---|
| 809 | |
|---|
| 810 | viewer.addEventHandler( new osgViewer::StatsHandler() ); |
|---|
| 811 | |
|---|
| 812 | |
|---|
| 813 | viewer.addEventHandler(new osgViewer::RecordCameraPathHandler); |
|---|
| 814 | |
|---|
| 815 | |
|---|
| 816 | viewer.addEventHandler(new osgViewer::WindowSizeHandler); |
|---|
| 817 | |
|---|
| 818 | |
|---|
| 819 | viewer.addEventHandler( new osgViewer::ThreadingHandler() ); |
|---|
| 820 | |
|---|
| 821 | osg::ref_ptr<osgShadow::ShadowedScene> shadowedScene = new osgShadow::ShadowedScene; |
|---|
| 822 | |
|---|
| 823 | shadowedScene->setReceivesShadowTraversalMask(ReceivesShadowTraversalMask); |
|---|
| 824 | shadowedScene->setCastsShadowTraversalMask(CastsShadowTraversalMask); |
|---|
| 825 | |
|---|
| 826 | osg::ref_ptr<osgShadow::MinimalShadowMap> msm = NULL; |
|---|
| 827 | if (arguments.read("--sv")) |
|---|
| 828 | { |
|---|
| 829 | |
|---|
| 830 | osg::DisplaySettings::instance()->setMinimumNumStencilBits(8); |
|---|
| 831 | |
|---|
| 832 | osg::ref_ptr<osgShadow::ShadowVolume> sv = new osgShadow::ShadowVolume; |
|---|
| 833 | sv->setDynamicShadowVolumes(updateLightPosition); |
|---|
| 834 | while (arguments.read("--two-sided")) sv->setDrawMode(osgShadow::ShadowVolumeGeometry::STENCIL_TWO_SIDED); |
|---|
| 835 | while (arguments.read("--two-pass")) sv->setDrawMode(osgShadow::ShadowVolumeGeometry::STENCIL_TWO_PASS); |
|---|
| 836 | |
|---|
| 837 | shadowedScene->setShadowTechnique(sv.get()); |
|---|
| 838 | } |
|---|
| 839 | else if (arguments.read("--st")) |
|---|
| 840 | { |
|---|
| 841 | osg::ref_ptr<osgShadow::ShadowTexture> st = new osgShadow::ShadowTexture; |
|---|
| 842 | shadowedScene->setShadowTechnique(st.get()); |
|---|
| 843 | } |
|---|
| 844 | else if (arguments.read("--stsm")) |
|---|
| 845 | { |
|---|
| 846 | osg::ref_ptr<osgShadow::StandardShadowMap> st = new osgShadow::StandardShadowMap; |
|---|
| 847 | shadowedScene->setShadowTechnique(st.get()); |
|---|
| 848 | } |
|---|
| 849 | else if (arguments.read("--pssm")) |
|---|
| 850 | { |
|---|
| 851 | int mapcount = 3; |
|---|
| 852 | while (arguments.read("--mapcount", mapcount)); |
|---|
| 853 | osg::ref_ptr<osgShadow::ParallelSplitShadowMap> pssm = new osgShadow::ParallelSplitShadowMap(NULL,mapcount); |
|---|
| 854 | |
|---|
| 855 | int mapres = 1024; |
|---|
| 856 | while (arguments.read("--mapres", mapres)) |
|---|
| 857 | pssm->setTextureResolution(mapres); |
|---|
| 858 | |
|---|
| 859 | while (arguments.read("--debug-color")) { pssm->setDebugColorOn(); } |
|---|
| 860 | |
|---|
| 861 | |
|---|
| 862 | int minNearSplit=0; |
|---|
| 863 | while (arguments.read("--minNearSplit", minNearSplit)) |
|---|
| 864 | if ( minNearSplit > 0 ) { |
|---|
| 865 | pssm->setMinNearDistanceForSplits(minNearSplit); |
|---|
| 866 | std::cout << "ParallelSplitShadowMap : setMinNearDistanceForSplits(" << minNearSplit <<")" << std::endl; |
|---|
| 867 | } |
|---|
| 868 | |
|---|
| 869 | int maxfardist = 0; |
|---|
| 870 | while (arguments.read("--maxFarDist", maxfardist)) |
|---|
| 871 | if ( maxfardist > 0 ) { |
|---|
| 872 | pssm->setMaxFarDistance(maxfardist); |
|---|
| 873 | std::cout << "ParallelSplitShadowMap : setMaxFarDistance(" << maxfardist<<")" << std::endl; |
|---|
| 874 | } |
|---|
| 875 | |
|---|
| 876 | int moveVCamFactor = 0; |
|---|
| 877 | while (arguments.read("--moveVCamFactor", moveVCamFactor)) |
|---|
| 878 | if ( maxfardist > 0 ) { |
|---|
| 879 | pssm->setMoveVCamBehindRCamFactor(moveVCamFactor); |
|---|
| 880 | std::cout << "ParallelSplitShadowMap : setMoveVCamBehindRCamFactor(" << moveVCamFactor<<")" << std::endl; |
|---|
| 881 | } |
|---|
| 882 | |
|---|
| 883 | |
|---|
| 884 | |
|---|
| 885 | double polyoffsetfactor = pssm->getPolygonOffset().x(); |
|---|
| 886 | double polyoffsetunit = pssm->getPolygonOffset().y(); |
|---|
| 887 | while (arguments.read("--PolyOffset-Factor", polyoffsetfactor)); |
|---|
| 888 | while (arguments.read("--PolyOffset-Unit", polyoffsetunit)); |
|---|
| 889 | pssm->setPolygonOffset(osg::Vec2(polyoffsetfactor,polyoffsetunit)); |
|---|
| 890 | |
|---|
| 891 | shadowedScene->setShadowTechnique(pssm.get()); |
|---|
| 892 | } |
|---|
| 893 | else if (arguments.read("--ssm")) |
|---|
| 894 | { |
|---|
| 895 | osg::ref_ptr<osgShadow::SoftShadowMap> sm = new osgShadow::SoftShadowMap; |
|---|
| 896 | shadowedScene->setShadowTechnique(sm.get()); |
|---|
| 897 | } |
|---|
| 898 | else if ( arguments.read("--lispsm") ) |
|---|
| 899 | { |
|---|
| 900 | if( arguments.read( "--ViewBounds" ) ) |
|---|
| 901 | msm = new osgShadow::LightSpacePerspectiveShadowMapVB; |
|---|
| 902 | else if( arguments.read( "--CullBounds" ) ) |
|---|
| 903 | msm = new osgShadow::LightSpacePerspectiveShadowMapCB; |
|---|
| 904 | else |
|---|
| 905 | msm = new osgShadow::LightSpacePerspectiveShadowMapDB; |
|---|
| 906 | } |
|---|
| 907 | else if( arguments.read("--msm") ) |
|---|
| 908 | { |
|---|
| 909 | if( arguments.read( "--ViewBounds" ) ) |
|---|
| 910 | msm = new osgShadow::MinimalShadowMap; |
|---|
| 911 | else if( arguments.read( "--CullBounds" ) ) |
|---|
| 912 | msm = new osgShadow::MinimalCullBoundsShadowMap; |
|---|
| 913 | else |
|---|
| 914 | msm = new osgShadow::MinimalDrawBoundsShadowMap; |
|---|
| 915 | } |
|---|
| 916 | else |
|---|
| 917 | { |
|---|
| 918 | osg::ref_ptr<osgShadow::ShadowMap> sm = new osgShadow::ShadowMap; |
|---|
| 919 | shadowedScene->setShadowTechnique(sm.get()); |
|---|
| 920 | |
|---|
| 921 | int mapres = 1024; |
|---|
| 922 | while (arguments.read("--mapres", mapres)) |
|---|
| 923 | sm->setTextureSize(osg::Vec2s(mapres,mapres)); |
|---|
| 924 | } |
|---|
| 925 | |
|---|
| 926 | if( msm ) |
|---|
| 927 | { |
|---|
| 928 | shadowedScene->setShadowTechnique( msm.get() ); |
|---|
| 929 | while( arguments.read("--debugHUD") ) |
|---|
| 930 | msm->setDebugDraw( true ); |
|---|
| 931 | |
|---|
| 932 | float minLightMargin = 10.f; |
|---|
| 933 | float maxFarPlane = 0; |
|---|
| 934 | unsigned int texSize = 1024; |
|---|
| 935 | unsigned int baseTexUnit = 0; |
|---|
| 936 | unsigned int shadowTexUnit = 1; |
|---|
| 937 | |
|---|
| 938 | while ( arguments.read("--moveVCamFactor", minLightMargin ) ); |
|---|
| 939 | while ( arguments.read("--minLightMargin", minLightMargin ) ); |
|---|
| 940 | while ( arguments.read("--maxFarDist", maxFarPlane ) ); |
|---|
| 941 | while ( arguments.read("--mapres", texSize )); |
|---|
| 942 | while ( arguments.read("--baseTextureUnit", baseTexUnit) ); |
|---|
| 943 | while ( arguments.read("--shadowTextureUnit", shadowTexUnit) ); |
|---|
| 944 | |
|---|
| 945 | msm->setMinLightMargin( minLightMargin ); |
|---|
| 946 | msm->setMaxFarPlane( maxFarPlane ); |
|---|
| 947 | msm->setTextureSize( osg::Vec2s( texSize, texSize ) ); |
|---|
| 948 | msm->setShadowTextureCoordIndex( shadowTexUnit ); |
|---|
| 949 | msm->setShadowTextureUnit( shadowTexUnit ); |
|---|
| 950 | msm->setBaseTextureCoordIndex( baseTexUnit ); |
|---|
| 951 | msm->setBaseTextureUnit( baseTexUnit ); |
|---|
| 952 | } |
|---|
| 953 | |
|---|
| 954 | osg::ref_ptr<osg::Node> model = osgDB::readNodeFiles(arguments); |
|---|
| 955 | if (model.valid()) |
|---|
| 956 | { |
|---|
| 957 | model->setNodeMask(CastsShadowTraversalMask | ReceivesShadowTraversalMask); |
|---|
| 958 | } |
|---|
| 959 | else |
|---|
| 960 | { |
|---|
| 961 | model = createTestModel(arguments); |
|---|
| 962 | } |
|---|
| 963 | |
|---|
| 964 | |
|---|
| 965 | osg::ComputeBoundsVisitor cbbv; |
|---|
| 966 | model->accept(cbbv); |
|---|
| 967 | osg::BoundingBox bb = cbbv.getBoundingBox(); |
|---|
| 968 | |
|---|
| 969 | if (lightpos.w()==1.0) |
|---|
| 970 | { |
|---|
| 971 | lightpos.x() = bb.xMin()+(bb.xMax()-bb.xMin())*lightpos.x(); |
|---|
| 972 | lightpos.y() = bb.yMin()+(bb.yMax()-bb.yMin())*lightpos.y(); |
|---|
| 973 | lightpos.z() = bb.zMin()+(bb.zMax()-bb.zMin())*lightpos.z(); |
|---|
| 974 | } |
|---|
| 975 | |
|---|
| 976 | if ( arguments.read("--base")) |
|---|
| 977 | { |
|---|
| 978 | |
|---|
| 979 | osg::Geode* geode = new osg::Geode; |
|---|
| 980 | |
|---|
| 981 | osg::Vec3 widthVec(bb.radius(), 0.0f, 0.0f); |
|---|
| 982 | osg::Vec3 depthVec(0.0f, bb.radius(), 0.0f); |
|---|
| 983 | osg::Vec3 centerBase( (bb.xMin()+bb.xMax())*0.5f, (bb.yMin()+bb.yMax())*0.5f, bb.zMin()-bb.radius()*0.1f ); |
|---|
| 984 | |
|---|
| 985 | geode->addDrawable( osg::createTexturedQuadGeometry( centerBase-widthVec*1.5f-depthVec*1.5f, |
|---|
| 986 | widthVec*3.0f, depthVec*3.0f) ); |
|---|
| 987 | |
|---|
| 988 | geode->setNodeMask(shadowedScene->getReceivesShadowTraversalMask()); |
|---|
| 989 | |
|---|
| 990 | geode->getOrCreateStateSet()->setTextureAttributeAndModes(0, new osg::Texture2D(osgDB::readImageFile("Images/lz.rgb"))); |
|---|
| 991 | |
|---|
| 992 | shadowedScene->addChild(geode); |
|---|
| 993 | } |
|---|
| 994 | |
|---|
| 995 | osg::ref_ptr<osg::LightSource> ls = new osg::LightSource; |
|---|
| 996 | ls->getLight()->setPosition(lightpos); |
|---|
| 997 | |
|---|
| 998 | bool spotlight = false; |
|---|
| 999 | if (arguments.read("--spotLight")) |
|---|
| 1000 | { |
|---|
| 1001 | spotlight = true; |
|---|
| 1002 | |
|---|
| 1003 | osg::Vec3 center = bb.center(); |
|---|
| 1004 | osg::Vec3 lightdir = center - osg::Vec3(lightpos.x(), lightpos.y(), lightpos.z()); |
|---|
| 1005 | lightdir.normalize(); |
|---|
| 1006 | ls->getLight()->setDirection(lightdir); |
|---|
| 1007 | ls->getLight()->setSpotCutoff(25.0f); |
|---|
| 1008 | |
|---|
| 1009 | |
|---|
| 1010 | osgShadow::ShadowMap* shadowMap = dynamic_cast<osgShadow::ShadowMap*>(shadowedScene->getShadowTechnique()); |
|---|
| 1011 | if( shadowMap ) shadowMap->setLight(ls.get()); |
|---|
| 1012 | } |
|---|
| 1013 | |
|---|
| 1014 | if ( arguments.read("--coloured-light")) |
|---|
| 1015 | { |
|---|
| 1016 | ls->getLight()->setAmbient(osg::Vec4(1.0,0.0,0.0,1.0)); |
|---|
| 1017 | ls->getLight()->setDiffuse(osg::Vec4(0.0,1.0,0.0,1.0)); |
|---|
| 1018 | } |
|---|
| 1019 | else |
|---|
| 1020 | { |
|---|
| 1021 | ls->getLight()->setAmbient(osg::Vec4(0.2,0.2,0.2,1.0)); |
|---|
| 1022 | ls->getLight()->setDiffuse(osg::Vec4(0.8,0.8,0.8,1.0)); |
|---|
| 1023 | } |
|---|
| 1024 | |
|---|
| 1025 | shadowedScene->addChild(model.get()); |
|---|
| 1026 | shadowedScene->addChild(ls.get()); |
|---|
| 1027 | |
|---|
| 1028 | viewer.setSceneData(shadowedScene.get()); |
|---|
| 1029 | |
|---|
| 1030 | osg::ref_ptr< DumpShadowVolumesHandler > dumpShadowVolumes = new DumpShadowVolumesHandler; |
|---|
| 1031 | |
|---|
| 1032 | viewer.addEventHandler(new ChangeFOVHandler(viewer.getCamera())); |
|---|
| 1033 | viewer.addEventHandler( dumpShadowVolumes.get() ); |
|---|
| 1034 | |
|---|
| 1035 | |
|---|
| 1036 | viewer.realize(); |
|---|
| 1037 | |
|---|
| 1038 | if (fov!=0.0) |
|---|
| 1039 | { |
|---|
| 1040 | double fovy, aspectRatio, zNear, zFar; |
|---|
| 1041 | viewer.getCamera()->getProjectionMatrix().getPerspective(fovy, aspectRatio, zNear, zFar); |
|---|
| 1042 | |
|---|
| 1043 | std::cout << "Setting FOV to " << fov << std::endl; |
|---|
| 1044 | viewer.getCamera()->getProjectionMatrix().makePerspective(fov, aspectRatio, zNear, zFar); |
|---|
| 1045 | } |
|---|
| 1046 | |
|---|
| 1047 | |
|---|
| 1048 | if ( arguments.read("--debugHUD")) |
|---|
| 1049 | { |
|---|
| 1050 | osgViewer::Viewer::Windows windows; |
|---|
| 1051 | viewer.getWindows(windows); |
|---|
| 1052 | |
|---|
| 1053 | if (windows.empty()) return 1; |
|---|
| 1054 | |
|---|
| 1055 | osgShadow::ShadowMap* sm = dynamic_cast<osgShadow::ShadowMap*>(shadowedScene->getShadowTechnique()); |
|---|
| 1056 | if( sm ) { |
|---|
| 1057 | osg::ref_ptr<osg::Camera> hudCamera = sm->makeDebugHUD(); |
|---|
| 1058 | |
|---|
| 1059 | |
|---|
| 1060 | hudCamera->setGraphicsContext(windows[0]); |
|---|
| 1061 | hudCamera->setViewport(0,0,windows[0]->getTraits()->width, windows[0]->getTraits()->height); |
|---|
| 1062 | |
|---|
| 1063 | viewer.addSlave(hudCamera.get(), false); |
|---|
| 1064 | } |
|---|
| 1065 | } |
|---|
| 1066 | |
|---|
| 1067 | |
|---|
| 1068 | |
|---|
| 1069 | |
|---|
| 1070 | while (!viewer.done()) |
|---|
| 1071 | { |
|---|
| 1072 | { |
|---|
| 1073 | osgShadow::MinimalShadowMap * msm = dynamic_cast<osgShadow::MinimalShadowMap*>( shadowedScene->getShadowTechnique() ); |
|---|
| 1074 | |
|---|
| 1075 | if( msm ) { |
|---|
| 1076 | |
|---|
| 1077 | |
|---|
| 1078 | |
|---|
| 1079 | |
|---|
| 1080 | osg::NodePath np = viewer.getCoordinateSystemNodePath(); |
|---|
| 1081 | if( !np.empty() ) { |
|---|
| 1082 | osg::CoordinateSystemNode * csn = |
|---|
| 1083 | dynamic_cast<osg::CoordinateSystemNode *>( np.back() ); |
|---|
| 1084 | |
|---|
| 1085 | if( csn ) { |
|---|
| 1086 | osg::Vec3d pos = |
|---|
| 1087 | viewer.getCameraManipulator()->getMatrix().getTrans(); |
|---|
| 1088 | |
|---|
| 1089 | msm->setModellingSpaceToWorldTransform |
|---|
| 1090 | ( csn->computeLocalCoordinateFrame( pos ) ); |
|---|
| 1091 | } |
|---|
| 1092 | } |
|---|
| 1093 | } |
|---|
| 1094 | } |
|---|
| 1095 | |
|---|
| 1096 | if (updateLightPosition) |
|---|
| 1097 | { |
|---|
| 1098 | float t = viewer.getFrameStamp()->getSimulationTime(); |
|---|
| 1099 | |
|---|
| 1100 | if (lightpos.w()==1.0) |
|---|
| 1101 | { |
|---|
| 1102 | lightpos.set(bb.center().x()+sinf(t)*bb.radius(), bb.center().y() + cosf(t)*bb.radius(), bb.zMax() + bb.radius()*3.0f ,1.0f); |
|---|
| 1103 | } |
|---|
| 1104 | else |
|---|
| 1105 | { |
|---|
| 1106 | lightpos.set(sinf(t),cosf(t),1.0f,0.0f); |
|---|
| 1107 | } |
|---|
| 1108 | ls->getLight()->setPosition(lightpos); |
|---|
| 1109 | |
|---|
| 1110 | osg::Vec3f lightDir(-lightpos.x(),-lightpos.y(),-lightpos.z()); |
|---|
| 1111 | if(spotlight) |
|---|
| 1112 | lightDir = osg::Vec3(bb.center().x()+sinf(t)*bb.radius()/2.0, bb.center().y() + cosf(t)*bb.radius()/2.0, bb.center().z()) |
|---|
| 1113 | - osg::Vec3(lightpos.x(), lightpos.y(), lightpos.z()) ; |
|---|
| 1114 | lightDir.normalize(); |
|---|
| 1115 | ls->getLight()->setDirection(lightDir); |
|---|
| 1116 | } |
|---|
| 1117 | |
|---|
| 1118 | if( dumpShadowVolumes->get() ) |
|---|
| 1119 | { |
|---|
| 1120 | dumpShadowVolumes->set( false ); |
|---|
| 1121 | |
|---|
| 1122 | static int dumpFileNo = 0; |
|---|
| 1123 | dumpFileNo ++; |
|---|
| 1124 | char filename[256]; |
|---|
| 1125 | std::sprintf( filename, "shadowDump%d.osg", dumpFileNo ); |
|---|
| 1126 | |
|---|
| 1127 | osgShadow::MinimalShadowMap * msm = dynamic_cast<osgShadow::MinimalShadowMap*>( shadowedScene->getShadowTechnique() ); |
|---|
| 1128 | |
|---|
| 1129 | if( msm ) msm->setDebugDump( filename ); |
|---|
| 1130 | } |
|---|
| 1131 | |
|---|
| 1132 | viewer.frame(); |
|---|
| 1133 | } |
|---|
| 1134 | |
|---|
| 1135 | return 0; |
|---|
| 1136 | } |
|---|