From 1537af623572d1de7a763b69a0d7fea72dfb15a9 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 16 Mar 2010 12:05:41 +0000 Subject: [PATCH] Added osgTerrain::TerrainTile::set/getBlendingPolicy(BlendingPolicy) to enable control over whether the tile should have blending enabled on it. --- include/osgTerrain/TerrainTile | 27 +++++++--- src/osgTerrain/GeometryTechnique.cpp | 75 ++++++++++++++++------------ src/osgTerrain/TerrainTile.cpp | 6 ++- 3 files changed, 69 insertions(+), 39 deletions(-) diff --git a/include/osgTerrain/TerrainTile b/include/osgTerrain/TerrainTile index 6e49527d0..811aa3827 100644 --- a/include/osgTerrain/TerrainTile +++ b/include/osgTerrain/TerrainTile @@ -156,6 +156,21 @@ class OSGTERRAIN_EXPORT TerrainTile : public osg::Group /** Get whether the TeatBoundariesToValidDataAsDefaultValue hint.*/ bool getTreatBoundariesToValidDataAsDefaultValue() const { return _treatBoundariesToValidDataAsDefaultValue; } + + enum BlendingPolicy + { + DO_NOT_SET_BLENDING, + ENABLE_BLENDING, + ENABLE_BLENDING_WHEN_ALPHA_PRESENT /** Default - check colour layers for alpha value and if present enable blending. */ + }; + + /** Set the policy to use when deciding whether to enable/disable blending and use of transparent bin.*/ + void setBlendingPolicy(BlendingPolicy policy) { _blendingPolicy = policy; } + + /** Get the policy to use when deciding whether to enable/disable blending and use of transparent bin.*/ + BlendingPolicy getBlendingPolicy() const { return _blendingPolicy; } + + /** Set the dirty flag on/off.*/ void setDirty(bool dirty); @@ -171,7 +186,7 @@ class OSGTERRAIN_EXPORT TerrainTile : public osg::Group 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& getTileLoadedCallback(); @@ -185,27 +200,27 @@ class OSGTERRAIN_EXPORT TerrainTile : public osg::Group virtual ~TerrainTile(); - typedef std::vector< osg::ref_ptr > Layers; friend class Terrain; Terrain* _terrain; - + bool _dirty; bool _hasBeenTraversal; - + TileID _tileID; osg::ref_ptr _terrainTechnique; osg::ref_ptr _locator; - + osg::ref_ptr _elevationLayer; Layers _colorLayers; - + bool _requiresNormals; bool _treatBoundariesToValidDataAsDefaultValue; + BlendingPolicy _blendingPolicy; }; /** Helper callback for managing optional sets of layers, that loading of is deffered to this callback, diff --git a/src/osgTerrain/GeometryTechnique.cpp b/src/osgTerrain/GeometryTechnique.cpp index f6232523b..09090134a 100644 --- a/src/osgTerrain/GeometryTechnique.cpp +++ b/src/osgTerrain/GeometryTechnique.cpp @@ -107,21 +107,22 @@ void GeometryTechnique::setFilterMatrixAs(FilterType filterType) void GeometryTechnique::init() { - OSG_NOTIFY(osg::INFO)<<"Doing GeometryTechnique::init()"<setThreadSafeRefUnref(true); @@ -140,7 +141,7 @@ Locator* GeometryTechnique::computeMasterLocator() Locator* masterLocator = elevationLocator ? elevationLocator : colorLocator; if (!masterLocator) { - OSG_NOTIFY(osg::NOTICE)<<"Problem, no locator found in any of the terrain layers"< builder = osgDB::Registry::instance()->getKdTreeBuilder()->clone(); buffer._geode->accept(*builder); //osg::Timer_t after = osg::Timer::instance()->tick(); - //OSG_NOTIFY(osg::NOTICE)<<"KdTree build time "<delta_m(before, after)<delta_m(before, after)<s()<<", "<t()<<")"<s()<<", "<t()<<")"<setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); } layerToTextureMap[colorLayer] = texture2D; - // OSG_NOTIFY(osg::NOTICE)<<"Creating new ImageLayer texture "<s()="<s()<<" image->t()="<t()<s()="<s()<<" image->t()="<t()<setTextureAttributeAndModes(layerNum, texture2D, osg::StateAttribute::ON); @@ -801,21 +802,33 @@ void GeometryTechnique::applyColorLayers() void GeometryTechnique::applyTransparency() { - BufferData& buffer = getWriteBuffer(); - - bool containsTransparency = false; - for(unsigned int i=0; i<_terrainTile->getNumColorLayers(); ++i) + if (_terrainTile->getBlendingPolicy()==TerrainTile::DO_NOT_SET_BLENDING) { - osg::Image* image = (_terrainTile->getColorLayer(i)!=0) ? _terrainTile->getColorLayer(i)->getImage() : 0; - if (image) - { - containsTransparency = image->isImageTranslucent(); - break; - } + return; } - - if (containsTransparency) + + bool enableBlending = false; + + if (_terrainTile->getBlendingPolicy()==TerrainTile::ENABLE_BLENDING) { + enableBlending = true; + } + else if (_terrainTile->getBlendingPolicy()==TerrainTile::ENABLE_BLENDING_WHEN_ALPHA_PRESENT) + { + for(unsigned int i=0; i<_terrainTile->getNumColorLayers(); ++i) + { + osg::Image* image = (_terrainTile->getColorLayer(i)!=0) ? _terrainTile->getColorLayer(i)->getImage() : 0; + if (image) + { + enableBlending = image->isImageTranslucent(); + break; + } + } + } + + if (enableBlending) + { + BufferData& buffer = getWriteBuffer(); osg::StateSet* stateset = buffer._geode->getOrCreateStateSet(); stateset->setMode(GL_BLEND, osg::StateAttribute::ON); stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); @@ -885,7 +898,7 @@ void GeometryTechnique::traverse(osg::NodeVisitor& nv) if (_terrainTile->getDirty()) { - OSG_NOTIFY(osg::INFO)<<"******* Doing init ***********"<init(); } diff --git a/src/osgTerrain/TerrainTile.cpp b/src/osgTerrain/TerrainTile.cpp index d1dd836a7..bd8b95094 100644 --- a/src/osgTerrain/TerrainTile.cpp +++ b/src/osgTerrain/TerrainTile.cpp @@ -61,7 +61,8 @@ TerrainTile::TerrainTile(): _dirty(false), _hasBeenTraversal(false), _requiresNormals(true), - _treatBoundariesToValidDataAsDefaultValue(false) + _treatBoundariesToValidDataAsDefaultValue(false), + _blendingPolicy(ENABLE_BLENDING_WHEN_ALPHA_PRESENT) { setThreadSafeRefUnref(true); } @@ -74,7 +75,8 @@ TerrainTile::TerrainTile(const TerrainTile& terrain,const osg::CopyOp& copyop): _elevationLayer(terrain._elevationLayer), _colorLayers(terrain._colorLayers), _requiresNormals(terrain._requiresNormals), - _treatBoundariesToValidDataAsDefaultValue(terrain._treatBoundariesToValidDataAsDefaultValue) + _treatBoundariesToValidDataAsDefaultValue(terrain._treatBoundariesToValidDataAsDefaultValue), + _blendingPolicy(terrain._blendingPolicy) { if (terrain.getTerrainTechnique()) {