root/OpenSceneGraph/trunk/examples/osgoutline/osgoutline.cpp @ 12292

Revision 12292, 3.0 kB (checked in by robert, 4 years ago)

Ran svn propset -R svn:eol-style native . on the OpenSceneGraph

  • Property svn:eol-style set to native
Line 
1// -*-c++-*-
2
3/*
4 * Draw an outline around a model.
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
16int 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    // load outlined object
27    std::string modelFilename = arguments.argc() > 1 ? arguments[1] : "dumptruck.osg";
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    // create scene
36    osg::ref_ptr<osg::Group> root = new osg::Group;
37
38    {
39        // create outline effect
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        // load occluder
51        std::string occludedModelFilename = "cow.osg";
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        // occluder offset
60        const osg::BoundingSphere& bsphere = outlineModel->getBound();
61        const osg::Vec3 occluderOffset = osg::Vec3(0,1,0) * bsphere.radius() * 1.2f;
62
63        // occluder behind outlined model
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        // occluder in front of outlined model
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    // must have stencil buffer...
77    osg::DisplaySettings::instance()->setMinimumNumStencilBits(1);
78
79    // construct the viewer
80    osgViewer::Viewer viewer;
81    viewer.setSceneData(root.get());
82
83    // must clear stencil buffer...
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}
Note: See TracBrowser for help on using the browser.