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
This commit is contained in:
@@ -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<osgTerrain::Layer> _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<osgTerrain::TerrainNode> terrain = new osgTerrain::TerrainNode;
|
||||
osg::ref_ptr<osgTerrain::Locator> locator = new osgTerrain::EllipsoidLocator(-osg::PI, -osg::PI*0.5, 2.0*osg::PI, osg::PI, 0.0);
|
||||
osg::ref_ptr<osgTerrain::ValidDataOperator> validDataOperator = new osgTerrain::NoDataValue(0.0);
|
||||
osg::ref_ptr<osgTerrain::Layer> 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(pos<arguments.argc())
|
||||
{
|
||||
@@ -243,6 +288,8 @@ int main(int argc, char** argv)
|
||||
|
||||
terrain->setElevationLayer(hfl.get());
|
||||
|
||||
lastAppliedLayer = hfl.get();
|
||||
|
||||
osg::notify(osg::NOTICE)<<"created osgTerrain::HeightFieldLayer"<<std::endl;
|
||||
}
|
||||
else
|
||||
@@ -275,6 +322,8 @@ int main(int argc, char** argv)
|
||||
|
||||
terrain->setElevationLayer(imageLayer.get());
|
||||
|
||||
lastAppliedLayer = imageLayer.get();
|
||||
|
||||
osg::notify(osg::NOTICE)<<"created Elevation osgTerrain::ImageLayer"<<std::endl;
|
||||
}
|
||||
else
|
||||
@@ -306,7 +355,9 @@ int main(int argc, char** argv)
|
||||
}
|
||||
|
||||
terrain->setColorLayer(layerNum, imageLayer.get());
|
||||
|
||||
|
||||
lastAppliedLayer = imageLayer.get();
|
||||
|
||||
osg::notify(osg::NOTICE)<<"created Color osgTerrain::ImageLayer"<<std::endl;
|
||||
}
|
||||
else
|
||||
@@ -373,6 +424,8 @@ int main(int argc, char** argv)
|
||||
terrain->setTerrainTechnique(geometryTechnique.get());
|
||||
|
||||
viewer.addEventHandler(new FilterHandler(geometryTechnique.get()));
|
||||
|
||||
viewer.addEventHandler(new LayerHandler(lastAppliedLayer.get()));
|
||||
|
||||
if (!terrain) return 0;
|
||||
|
||||
|
||||
@@ -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<osg::HeightField> _heightField;
|
||||
unsigned int _modifiedCount;
|
||||
osg::ref_ptr<osg::HeightField> _heightField;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -144,7 +144,11 @@ bool ImageLayer::transform(float offset, float scale)
|
||||
{
|
||||
if (!_image.valid()) return false;
|
||||
|
||||
osg::notify(osg::NOTICE)<<"ImageLayer::transform("<<offset<<","<<scale<<")"<<std::endl;;
|
||||
|
||||
processImage(_image.get(), TransformOperator(offset,scale));
|
||||
|
||||
dirty();
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -208,24 +212,46 @@ bool ImageLayer::getValue(unsigned int i, unsigned int j, osg::Vec4& value) cons
|
||||
return false;
|
||||
}
|
||||
|
||||
void ImageLayer::dirty()
|
||||
{
|
||||
if (_image.valid()) _image->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("<<offset<<","<<scale<<")"<<std::endl;;
|
||||
|
||||
for(osg::FloatArray::iterator itr = heights->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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user