Removed TileSystem class, and added support for TerrainTile's automatically

registering and unregistering themseles with the enclosing Terrain node.
This commit is contained in:
Robert Osfield 2008-03-27 13:21:36 +00:00
parent 31bc0dd9e3
commit 2567b810cf
10 changed files with 147 additions and 145 deletions

View File

@ -46,13 +46,32 @@ class OSGTERRAIN_EXPORT Terrain : public osg::Group
/** Get the const TerrainTechnique*/ /** Get the const TerrainTechnique*/
const TerrainTechnique* getTerrainTechnique() const { return _terrainTechnique.get(); } const TerrainTechnique* getTerrainTechnique() const { return _terrainTechnique.get(); }
/** Get the TerrainTile for a given TileID.*/
TerrainTile* getTile(const TileID& tileID);
/** Get the const TerrainTile for a given TileID.*/
const TerrainTile* getTile(const TileID& tileID) const;
protected: protected:
virtual ~Terrain(); virtual ~Terrain();
friend class TerrainTile;
void registerTerrainTile(TerrainTile* tile);
void unregisterTerrainTile(TerrainTile* tile);
typedef std::map< TileID, TerrainTile* > TerrainTileMap;
typedef std::set< TerrainTile* > TerrainTileSet;
osg::ref_ptr<TerrainTechnique> _terrainTechnique; osg::ref_ptr<TerrainTechnique> _terrainTechnique;
TerrainTileSet _terrainTileSet;
TerrainTileMap _terrainTileMap;
}; };

View File

@ -39,6 +39,16 @@ class TileID
x(in_x), x(in_x),
y(in_y) {} y(in_y) {}
bool operator == (const TileID& rhs) const
{
return (layer==rhs.layer) && (x==rhs.x) && (y==rhs.y);
}
bool operator != (const TileID& rhs) const
{
return (layer!=rhs.layer) || (x!=rhs.x) || (y!=rhs.y);
}
bool operator < (const TileID& rhs) const bool operator < (const TileID& rhs) const
{ {
if (layer<rhs.layer) return true; if (layer<rhs.layer) return true;
@ -48,6 +58,8 @@ class TileID
return y<rhs.y; return y<rhs.y;
} }
bool valid() const { return layer>=0; }
int layer; int layer;
int x; int x;
int y; int y;
@ -74,7 +86,7 @@ class OSGTERRAIN_EXPORT TerrainTile : public osg::Group
/** Set the Terrain that this Terrain tile is a member of.*/ /** Set the Terrain that this Terrain tile is a member of.*/
void setTerrain(Terrain* ts) { _terrain = ts; } void setTerrain(Terrain* ts);
/** Get the Terrain that this Terrain tile is a member of.*/ /** Get the Terrain that this Terrain tile is a member of.*/
Terrain* getTerrain() { return _terrain; } Terrain* getTerrain() { return _terrain; }
@ -83,12 +95,15 @@ class OSGTERRAIN_EXPORT TerrainTile : public osg::Group
const Terrain* getTerrain() const { return _terrain; } const Terrain* getTerrain() const { return _terrain; }
void setTileID(const TileID& tileID) { _tileID = tileID; } /** Set the TileID (layer, x,y) of the TerrainTile.
* The TileID is used so it can be located by its neighbours
* via the enclosing Terrain node that manages a map of TileID to TerraiTiles.*/
void setTileID(const TileID& tileID);
/** Get the TileID (layer, x,y) of the TerrainTile.*/
const TileID& getTileID() const { return _tileID; } const TileID& getTileID() const { return _tileID; }
/** Set the TerrainTechnique*/ /** Set the TerrainTechnique*/
void setTerrainTechnique(TerrainTechnique* TerrainTechnique); void setTerrainTechnique(TerrainTechnique* TerrainTechnique);
@ -155,6 +170,7 @@ class OSGTERRAIN_EXPORT TerrainTile : public osg::Group
typedef std::vector< osg::ref_ptr<Layer> > Layers; typedef std::vector< osg::ref_ptr<Layer> > Layers;
friend class Terrain;
Terrain* _terrain; Terrain* _terrain;

