From 2b84aa77608f2ff06ab289cb3af297c9a3e2e986 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 6 Jul 2007 16:47:08 +0000 Subject: [PATCH] Added dirty count support to osgTerrain::Layer clases. Added LayerHandler to osgterrain example that modifies the layers in response to pressing the 's' and 'q' keys --- examples/osgterrain/osgterrain.cpp | 55 +++++++++++++++++++++++++++++- include/osgTerrain/Layer | 20 ++++++++++- src/osgTerrain/Layer.cpp | 47 ++++++++++++++++++++++++- 3 files changed, 119 insertions(+), 3 deletions(-) diff --git a/examples/osgterrain/osgterrain.cpp b/examples/osgterrain/osgterrain.cpp index a4b4cbb41..bd1764a9b 100644 --- a/examples/osgterrain/osgterrain.cpp +++ b/examples/osgterrain/osgterrain.cpp @@ -122,6 +122,49 @@ protected: +class LayerHandler : public osgGA::GUIEventHandler +{ +public: + + LayerHandler(osgTerrain::Layer* layer): + _layer(layer) {} + + bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa) + { + if (!_layer) return false; + + float scale = 1.2; + + switch(ea.getEventType()) + { + case(osgGA::GUIEventAdapter::KEYDOWN): + { + if (ea.getKey() == 'q') + { + _layer->transform(0.0, scale); + return true; + } + else if (ea.getKey() == 'a') + { + _layer->transform(0.0, 1.0f/scale); + return true; + } + break; + } + default: + break; + } + return false; + + } + +protected: + + osg::observer_ptr _layer; + +}; + + int main(int argc, char** argv) { osg::ArgumentParser arguments(&argc, argv); @@ -174,6 +217,7 @@ int main(int argc, char** argv) osg::ref_ptr terrain = new osgTerrain::TerrainNode; osg::ref_ptr locator = new osgTerrain::EllipsoidLocator(-osg::PI, -osg::PI*0.5, 2.0*osg::PI, osg::PI, 0.0); osg::ref_ptr validDataOperator = new osgTerrain::NoDataValue(0.0); + osg::ref_ptr lastAppliedLayer; unsigned int layerNum = 0; @@ -184,6 +228,7 @@ int main(int argc, char** argv) float scale = 1.0f; float offset = 0.0f; + int pos = 1; while(possetElevationLayer(hfl.get()); + lastAppliedLayer = hfl.get(); + osg::notify(osg::NOTICE)<<"created osgTerrain::HeightFieldLayer"<setElevationLayer(imageLayer.get()); + lastAppliedLayer = imageLayer.get(); + osg::notify(osg::NOTICE)<<"created Elevation osgTerrain::ImageLayer"<setColorLayer(layerNum, imageLayer.get()); - + + lastAppliedLayer = imageLayer.get(); + osg::notify(osg::NOTICE)<<"created Color osgTerrain::ImageLayer"<setTerrainTechnique(geometryTechnique.get()); viewer.addEventHandler(new FilterHandler(geometryTechnique.get())); + + viewer.addEventHandler(new LayerHandler(lastAppliedLayer.get())); if (!terrain) return 0; diff --git a/include/osgTerrain/Layer b/include/osgTerrain/Layer index 027f3b4d3..cf345bac8 100644 --- a/include/osgTerrain/Layer +++ b/include/osgTerrain/Layer @@ -141,6 +141,15 @@ class OSGTERRAIN_EXPORT Layer : public osg::Object return false; } + /** increment the modified count."*/ + virtual void dirty() {}; + + /** Set the modified count value. */ + virtual void setModifiedCount(unsigned int /*value*/) {}; + + /** Get modified count value. */ + virtual unsigned int getModifiedCount() const { return 0; } + virtual osg::BoundingSphere computeBound() const; protected: @@ -176,6 +185,10 @@ class OSGTERRAIN_EXPORT ImageLayer : public Layer virtual bool getValue(unsigned int i, unsigned int j, osg::Vec3& value) const; virtual bool getValue(unsigned int i, unsigned int j, osg::Vec4& value) const; + virtual void dirty(); + virtual void setModifiedCount(unsigned int value); + virtual unsigned int getModifiedCount() const; + protected: virtual ~ImageLayer() {} @@ -207,11 +220,16 @@ class OSGTERRAIN_EXPORT HeightFieldLayer : public Layer virtual bool getValue(unsigned int i, unsigned int j, osg::Vec3& value) const; virtual bool getValue(unsigned int i, unsigned int j, osg::Vec4& value) const; + virtual void dirty(); + virtual void setModifiedCount(unsigned int value); + virtual unsigned int getModifiedCount() const; + protected: virtual ~HeightFieldLayer() {} - osg::ref_ptr _heightField; + unsigned int _modifiedCount; + osg::ref_ptr _heightField; }; diff --git a/src/osgTerrain/Layer.cpp b/src/osgTerrain/Layer.cpp index 143179a15..3d94e98c6 100644 --- a/src/osgTerrain/Layer.cpp +++ b/src/osgTerrain/Layer.cpp @@ -144,7 +144,11 @@ bool ImageLayer::transform(float offset, float scale) { if (!_image.valid()) return false; + osg::notify(osg::NOTICE)<<"ImageLayer::transform("<dirty(); +} + +void ImageLayer::setModifiedCount(unsigned int value) +{ + if (!_image) return; + else _image->setModifiedCount(value); +} + +unsigned int ImageLayer::getModifiedCount() const +{ + if (!_image) return 0; + else return _image->getModifiedCount(); +} + + ///////////////////////////////////////////////////////////////////////////// // // HieghtFieldLayer // -HeightFieldLayer::HeightFieldLayer() +HeightFieldLayer::HeightFieldLayer(): + _modifiedCount(0) { } HeightFieldLayer::HeightFieldLayer(const HeightFieldLayer& hfLayer,const osg::CopyOp& copyop): Layer(hfLayer,copyop), + _modifiedCount(0), _heightField(hfLayer._heightField) { + if (_heightField.valid()) ++_modifiedCount; } void HeightFieldLayer::setHeightField(osg::HeightField* hf) { _heightField = hf; + dirty(); } @@ -237,12 +263,16 @@ bool HeightFieldLayer::transform(float offset, float scale) osg::FloatArray* heights = _heightField->getFloatArray(); if (!heights) return false; + osg::notify(osg::NOTICE)<<"HeightFieldLayer::transform("<begin(); itr != heights->end(); ++itr) { *itr = offset + (*itr) * scale; } + + dirty(); return true; } @@ -276,3 +306,18 @@ bool HeightFieldLayer::getValue(unsigned int i, unsigned int j, osg::Vec4& value value.w() = _defaultValue.w(); return true; } + +void HeightFieldLayer::dirty() +{ + ++_modifiedCount; +} + +void HeightFieldLayer::setModifiedCount(unsigned int value) +{ + _modifiedCount = value; +} + +unsigned int HeightFieldLayer::getModifiedCount() const +{ + return _modifiedCount; +}