| [6941] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| [4582] | 19 | #include <osgUtil/UpdateVisitor> |
|---|
| 20 | |
|---|
| 21 | #include <osgDB/ReadFile> |
|---|
| 22 | |
|---|
| 23 | #include <osg/ShapeDrawable> |
|---|
| 24 | #include <osg/PositionAttitudeTransform> |
|---|
| 25 | |
|---|
| [5923] | 26 | #include <osgGA/TrackballManipulator> |
|---|
| [12503] | 27 | #include <osgGA/StateSetManipulator> |
|---|
| [4582] | 28 | |
|---|
| [5923] | 29 | #include <osgViewer/Viewer> |
|---|
| [12503] | 30 | #include <osgViewer/ViewerEventHandlers> |
|---|
| [5923] | 31 | |
|---|
| [4582] | 32 | const double r_earth = 6378.137; |
|---|
| 33 | const double r_sun = 695990.0; |
|---|
| 34 | const double AU = 149697900.0; |
|---|
| 35 | |
|---|
| 36 | osg::Node* createScene() |
|---|
| 37 | { |
|---|
| [4805] | 38 | |
|---|
| 39 | osg::ShapeDrawable *earth_sd = new osg::ShapeDrawable; |
|---|
| 40 | osg::Sphere* earth_sphere = new osg::Sphere; |
|---|
| [10001] | 41 | earth_sphere->setName("EarthSphere"); |
|---|
| [4805] | 42 | earth_sphere->setRadius(r_earth); |
|---|
| 43 | earth_sd->setShape(earth_sphere); |
|---|
| 44 | earth_sd->setColor(osg::Vec4(0, 0, 1.0, 1.0)); |
|---|
| [4582] | 45 | |
|---|
| [10001] | 46 | osg::Geode* earth_geode = new osg::Geode; |
|---|
| 47 | earth_geode->setName("EarthGeode"); |
|---|
| 48 | earth_geode->addDrawable(earth_sd); |
|---|
| 49 | |
|---|
| [4805] | 50 | |
|---|
| 51 | osg::ShapeDrawable *sun_sd = new osg::ShapeDrawable; |
|---|
| 52 | osg::Sphere* sun_sphere = new osg::Sphere; |
|---|
| [10001] | 53 | sun_sphere->setName("SunSphere"); |
|---|
| [4805] | 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)); |
|---|
| [4582] | 57 | |
|---|
| [5054] | 58 | osg::Geode* sun_geode = new osg::Geode; |
|---|
| [10001] | 59 | sun_geode->setName("SunGeode"); |
|---|
| [5054] | 60 | sun_geode->addDrawable(sun_sd); |
|---|
| [4582] | 61 | |
|---|
| [4805] | 62 | |
|---|
| 63 | osg::PositionAttitudeTransform *pat = new osg::PositionAttitudeTransform; |
|---|
| 64 | pat->setPosition(osg::Vec3d(0.0, AU, 0.0)); |
|---|
| [10001] | 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); |
|---|
| [10021] | 75 | const double dx = 2.0*osg::PI/n_points; |
|---|
| [10001] | 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 | } |
|---|
| [4582] | 86 | |
|---|
| [10001] | 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 | |
|---|
| 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 | |
|---|
| [4805] | 117 | osg::Group* scene = new osg::Group; |
|---|
| [10001] | 118 | scene->setName("SceneGroup"); |
|---|
| 119 | scene->addChild(earth_geode); |
|---|
| [4805] | 120 | scene->addChild(pat); |
|---|
| [10001] | 121 | scene->addChild(earthOrbitPAT); |
|---|
| 122 | |
|---|
| [4805] | 123 | return scene; |
|---|
| [4582] | 124 | } |
|---|
| 125 | |
|---|
| 126 | int main( int argc, char **argv ) |
|---|
| 127 | { |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 131 | |
|---|
| [4626] | 132 | |
|---|
| [5923] | 133 | osgViewer::Viewer viewer; |
|---|
| [4582] | 134 | |
|---|
| [12503] | 135 | |
|---|
| 136 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | viewer.addEventHandler( new osgViewer::StatsHandler() ); |
|---|
| 140 | |
|---|
| [4626] | 141 | bool needToSetHomePosition = false; |
|---|
| [4582] | 142 | |
|---|
| [4626] | 143 | |
|---|
| 144 | osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments); |
|---|
| [4582] | 145 | |
|---|
| [4626] | 146 | |
|---|
| 147 | if (!scene) |
|---|
| 148 | { |
|---|
| 149 | scene = createScene(); |
|---|
| 150 | needToSetHomePosition = true; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| [12503] | 153 | viewer.setSceneData(scene.get()); |
|---|
| [4582] | 154 | |
|---|
| [5923] | 155 | viewer.setCameraManipulator(new osgGA::TrackballManipulator); |
|---|
| 156 | |
|---|
| [4626] | 157 | if (needToSetHomePosition) |
|---|
| 158 | { |
|---|
| [12503] | 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)); |
|---|
| [4626] | 160 | } |
|---|
| 161 | |
|---|
| [12503] | 162 | double zNear=1.0, zMid=10.0, zFar=1000.0; |
|---|
| 163 | if (arguments.read("--depth-partition",zNear, zMid, zFar)) |
|---|
| 164 | { |
|---|
| 165 | |
|---|
| 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 | |
|---|
| 176 | viewer.setUpDepthPartition(); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| [4582] | 179 | |
|---|
| [5923] | 180 | return viewer.run(); |
|---|
| [4582] | 181 | } |
|---|