2005-10-10 18:10:44 +08:00
|
|
|
#include <osgUtil/UpdateVisitor>
|
|
|
|
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
|
|
|
|
#include <osg/ShapeDrawable>
|
|
|
|
#include <osg/PositionAttitudeTransform>
|
|
|
|
|
2007-01-07 05:35:57 +08:00
|
|
|
#include <osgGA/TrackballManipulator>
|
|
|
|
|
|
|
|
#include <osgViewer/Viewer>
|
2005-10-10 18:10:44 +08:00
|
|
|
|
|
|
|
#include "DepthPartitionNode.h"
|
|
|
|
|
|
|
|
const double r_earth = 6378.137;
|
|
|
|
const double r_sun = 695990.0;
|
|
|
|
const double AU = 149697900.0;
|
|
|
|
|
|
|
|
osg::Node* createScene()
|
|
|
|
{
|
2005-11-18 04:22:55 +08:00
|
|
|
// Create the Earth, in blue
|
|
|
|
osg::ShapeDrawable *earth_sd = new osg::ShapeDrawable;
|
|
|
|
osg::Sphere* earth_sphere = new osg::Sphere;
|
|
|
|
earth_sphere->setRadius(r_earth);
|
|
|
|
earth_sd->setShape(earth_sphere);
|
|
|
|
earth_sd->setColor(osg::Vec4(0, 0, 1.0, 1.0));
|
|
|
|
|
|
|
|
osg::Geode* earth = new osg::Geode;
|
|
|
|
earth->setName("earth");
|
|
|
|
earth->addDrawable(earth_sd);
|
|
|
|
|
|
|
|
// Create the Sun, in yellow
|
|
|
|
osg::ShapeDrawable *sun_sd = new osg::ShapeDrawable;
|
|
|
|
osg::Sphere* sun_sphere = new osg::Sphere;
|
|
|
|
sun_sphere->setRadius(r_sun);
|
|
|
|
sun_sd->setShape(sun_sphere);
|
|
|
|
sun_sd->setColor(osg::Vec4(1.0, 0.0, 0.0, 1.0));
|
|
|
|
|
2006-03-18 06:22:57 +08:00
|
|
|
osg::Geode* sun_geode = new osg::Geode;
|
|
|
|
sun_geode->setName("sun");
|
|
|
|
sun_geode->addDrawable(sun_sd);
|
2005-11-18 04:22:55 +08:00
|
|
|
|
|
|
|
// Move the sun behind the earth
|
|
|
|
osg::PositionAttitudeTransform *pat = new osg::PositionAttitudeTransform;
|
|
|
|
pat->setPosition(osg::Vec3d(0.0, AU, 0.0));
|
|
|
|
|
|
|
|
osg::Group* scene = new osg::Group;
|
|
|
|
scene->addChild(earth);
|
|
|
|
scene->addChild(pat);
|
2006-03-18 06:22:57 +08:00
|
|
|
pat->addChild(sun_geode);
|
2005-11-18 04:22:55 +08:00
|
|
|
|
|
|
|
return scene;
|
2005-10-10 18:10:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
|
|
|
|
// use an ArgumentParser object to manage the program arguments.
|
|
|
|
osg::ArgumentParser arguments(&argc,argv);
|
|
|
|
|
2005-10-27 21:53:55 +08:00
|
|
|
// construct the viewer.
|
2007-01-07 05:35:57 +08:00
|
|
|
osgViewer::Viewer viewer;
|
2005-10-10 18:10:44 +08:00
|
|
|
|
2005-10-27 21:53:55 +08:00
|
|
|
bool needToSetHomePosition = false;
|
2005-10-10 18:10:44 +08:00
|
|
|
|
2005-10-27 21:53:55 +08:00
|
|
|
// read the scene from the list of file specified commandline args.
|
|
|
|
osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
|
2005-10-10 18:10:44 +08:00
|
|
|
|
2005-10-27 21:53:55 +08:00
|
|
|
// if one hasn't been loaded create an earth and sun test model.
|
|
|
|
if (!scene)
|
|
|
|
{
|
|
|
|
scene = createScene();
|
|
|
|
needToSetHomePosition = true;
|
|
|
|
}
|
|
|
|
|
2005-10-10 18:10:44 +08:00
|
|
|
// Create a DepthPartitionNode to manage partitioning of the scene
|
2005-10-27 21:53:55 +08:00
|
|
|
osg::ref_ptr<DepthPartitionNode> dpn = new DepthPartitionNode;
|
|
|
|
dpn->addChild(scene.get());
|
2005-10-10 18:10:44 +08:00
|
|
|
dpn->setActive(true); // Control whether the node analyzes the scene
|
|
|
|
|
2005-10-27 21:53:55 +08:00
|
|
|
// pass the loaded scene graph to the viewer.
|
|
|
|
viewer.setSceneData(dpn.get());
|
2005-10-10 18:10:44 +08:00
|
|
|
|
2007-01-07 05:35:57 +08:00
|
|
|
viewer.setCameraManipulator(new osgGA::TrackballManipulator);
|
2005-10-10 18:10:44 +08:00
|
|
|
|
2007-01-07 05:35:57 +08:00
|
|
|
if (needToSetHomePosition)
|
2005-10-10 18:10:44 +08:00
|
|
|
{
|
2007-01-07 05:35:57 +08:00
|
|
|
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));
|
2005-10-10 18:10:44 +08:00
|
|
|
}
|
2005-10-27 21:53:55 +08:00
|
|
|
|
2007-01-07 05:35:57 +08:00
|
|
|
// depth partion node is only supports single window/single threaded at present.
|
|
|
|
viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
|
2005-10-10 18:10:44 +08:00
|
|
|
|
2007-01-07 05:35:57 +08:00
|
|
|
return viewer.run();
|
2005-10-10 18:10:44 +08:00
|
|
|
}
|
|
|
|
|