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

Revision 10001, 5.9 kB (checked in by robert, 5 years ago)

From Ravi Mathur, "OK I have been away for a looong time, but still occasionally watching from a distance, and saw the bug people have reported about the DepthPartitionNode? not handling scaled models properly.

I believe this is now fixed ... I have attached the new DistanceAccumulator?.cpp, along with a modified example file that uses a PositionAttitudeTransform? to draw the Earth's orbit around the Sun."

  • 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->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*M_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    bool needToSetHomePosition = false;
136
137    // read the scene from the list of file specified commandline args.
138    osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
139
140    // if one hasn't been loaded create an earth and sun test model.
141    if (!scene)
142    {
143        scene = createScene();
144        needToSetHomePosition = true;
145    }
146   
147    // Create a DepthPartitionNode to manage partitioning of the scene
148    osg::ref_ptr<DepthPartitionNode> dpn = new DepthPartitionNode;
149    dpn->addChild(scene.get());
150    dpn->setActive(true); // Control whether the node analyzes the scene
151       
152    // pass the loaded scene graph to the viewer.
153    viewer.setSceneData(dpn.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    // depth partion node is only supports single window/single threaded at present.
163    viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
164
165    return viewer.run();
166}
Note: See TracBrowser for help on using the browser.