root/OpenSceneGraph/trunk/examples/osgdepthpartition/osgdepthpartition.cpp @ 6941

Revision 6941, 3.6 kB (checked in by robert, 8 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgdepthpartion.
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 <osgUtil/UpdateVisitor>
20
21#include <osgDB/ReadFile>
22
23#include <osg/ShapeDrawable>
24#include <osg/PositionAttitudeTransform>
25
26#include <osgGA/TrackballManipulator>
27
28#include <osgViewer/Viewer>
29
30#include "DepthPartitionNode.h"
31
32const double r_earth = 6378.137;
33const double r_sun = 695990.0;
34const double AU = 149697900.0;
35
36osg::Node* createScene()
37{
38    // Create the Earth, in blue
39    osg::ShapeDrawable *earth_sd = new osg::ShapeDrawable;
40    osg::Sphere* earth_sphere = new osg::Sphere;
41    earth_sphere->setRadius(r_earth);
42    earth_sd->setShape(earth_sphere);
43    earth_sd->setColor(osg::Vec4(0, 0, 1.0, 1.0));
44
45    osg::Geode* earth = new osg::Geode;
46    earth->setName("earth");
47    earth->addDrawable(earth_sd);
48
49    // Create the Sun, in yellow
50    osg::ShapeDrawable *sun_sd = new osg::ShapeDrawable;
51    osg::Sphere* sun_sphere = new osg::Sphere;
52    sun_sphere->setRadius(r_sun);
53    sun_sd->setShape(sun_sphere);
54    sun_sd->setColor(osg::Vec4(1.0, 0.0, 0.0, 1.0));
55
56    osg::Geode* sun_geode = new osg::Geode;
57    sun_geode->setName("sun");
58    sun_geode->addDrawable(sun_sd);
59
60    // Move the sun behind the earth
61    osg::PositionAttitudeTransform *pat = new osg::PositionAttitudeTransform;
62    pat->setPosition(osg::Vec3d(0.0, AU, 0.0));
63
64    osg::Group* scene = new osg::Group;
65    scene->addChild(earth);
66    scene->addChild(pat);
67    pat->addChild(sun_geode);
68
69    return scene;
70}
71
72int main( int argc, char **argv )
73{
74
75    // use an ArgumentParser object to manage the program arguments.
76    osg::ArgumentParser arguments(&argc,argv);
77
78    // construct the viewer.
79    osgViewer::Viewer viewer;
80
81    bool needToSetHomePosition = false;
82
83    // read the scene from the list of file specified commandline args.
84    osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
85
86    // if one hasn't been loaded create an earth and sun test model.
87    if (!scene)
88    {
89        scene = createScene();
90        needToSetHomePosition = true;
91    }
92   
93    // Create a DepthPartitionNode to manage partitioning of the scene
94    osg::ref_ptr<DepthPartitionNode> dpn = new DepthPartitionNode;
95    dpn->addChild(scene.get());
96    dpn->setActive(true); // Control whether the node analyzes the scene
97       
98    // pass the loaded scene graph to the viewer.
99    viewer.setSceneData(dpn.get());
100
101    viewer.setCameraManipulator(new osgGA::TrackballManipulator);
102
103    if (needToSetHomePosition)
104    {
105        viewer.getCameraManipulator()->setHomePosition(osg::Vec3d(0.0,-5.0*r_earth,0.0),osg::Vec3d(0.0,0.0,0.0),osg::Vec3d(0.0,0.0,1.0));
106    }
107   
108    // depth partion node is only supports single window/single threaded at present.
109    viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
110
111    return viewer.run();
112}
Note: See TracBrowser for help on using the browser.