/* -*-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 #include #include #include #include namespace osgTerrain { extern OSGTERRAIN_EXPORT const osgTerrain::Locator* computeMasterLocator(const osgTerrain::TerrainTile* tile); class OSGTERRAIN_EXPORT SharedGeometry : public osg::Geometry { public: SharedGeometry(); SharedGeometry(const SharedGeometry&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); META_Node(osgTerrain, SharedGeometry); typedef std::vector VertexToHeightFieldMapping; void setVertexToHeightFieldMapping(const VertexToHeightFieldMapping& vthfm) { _vertexToHeightFieldMapping = vthfm; } VertexToHeightFieldMapping& getVertexToHeightFieldMapping() { return _vertexToHeightFieldMapping; } const VertexToHeightFieldMapping& getVertexToHeightFieldMapping() const { return _vertexToHeightFieldMapping; } using osg::Geometry::supports; using osg::Geometry::accept; protected: virtual ~SharedGeometry(); 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 (sxrhs.sx) return false; if (sxrhs.sx) return false; if (yrhs.y) return false; if (nxrhs.nx) return false; return (ny > GeometryMap; virtual bool createKeyForTile(TerrainTile* tile, GeometryKey& key); enum LayerType { HEIGHTFIELD_LAYER, COLOR_LAYER, CONTOUR_LAYER }; typedef std::vector LayerTypes; typedef std::map > ProgramMap; virtual osg::ref_ptr getOrCreateProgram(LayerTypes& layerTypes); virtual osg::ref_ptr getOrCreateGeometry(osgTerrain::TerrainTile* tile); virtual osg::ref_ptr getTileSubgraph(osgTerrain::TerrainTile* tile); virtual void applyLayers(osgTerrain::TerrainTile* tile, osg::StateSet* stateset); void setUseGeometryShader(bool flag) { _useGeometryShader = flag; } bool getUseGeometryShader() const { return _useGeometryShader; } protected: virtual ~GeometryPool(); bool _useGeometryShader; OpenThreads::Mutex _geometryMapMutex; GeometryMap _geometryMap; OpenThreads::Mutex _programMapMutex; ProgramMap _programMap; }; 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 _heightField; osg::ref_ptr _geometry; osg::ref_ptr _vertices; }; } #endif