root/OpenSceneGraph/trunk/examples/osgclip/osgclip.cpp @ 1954

Revision 1954, 5.2 kB (checked in by robert, 12 years ago)

Removed redundent write_usage function.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/MatrixTransform>
2#include <osg/ClipNode>
3#include <osg/Billboard>
4#include <osg/Geode>
5#include <osg/Group>
6#include <osg/Notify>
7#include <osg/Material>
8#include <osg/PolygonOffset>
9#include <osg/PolygonMode>
10#include <osg/LineStipple>
11
12#include <osgDB/Registry>
13#include <osgDB/ReadFile>
14
15#include <osgGA/TrackballManipulator>
16#include <osgGA/FlightManipulator>
17#include <osgGA/DriveManipulator>
18#include <osgUtil/TransformCallback>
19
20#include <osgProducer/Viewer>
21
22#include <osgUtil/Optimizer>
23
24
25osg::Node* decorate_with_clip_node(osg::Node* subgraph)
26{
27    osg::Group* rootnode = new osg::Group;
28   
29
30    // create wireframe view of the model so the user can see
31    // what parts are being culled away.
32    osg::StateSet* stateset = new osg::StateSet;
33    //osg::Material* material = new osg::Material;
34    osg::PolygonMode* polymode = new osg::PolygonMode;
35    polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
36    stateset->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
37   
38    osg::Group* wireframe_subgraph = new osg::Group;
39    wireframe_subgraph->setStateSet(stateset);
40    wireframe_subgraph->addChild(subgraph);
41    rootnode->addChild(wireframe_subgraph);
42
43/*
44    // simple approach to adding a clipnode above a subrgaph.
45
46    // create clipped part.
47    osg::ClipNode* clipped_subgraph = new osg::ClipNode;
48
49    osg::BoundingSphere bs = subgraph->getBound();
50    bs.radius()*= 0.4f;
51
52    osg::BoundingBox bb;
53    bb.expandBy(bs);
54
55
56    clipped_subgraph->createClipBox(bb);
57    clipped_subgraph->addChild(subgraph);
58    rootnode->addChild(clipped_subgraph);
59*/
60
61
62    // more complex approach to managing ClipNode, allowing
63    // ClipNode node to be transformed independantly from the subgraph
64    // that it is clipping.
65   
66    osg::MatrixTransform* transform= new osg::MatrixTransform;
67
68    osg::NodeCallback* nc = new osgUtil::TransformCallback(subgraph->getBound().center(),osg::Vec3(0.0f,0.0f,1.0f),osg::inDegrees(45.0f));
69    transform->setUpdateCallback(nc);
70
71    osg::ClipNode* clipnode = new osg::ClipNode;
72    osg::BoundingSphere bs = subgraph->getBound();
73    bs.radius()*= 0.4f;
74
75    osg::BoundingBox bb;
76    bb.expandBy(bs);
77
78    clipnode->createClipBox(bb);
79    clipnode->setCullingActive(false);
80
81    transform->addChild(clipnode);
82    rootnode->addChild(transform);
83
84
85    // create clipped part.
86    osg::Group* clipped_subgraph = new osg::Group;
87
88    clipped_subgraph->setStateSet(clipnode->getStateSet());
89    clipped_subgraph->addChild(subgraph);
90    rootnode->addChild(clipped_subgraph);
91
92    return rootnode;
93}
94
95
96int main( int argc, char **argv )
97{
98
99    // use an ArgumentParser object to manage the program arguments.
100    osg::ArgumentParser arguments(&argc,argv);
101
102    // set up the usage document, in case we need to print out how to use this program.
103    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use multi-pass and osg::ClipNode to clip parts of the scene away..");
104    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
105    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
106   
107    // initialize the viewer.
108    osgProducer::Viewer viewer(arguments);
109
110    // set up the value with sensible default event handlers.
111    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
112
113    // get details on keyboard and mouse bindings used by the viewer.
114    viewer.getUsage(*arguments.getApplicationUsage());
115
116    // if user request help write it out to cout.
117    if (arguments.read("-h") || arguments.read("--help"))
118    {
119        arguments.getApplicationUsage()->write(std::cout);
120        return 1;
121    }
122
123    // any option left unread are converted into errors to write out later.
124    arguments.reportRemainingOptionsAsUnrecognized();
125
126    // report any errors if they have occured when parsing the program aguments.
127    if (arguments.errors())
128    {
129        arguments.writeErrorMessages(std::cout);
130        return 1;
131    }
132   
133    if (arguments.argc()<=1)
134    {
135        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
136        return 1;
137    }
138
139    // load the nodes from the commandline arguments.
140    osg::Node* loadedModel = osgDB::readNodeFiles(arguments);
141    if (!loadedModel)
142    {
143        return 1;
144    }
145 
146    // decorate the scenegraph with a clip node.
147    osg::Node* rootnode = decorate_with_clip_node(loadedModel);
148
149 
150   
151    // run optimization over the scene graph
152    osgUtil::Optimizer optimzer;
153    optimzer.optimize(rootnode);
154     
155    // set the scene to render
156    viewer.setSceneData(rootnode);
157
158    // create the windows and run the threads.
159    viewer.realize();
160
161    while( !viewer.done() )
162    {
163        // wait for all cull and draw threads to complete.
164        viewer.sync();
165
166        // update the scene by traversing it with the the update visitor which will
167        // call all node update callbacks and animations.
168        viewer.update();
169         
170        // fire off the cull and draw traversals of the scene.
171        viewer.frame();
172       
173    }
174   
175    // wait for all cull and draw threads to complete before exit.
176    viewer.sync();
177
178    return 0;
179}
Note: See TracBrowser for help on using the browser.