OpenSceneGraph/examples/osgterrain/osgterrain.cpp

141 lines
4.3 KiB
C++
Raw Normal View History

2007-03-14 20:43:29 +08:00
#include <osgViewer/Viewer>
#include <osg/Group>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/Texture2D>
#include <osg/PositionAttitudeTransform>
#include <osg/MatrixTransform>
#include <osg/CoordinateSystemNode>
#include <osg/ClusterCullingCallback>
2007-03-19 18:38:44 +08:00
#include <osg/ArgumentParser>
2007-03-14 20:43:29 +08:00
#include <osgDB/FileUtils>
#include <osgDB/ReadFile>
#include <osgText/FadeText>
2007-03-19 18:38:44 +08:00
#include <osgTerrain/TerrainNode>
#include <osgTerrain/GeometryTechnique>
2007-03-19 18:38:44 +08:00
#include <osgTerrain/Layer>
2007-03-14 20:43:29 +08:00
#include <iostream>
2007-03-19 18:38:44 +08:00
int main(int argc, char** argv)
2007-03-14 20:43:29 +08:00
{
2007-03-19 18:38:44 +08:00
osg::ArgumentParser arguments(&argc, argv);
2007-03-14 20:43:29 +08:00
2007-03-19 18:38:44 +08:00
// construct the viewer.
osgViewer::Viewer viewer;
2007-03-14 20:43:29 +08:00
2007-03-19 18:38:44 +08:00
double x = 0.0;
double y = 0.0;
double w = 1.0;
double h = 1.0;
2007-03-14 20:43:29 +08:00
osg::ref_ptr<osgTerrain::TerrainNode> terrain = new osgTerrain::TerrainNode;
2007-03-27 19:23:57 +08:00
osg::ref_ptr<osgTerrain::Locator> locator = new osgTerrain::EllipsoidLocator(-osg::PI, -osg::PI*0.5, 2.0*osg::PI, osg::PI, 0.0);
2007-03-14 20:43:29 +08:00
2007-03-19 18:38:44 +08:00
bool readParameter = false;
do
2007-03-14 20:43:29 +08:00
{
2007-03-19 18:38:44 +08:00
readParameter = false;
std::string filename;
2007-03-27 19:23:57 +08:00
if (arguments.read("-e",x,y,w,h))
{
2007-03-27 19:23:57 +08:00
// define the extents.
locator = new osgTerrain::EllipsoidLocator(x,y,w,h,0);
readParameter = true;
}
2007-03-19 18:38:44 +08:00
if (arguments.read("--hf",filename))
2007-03-14 20:43:29 +08:00
{
2007-03-19 18:38:44 +08:00
readParameter = true;
osg::notify(osg::NOTICE)<<"--hf "<<filename<<" x="<<x<<" y="<<y<<" w="<<w<<" h="<<h<<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;
}
2007-03-19 18:38:44 +08:00
}
2007-03-27 19:23:57 +08:00
if (arguments.read("-h",filename) || arguments.read("--elevation-image",filename))
{
readParameter = true;
osg::notify(osg::NOTICE)<<"--elevation-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->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))
2007-03-19 18:38:44 +08:00
{
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;
}
2007-03-14 20:43:29 +08:00
}
2007-03-19 18:38:44 +08:00
} while (readParameter);
2007-03-14 20:43:29 +08:00
osg::ref_ptr<osgTerrain::GeometryTechnique> geometryTechnique = new osgTerrain::GeometryTechnique;
2007-03-14 20:43:29 +08:00
terrain->setTerrainTechnique(geometryTechnique.get());
2007-03-14 20:43:29 +08:00
if (!terrain) return 0;
// return 0;
2007-03-14 20:43:29 +08:00
// add a viewport to the viewer and attach the scene graph.
viewer.setSceneData(terrain.get());
2007-03-14 20:43:29 +08:00
return viewer.run();
}