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

Revision 12503, 6.4 kB (checked in by robert, 3 years ago)

Simplified the osgdepthpartion example to use the osgView::View::setUpDepthPartition(..) feature

  • 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#include <osgGA/StateSetManipulator>
28
29#include <osgViewer/Viewer>
30#include <osgViewer/ViewerEventHandlers>
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->setName("EarthSphere");
42    earth_sphere->setRadius(r_earth);
43    earth_sd->setShape(earth_sphere);
44    earth_sd->setColor(osg::Vec4(0, 0, 1.0, 1.0));
45
46    osg::Geode* earth_geode = new osg::Geode;
47    earth_geode->setName("EarthGeode");
48    earth_geode->addDrawable(earth_sd);
49   
50    // Create the Sun, in yellow
51    osg::ShapeDrawable *sun_sd = new osg::ShapeDrawable;
52    osg::Sphere* sun_sphere = new osg::Sphere;
53    sun_sphere->setName("SunSphere");
54    sun_sphere->setRadius(r_sun);
55    sun_sd->setShape(sun_sphere);
56    sun_sd->setColor(osg::Vec4(1.0, 0.0, 0.0, 1.0));
57
58    osg::Geode* sun_geode = new osg::Geode;
59    sun_geode->setName("SunGeode");
60    sun_geode->addDrawable(sun_sd);
61
62    // Move the sun behind the earth
63    osg::PositionAttitudeTransform *pat = new osg::PositionAttitudeTransform;
64    pat->setPosition(osg::Vec3d(0.0, AU, 0.0));
65    pat->addChild(sun_geode);
66   
67    osg::Geometry * unitCircle = new osg::Geometry();
68    {
69      osg::Vec4Array * colours = new osg::Vec4Array(1);
70      (*colours)[0] = osg::Vec4d(1.0,1.0,1.0,1.0);
71      unitCircle->setColorArray(colours);
72      unitCircle->setColorBinding(osg::Geometry::BIND_OVERALL);
73      const unsigned int n_points = 1024;
74      osg::Vec3Array * coords = new osg::Vec3Array(n_points);
75      const double dx = 2.0*osg::PI/n_points;
76      double s,c;
77      for (unsigned int j=0; j<n_points; ++j) {
78    s = sin(dx*j);
79    c = cos(dx*j);
80    (*coords)[j].set(osg::Vec3d(c,s,0.0));
81      }
82      unitCircle->setVertexArray(coords);
83      unitCircle->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
84      unitCircle->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,0,n_points));
85    }
86
87    osg::Geometry *axes = new osg::Geometry;
88    {
89      osg::Vec4Array *colours = new osg::Vec4Array(1);
90      (*colours)[0] = osg::Vec4d(1.0,0.0,0.0,1.0);
91      axes->setColorArray(colours);
92      axes->setColorBinding(osg::Geometry::BIND_OVERALL);
93      osg::Vec3Array *coords = new osg::Vec3Array(6);
94      (*coords)[0].set(osg::Vec3d(0.0, 0.0, 0.0));
95      (*coords)[1].set(osg::Vec3d(0.5, 0.0, 0.0));
96      (*coords)[2].set(osg::Vec3d(0.0, 0.0, 0.0));
97      (*coords)[3].set(osg::Vec3d(0.0, 0.5, 0.0));
98      (*coords)[4].set(osg::Vec3d(0.0, 0.0, 0.0));
99      (*coords)[5].set(osg::Vec3d(0.0, 0.0, 0.5));
100      axes->setVertexArray(coords);
101      axes->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
102      axes->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,6));
103    }
104   
105    // Earth orbit
106    osg::Geode * earthOrbitGeode = new osg::Geode;
107    earthOrbitGeode->addDrawable(unitCircle);
108    earthOrbitGeode->addDrawable(axes);
109    earthOrbitGeode->setName("EarthOrbitGeode");
110   
111    osg::PositionAttitudeTransform * earthOrbitPAT = new osg::PositionAttitudeTransform;
112    earthOrbitPAT->setScale(osg::Vec3d(AU,AU,AU));
113    earthOrbitPAT->setPosition(osg::Vec3d(0.0, AU, 0.0));
114    earthOrbitPAT->addChild(earthOrbitGeode);
115    earthOrbitPAT->setName("EarthOrbitPAT");
116   
117    osg::Group* scene = new osg::Group;
118    scene->setName("SceneGroup");
119    scene->addChild(earth_geode);
120    scene->addChild(pat);
121    scene->addChild(earthOrbitPAT);
122   
123    return scene;
124}
125
126int main( int argc, char **argv )
127{
128
129    // use an ArgumentParser object to manage the program arguments.
130    osg::ArgumentParser arguments(&argc,argv);
131
132    // construct the viewer.
133    osgViewer::Viewer viewer;
134
135    // add the state manipulator
136    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
137
138    // add stats
139    viewer.addEventHandler( new osgViewer::StatsHandler() );
140
141    bool needToSetHomePosition = false;
142
143    // read the scene from the list of file specified commandline args.
144    osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
145
146    // if one hasn't been loaded create an earth and sun test model.
147    if (!scene)
148    {
149        scene = createScene();
150        needToSetHomePosition = true;
151    }
152    // pass the loaded scene graph to the viewer.
153    viewer.setSceneData(scene.get());
154
155    viewer.setCameraManipulator(new osgGA::TrackballManipulator);
156
157    if (needToSetHomePosition)
158    {
159        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));
160    }
161   
162    double zNear=1.0, zMid=10.0, zFar=1000.0;
163    if (arguments.read("--depth-partition",zNear, zMid, zFar))
164    {
165        // set up depth partitioning
166        osg::ref_ptr<osgViewer::DepthPartitionSettings> dps = new osgViewer::DepthPartitionSettings;
167        dps->_mode = osgViewer::DepthPartitionSettings::FIXED_RANGE;
168        dps->_zNear = zNear;
169        dps->_zMid = zMid;
170        dps->_zFar = zFar;
171        viewer.setUpDepthPartition(dps.get());
172    }
173    else
174    {
175        // set up depth partitioning with default settings
176        viewer.setUpDepthPartition();
177    }
178   
179
180    return viewer.run();
181}
Note: See TracBrowser for help on using the browser.