Added Locator and layer setting to osgterrain example
This commit is contained in:
parent
b419fa93ef
commit
1cdda0654f
@ -22,6 +22,43 @@
|
||||
#include <iostream>
|
||||
|
||||
|
||||
class MyLocator : public osgTerrain::Locator
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
MyLocator(double x, double y, double width, double height):
|
||||
_x(x),
|
||||
_y(y),
|
||||
_width(width),
|
||||
_height(height) {}
|
||||
|
||||
|
||||
bool convertLocalToModel(const osg::Vec3d& local, osg::Vec3d& world)
|
||||
{
|
||||
world.x() = _x + local.x()*_width;
|
||||
world.y() = _y + local.y()*_height;
|
||||
world.z() = _z + local.z();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool convertModelToWorld(const osg::Vec3d& world, osg::Vec3d& local)
|
||||
{
|
||||
local.x() = (world.x()- _x) / _width;
|
||||
local.y() = (world.y() - _y) / _height;
|
||||
local.z() = world.z() - _z;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
double _x;
|
||||
double _y;
|
||||
double _z;
|
||||
double _width;
|
||||
double _height;
|
||||
};
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
osg::ArgumentParser arguments(&argc, argv);
|
||||
@ -35,6 +72,8 @@ int main(int argc, char** argv)
|
||||
double h = 1.0;
|
||||
|
||||
osg::ref_ptr<osgTerrain::TerrainNode> terrain = new osgTerrain::TerrainNode;
|
||||
osg::ref_ptr<osgTerrain::Locator> locator = new MyLocator(0.0, 0.0, 1.0, 1.0);
|
||||
|
||||
|
||||
bool readParameter = false;
|
||||
do
|
||||
@ -42,21 +81,79 @@ int main(int argc, char** argv)
|
||||
readParameter = false;
|
||||
std::string filename;
|
||||
|
||||
if (arguments.read("-x",x)) readParameter = true;
|
||||
if (arguments.read("-y",y)) readParameter = true;
|
||||
if (arguments.read("-w",w)) readParameter = true;
|
||||
if (arguments.read("-h",h)) readParameter = true;
|
||||
if (arguments.read("-l",x,y,w,h))
|
||||
{
|
||||
locator = new MyLocator(x,y,w,h);
|
||||
readParameter = true;
|
||||
}
|
||||
|
||||
if (arguments.read("--dem",filename))
|
||||
if (arguments.read("--hf",filename))
|
||||
{
|
||||
readParameter = true;
|
||||
osg::notify(osg::NOTICE)<<"--dem "<<filename<<" x="<<x<<" y="<<y<<" w="<<w<<" h="<<h<<std::endl;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (arguments.read("--image",filename))
|
||||
if (arguments.read("-e",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))
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
} while (readParameter);
|
||||
@ -67,6 +164,8 @@ int main(int argc, char** argv)
|
||||
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());
|
||||
|
@ -31,6 +31,9 @@ class OSGTERRAIN_EXPORT Layer : public osg::Object
|
||||
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
|
||||
Layer(const Layer&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
|
||||
|
||||
META_Object(osgTerrain, Layer);
|
||||
|
||||
|
||||
void setLocator(Locator* locator) { _locator = locator; }
|
||||
|
||||
Locator* getLocator() { return _locator.get(); }
|
||||
|
@ -30,8 +30,10 @@ class OSGTERRAIN_EXPORT Locator : public osg::Object
|
||||
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
|
||||
Locator(const Locator&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
|
||||
|
||||
virtual bool convertLocalToModel(const osg::Vec3d& local, osg::Vec3d& world) = 0;
|
||||
virtual bool convertModelToWorld(const osg::Vec3d& local, osg::Vec3d& world) = 0;
|
||||
META_Object(osgTerrain, Locator);
|
||||
|
||||
virtual bool convertLocalToModel(const osg::Vec3d& local, osg::Vec3d& world) {};
|
||||
virtual bool convertModelToWorld(const osg::Vec3d& world, osg::Vec3d& local) {};
|
||||
|
||||
protected:
|
||||
|
||||
|
@ -79,12 +79,12 @@ LINK_INTERNAL(${LIB_NAME}
|
||||
osgUtil
|
||||
osg
|
||||
)
|
||||
|
||||
LINK_CORELIB_DEFAULT(${LIB_NAME})
|
||||
|
||||
IF(MINGW)
|
||||
LINK_EXTERNAL(${LIB_NAME}
|
||||
gdi32
|
||||
)
|
||||
ENDIF(MINGW)
|
||||
IF(MINGW OR CYGWIN)
|
||||
LINK_EXTERNAL(${LIB_NAME} gdi32 )
|
||||
ENDIF(MINGW OR CYGWIN)
|
||||
|
||||
|
||||
INCLUDE(ModuleInstall OPTIONAL)
|
||||
|
Loading…
Reference in New Issue
Block a user