Added supoort for transforming layers by an offset and scale

This commit is contained in:
Robert Osfield
2007-05-10 18:07:54 +00:00
parent c1332f6893
commit 8c7b5b5865
3 changed files with 127 additions and 0 deletions

View File

@@ -88,6 +88,9 @@ int main(int argc, char** argv)
bool readParameter = false;
float minValue, maxValue;
float scale = 1.0f;
float offset = 0.0f;
int pos = 1;
while(pos<arguments.argc())
{
@@ -107,6 +110,12 @@ int main(int argc, char** argv)
readParameter = true;
}
else if (arguments.read(pos, "--transform",offset, scale) || arguments.read(pos, "-t",offset, scale))
{
// define the extents.
readParameter = true;
}
else if (arguments.read(pos, "--cartizian",x,y,w,h))
{
// define the extents.
@@ -128,6 +137,11 @@ int main(int argc, char** argv)
hfl->setLocator(locator.get());
if (offset!=0.0f || scale!=1.0f)
{
hfl->transform(offset,scale);
}
terrain->setElevationLayer(hfl.get());
osg::notify(osg::NOTICE)<<"created osgTerrain::HeightFieldLayer"<<std::endl;
@@ -137,6 +151,9 @@ int main(int argc, char** argv)
osg::notify(osg::NOTICE)<<"failed to create osgTerrain::HeightFieldLayer"<<std::endl;
}
scale = 1.0f;
offset = 0.0f;
}
else if (arguments.read(pos, "-d",filename) || arguments.read(pos, "--elevation-image",filename))
@@ -151,6 +168,11 @@ int main(int argc, char** argv)
imageLayer->setImage(image.get());
imageLayer->setLocator(locator.get());
if (offset!=0.0f || scale!=1.0f)
{
imageLayer->transform(offset,scale);
}
terrain->setElevationLayer(imageLayer.get());
osg::notify(osg::NOTICE)<<"created Elevation osgTerrain::ImageLayer"<<std::endl;
@@ -159,6 +181,10 @@ int main(int argc, char** argv)
{
osg::notify(osg::NOTICE)<<"failed to create osgTerrain::ImageLayer"<<std::endl;
}
scale = 1.0f;
offset = 0.0f;
}
else if (arguments.read(pos, "-c",filename) || arguments.read(pos, "--image",filename))
@@ -173,6 +199,11 @@ int main(int argc, char** argv)
imageLayer->setImage(image.get());
imageLayer->setLocator(locator.get());
if (offset!=0.0f || scale!=1.0f)
{
imageLayer->transform(offset,scale);
}
terrain->setColorLayer(layerNum, imageLayer.get());
osg::notify(osg::NOTICE)<<"created Color osgTerrain::ImageLayer"<<std::endl;
@@ -181,6 +212,10 @@ int main(int argc, char** argv)
{
osg::notify(osg::NOTICE)<<"failed to create osgTerrain::ImageLayer"<<std::endl;
}
scale = 1.0f;
offset = 0.0f;
}
else if (arguments.read(pos, "--filter",filterName))

View File

