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

Revision 4713, 5.2 kB (checked in by robert, 9 years ago)

Added the support in AnimationPathCallback? for taking a pivot, axis and rotation rate
paramters in its constructor to allow it to be used to create rotations around a point.
This provides the same interface and functionality as osgUtil::TransformCallback? but
has the advantage and AnimationPathCallback? is fully supported by the .osg and .ive file formats.

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