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

Revision 12529, 4.4 kB (checked in by robert, 3 years ago)

Replaced .osg with .osgt file usage

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgclip.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osg/MatrixTransform>
20#include <osg/ClipNode>
21#include <osg/Billboard>
22#include <osg/Geode>
23#include <osg/Group>
24#include <osg/Notify>
25#include <osg/Material>
26#include <osg/PolygonOffset>
27#include <osg/PolygonMode>
28#include <osg/LineStipple>
29#include <osg/AnimationPath>
30
31#include <osgDB/Registry>
32#include <osgDB/ReadFile>
33
34#include <osgGA/TrackballManipulator>
35#include <osgGA/FlightManipulator>
36#include <osgGA/DriveManipulator>
37
38#include <osgViewer/Viewer>
39
40#include <osgUtil/Optimizer>
41
42
43osg::Node* decorate_with_clip_node(osg::Node* subgraph)
44{
45    osg::Group* rootnode = new osg::Group;
46   
47
48    // create wireframe view of the model so the user can see
49    // what parts are being culled away.
50    osg::StateSet* stateset = new osg::StateSet;
51    //osg::Material* material = new osg::Material;
52    osg::PolygonMode* polymode = new osg::PolygonMode;
53    polymode->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE);
54    stateset->setAttributeAndModes(polymode,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
55   
56    osg::Group* wireframe_subgraph = new osg::Group;
57    wireframe_subgraph->setStateSet(stateset);
58    wireframe_subgraph->addChild(subgraph);
59    rootnode->addChild(wireframe_subgraph);
60
61/*
62    // simple approach to adding a clipnode above a subrgaph.
63
64    // create clipped part.
65    osg::ClipNode* clipped_subgraph = new osg::ClipNode;
66
67    osg::BoundingSphere bs = subgraph->getBound();
68    bs.radius()*= 0.4f;
69
70    osg::BoundingBox bb;
71    bb.expandBy(bs);
72
73
74    clipped_subgraph->createClipBox(bb);
75    clipped_subgraph->addChild(subgraph);
76    rootnode->addChild(clipped_subgraph);
77*/
78
79
80    // more complex approach to managing ClipNode, allowing
81    // ClipNode node to be transformed independantly from the subgraph
82    // that it is clipping.
83   
84    osg::MatrixTransform* transform= new osg::MatrixTransform;
85
86    osg::NodeCallback* nc = new osg::AnimationPathCallback(subgraph->getBound().center(),osg::Vec3(0.0f,0.0f,1.0f),osg::inDegrees(45.0f));
87    transform->setUpdateCallback(nc);
88
89    osg::ClipNode* clipnode = new osg::ClipNode;
90    osg::BoundingSphere bs = subgraph->getBound();
91    bs.radius()*= 0.4f;
92
93    osg::BoundingBox bb;
94    bb.expandBy(bs);
95
96    clipnode->createClipBox(bb);
97    clipnode->setCullingActive(false);
98
99    transform->addChild(clipnode);
100    rootnode->addChild(transform);
101
102
103    // create clipped part.
104    osg::Group* clipped_subgraph = new osg::Group;
105
106    clipped_subgraph->setStateSet(clipnode->getStateSet());
107    clipped_subgraph->addChild(subgraph);
108    rootnode->addChild(clipped_subgraph);
109
110    return rootnode;
111}
112
113
114int main( int argc, char **argv )
115{
116    // use an ArgumentParser object to manage the program arguments.
117    osg::ArgumentParser arguments(&argc,argv);
118
119    // load the nodes from the commandline arguments.
120    osg::Node* loadedModel = osgDB::readNodeFiles(arguments);
121
122
123    // if not loaded assume no arguments passed in, try use default mode instead.
124    if (!loadedModel) loadedModel = osgDB::readNodeFile("cow.osgt");
125
126
127    if (!loadedModel)
128    {
129        osg::notify(osg::NOTICE)<<"Please specifiy a filename and the command line"<<std::endl;
130        return 1;
131    }
132 
133    // decorate the scenegraph with a clip node.
134    osg::Node* rootnode = decorate_with_clip_node(loadedModel);
135     
136    // run optimization over the scene graph
137    osgUtil::Optimizer optimzer;
138    optimzer.optimize(rootnode);
139   
140    osgViewer::Viewer viewer;
141     
142    // set the scene to render
143    viewer.setSceneData(rootnode);
144
145    return viewer.run();
146}
Note: See TracBrowser for help on using the browser.