@@ -43,6 +43,8 @@ class OSGTERRAIN_EXPORT Layer : public osg::Object
void setDefaultValue(const osg::Vec4& value) { _defaultValue = value; }
const osg::Vec4& getDefaultValue() const { return _defaultValue; }
virtual bool transform(float offset, float scale) { return false; }
virtual bool getValue(unsigned int /*i*/, unsigned int /*j*/, float& /*value*/) const { return false; }
virtual bool getValue(unsigned int /*i*/, unsigned int /*j*/, osg::Vec2& /*value*/) const { return false; }
@@ -127,6 +129,7 @@ class OSGTERRAIN_EXPORT ImageLayer : public Layer
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
ImageLayer(const ImageLayer& imageLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
virtual bool transform(float offset, float scale);
void setImage(osg::Image* image);
osg::Image* getImage() { return _image.get(); }
@@ -157,6 +160,7 @@ class OSGTERRAIN_EXPORT HeightFieldLayer : public Layer
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
HeightFieldLayer(const HeightFieldLayer& hfLayer,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
virtual bool transform(float offset, float scale);
void setHeightField(osg::HeightField* hf);
osg::HeightField* getHeightField() { return _heightField.get(); }

View File

@@ -78,6 +78,76 @@ void ImageLayer::setImage(osg::Image* image)
_image = image;
}
template <typename T, class O>
void _processRow(unsigned int num, GLenum pixelFormat, T* data,const O& operation)
{
switch(pixelFormat)
{
case(GL_LUMINANCE): { for(unsigned int i=0;i<num;++i) { operation(*data++); } } break;
case(GL_ALPHA): { for(unsigned int i=0;i<num;++i) { operation(*data++); } } break;
case(GL_LUMINANCE_ALPHA): { for(unsigned int i=0;i<num;++i) { operation(*data++); operation(*data++); } } break;
case(GL_RGB): { for(unsigned int i=0;i<num;++i) { operation(*data++); operation(*data++); operation(*data++); } } break;
case(GL_RGBA): { for(unsigned int i=0;i<num;++i) { operation(*data++); operation(*data++); operation(*data++); operation(*data++); } } break;
case(GL_BGR): { for(unsigned int i=0;i<num;++i) { operation(*data++); operation(*data++); operation(*data++); } } break;
case(GL_BGRA): { for(unsigned int i=0;i<num;++i) { operation(*data++); operation(*data++); operation(*data++); operation(*data++); } } break;
}
}
template <class O>
void processRow(unsigned int num, GLenum pixelFormat, GLenum dataType, unsigned char* data, const O& operation)
{
switch(dataType)
{
case(GL_BYTE): _processRow(num,pixelFormat, (char*)data, operation); break;
case(GL_UNSIGNED_BYTE): _processRow(num,pixelFormat, (unsigned char*)data, operation); break;
case(GL_SHORT): _processRow(num,pixelFormat, (short*) data, operation); break;
case(GL_UNSIGNED_SHORT): _processRow(num,pixelFormat, (unsigned short*)data, operation); break;
case(GL_INT): _processRow(num,pixelFormat, (int*) data, operation); break;
case(GL_UNSIGNED_INT): _processRow(num,pixelFormat, (unsigned int*) data, operation); break;
case(GL_FLOAT): _processRow(num,pixelFormat, (float*) data, operation); break;
}
}
template <class O>
void processImage(osg::Image* image, const O& operation)
{
if (!image) return;
for(int r=0;r<image->r();++r)
{
for(int t=0;t<image->t();++t)
{
processRow(image->s(), image->getPixelFormat(), image->getDataType(), image->data(0,t,r), operation);
}
}
}
struct TransformOperator
{
TransformOperator(float offset, float scale):
_offset(offset),
_scale(scale) {}
inline void operator() (unsigned char& v) const { v = (unsigned char)(_offset + (float)v * _scale); }
inline void operator() (unsigned short& v) const { v = (unsigned short)(_offset + (float)v * _scale); }
inline void operator() (unsigned int& v) const { v = (unsigned int)(_offset + (float)v * _scale); }
inline void operator() (char& v) const { v = (char)(_offset + (float)v * _scale); }
inline void operator() (short& v) const { v = (short)(_offset + (float)v * _scale); }
inline void operator() (int& v) const { v = (int)(_offset + (float)v * _scale); }
inline void operator() (float& v) const { v = _offset + v * _scale; }
float _offset, _scale;
};
bool ImageLayer::transform(float offset, float scale)
{
if (!_image.valid()) return false;
processImage(_image.get(), TransformOperator(offset,scale));
return true;
}
bool ImageLayer::getValue(unsigned int i, unsigned int j, float& value) const
{
@@ -159,6 +229,24 @@ void HeightFieldLayer::setHeightField(osg::HeightField* hf)
}
bool HeightFieldLayer::transform(float offset, float scale)
{
if (!_heightField) return false;
osg::FloatArray* heights = _heightField->getFloatArray();
if (!heights) return false;
for(osg::FloatArray::iterator itr = heights->begin();
itr != heights->end();
++itr)
{
*itr = offset + (*itr) * scale;
}
return true;
}
bool HeightFieldLayer::getValue(unsigned int i, unsigned int j, float& value) const
{
value = _heightField->getHeight(i,j);