| 1 | #include <osgUtil/UpdateVisitor> |
|---|
| 2 | |
|---|
| 3 | #include <osgDB/ReadFile> |
|---|
| 4 | |
|---|
| 5 | #include <osg/ShapeDrawable> |
|---|
| 6 | #include <osg/PositionAttitudeTransform> |
|---|
| 7 | |
|---|
| 8 | #include <osgGA/TrackballManipulator> |
|---|
| 9 | |
|---|
| 10 | #include <osgViewer/Viewer> |
|---|
| 11 | |
|---|
| 12 | #include "DepthPartitionNode.h" |
|---|
| 13 | |
|---|
| 14 | const double r_earth = 6378.137; |
|---|
| 15 | const double r_sun = 695990.0; |
|---|
| 16 | const double AU = 149697900.0; |
|---|
| 17 | |
|---|
| 18 | osg::Node* createScene() |
|---|
| 19 | { |
|---|
| 20 | |
|---|
| 21 | osg::ShapeDrawable *earth_sd = new osg::ShapeDrawable; |
|---|
| 22 | osg::Sphere* earth_sphere = new osg::Sphere; |
|---|
| 23 | earth_sphere->setRadius(r_earth); |
|---|
| 24 | earth_sd->setShape(earth_sphere); |
|---|
| 25 | earth_sd->setColor(osg::Vec4(0, 0, 1.0, 1.0)); |
|---|
| 26 | |
|---|
| 27 | osg::Geode* earth = new osg::Geode; |
|---|
| 28 | earth->setName("earth"); |
|---|
| 29 | earth->addDrawable(earth_sd); |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | osg::ShapeDrawable *sun_sd = new osg::ShapeDrawable; |
|---|
| 33 | osg::Sphere* sun_sphere = new osg::Sphere; |
|---|
| 34 | sun_sphere->setRadius(r_sun); |
|---|
| 35 | sun_sd->setShape(sun_sphere); |
|---|
| 36 | sun_sd->setColor(osg::Vec4(1.0, 0.0, 0.0, 1.0)); |
|---|
| 37 | |
|---|
| 38 | osg::Geode* sun_geode = new osg::Geode; |
|---|
| 39 | sun_geode->setName("sun"); |
|---|
| 40 | sun_geode->addDrawable(sun_sd); |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | osg::PositionAttitudeTransform *pat = new osg::PositionAttitudeTransform; |
|---|
| 44 | pat->setPosition(osg::Vec3d(0.0, AU, 0.0)); |
|---|
| 45 | |
|---|
| 46 | osg::Group* scene = new osg::Group; |
|---|
| 47 | scene->addChild(earth); |
|---|
| 48 | scene->addChild(pat); |
|---|
| 49 | pat->addChild(sun_geode); |
|---|
| 50 | |
|---|
| 51 | return scene; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | int main( int argc, char **argv ) |
|---|
| 55 | { |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | osgViewer::Viewer viewer; |
|---|
| 62 | |
|---|
| 63 | bool needToSetHomePosition = false; |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments); |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | if (!scene) |
|---|
| 70 | { |
|---|
| 71 | scene = createScene(); |
|---|
| 72 | needToSetHomePosition = true; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | osg::ref_ptr<DepthPartitionNode> dpn = new DepthPartitionNode; |
|---|
| 77 | dpn->addChild(scene.get()); |
|---|
| 78 | dpn->setActive(true); |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | viewer.setSceneData(dpn.get()); |
|---|
| 82 | |
|---|
| 83 | viewer.setCameraManipulator(new osgGA::TrackballManipulator); |
|---|
| 84 | |
|---|
| 85 | if (needToSetHomePosition) |
|---|
| 86 | { |
|---|
| 87 | 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)); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); |
|---|
| 92 | |
|---|
| 93 | return viewer.run(); |
|---|
| 94 | } |
|---|