View File

@ -1,46 +0,0 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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.
*/
#ifndef OSGTERRAIN_TILESYSTEM
#define OSGTERRAIN_TILESYSTEM 1
#include <osgTerrain/TerrainTile>
#include <osg/Object>
#include <osg/observer_ptr>
namespace osgTerrain {
/** TileSystem provides the mechanism for computing the position in space of tiles.*/
class OSGTERRAIN_EXPORT TileSystem : public osg::Object
{
public:
TileSystem();
TileSystem(const TileSystem&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osgTerrain, TileSystem);
protected:
virtual ~TileSystem();
typedef std::map< TileID, osg::observer_ptr<TerrainTile> > TileMap;
TileMap _tileMap;
};
}
#endif

View File

@ -13,7 +13,6 @@ SET(LIB_PUBLIC_HEADERS
${HEADER_PATH}/TerrainTile ${HEADER_PATH}/TerrainTile
${HEADER_PATH}/TerrainTechnique ${HEADER_PATH}/TerrainTechnique
${HEADER_PATH}/Terrain ${HEADER_PATH}/Terrain
${HEADER_PATH}/TileSystem
${HEADER_PATH}/GeometryTechnique ${HEADER_PATH}/GeometryTechnique
${HEADER_PATH}/ValidDataOperator ${HEADER_PATH}/ValidDataOperator
${HEADER_PATH}/Version ${HEADER_PATH}/Version
@ -28,7 +27,6 @@ ADD_LIBRARY(${LIB_NAME}
TerrainTile.cpp TerrainTile.cpp
TerrainTechnique.cpp TerrainTechnique.cpp
Terrain.cpp Terrain.cpp
TileSystem.cpp
GeometryTechnique.cpp GeometryTechnique.cpp
Version.cpp Version.cpp
) )

View File

@ -22,6 +22,15 @@ Terrain::Terrain()
Terrain::~Terrain() Terrain::~Terrain()
{ {
for(TerrainTileSet::iterator itr = _terrainTileSet.begin();
itr != _terrainTileSet.end();
++itr)
{
const_cast<TerrainTile*>(*itr)->_terrain = 0;
}
_terrainTileSet.clear();
_terrainTileMap.clear();
} }
Terrain::Terrain(const Terrain& ts, const osg::CopyOp& copyop) Terrain::Terrain(const Terrain& ts, const osg::CopyOp& copyop)
@ -32,3 +41,47 @@ void Terrain::traverse(osg::NodeVisitor& nv)
{ {
Group::traverse(nv); Group::traverse(nv);
} }
TerrainTile* Terrain::getTile(const TileID& tileID)
{
TerrainTileMap::iterator itr = _terrainTileMap.find(tileID);
if (itr != _terrainTileMap.end()) return 0;
return itr->second;
}
const TerrainTile* Terrain::getTile(const TileID& tileID) const
{
TerrainTileMap::const_iterator itr = _terrainTileMap.find(tileID);
if (itr != _terrainTileMap.end()) return 0;
return itr->second;
}
void Terrain::registerTerrainTile(TerrainTile* tile)
{
if (!tile) return;
if (tile->getTileID().valid())
{
_terrainTileMap[tile->getTileID()] = tile;
}
_terrainTileSet.insert(tile);
osg::notify(osg::INFO)<<"Terrain::registerTerrainTile "<<tile<<" total number of tile "<<_terrainTileSet.size()<<std::endl;
}
void Terrain::unregisterTerrainTile(TerrainTile* tile)
{
if (!tile) return;
if (tile->getTileID().valid())
{
_terrainTileMap.erase(tile->getTileID());
}
_terrainTileSet.erase(tile);
osg::notify(osg::INFO)<<"Terrain::unregisterTerrainTile "<<tile<<" total number of tile "<<_terrainTileSet.size()<<std::endl;
}

View File

