GeometryPool
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2014 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_GEOMETRYPOOL
#define OSGTERRAIN_GEOMETRYPOOL 1
#include <osg/Geometry>
#include <osg/MatrixTransform>
#include <osg/Program>
#include <OpenThreads/Mutex>
#include <osgTerrain/TerrainTile>
namespace osgTerrain {
extern OSGTERRAIN_EXPORT const osgTerrain::Locator* computeMasterLocator(const osgTerrain::TerrainTile* tile);
class OSGTERRAIN_EXPORT SharedGeometry : public osg::Drawable
{
public:
SharedGeometry();
SharedGeometry(const SharedGeometry&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Node(osgTerrain, SharedGeometry);
void setVertexArray(osg::Array* array) { _vertexArray = array; }
osg::Array* getVertexArray() { return _vertexArray.get(); }
const osg::Array* getVertexArray() const { return _vertexArray.get(); }
void setNormalArray(osg::Array* array) { _normalArray = array; }
osg::Array* getNormalArray() { return _normalArray.get(); }
const osg::Array* getNormalArray() const { return _normalArray.get(); }
void setColorArray(osg::Array* array) { _colorArray = array; }
osg::Array* getColorArray() { return _colorArray.get(); }
const osg::Array* getColorArray() const { return _colorArray.get(); }
void setTexCoordArray(osg::Array* array) { _texcoordArray = array; }
osg::Array* getTexCoordArray() { return _texcoordArray.get(); }
const osg::Array* getTexCoordArray() const { return _texcoordArray.get(); }
void setDrawElements(osg::DrawElements* array) { _drawElements = array; }
osg::DrawElements* getDrawElements() { return _drawElements.get(); }
const osg::DrawElements* getDrawElements() const { return _drawElements.get(); }
typedef std::vector<unsigned int> VertexToHeightFieldMapping;
void setVertexToHeightFieldMapping(const VertexToHeightFieldMapping& vthfm) { _vertexToHeightFieldMapping = vthfm; }
VertexToHeightFieldMapping& getVertexToHeightFieldMapping() { return _vertexToHeightFieldMapping; }
const VertexToHeightFieldMapping& getVertexToHeightFieldMapping() const { return _vertexToHeightFieldMapping; }
osg::VertexArrayState* createVertexArrayStateImplementation(osg::RenderInfo& renderInfo) const;
void compileGLObjects(osg::RenderInfo& renderInfo) const;
void drawImplementation(osg::RenderInfo& renderInfo) const;
void resizeGLObjectBuffers(unsigned int maxSize);
void releaseGLObjects(osg::State* state) const;
virtual bool supports(const osg::Drawable::AttributeFunctor&) const { return true; }
virtual void accept(osg::Drawable::AttributeFunctor&);
virtual bool supports(const osg::Drawable::ConstAttributeFunctor&) const { return true; }
virtual void accept(osg::Drawable::ConstAttributeFunctor&) const;
virtual bool supports(const osg::PrimitiveFunctor&) const { return true; }
virtual void accept(osg::PrimitiveFunctor&) const;
virtual bool supports(const osg::PrimitiveIndexFunctor&) const { return true; }
virtual void accept(osg::PrimitiveIndexFunctor&) const;
protected:
virtual ~SharedGeometry();
osg::ref_ptr<osg::Array> _vertexArray;
osg::ref_ptr<osg::Array> _normalArray;
osg::ref_ptr<osg::Array> _colorArray;
osg::ref_ptr<osg::Array> _texcoordArray;
osg::ref_ptr<osg::DrawElements> _drawElements;
VertexToHeightFieldMapping _vertexToHeightFieldMapping;
};
class OSGTERRAIN_EXPORT GeometryPool : public osg::Referenced
{
public:
GeometryPool();
struct GeometryKey
{
GeometryKey(): sx(0.0), sy(0.0), y(0.0), nx(0), ny(0) {}
bool operator < (const GeometryKey& rhs) const
{
if (sx<rhs.sx) return true;
if (sx>rhs.sx) return false;
if (sx<rhs.sx) return true;
if (sx>rhs.sx) return false;
if (y<rhs.y) return true;
if (y>rhs.y) return false;
if (nx<rhs.nx) return true;
if (nx>rhs.nx) return false;
return (ny<rhs.ny);
}
double sx;
double sy;
double y;
int nx;
int ny;
};
typedef std::map< GeometryKey, osg::ref_ptr<SharedGeometry> > GeometryMap;
virtual bool createKeyForTile(TerrainTile* tile, GeometryKey& key);
enum LayerType
{
HEIGHTFIELD_LAYER,
COLOR_LAYER,
CONTOUR_LAYER
};
typedef std::vector<LayerType> LayerTypes;
typedef std::map<LayerTypes, osg::ref_ptr<osg::Program> > ProgramMap;
osg::StateSet* getRootStateSetForTerrain(Terrain* terrain);
virtual osg::ref_ptr<osg::Program> getOrCreateProgram(LayerTypes& layerTypes);
virtual osg::ref_ptr<SharedGeometry> getOrCreateGeometry(osgTerrain::TerrainTile* tile);
virtual osg::ref_ptr<osg::MatrixTransform> getTileSubgraph(osgTerrain::TerrainTile* tile);
virtual void applyLayers(osgTerrain::TerrainTile* tile, osg::StateSet* stateset);
protected:
virtual ~GeometryPool();
OpenThreads::Mutex _geometryMapMutex;
GeometryMap _geometryMap;
OpenThreads::Mutex _programMapMutex;
ProgramMap _programMap;
osg::ref_ptr<osg::StateSet> _rootStateSet;
bool _rootStateSetAssigned;
};
class OSGTERRAIN_EXPORT HeightFieldDrawable : public osg::Drawable
{
public:
HeightFieldDrawable();
HeightFieldDrawable(const HeightFieldDrawable&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Node(osgTerrain, HeightFieldDrawable);
void setHeightField(osg::HeightField* hf) { _heightField = hf; }
osg::HeightField* getHeightField() { return _heightField.get(); }
const osg::HeightField* getHeightField() const { return _heightField.get(); }
void setGeometry(SharedGeometry* geom) { _geometry = geom; }
SharedGeometry* getGeometry() { return _geometry.get(); }
const SharedGeometry* getGeometry() const { return _geometry.get(); }
void setVertices(osg::Vec3Array* vertices) { _vertices = vertices; }
osg::Vec3Array* getVertices() { return _vertices.get(); }
const osg::Vec3Array* getVertices() const { return _vertices.get(); }
virtual void drawImplementation(osg::RenderInfo& renderInfo) const;
virtual void compileGLObjects(osg::RenderInfo& renderInfo) const;
virtual void resizeGLObjectBuffers(unsigned int maxSize);
virtual void releaseGLObjects(osg::State* state=0) const;
virtual bool supports(const osg::Drawable::AttributeFunctor&) const { return true; }
virtual void accept(osg::Drawable::AttributeFunctor&);
virtual bool supports(const osg::Drawable::ConstAttributeFunctor&) const { return true; }
virtual void accept(osg::Drawable::ConstAttributeFunctor&) const;
virtual bool supports(const osg::PrimitiveFunctor&) const { return true; }
virtual void accept(osg::PrimitiveFunctor&) const;
virtual bool supports(const osg::PrimitiveIndexFunctor&) const { return true; }
virtual void accept(osg::PrimitiveIndexFunctor&) const;
protected:
virtual ~HeightFieldDrawable();
osg::ref_ptr<osg::HeightField> _heightField;
osg::ref_ptr<SharedGeometry> _geometry;
osg::ref_ptr<osg::Vec3Array> _vertices;
};
}
#endif
Geometry
MatrixTransform
Mutex
TerrainTile
OpenThreads::Mutex
This class provides an object-oriented thread mutex interface.
Definition Mutex:31
osgTerrain::GeometryPool::_programMap
ProgramMap _programMap
Definition GeometryPool:168
osgTerrain::GeometryPool::getRootStateSetForTerrain
osg::StateSet * getRootStateSetForTerrain(Terrain *terrain)
osgTerrain::GeometryPool::GeometryPool
GeometryPool()
osgTerrain::GeometryPool::GeometryMap
std::map< GeometryKey, osg::ref_ptr< SharedGeometry > > GeometryMap
Definition GeometryPool:137
osgTerrain::GeometryPool::_rootStateSet
osg::ref_ptr< osg::StateSet > _rootStateSet
Definition GeometryPool:170
osgTerrain::GeometryPool::ProgramMap
std::map< LayerTypes, osg::ref_ptr< osg::Program > > ProgramMap
Definition GeometryPool:149
osgTerrain::GeometryPool::_geometryMapMutex
OpenThreads::Mutex _geometryMapMutex
Definition GeometryPool:164
osgTerrain::GeometryPool::~GeometryPool
virtual ~GeometryPool()
osgTerrain::GeometryPool::getTileSubgraph
virtual osg::ref_ptr< osg::MatrixTransform > getTileSubgraph(osgTerrain::TerrainTile *tile)
osgTerrain::GeometryPool::_rootStateSetAssigned
bool _rootStateSetAssigned
Definition GeometryPool:171
osgTerrain::GeometryPool::LayerTypes
std::vector< LayerType > LayerTypes
Definition GeometryPool:148
osgTerrain::GeometryPool::createKeyForTile
virtual bool createKeyForTile(TerrainTile *tile, GeometryKey &key)
osgTerrain::GeometryPool::getOrCreateGeometry
virtual osg::ref_ptr< SharedGeometry > getOrCreateGeometry(osgTerrain::TerrainTile *tile)
osgTerrain::GeometryPool::applyLayers
virtual void applyLayers(osgTerrain::TerrainTile *tile, osg::StateSet *stateset)
osgTerrain::GeometryPool::getOrCreateProgram
virtual osg::ref_ptr< osg::Program > getOrCreateProgram(LayerTypes &layerTypes)
osgTerrain::GeometryPool::LayerType
LayerType
Definition GeometryPool:142
osgTerrain::GeometryPool::HEIGHTFIELD_LAYER
@ HEIGHTFIELD_LAYER
Definition GeometryPool:143
osgTerrain::GeometryPool::COLOR_LAYER
@ COLOR_LAYER
Definition GeometryPool:144
osgTerrain::GeometryPool::CONTOUR_LAYER
@ CONTOUR_LAYER
Definition GeometryPool:145
osgTerrain::GeometryPool::_programMapMutex
OpenThreads::Mutex _programMapMutex
Definition GeometryPool:167
osgTerrain::GeometryPool::_geometryMap
GeometryMap _geometryMap
Definition GeometryPool:165
osgTerrain::HeightFieldDrawable::accept
virtual void accept(osg::Drawable::ConstAttributeFunctor &) const
osgTerrain::HeightFieldDrawable::getGeometry
SharedGeometry * getGeometry()
Definition GeometryPool:189
osgTerrain::HeightFieldDrawable::accept
virtual void accept(osg::PrimitiveFunctor &) const
osgTerrain::HeightFieldDrawable::setHeightField
void setHeightField(osg::HeightField *hf)
Definition GeometryPool:184
osgTerrain::HeightFieldDrawable::getVertices
const osg::Vec3Array * getVertices() const
Definition GeometryPool:194
osgTerrain::HeightFieldDrawable::drawImplementation
virtual void drawImplementation(osg::RenderInfo &renderInfo) const
osgTerrain::HeightFieldDrawable::_vertices
osg::ref_ptr< osg::Vec3Array > _vertices
Definition GeometryPool:220
osgTerrain::HeightFieldDrawable::~HeightFieldDrawable
virtual ~HeightFieldDrawable()
osgTerrain::HeightFieldDrawable::getVertices
osg::Vec3Array * getVertices()
Definition GeometryPool:193
osgTerrain::HeightFieldDrawable::supports
virtual bool supports(const osg::Drawable::ConstAttributeFunctor &) const
Definition GeometryPool:205
osgTerrain::HeightFieldDrawable::getGeometry
const SharedGeometry * getGeometry() const
Definition GeometryPool:190
osgTerrain::HeightFieldDrawable::META_Node
META_Node(osgTerrain, HeightFieldDrawable)
osgTerrain::HeightFieldDrawable::resizeGLObjectBuffers
virtual void resizeGLObjectBuffers(unsigned int maxSize)
osgTerrain::HeightFieldDrawable::getHeightField
const osg::HeightField * getHeightField() const
Definition GeometryPool:186
osgTerrain::HeightFieldDrawable::HeightFieldDrawable
HeightFieldDrawable(const HeightFieldDrawable &, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
osgTerrain::HeightFieldDrawable::releaseGLObjects
virtual void releaseGLObjects(osg::State *state=0) const
osgTerrain::HeightFieldDrawable::getHeightField
osg::HeightField * getHeightField()
Definition GeometryPool:185
osgTerrain::HeightFieldDrawable::supports
virtual bool supports(const osg::PrimitiveIndexFunctor &) const
Definition GeometryPool:211
osgTerrain::HeightFieldDrawable::_geometry
osg::ref_ptr< SharedGeometry > _geometry
Definition GeometryPool:219
osgTerrain::HeightFieldDrawable::supports
virtual bool supports(const osg::PrimitiveFunctor &) const
Definition GeometryPool:208
osgTerrain::HeightFieldDrawable::supports
virtual bool supports(const osg::Drawable::AttributeFunctor &) const
Definition GeometryPool:202
osgTerrain::HeightFieldDrawable::setVertices
void setVertices(osg::Vec3Array *vertices)
Definition GeometryPool:192
osgTerrain::HeightFieldDrawable::_heightField
osg::ref_ptr< osg::HeightField > _heightField
Definition GeometryPool:218
osgTerrain::HeightFieldDrawable::compileGLObjects
virtual void compileGLObjects(osg::RenderInfo &renderInfo) const
osgTerrain::HeightFieldDrawable::accept
virtual void accept(osg::Drawable::AttributeFunctor &)
osgTerrain::HeightFieldDrawable::accept
virtual void accept(osg::PrimitiveIndexFunctor &) const
osgTerrain::HeightFieldDrawable::HeightFieldDrawable
HeightFieldDrawable()
osgTerrain::HeightFieldDrawable::setGeometry
void setGeometry(SharedGeometry *geom)
Definition GeometryPool:188
osgTerrain::Locator
Definition osgTerrain/Locator:26
osgTerrain::SharedGeometry
Definition GeometryPool:32
osgTerrain::SharedGeometry::getColorArray
osg::Array * getColorArray()
Definition GeometryPool:49
osgTerrain::SharedGeometry::supports
virtual bool supports(const osg::PrimitiveIndexFunctor &) const
Definition GeometryPool:87
osgTerrain::SharedGeometry::_vertexArray
osg::ref_ptr< osg::Array > _vertexArray
Definition GeometryPool:94
osgTerrain::SharedGeometry::getTexCoordArray
const osg::Array * getTexCoordArray() const
Definition GeometryPool:54
osgTerrain::SharedGeometry::accept
virtual void accept(osg::Drawable::ConstAttributeFunctor &) const
osgTerrain::SharedGeometry::setColorArray
void setColorArray(osg::Array *array)
Definition GeometryPool:48
osgTerrain::SharedGeometry::createVertexArrayStateImplementation
osg::VertexArrayState * createVertexArrayStateImplementation(osg::RenderInfo &renderInfo) const
osgTerrain::SharedGeometry::VertexToHeightFieldMapping
std::vector< unsigned int > VertexToHeightFieldMapping
Definition GeometryPool:61
osgTerrain::SharedGeometry::releaseGLObjects
void releaseGLObjects(osg::State *state) const
osgTerrain::SharedGeometry::_normalArray
osg::ref_ptr< osg::Array > _normalArray
Definition GeometryPool:95
osgTerrain::SharedGeometry::setNormalArray
void setNormalArray(osg::Array *array)
Definition GeometryPool:44
osgTerrain::SharedGeometry::SharedGeometry
SharedGeometry(const SharedGeometry &, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
osgTerrain::SharedGeometry::_vertexToHeightFieldMapping
VertexToHeightFieldMapping _vertexToHeightFieldMapping
Definition GeometryPool:100
osgTerrain::SharedGeometry::resizeGLObjectBuffers
void resizeGLObjectBuffers(unsigned int maxSize)
osgTerrain::SharedGeometry::supports
virtual bool supports(const osg::PrimitiveFunctor &) const
Definition GeometryPool:84
osgTerrain::SharedGeometry::getVertexArray
osg::Array * getVertexArray()
Definition GeometryPool:41
osgTerrain::SharedGeometry::getColorArray
const osg::Array * getColorArray() const
Definition GeometryPool:50
osgTerrain::SharedGeometry::getVertexArray
const osg::Array * getVertexArray() const
Definition GeometryPool:42
osgTerrain::SharedGeometry::setVertexToHeightFieldMapping
void setVertexToHeightFieldMapping(const VertexToHeightFieldMapping &vthfm)
Definition GeometryPool:63
osgTerrain::SharedGeometry::drawImplementation
void drawImplementation(osg::RenderInfo &renderInfo) const
osgTerrain::SharedGeometry::setDrawElements
void setDrawElements(osg::DrawElements *array)
Definition GeometryPool:56
osgTerrain::SharedGeometry::supports
virtual bool supports(const osg::Drawable::ConstAttributeFunctor &) const
Definition GeometryPool:81
osgTerrain::SharedGeometry::META_Node
META_Node(osgTerrain, SharedGeometry)
osgTerrain::SharedGeometry::accept
virtual void accept(osg::PrimitiveIndexFunctor &) const
osgTerrain::SharedGeometry::~SharedGeometry
virtual ~SharedGeometry()
osgTerrain::SharedGeometry::getNormalArray
osg::Array * getNormalArray()
Definition GeometryPool:45
osgTerrain::SharedGeometry::_drawElements
osg::ref_ptr< osg::DrawElements > _drawElements
Definition GeometryPool:98
osgTerrain::SharedGeometry::setTexCoordArray
void setTexCoordArray(osg::Array *array)
Definition GeometryPool:52
osgTerrain::SharedGeometry::getVertexToHeightFieldMapping
const VertexToHeightFieldMapping & getVertexToHeightFieldMapping() const
Definition GeometryPool:66
osgTerrain::SharedGeometry::_colorArray
osg::ref_ptr< osg::Array > _colorArray
Definition GeometryPool:96
osgTerrain::SharedGeometry::supports
virtual bool supports(const osg::Drawable::AttributeFunctor &) const
Definition GeometryPool:78
osgTerrain::SharedGeometry::accept
virtual void accept(osg::PrimitiveFunctor &) const
osgTerrain::SharedGeometry::setVertexArray
void setVertexArray(osg::Array *array)
Definition GeometryPool:40
osgTerrain::SharedGeometry::getNormalArray
const osg::Array * getNormalArray() const
Definition GeometryPool:46
osgTerrain::SharedGeometry::getTexCoordArray
osg::Array * getTexCoordArray()
Definition GeometryPool:53
osgTerrain::SharedGeometry::getDrawElements
const osg::DrawElements * getDrawElements() const
Definition GeometryPool:58
osgTerrain::SharedGeometry::accept
virtual void accept(osg::Drawable::AttributeFunctor &)
osgTerrain::SharedGeometry::getDrawElements
osg::DrawElements * getDrawElements()
Definition GeometryPool:57
osgTerrain::SharedGeometry::SharedGeometry
SharedGeometry()
osgTerrain::SharedGeometry::compileGLObjects
void compileGLObjects(osg::RenderInfo &renderInfo) const
osgTerrain::SharedGeometry::getVertexToHeightFieldMapping
VertexToHeightFieldMapping & getVertexToHeightFieldMapping()
Definition GeometryPool:65
osgTerrain::SharedGeometry::_texcoordArray
osg::ref_ptr< osg::Array > _texcoordArray
Definition GeometryPool:97
osgTerrain::TerrainTile
Definition TerrainTile:68
osgTerrain::Terrain
Definition Terrain:28
osg::Array
Definition Array:61
osg::CopyOp
Definition CopyOp:41
osg::CopyOp::SHALLOW_COPY
@ SHALLOW_COPY
Definition CopyOp:47
osg::DrawElements
Definition PrimitiveSet:337
osg::Drawable::AttributeFunctor
Definition Drawable:426
osg::Drawable::ConstAttributeFunctor
Definition Drawable:461
osg::Drawable
Definition Drawable:89
osg::HeightField
Definition Shape:483
osg::PrimitiveFunctor
Definition PrimitiveSet:54
osg::PrimitiveIndexFunctor
Definition PrimitiveSet:103
osg::Referenced
Definition Referenced:44
osg::RenderInfo
Definition RenderInfo:28
osg::StateSet
Definition StateSet:46
osg::State
Definition State:80
osg::VertexArrayState
Definition VertexArrayState:25
osg::ref_ptr
Definition ref_ptr:32
osgTerrain
Definition Node:29
osgTerrain::computeMasterLocator
OSGTERRAIN_EXPORT const osgTerrain::Locator * computeMasterLocator(const osgTerrain::TerrainTile *tile)
osg::Vec3Array
TemplateArray< Vec3, Array::Vec3ArrayType, 3, GL_FLOAT > Vec3Array
Definition Array:449
OSGTERRAIN_EXPORT
#define OSGTERRAIN_EXPORT
Definition osgTerrain/Export:39
Program
osgTerrain::GeometryPool::GeometryKey
Definition GeometryPool:109
osgTerrain::GeometryPool::GeometryKey::sy
double sy
Definition GeometryPool:130
osgTerrain::GeometryPool::GeometryKey::y
double y
Definition GeometryPool:131
osgTerrain::GeometryPool::GeometryKey::sx
double sx
Definition GeometryPool:129
osgTerrain::GeometryPool::GeometryKey::ny
int ny
Definition GeometryPool:134
osgTerrain::GeometryPool::GeometryKey::nx
int nx
Definition GeometryPool:133
osgTerrain::GeometryPool::GeometryKey::GeometryKey
GeometryKey()
Definition GeometryPool:110