2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osgdepthpartion.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
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>
|
2011-06-08 17:24:29 +08:00
|
|
|
#include <osgGA/StateSetManipulator>
|
2007-01-07 05:35:57 +08:00
|
|
|
|
|
|
|
#include <osgViewer/Viewer>
|
2011-06-08 17:24:29 +08:00
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
2005-10-10 18:10:44 +08:00
|
|
|
|
|
|
|
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;
|
2009-04-09 22:25:14 +08:00
|
|
|
earth_sphere->setName("EarthSphere");
|
2005-11-18 04:22:55 +08:00
|
|
|
earth_sphere->setRadius(r_earth);
|
|
|
|
earth_sd->setShape(earth_sphere);
|
|
|
|
earth_sd->setColor(osg::Vec4(0, 0, 1.0, 1.0));
|
|
|
|
|
2009-04-09 22:25:14 +08:00
|
|
|
osg::Geode* earth_geode = new osg::Geode;
|
|
|
|
earth_geode->setName("EarthGeode");
|
|
|
|
earth_geode->addDrawable(earth_sd);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
// Create the Sun, in yellow
|
|
|
|
osg::ShapeDrawable *sun_sd = new osg::ShapeDrawable;
|
|
|
|
osg::Sphere* sun_sphere = new osg::Sphere;
|
2009-04-09 22:25:14 +08:00
|
|
|
sun_sphere->setName("SunSphere");
|
2005-11-18 04:22:55 +08:00
|
|
|
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;
|
2009-04-09 22:25:14 +08:00
|
|
|
sun_geode->setName("SunGeode");
|
2006-03-18 06:22:57 +08:00
|
|
|
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));
|
2009-04-09 22:25:14 +08:00
|
|
|
pat->addChild(sun_geode);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2009-04-09 22:25:14 +08:00
|
|
|
osg::Geometry * unitCircle = new osg::Geometry();
|
|
|
|
{
|
|
|
|
osg::Vec4Array * colours = new osg::Vec4Array(1);
|
|
|
|
(*colours)[0] = osg::Vec4d(1.0,1.0,1.0,1.0);
|
2013-06-27 17:54:12 +08:00
|
|
|
unitCircle->setColorArray(colours, osg::Array::BIND_OVERALL);
|
2009-04-09 22:25:14 +08:00
|
|
|
const unsigned int n_points = 1024;
|
|
|
|
osg::Vec3Array * coords = new osg::Vec3Array(n_points);
|
2009-04-10 19:19:34 +08:00
|
|
|
const double dx = 2.0*osg::PI/n_points;
|
2009-04-09 22:25:14 +08:00
|
|
|
double s,c;
|
|
|
|
for (unsigned int j=0; j<n_points; ++j) {
|
|
|
|
s = sin(dx*j);
|
|
|
|
c = cos(dx*j);
|
|
|
|
(*coords)[j].set(osg::Vec3d(c,s,0.0));
|
|
|
|
}
|
|
|
|
unitCircle->setVertexArray(coords);
|
|
|
|
unitCircle->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
|
|
|
|
unitCircle->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,0,n_points));
|
|
|
|
}
|
2005-11-18 04:22:55 +08:00
|
|
|
|
2009-04-09 22:25:14 +08:00
|
|
|
osg::Geometry *axes = new osg::Geometry;
|
|
|
|
{
|
|
|
|
osg::Vec4Array *colours = new osg::Vec4Array(1);
|
|
|
|
(*colours)[0] = osg::Vec4d(1.0,0.0,0.0,1.0);
|
2013-06-27 17:54:12 +08:00
|
|
|
axes->setColorArray(colours, osg::Array::BIND_OVERALL);
|
2009-04-09 22:25:14 +08:00
|
|
|
osg::Vec3Array *coords = new osg::Vec3Array(6);
|
|
|
|
(*coords)[0].set(osg::Vec3d(0.0, 0.0, 0.0));
|
|
|
|
(*coords)[1].set(osg::Vec3d(0.5, 0.0, 0.0));
|
|
|
|
(*coords)[2].set(osg::Vec3d(0.0, 0.0, 0.0));
|
|
|
|
(*coords)[3].set(osg::Vec3d(0.0, 0.5, 0.0));
|
|
|
|
(*coords)[4].set(osg::Vec3d(0.0, 0.0, 0.0));
|
|
|
|
(*coords)[5].set(osg::Vec3d(0.0, 0.0, 0.5));
|
|
|
|
axes->setVertexArray(coords);
|
|
|
|
axes->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
|
|
|
|
axes->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,6));
|
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2009-04-09 22:25:14 +08:00
|
|
|
// Earth orbit
|
|
|
|
osg::Geode * earthOrbitGeode = new osg::Geode;
|
|
|
|
earthOrbitGeode->addDrawable(unitCircle);
|
|
|
|
earthOrbitGeode->addDrawable(axes);
|
|
|
|
earthOrbitGeode->setName("EarthOrbitGeode");
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2009-04-09 22:25:14 +08:00
|
|
|
osg::PositionAttitudeTransform * earthOrbitPAT = new osg::PositionAttitudeTransform;
|
|
|
|
earthOrbitPAT->setScale(osg::Vec3d(AU,AU,AU));
|
|
|
|
earthOrbitPAT->setPosition(osg::Vec3d(0.0, AU, 0.0));
|
|
|
|
earthOrbitPAT->addChild(earthOrbitGeode);
|
|
|
|
earthOrbitPAT->setName("EarthOrbitPAT");
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
osg::Group* scene = new osg::Group;
|
2009-04-09 22:25:14 +08:00
|
|
|
scene->setName("SceneGroup");
|
|
|
|
scene->addChild(earth_geode);
|
2005-11-18 04:22:55 +08:00
|
|
|
scene->addChild(pat);
|
2009-04-09 22:25:14 +08:00
|
|
|
scene->addChild(earthOrbitPAT);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
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.
|
2020-03-13 16:33:52 +08:00
|
|
|
osgViewer::Viewer viewer(arguments);
|
2005-10-10 18:10:44 +08:00
|
|
|
|
2011-06-08 17:24:29 +08:00
|
|
|
// add the state manipulator
|
|
|
|
viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
|
|
|
|
|
|
|
|
// add stats
|
|
|
|
viewer.addEventHandler( new osgViewer::StatsHandler() );
|
|
|
|
|
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.
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Node> scene = osgDB::readRefNodeFiles(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.
|
2013-06-27 17:54:12 +08:00
|
|
|
if (!scene)
|
2005-10-27 21:53:55 +08:00
|
|
|
{
|
2013-06-27 17:54:12 +08:00
|
|
|
scene = createScene();
|
2005-10-27 21:53:55 +08:00
|
|
|
needToSetHomePosition = true;
|
|
|
|
}
|
|
|
|
// pass the loaded scene graph to the viewer.
|
2011-06-08 17:24:29 +08:00
|
|
|
viewer.setSceneData(scene.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
|
|
|
{
|
2011-06-08 17:24:29 +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));
|
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2011-06-08 17:24:29 +08:00
|
|
|
double zNear=1.0, zMid=10.0, zFar=1000.0;
|
|
|
|
if (arguments.read("--depth-partition",zNear, zMid, zFar))
|
|
|
|
{
|
|
|
|
// set up depth partitioning
|
|
|
|
osg::ref_ptr<osgViewer::DepthPartitionSettings> dps = new osgViewer::DepthPartitionSettings;
|
|
|
|
dps->_mode = osgViewer::DepthPartitionSettings::FIXED_RANGE;
|
|
|
|
dps->_zNear = zNear;
|
|
|
|
dps->_zMid = zMid;
|
|
|
|
dps->_zFar = zFar;
|
|
|
|
viewer.setUpDepthPartition(dps.get());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// set up depth partitioning with default settings
|
|
|
|
viewer.setUpDepthPartition();
|
2005-10-10 18:10:44 +08:00
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|