@ -45,8 +45,32 @@ TerrainTile::TerrainTile(const TerrainTile& terrain,const osg::CopyOp& copyop):
TerrainTile::~TerrainTile() TerrainTile::~TerrainTile()
{ {
if (_terrain) setTerrain(0);
} }
void TerrainTile::setTerrain(Terrain* ts)
{
if (_terrain == ts) return;
if (_terrain) _terrain->unregisterTerrainTile(this);
_terrain = ts;
if (_terrain) _terrain->registerTerrainTile(this);
}
void TerrainTile::setTileID(const TileID& tileID)
{
if (_tileID == tileID) return;
if (_terrain) _terrain->unregisterTerrainTile(this);
_tileID = tileID;
if (_terrain) _terrain->registerTerrainTile(this);
}
void TerrainTile::traverse(osg::NodeVisitor& nv) void TerrainTile::traverse(osg::NodeVisitor& nv)
{ {
if (!_hasBeenTraversal) if (!_hasBeenTraversal)
@ -64,7 +88,7 @@ void TerrainTile::traverse(osg::NodeVisitor& nv)
if (ts) if (ts)
{ {
osg::notify(osg::INFO)<<"Assigning terrain system "<<ts<<std::endl; osg::notify(osg::INFO)<<"Assigning terrain system "<<ts<<std::endl;
_terrain = ts; setTerrain(ts);
} }
} }
} }

View File

@ -1,29 +0,0 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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 <osgTerrain/TileSystem>
using namespace osgTerrain;
TileSystem::TileSystem()
{
}
TileSystem::TileSystem(const TileSystem& tileSystem,const osg::CopyOp& copyop):
osg::Object(tileSystem)
{
}
TileSystem::~TileSystem()
{
}

View File

@ -15,6 +15,7 @@
#include <osg/Object> #include <osg/Object>
#include <osgTerrain/Terrain> #include <osgTerrain/Terrain>
#include <osgTerrain/TerrainTechnique> #include <osgTerrain/TerrainTechnique>
#include <osgTerrain/TerrainTile>
// Must undefine IN and OUT macros defined in Windows headers // Must undefine IN and OUT macros defined in Windows headers
#ifdef IN #ifdef IN
@ -84,6 +85,28 @@ BEGIN_OBJECT_REFLECTOR(osgTerrain::Terrain)
__C5_TerrainTechnique_P1__getTerrainTechnique, __C5_TerrainTechnique_P1__getTerrainTechnique,
"Get the const TerrainTechnique. ", "Get the const TerrainTechnique. ",
""); "");
I_Method1(osgTerrain::TerrainTile *, getTile, IN, const osgTerrain::TileID &, tileID,
Properties::NON_VIRTUAL,
__TerrainTile_P1__getTile__C5_TileID_R1,
"Get the TerrainTile for a given TileID. ",
"");
I_Method1(const osgTerrain::TerrainTile *, getTile, IN, const osgTerrain::TileID &, tileID,
Properties::NON_VIRTUAL,
__C5_TerrainTile_P1__getTile__C5_TileID_R1,
"Get the const TerrainTile for a given TileID. ",
"");
I_ProtectedMethod1(void, registerTerrainTile, IN, osgTerrain::TerrainTile *, tile,
Properties::NON_VIRTUAL,
Properties::NON_CONST,
__void__registerTerrainTile__TerrainTile_P1,
"",
"");
I_ProtectedMethod1(void, unregisterTerrainTile, IN, osgTerrain::TerrainTile *, tile,
Properties::NON_VIRTUAL,
Properties::NON_CONST,
__void__unregisterTerrainTile__TerrainTile_P1,
"",
"");
I_SimpleProperty(osgTerrain::TerrainTechnique *, TerrainTechnique, I_SimpleProperty(osgTerrain::TerrainTechnique *, TerrainTechnique,
__TerrainTechnique_P1__getTerrainTechnique, __TerrainTechnique_P1__getTerrainTechnique,
__void__setTerrainTechnique__osgTerrain_TerrainTechnique_P1); __void__setTerrainTechnique__osgTerrain_TerrainTechnique_P1);

View File

