Introduced TerrainTile::TileLoadedCallback

This commit is contained in:
Robert Osfield 2008-09-10 18:11:54 +00:00
parent ed4bd41574
commit b4b5b5ea41
8 changed files with 82 additions and 10 deletions

View File

@ -39,6 +39,7 @@
#include <osgGA/TerrainManipulator>
#include <osgTerrain/Terrain>
#include <osgTerrain/TerrainTile>
#include <osgViewer/ViewerEventHandlers>
#include <osgViewer/Viewer>
@ -232,6 +233,24 @@ protected:
osg::ref_ptr<osgTerrain::Terrain> _terrain;
};
struct CustomTileLoadedCallback : public osgTerrain::TerrainTile::TileLoadedCallback
{
CustomTileLoadedCallback()
{
}
virtual bool deferExternalLayerLoading() const
{
return true;
}
virtual void loaded(osgTerrain::TerrainTile* tile, const osgDB::ReaderWriter::Options* options) const
{
osg::notify(osg::NOTICE)<<"Need to decide what to do here guys"<<std::endl;
}
};
int main( int argc, char **argv )
{
// use an ArgumentParser object to manage the program arguments.
@ -240,6 +259,10 @@ int main( int argc, char **argv )
arguments.getApplicationUsage()->addCommandLineOption("-r","Set the terrain sample ratio.");
arguments.getApplicationUsage()->addCommandLineOption("--login <url> <username> <password>","Provide authentication information for http file access.");
// set the tile loaded callback to load the optional imagery
osgTerrain::TerrainTile::setTileLoadedCallback(new CustomTileLoadedCallback());
// construct the viewer.
osgViewer::Viewer viewer(arguments);

View File

@ -17,6 +17,8 @@
#include <osg/Group>
#include <osg/CoordinateSystemNode>
#include <osgDB/ReaderWriter>
#include <osgTerrain/TerrainTechnique>
#include <osgTerrain/Layer>
#include <osgTerrain/Locator>
@ -145,8 +147,8 @@ class OSGTERRAIN_EXPORT TerrainTile : public osg::Group
/** Get the number of colour layers.*/
unsigned int getNumColorLayers() const { return _colorLayers.size(); }
/** Set hint to whether the TerrainTechnique should create per vertex normals for lighting purposes.*/
void setRequiresNormals(bool flag) { _requiresNormals = flag; }
@ -169,6 +171,16 @@ class OSGTERRAIN_EXPORT TerrainTile : public osg::Group
/** Compute the bounding volume of the terrain by computing the union of the bounding volumes of all layers.*/
virtual osg::BoundingSphere computeBound() const;
/** Callback for post processing loaded TerrainTile, and for filling in missing elements such as external external imagery.*/
struct TileLoadedCallback : public osg::Referenced
{
virtual bool deferExternalLayerLoading() const = 0;
virtual void loaded(osgTerrain::TerrainTile* tile, const osgDB::ReaderWriter::Options* options) const = 0;
};
static void setTileLoadedCallback(TileLoadedCallback* lc);
static osg::ref_ptr<TileLoadedCallback>& getTileLoadedCallback();
protected:
virtual ~TerrainTile();

View File

@ -1007,7 +1007,7 @@ osg::Image* DataInputStream::readImage(IncludeImageMode mode)
// Only read image name from stream.
{
std::string filename = readString();
if(filename.compare("")!=0){
if(!filename.empty()){
return readImage(filename);
}
}

View File

@ -15,6 +15,8 @@
#include "ImageLayer.h"
#include "Layer.h"
#include <osgTerrain/TerrainTile>
#include <osgDB/ReadFile>
using namespace ive;
@ -59,8 +61,18 @@ void ImageLayer::read(DataInputStream* in)
throw Exception("ImageLayer::read(): Could not cast this osgLayer::Layer to an osg::Group.");
bool deferExternalLayerLoading = osgTerrain::TerrainTile::getTileLoadedCallback().valid() ?
osgTerrain::TerrainTile::getTileLoadedCallback()->deferExternalLayerLoading() : false;
// Should we read image data from stream
IncludeImageMode includeImg = (IncludeImageMode)in->readChar();
setImage(in->readImage(includeImg));
if (includeImg==IMAGE_REFERENCE_FILE && deferExternalLayerLoading)
{
setFileName(in->readString());
}
else
{
setImage(in->readImage(includeImg));
}
}

View File

@ -66,7 +66,7 @@ void TerrainTile::write(DataOutputStream* out)
}
writeTerrainTechnique(out, getTerrainTechnique());
}
void TerrainTile::read(DataInputStream* in)
@ -119,6 +119,8 @@ void TerrainTile::read(DataInputStream* in)
setTerrainTechnique(readTerrainTechnique(in));
if (osgTerrain::TerrainTile::getTileLoadedCallback().valid())
osgTerrain::TerrainTile::getTileLoadedCallback()->loaded(this, in->getOptions());
}
void TerrainTile::writeTerrainTechnique(DataOutputStream* out, osgTerrain::TerrainTechnique* technique)

View File

@ -13,6 +13,8 @@
#include <osgDB/Output>
#include <osgDB/ParameterOutput>
#include <osgTerrain/TerrainTile>
bool ImageLayer_readLocalData(osg::Object &obj, osgDB::Input &fr);
bool ImageLayer_writeLocalData(const osg::Object &obj, osgDB::Output &fw);
@ -36,11 +38,18 @@ bool ImageLayer_readLocalData(osg::Object& obj, osgDB::Input &fr)
std::string filename = fr[1].getStr();
if (!filename.empty())
{
osg::ref_ptr<osg::Image> image = fr.readImage(filename.c_str());
if (image.valid())
bool deferExternalLayerLoading = osgTerrain::TerrainTile::getTileLoadedCallback().valid() ?
osgTerrain::TerrainTile::getTileLoadedCallback()->deferExternalLayerLoading() : false;
layer.setFileName(filename);
if (!deferExternalLayerLoading)
{
layer.setFileName(filename);
layer.setImage(image.get());
osg::ref_ptr<osg::Image> image = fr.readImage(filename.c_str());
if (image.valid())
{
layer.setImage(image.get());
}
}
}

View File

@ -267,6 +267,9 @@ bool TerrainTile_readLocalData(osg::Object& obj, osgDB::Input &fr)
}
if (osgTerrain::TerrainTile::getTileLoadedCallback().valid())
osgTerrain::TerrainTile::getTileLoadedCallback()->loaded(&terrainTile, fr.getOptions());
return itrAdvanced;
}

View File

@ -19,6 +19,17 @@
using namespace osg;
using namespace osgTerrain;
void TerrainTile::setTileLoadedCallback(TerrainTile::TileLoadedCallback* lc)
{
getTileLoadedCallback() = lc;
}
osg::ref_ptr<TerrainTile::TileLoadedCallback>& TerrainTile::getTileLoadedCallback()
{
static osg::ref_ptr<TileLoadedCallback> s_TileLoadedCallback;
return s_TileLoadedCallback;
}
TerrainTile::TerrainTile():
_terrain(0),
_hasBeenTraversal(false),