Files
OpenSceneGraph/src/osgPlugins/ive/TerrainTile.cpp
Robert Osfield f61a6aa4e7 Refactored the way that the DatabasePager passes the Terrain decorator node onto the TerrainTile.
The DatabasePager now passes the Terrain pointer into the ReaderWriter's via osgDB::Options object,
rather than pushing a NodePath containing the Terrain onto NodeVisitor.  This
change means that the DatabasePager nolonger needs to observer the whole NodePath and
will be lighter and quicker for it.

The change also means that ReadFileCallback can now run custom NodeVisitor's on the scene graph without
having to worry about TerrainTile's constructing scene graphs prior to the Terrain being assigned.

Also changed is the NodeVisitor::DatabaseRequestHandler which now requires a NodePath to the node that you wish
to add to rather than just the pointer to the node you wish to add to.  This is more robust when handling scenes
with multiple parental paths, whereas previously errors could have occurred due to the default of picking the first
available parental path.  This change means that subclasses of DatabasePager will need to be updated to use this new
function entry point.
2011-01-12 19:29:24 +00:00

178 lines
4.9 KiB
C++

/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include "Exception.h"
#include "TerrainTile.h"
#include "Group.h"
#include "Layer.h"
#include <osgDB/Options>
#include <osgTerrain/GeometryTechnique>
#include <osgTerrain/Terrain>
using namespace ive;
void TerrainTile::write(DataOutputStream* out)
{
// Write Terrain's identification.
out->writeInt(IVETERRAINTILE);
// If the osg class is inherited by any other class we should also write this to file.
osg::Group* group = dynamic_cast<osg::Group*>(this);
if(group)
((ive::Group*)(group))->write(out);
else
out_THROW_EXCEPTION("Terrain::write(): Could not cast this osgTerrain::Terrain to an osg::Group.");
if (out->getVersion() >= VERSION_0044)
{
out->writeInt(getBlendingPolicy());
}
if (out->getVersion() >= VERSION_0026)
{
out->writeInt(getTileID().level);
out->writeInt(getTileID().x);
out->writeInt(getTileID().y);
}
if (out->getVersion() >= VERSION_0023)
{
out->writeLocator(getLocator());
out->writeLayer(getElevationLayer());
out->writeUInt(getNumColorLayers());
for(unsigned int i=0; i<getNumColorLayers(); ++i)
{
out->writeLayer(getColorLayer(i));
}
}
else
{
LayerHelper helper;
helper.writeLocator(out, getLocator());
helper.writeLayer(out, getElevationLayer());
out->writeUInt(getNumColorLayers());
for(unsigned int i=0; i<getNumColorLayers(); ++i)
{
helper.writeLayer(out, getColorLayer(i));
}
}
writeTerrainTechnique(out, getTerrainTechnique());
}
void TerrainTile::read(DataInputStream* in)
{
// Peek on Terrain's identification.
int id = in->peekInt();
if (id != IVETERRAINTILE) in_THROW_EXCEPTION("TerrainTile::read(): Expected Terrain identification.");
// Read Terrain's identification.
id = in->readInt();
// If the osg class is inherited by any other class we should also read this from file.
osg::Group* group = dynamic_cast<osg::Group*>(this);
if(group)
((ive::Group*)(group))->read(in);
else
in_THROW_EXCEPTION("Terrain::read(): Could not cast this osgTerrain::Terrain to an osg::Group.");
if (in->getVersion() >= VERSION_0044)
{
setBlendingPolicy(static_cast<osgTerrain::TerrainTile::BlendingPolicy>(in->readInt()));
}
if (in->getVersion() >= VERSION_0026)
{
int level = in->readInt();
int x = in->readInt();
int y = in->readInt();
setTileID(osgTerrain::TileID(level,x,y));
// OSG_NOTICE<<"Read TileID("<<level<<", "<<x<<", "<<y<<")"<<std::endl;
}
if (in->getVersion() >= VERSION_0023)
{
setLocator(in->readLocator());
setElevationLayer(in->readLayer());
unsigned int numColorLayers = in->readUInt();
for(unsigned int i=0; i<numColorLayers; ++i)
{
setColorLayer(i, in->readLayer());
}
}
else
{
LayerHelper helper;
setLocator(helper.readLocator(in));
setElevationLayer(helper.readLayer(in));
unsigned int numColorLayers = in->readUInt();
for(unsigned int i=0; i<numColorLayers; ++i)
{
setColorLayer(i, helper.readLayer(in));
}
}
setTerrainTechnique(readTerrainTechnique(in));
if (in->getOptions())
{
osg::ref_ptr<osg::Node> node;
if (in->getOptions()->getTerrain().lock(node))
{
setTerrain(node->asTerrain());
}
}
if (osgTerrain::TerrainTile::getTileLoadedCallback().valid())
osgTerrain::TerrainTile::getTileLoadedCallback()->loaded(this, in->getOptions());
}
void TerrainTile::writeTerrainTechnique(DataOutputStream* out, osgTerrain::TerrainTechnique* technique)
{
if (dynamic_cast<osgTerrain::GeometryTechnique*>(technique))
{
out->writeBool(true);
out->writeInt(IVEGEOMETRYTECHNIQUE);
}
else
{
out->writeBool(false);
}
}
osgTerrain::TerrainTechnique* TerrainTile::readTerrainTechnique(DataInputStream* in)
{
bool hasTechnique = in->readBool();
if (!hasTechnique) return 0;
int id = in->readInt();
if (id==IVEGEOMETRYTECHNIQUE)
{
return new osgTerrain::GeometryTechnique;
}
else
{
return 0;
}
}