@ -96,12 +96,12 @@ BEGIN_OBJECT_REFLECTOR(osgTerrain::TerrainTile)
I_Method1(void, setTileID, IN, const osgTerrain::TileID &, tileID, I_Method1(void, setTileID, IN, const osgTerrain::TileID &, tileID,
Properties::NON_VIRTUAL, Properties::NON_VIRTUAL,
__void__setTileID__C5_TileID_R1, __void__setTileID__C5_TileID_R1,
"", "Set the TileID (layer, x,y) of the TerrainTile. ",
""); "The TileID is used so it can be located by its neighbours via the enclosing Terrain node that manages a map of TileID to TerraiTiles. ");
I_Method0(const osgTerrain::TileID &, getTileID, I_Method0(const osgTerrain::TileID &, getTileID,
Properties::NON_VIRTUAL, Properties::NON_VIRTUAL,
__C5_TileID_R1__getTileID, __C5_TileID_R1__getTileID,
"", "Get the TileID (layer, x,y) of the TerrainTile. ",
""); "");
I_Method1(void, setTerrainTechnique, IN, osgTerrain::TerrainTechnique *, TerrainTechnique, I_Method1(void, setTerrainTechnique, IN, osgTerrain::TerrainTechnique *, TerrainTechnique,
Properties::NON_VIRTUAL, Properties::NON_VIRTUAL,
@ -232,6 +232,11 @@ BEGIN_VALUE_REFLECTOR(osgTerrain::TileID)
____TileID__int__int__int, ____TileID__int__int__int,
"", "",
""); "");
I_Method0(bool, valid,
Properties::NON_VIRTUAL,
__bool__valid,
"",
"");
I_PublicMemberProperty(int, layer); I_PublicMemberProperty(int, layer);
I_PublicMemberProperty(int, x); I_PublicMemberProperty(int, x);
I_PublicMemberProperty(int, y); I_PublicMemberProperty(int, y);

View File

@ -1,61 +0,0 @@
// ***************************************************************************
//
// Generated automatically by genwrapper.
// Please DO NOT EDIT this file!
//
// ***************************************************************************
#include <osgIntrospection/ReflectionMacros>
#include <osgIntrospection/TypedMethodInfo>
#include <osgIntrospection/StaticMethodInfo>
#include <osgIntrospection/Attributes>
#include <osg/CopyOp>
#include <osg/Object>
#include <osgTerrain/TileSystem>
// Must undefine IN and OUT macros defined in Windows headers
#ifdef IN
#undef IN
#endif
#ifdef OUT
#undef OUT
#endif
BEGIN_OBJECT_REFLECTOR(osgTerrain::TileSystem)
I_DeclaringFile("osgTerrain/TileSystem");
I_BaseType(osg::Object);
I_Constructor0(____TileSystem,
"",
"");
I_ConstructorWithDefaults2(IN, const osgTerrain::TileSystem &, x, , IN, const osg::CopyOp &, copyop, osg::CopyOp::SHALLOW_COPY,
____TileSystem__C5_TileSystem_R1__C5_osg_CopyOp_R1,
"",
"");
I_Method0(osg::Object *, cloneType,
Properties::VIRTUAL,
__osg_Object_P1__cloneType,
"Clone the type of an object, with Object* return type. ",
"Must be defined by derived classes. ");
I_Method1(osg::Object *, clone, IN, const osg::CopyOp &, copyop,
Properties::VIRTUAL,
__osg_Object_P1__clone__C5_osg_CopyOp_R1,
"Clone an object, with Object* return type. ",
"Must be defined by derived classes. ");
I_Method1(bool, isSameKindAs, IN, const osg::Object *, obj,
Properties::VIRTUAL,
__bool__isSameKindAs__C5_osg_Object_P1,
"",
"");
I_Method0(const char *, libraryName,
Properties::VIRTUAL,
__C5_char_P1__libraryName,
"return the name of the object's library. ",
"Must be defined by derived classes. The OpenSceneGraph convention is that the namespace of a library is the same as the library name. ");
I_Method0(const char *, className,
Properties::VIRTUAL,
__C5_char_P1__className,
"return the name of the object's class type. ",
"Must be defined by derived classes. ");
END_REFLECTOR