| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | #include <osg/Group> |
|---|
| 8 | #include <osg/PositionAttitudeTransform> |
|---|
| 9 | |
|---|
| 10 | #include <osgDB/ReadFile> |
|---|
| 11 | #include <osgViewer/Viewer> |
|---|
| 12 | |
|---|
| 13 | #include <osgFX/Outline> |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | int main(int argc, char** argv) |
|---|
| 17 | { |
|---|
| 18 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 19 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] <file>"); |
|---|
| 20 | arguments.getApplicationUsage()->addCommandLineOption("--testOcclusion","Test occlusion by other objects"); |
|---|
| 21 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 22 | |
|---|
| 23 | bool testOcclusion = false; |
|---|
| 24 | while (arguments.read("--testOcclusion")) { testOcclusion = true; } |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | std::string modelFilename = arguments.argc() > 1 ? arguments[1] : "dumptruck.osgt"; |
|---|
| 28 | osg::ref_ptr<osg::Node> outlineModel = osgDB::readNodeFile(modelFilename); |
|---|
| 29 | if (!outlineModel) |
|---|
| 30 | { |
|---|
| 31 | osg::notify(osg::FATAL) << "Unable to load model '" << modelFilename << "'\n"; |
|---|
| 32 | return -1; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | osg::ref_ptr<osg::Group> root = new osg::Group; |
|---|
| 37 | |
|---|
| 38 | { |
|---|
| 39 | |
|---|
| 40 | osg::ref_ptr<osgFX::Outline> outline = new osgFX::Outline; |
|---|
| 41 | root->addChild(outline.get()); |
|---|
| 42 | |
|---|
| 43 | outline->setWidth(8); |
|---|
| 44 | outline->setColor(osg::Vec4(1,1,0,1)); |
|---|
| 45 | outline->addChild(outlineModel.get()); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | if (testOcclusion) |
|---|
| 49 | { |
|---|
| 50 | |
|---|
| 51 | std::string occludedModelFilename = "cow.osgt"; |
|---|
| 52 | osg::ref_ptr<osg::Node> occludedModel = osgDB::readNodeFile(occludedModelFilename); |
|---|
| 53 | if (!occludedModel) |
|---|
| 54 | { |
|---|
| 55 | osg::notify(osg::FATAL) << "Unable to load model '" << occludedModelFilename << "'\n"; |
|---|
| 56 | return -1; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | const osg::BoundingSphere& bsphere = outlineModel->getBound(); |
|---|
| 61 | const osg::Vec3 occluderOffset = osg::Vec3(0,1,0) * bsphere.radius() * 1.2f; |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | osg::ref_ptr<osg::PositionAttitudeTransform> modelTransform0 = new osg::PositionAttitudeTransform; |
|---|
| 65 | modelTransform0->setPosition(bsphere.center() + occluderOffset); |
|---|
| 66 | modelTransform0->addChild(occludedModel.get()); |
|---|
| 67 | root->addChild(modelTransform0.get()); |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | osg::ref_ptr<osg::PositionAttitudeTransform> modelTransform1 = new osg::PositionAttitudeTransform; |
|---|
| 71 | modelTransform1->setPosition(bsphere.center() - occluderOffset); |
|---|
| 72 | modelTransform1->addChild(occludedModel.get()); |
|---|
| 73 | root->addChild(modelTransform1.get()); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | osg::DisplaySettings::instance()->setMinimumNumStencilBits(1); |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | osgViewer::Viewer viewer; |
|---|
| 81 | viewer.setSceneData(root.get()); |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | unsigned int clearMask = viewer.getCamera()->getClearMask(); |
|---|
| 85 | viewer.getCamera()->setClearMask(clearMask | GL_STENCIL_BUFFER_BIT); |
|---|
| 86 | viewer.getCamera()->setClearStencil(0); |
|---|
| 87 | |
|---|
| 88 | return viewer.run(); |
|---|
| 89 | } |
|---|