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

Revision 6908, 3.5 kB (checked in by robert, 8 years ago)

Added default file load to avoid the need for command line option.

  • 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#include <osg/AnimationPath>
12
13#include <osgDB/Registry>
14#include <osgDB/ReadFile>
15
16#include <osgGA/TrackballManipulator>
17#include <osgGA/FlightManipulator>
18#include <osgGA/DriveManipulator>
19
20#include <osgViewer/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 osg::AnimationPathCallback(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    // use an ArgumentParser object to manage the program arguments.
99    osg::ArgumentParser arguments(&argc,argv);
100
101    // load the nodes from the commandline arguments.
102    osg::Node* loadedModel = osgDB::readNodeFiles(arguments);
103
104
105    // if not loaded assume no arguments passed in, try use default mode instead.
106    if (!loadedModel) loadedModel = osgDB::readNodeFile("cow.osg");
107
108
109    if (!loadedModel)
110    {
111        osg::notify(osg::NOTICE)<<"Please specifiy a filename and the command line"<<std::endl;
112        return 1;
113    }
114 
115    // decorate the scenegraph with a clip node.
116    osg::Node* rootnode = decorate_with_clip_node(loadedModel);
117     
118    // run optimization over the scene graph
119    osgUtil::Optimizer optimzer;
120    optimzer.optimize(rootnode);
121   
122    osgViewer::Viewer viewer;
123     
124    // set the scene to render
125    viewer.setSceneData(rootnode);
126
127    return viewer.run();
128}
Note: See TracBrowser for help on using the browser.