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

Revision 1832, 7.1 kB (checked in by robert, 11 years ago)

Changed the Viewer::realize() calls across to not using the threading paramter
leaving it up to the Viewer to specify the mode (which by default is MultiThreaded?).
Added a check for the presence of osgParticle systems so that threading is
disabled in this case.

  • 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
24void write_usage(std::ostream& out,const std::string& name)
25{
26    out << std::endl;
27    out <<"usage:"<< std::endl;
28    out <<"    "<<name<<" [options] infile1 [infile2 ...]"<< std::endl;
29    out << std::endl;
30    out <<"options:"<< std::endl;
31    out <<"    -l libraryName      - load plugin of name libraryName"<< std::endl;
32    out <<"                          i.e. -l osgdb_pfb"<< std::endl;
33    out <<"                          Useful for loading reader/writers which can load"<< std::endl;
34    out <<"                          other file formats in addition to its extension."<< std::endl;
35    out <<"    -e extensionName    - load reader/wrter plugin for file extension"<< std::endl;
36    out <<"                          i.e. -e pfb"<< std::endl;
37    out <<"                          Useful short hand for specifying full library name as"<< std::endl;
38    out <<"                          done with -l above, as it automatically expands to"<< std::endl;
39    out <<"                          the full library name appropriate for each platform."<< std::endl;
40    out <<std::endl;
41    out <<"    -stereo             - switch on stereo rendering, using the default of,"<< std::endl;
42    out <<"                          ANAGLYPHIC or the value set in the OSG_STEREO_MODE "<< std::endl;
43    out <<"                          environmental variable. See doc/stereo.html for "<< std::endl;
44    out <<"                          further details on setting up accurate stereo "<< std::endl;
45    out <<"                          for your system. "<< std::endl;
46    out <<"    -stereo ANAGLYPHIC  - switch on anaglyphic(red/cyan) stereo rendering."<< std::endl;
47    out <<"    -stereo QUAD_BUFFER - switch on quad buffered stereo rendering."<< std::endl;
48    out <<std::endl;
49    out <<"    -stencil            - use a visual with stencil buffer enabled, this "<< std::endl;
50    out <<"                          also allows the depth complexity statistics mode"<< std::endl;
51    out <<"                          to be used (press 'p' three times to cycle to it)."<< std::endl;
52    out << std::endl;
53}
54
55
56osg::Node* decorate_with_clip_node(osg::Node* subgraph)
57{
58    osg::Group* rootnode = new osg::Group;
59   
60
61    // create wireframe view of the model so the user can see
62    // what parts are being culled away.
63    osg::StateSet* stateset = new osg::StateSet;
64    //osg::Material* material = new osg::Material;
65    osg::PolygonMode* polymode = new osg::PolygonMode;
66    polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
67    stateset->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
68   
69    osg::Group* wireframe_subgraph = new osg::Group;
70    wireframe_subgraph->setStateSet(stateset);
71    wireframe_subgraph->addChild(subgraph);
72    rootnode->addChild(wireframe_subgraph);
73
74/*
75    // simple approach to adding a clipnode above a subrgaph.
76
77    // create clipped part.
78    osg::ClipNode* clipped_subgraph = new osg::ClipNode;
79
80    osg::BoundingSphere bs = subgraph->getBound();
81    bs.radius()*= 0.4f;
82
83    osg::BoundingBox bb;
84    bb.expandBy(bs);
85
86
87    clipped_subgraph->createClipBox(bb);
88    clipped_subgraph->addChild(subgraph);
89    rootnode->addChild(clipped_subgraph);
90*/
91
92
93    // more complex approach to managing ClipNode, allowing
94    // ClipNode node to be transformed independantly from the subgraph
95    // that it is clipping.
96   
97    osg::MatrixTransform* transform= new osg::MatrixTransform;
98
99    osg::NodeCallback* nc = new osgUtil::TransformCallback(subgraph->getBound().center(),osg::Vec3(0.0f,0.0f,1.0f),osg::inDegrees(45.0f));
100    transform->setUpdateCallback(nc);
101
102    osg::ClipNode* clipnode = new osg::ClipNode;
103    osg::BoundingSphere bs = subgraph->getBound();
104    bs.radius()*= 0.4f;
105
106    osg::BoundingBox bb;
107    bb.expandBy(bs);
108
109    clipnode->createClipBox(bb);
110    clipnode->setCullingActive(false);
111
112    transform->addChild(clipnode);
113    rootnode->addChild(transform);
114
115
116    // create clipped part.
117    osg::Group* clipped_subgraph = new osg::Group;
118
119    clipped_subgraph->setStateSet(clipnode->getStateSet());
120    clipped_subgraph->addChild(subgraph);
121    rootnode->addChild(clipped_subgraph);
122
123    return rootnode;
124}
125
126
127int main( int argc, char **argv )
128{
129
130    // use an ArgumentParser object to manage the program arguments.
131    osg::ArgumentParser arguments(&argc,argv);
132
133    // set up the usage document, in case we need to print out how to use this program.
134    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
135    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
136   
137    // initialize the viewer.
138    osgProducer::Viewer viewer(arguments);
139
140    // set up the value with sensible default event handlers.
141    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
142
143    // get details on keyboard and mouse bindings used by the viewer.
144    viewer.getUsage(*arguments.getApplicationUsage());
145
146    // if user request help write it out to cout.
147    if (arguments.read("-h") || arguments.read("--help"))
148    {
149        arguments.getApplicationUsage()->write(std::cout);
150        return 1;
151    }
152
153    // any option left unread are converted into errors to write out later.
154    arguments.reportRemainingOptionsAsUnrecognized();
155
156    // report any errors if they have occured when parsing the program aguments.
157    if (arguments.errors())
158    {
159        arguments.writeErrorMessages(std::cout);
160        return 1;
161    }
162   
163    if (arguments.argc()<=1)
164    {
165        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
166        return 1;
167    }
168
169    // load the nodes from the commandline arguments.
170    osg::Node* loadedModel = osgDB::readNodeFiles(arguments);
171    if (!loadedModel)
172    {
173        write_usage(osg::notify(osg::NOTICE),argv[0]);
174        return 1;
175    }
176 
177    // decorate the scenegraph with a clip node.
178    osg::Node* rootnode = decorate_with_clip_node(loadedModel);
179
180 
181   
182    // run optimization over the scene graph
183    osgUtil::Optimizer optimzer;
184    optimzer.optimize(rootnode);
185     
186    // set the scene to render
187    viewer.setSceneData(rootnode);
188
189    // create the windows and run the threads.
190    viewer.realize();
191
192    while( !viewer.done() )
193    {
194        // wait for all cull and draw threads to complete.
195        viewer.sync();
196
197        // update the scene by traversing it with the the update visitor which will
198        // call all node update callbacks and animations.
199        viewer.update();
200         
201        // fire off the cull and draw traversals of the scene.
202        viewer.frame();
203       
204    }
205   
206    // wait for all cull and draw threads to complete before exit.
207    viewer.sync();
208
209    return 0;
210}
Note: See TracBrowser for help on using the browser.