OpenSceneGraph/examples/osgterrain/osgterrain.cpp
Robert Osfield 43c24da12d Added setValue method and osg::Image setup to osg::TransferFunction1D, and setup
of TransferFunction1D into osgterrain example.
2007-04-02 13:06:59 +00:00

205 lines
6.8 KiB
C++

#include <osg/Group>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/Texture2D>
#include <osg/PositionAttitudeTransform>
#include <osg/MatrixTransform>
#include <osg/CoordinateSystemNode>
#include <osg/ClusterCullingCallback>
#include <osg/ArgumentParser>
#include <osgDB/FileUtils>
#include <osgDB/ReadFile>
#include <osgText/FadeText>
#include <osgViewer/Viewer>
#include <osgViewer/StatsHandler>
#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
#include <osgTerrain/TerrainNode>
#include <osgTerrain/GeometryTechnique>
#include <osgTerrain/Layer>
#include <iostream>
int main(int argc, char** argv)
{
osg::ArgumentParser arguments(&argc, argv);
// construct the viewer.
osgViewer::Viewer viewer;
// set up the camera manipulators.
{
osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
std::string pathfile;
char keyForAnimationPath = '5';
while (arguments.read("-p",pathfile))
{
osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
if (apm || !apm->valid())
{
unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
keyswitchManipulator->selectMatrixManipulator(num);
++keyForAnimationPath;
}
}
viewer.setCameraManipulator( keyswitchManipulator.get() );
}
// add the state manipulator
viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
// add the stats handler
viewer.addEventHandler(new osgViewer::StatsHandler);
double x = 0.0;
double y = 0.0;
double w = 1.0;
double h = 1.0;
osg::ref_ptr<osgTerrain::TerrainNode> terrain = new osgTerrain::TerrainNode;
osg::ref_ptr<osgTerrain::Locator> locator = new osgTerrain::EllipsoidLocator(-osg::PI, -osg::PI*0.5, 2.0*osg::PI, osg::PI, 0.0);
bool readParameter = false;
do
{
readParameter = false;
std::string filename;
if (arguments.read("-e",x,y,w,h))
{
// define the extents.
locator = new osgTerrain::EllipsoidLocator(x,y,w,h,0);
readParameter = true;
}
if (arguments.read("--hf",filename))
{
readParameter = true;
osg::notify(osg::NOTICE)<<"--hf "<<filename<<std::endl;
osg::ref_ptr<osg::HeightField> hf = osgDB::readHeightFieldFile(filename);
if (hf.valid())
{
osg::ref_ptr<osgTerrain::HeightFieldLayer> hfl = new osgTerrain::HeightFieldLayer;
hfl->setHeightField(hf.get());
hfl->setLocator(locator.get());
terrain->setElevationLayer(hfl.get());
osg::notify(osg::NOTICE)<<"created osgTerrain::HeightFieldLayer"<<std::endl;
}
else
{
osg::notify(osg::NOTICE)<<"failed to create osgTerrain::HeightFieldLayer"<<std::endl;
}
}
if (arguments.read("-d",filename) || arguments.read("--elevation-image",filename))
{
readParameter = true;
osg::notify(osg::NOTICE)<<"--elevation-image "<<filename<<std::endl;
osg::ref_ptr<osg::Image> image = osgDB::readImageFile(filename);
if (image.valid())
{
osg::ref_ptr<osgTerrain::ImageLayer> imageLayer = new osgTerrain::ImageLayer;
imageLayer->setImage(image.get());
imageLayer->setLocator(locator.get());
terrain->setElevationLayer(imageLayer.get());
osg::notify(osg::NOTICE)<<"created Elevation osgTerrain::ImageLayer"<<std::endl;
}
else
{
osg::notify(osg::NOTICE)<<"failed to create osgTerrain::ImageLayer"<<std::endl;
}
}
if (arguments.read("-c",filename) || arguments.read("--image",filename))
{
readParameter = true;
osg::notify(osg::NOTICE)<<"--image "<<filename<<" x="<<x<<" y="<<y<<" w="<<w<<" h="<<h<<std::endl;
osg::ref_ptr<osg::Image> image = osgDB::readImageFile(filename);
if (image.valid())
{
osg::ref_ptr<osgTerrain::ImageLayer> imageLayer = new osgTerrain::ImageLayer;
imageLayer->setImage(image.get());
imageLayer->setLocator(locator.get());
terrain->setColorLayer(imageLayer.get());
osg::notify(osg::NOTICE)<<"created Color osgTerrain::ImageLayer"<<std::endl;
}
else
{
osg::notify(osg::NOTICE)<<"failed to create osgTerrain::ImageLayer"<<std::endl;
}
}
float minValue, maxValue;
if (arguments.read("--tf",minValue, maxValue))
{
readParameter = true;
osg::ref_ptr<osg::TransferFunction1D> tf = new osg::TransferFunction1D;
tf->setInputRange(minValue, maxValue);
tf->allocate(6);
tf->setValue(0, osg::Vec4(1.0,1.0,1.0,1.0));
tf->setValue(1, osg::Vec4(1.0,0.0,1.0,1.0));
tf->setValue(2, osg::Vec4(1.0,0.0,0.0,1.0));
tf->setValue(3, osg::Vec4(1.0,1.0,0.0,1.0));
tf->setValue(4, osg::Vec4(0.0,1.0,1.0,1.0));
tf->setValue(5, osg::Vec4(0.0,1.0,0.0,1.0));
osg::notify(osg::NOTICE)<<"--tf "<<minValue<<" "<<maxValue<<std::endl;
terrain->setColorTransferFunction(tf.get());
}
} while (readParameter);
osg::ref_ptr<osgTerrain::GeometryTechnique> geometryTechnique = new osgTerrain::GeometryTechnique;
terrain->setTerrainTechnique(geometryTechnique.get());
if (!terrain) return 0;
// return 0;
// add a viewport to the viewer and attach the scene graph.
viewer.setSceneData(terrain.get());
return viewer.run();
}