diff --git a/include/osg/Shape b/include/osg/Shape index aba762dc7..482e4db31 100644 --- a/include/osg/Shape +++ b/include/osg/Shape @@ -478,25 +478,9 @@ class OSG_EXPORT HeightField : public Shape { public: - HeightField(): - _columns(0), - _rows(0), - _origin(0.0f,0.0f,0.0f), - _dx(1.0f), - _dy(1.0f), - _skirtHeight(0.0f), - _borderWidth(0) {} + HeightField(); - HeightField(const HeightField& mesh,const CopyOp& copyop=CopyOp::SHALLOW_COPY): - Shape(mesh,copyop), - _columns(mesh._columns), - _rows(mesh._rows), - _origin(mesh._origin), - _dx(mesh._dx), - _dy(mesh._dy), - _skirtHeight(mesh._skirtHeight), - _borderWidth(mesh._borderWidth), - _heights(mesh._heights) {} + HeightField(const HeightField& mesh,const CopyOp& copyop=CopyOp::SHALLOW_COPY); META_Shape(osg, HeightField); @@ -515,6 +499,16 @@ class OSG_EXPORT HeightField : public Shape inline void setYInterval(float dy) { _dy = dy; } inline float getYInterval() const { return _dy; } + + /** Get the FloatArray height data.*/ + osg::FloatArray* getFloatArray() { return _heights.get(); } + + /** Get the const sFloatArray height data.*/ + const osg::FloatArray* getFloatArray() const { return _heights.get(); } + + HeightList& getHeightList() { return *_heights; } + + const HeightList& getHeightList() const { return *_heights; } /** Set the height of the skirt to render around the edge of HeightField. * The skirt is used as a means of disguising edge boundaries between adjacent HeightField, @@ -540,29 +534,26 @@ class OSG_EXPORT HeightField : public Shape /* set a single height point in the height field */ inline void setHeight(unsigned int c,unsigned int r,float value) { - _heights[c+r*_columns] = value; + (*_heights)[c+r*_columns] = value; } /* Get address of single height point in the height field, allows user to change. */ inline float& getHeight(unsigned int c,unsigned int r) { - return _heights[c+r*_columns]; + return (*_heights)[c+r*_columns]; } /* Get value of single height point in the height field, not editable. */ inline float getHeight(unsigned int c,unsigned int r) const { - return _heights[c+r*_columns]; + return (*_heights)[c+r*_columns]; } - HeightList& getHeightList() { return _heights; } - const HeightList& getHeightList() const { return _heights; } - inline Vec3 getVertex(unsigned int c,unsigned int r) const { return Vec3(_origin.x()+getXInterval()*(float)c, _origin.y()+getYInterval()*(float)r, - _origin.z()+_heights[c+r*_columns]); + _origin.z()+(*_heights)[c+r*_columns]); } Vec3 getNormal(unsigned int c,unsigned int r) const; @@ -573,17 +564,17 @@ class OSG_EXPORT HeightField : public Shape virtual ~HeightField(); - unsigned int _columns,_rows; + unsigned int _columns,_rows; - osg::Vec3 _origin; - float _dx; - float _dy; + osg::Vec3 _origin; + float _dx; + float _dy; - float _skirtHeight; - unsigned int _borderWidth; + float _skirtHeight; + unsigned int _borderWidth; - Quat _rotation; - HeightList _heights; + Quat _rotation; + osg::ref_ptr _heights; }; diff --git a/include/osgTerrain/Layer b/include/osgTerrain/Layer index 117731ed5..22a09aa98 100644 --- a/include/osgTerrain/Layer +++ b/include/osgTerrain/Layer @@ -40,6 +40,72 @@ class OSGTERRAIN_EXPORT Layer : public osg::Object const Locator* getLocator() const { return _locator.get(); } + virtual unsigned int getNumColumns() const { return 0; } + virtual unsigned int getNumRows() const { return 0; } + + 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; } + virtual bool getValue(unsigned int /*i*/, unsigned int /*j*/, osg::Vec3& /*value*/) const { return false; } + virtual bool getValue(unsigned int /*i*/, unsigned int /*j*/, osg::Vec4& /*value*/) const { return false; } + + inline void computeIndices(double ndc_x, double ndc_y, unsigned int& i, unsigned int& j, double& ir, double& jr) + { + ndc_x *= double(getNumColumns()-1); + ndc_y *= double(getNumRows()-1); + i = (unsigned int)(ndc_x); + j = (unsigned int)(ndc_y); + ir = ndc_x - double(i); + jr = ndc_y - double(j); + } + + + inline bool getInterpolatedValue(double ndc_x, double ndc_y, float& value) + { + unsigned int i,j; + double ir, jr; + computeIndices(ndc_x, ndc_y, i, j, ir, jr); + value = 0.0f; + double div = 0.0f; + float v,r; + + r = (1.0f-ir)*(1.0f-jr); + if (r>0.0 && getValue(i,j,v)) + { + value += v*r; + div += r; + } + + r = (ir)*(1.0f-jr); + if (r>0.0 && getValue(i+1,j,v)) + { + value += v*r; + div += r; + } + + r = (ir)*(jr); + if (r>0.0 && getValue(i+1,j+1,v)) + { + value += v*r; + div += r; + } + + r = (1.0f-ir)*(jr); + if (r>0.0 && getValue(i,j+1,v)) + { + value += v*r; + div += r; + } + + if (div != 0.0) + { + value /= div; + return true; + } + + value = 0.0; + return false; + } + virtual osg::BoundingSphere computeBound() const; protected: @@ -61,11 +127,17 @@ class OSGTERRAIN_EXPORT ImageLayer : public Layer void setImage(osg::Image* image); - osg::Image* getImage() { return _image.get(); } - const osg::Image* getImage() const { return _image.get(); } + virtual unsigned int getNumColumns() const { return _image.valid() ? _image->s() : 0; } + virtual unsigned int getNumRows() const { return _image.valid() ? _image->t() : 0; } + + virtual bool getValue(unsigned int i, unsigned int j, float& value) const; + virtual bool getValue(unsigned int i, unsigned int j, osg::Vec2& value) const; + 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; + protected: virtual ~ImageLayer() {} @@ -85,11 +157,17 @@ class OSGTERRAIN_EXPORT HeightFieldLayer : public Layer void setHeightField(osg::HeightField* hf); - osg::HeightField* getHeightField() { return _heightField.get(); } - const osg::HeightField* getHeightField() const { return _heightField.get(); } + virtual unsigned int getNumColumns() const { return _heightField.valid() ? _heightField->getNumColumns() : 0; } + virtual unsigned int getNumRows() const { return _heightField.valid() ? _heightField->getNumRows() : 0; } + + virtual bool getValue(unsigned int i, unsigned int j, float& value) const; + virtual bool getValue(unsigned int i, unsigned int j, osg::Vec2& value) const; + 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; + protected: virtual ~HeightFieldLayer() {} @@ -98,31 +176,6 @@ class OSGTERRAIN_EXPORT HeightFieldLayer : public Layer }; - -class OSGTERRAIN_EXPORT ArrayLayer : public Layer -{ - public: - - ArrayLayer(); - - /** Copy constructor using CopyOp to manage deep vs shallow copy.*/ - ArrayLayer(const ArrayLayer&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); - - - void setArray(osg::Array* array); - - osg::Array* getArray() { return _array.get(); } - - const osg::Array* getArray() const { return _array.get(); } - - protected: - - virtual ~ArrayLayer() {} - - osg::ref_ptr _array; - -}; - } #endif diff --git a/src/osg/Shape.cpp b/src/osg/Shape.cpp index fa7684d0e..2aafaf376 100644 --- a/src/osg/Shape.cpp +++ b/src/osg/Shape.cpp @@ -19,6 +19,31 @@ Shape::~Shape() { } +HeightField::HeightField(): + _columns(0), + _rows(0), + _origin(0.0f,0.0f,0.0f), + _dx(1.0f), + _dy(1.0f), + _skirtHeight(0.0f), + _borderWidth(0) +{ + _heights = new osg::FloatArray; +} + +HeightField::HeightField(const HeightField& mesh,const CopyOp& copyop): + Shape(mesh,copyop), + _columns(mesh._columns), + _rows(mesh._rows), + _origin(mesh._origin), + _dx(mesh._dx), + _dy(mesh._dy), + _skirtHeight(mesh._skirtHeight), + _borderWidth(mesh._borderWidth), + _heights(new osg::FloatArray(*mesh._heights)) +{ +} + HeightField::~HeightField() { } @@ -28,7 +53,7 @@ void HeightField::allocate(unsigned int numColumns,unsigned int numRows) { if (_columns!=numColumns || _rows!=numRows) { - _heights.resize(numColumns*numRows); + _heights->resize(numColumns*numRows); } _columns=numColumns; _rows=numRows; diff --git a/src/osgTerrain/Layer.cpp b/src/osgTerrain/Layer.cpp index 3e4ca0d4b..f574f44ed 100644 --- a/src/osgTerrain/Layer.cpp +++ b/src/osgTerrain/Layer.cpp @@ -12,6 +12,7 @@ */ #include +#include using namespace osgTerrain; @@ -78,6 +79,30 @@ void ImageLayer::setImage(osg::Image* image) } +bool ImageLayer::getValue(unsigned int i, unsigned int j, float& value) const +{ + osg::notify(osg::NOTICE)<<"Not implemented yet"< #include -#include #include #include #include #include #include +#include +#include +#include #include #include @@ -27,35 +29,6 @@ #undef OUT #endif -BEGIN_OBJECT_REFLECTOR(osgTerrain::ArrayLayer) - I_BaseType(osgTerrain::Layer); - I_Constructor0(____ArrayLayer, - "", - ""); - I_ConstructorWithDefaults2(IN, const osgTerrain::ArrayLayer &, x, , IN, const osg::CopyOp &, copyop, osg::CopyOp::SHALLOW_COPY, - ____ArrayLayer__C5_ArrayLayer_R1__C5_osg_CopyOp_R1, - "Copy constructor using CopyOp to manage deep vs shallow copy. ", - ""); - I_Method1(void, setArray, IN, osg::Array *, array, - Properties::NON_VIRTUAL, - __void__setArray__osg_Array_P1, - "", - ""); - I_Method0(osg::Array *, getArray, - Properties::NON_VIRTUAL, - __osg_Array_P1__getArray, - "", - ""); - I_Method0(const osg::Array *, getArray, - Properties::NON_VIRTUAL, - __C5_osg_Array_P1__getArray, - "", - ""); - I_SimpleProperty(osg::Array *, Array, - __osg_Array_P1__getArray, - __void__setArray__osg_Array_P1); -END_REFLECTOR - BEGIN_OBJECT_REFLECTOR(osgTerrain::HeightFieldLayer) I_BaseType(osgTerrain::Layer); I_Constructor0(____HeightFieldLayer, @@ -80,6 +53,36 @@ BEGIN_OBJECT_REFLECTOR(osgTerrain::HeightFieldLayer) __C5_osg_HeightField_P1__getHeightField, "", ""); + I_Method0(unsigned int, getNumColumns, + Properties::VIRTUAL, + __unsigned_int__getNumColumns, + "", + ""); + I_Method0(unsigned int, getNumRows, + Properties::VIRTUAL, + __unsigned_int__getNumRows, + "", + ""); + I_Method3(bool, getValue, IN, unsigned int, i, IN, unsigned int, j, IN, float &, value, + Properties::VIRTUAL, + __bool__getValue__unsigned_int__unsigned_int__float_R1, + "", + ""); + I_Method3(bool, getValue, IN, unsigned int, i, IN, unsigned int, j, IN, osg::Vec2 &, value, + Properties::VIRTUAL, + __bool__getValue__unsigned_int__unsigned_int__osg_Vec2_R1, + "", + ""); + I_Method3(bool, getValue, IN, unsigned int, i, IN, unsigned int, j, IN, osg::Vec3 &, value, + Properties::VIRTUAL, + __bool__getValue__unsigned_int__unsigned_int__osg_Vec3_R1, + "", + ""); + I_Method3(bool, getValue, IN, unsigned int, i, IN, unsigned int, j, IN, osg::Vec4 &, value, + Properties::VIRTUAL, + __bool__getValue__unsigned_int__unsigned_int__osg_Vec4_R1, + "", + ""); I_SimpleProperty(osg::HeightField *, HeightField, __osg_HeightField_P1__getHeightField, __void__setHeightField__osg_HeightField_P1); @@ -109,6 +112,36 @@ BEGIN_OBJECT_REFLECTOR(osgTerrain::ImageLayer) __C5_osg_Image_P1__getImage, "", ""); + I_Method0(unsigned int, getNumColumns, + Properties::VIRTUAL, + __unsigned_int__getNumColumns, + "", + ""); + I_Method0(unsigned int, getNumRows, + Properties::VIRTUAL, + __unsigned_int__getNumRows, + "", + ""); + I_Method3(bool, getValue, IN, unsigned int, i, IN, unsigned int, j, IN, float &, value, + Properties::VIRTUAL, + __bool__getValue__unsigned_int__unsigned_int__float_R1, + "", + ""); + I_Method3(bool, getValue, IN, unsigned int, i, IN, unsigned int, j, IN, osg::Vec2 &, value, + Properties::VIRTUAL, + __bool__getValue__unsigned_int__unsigned_int__osg_Vec2_R1, + "", + ""); + I_Method3(bool, getValue, IN, unsigned int, i, IN, unsigned int, j, IN, osg::Vec3 &, value, + Properties::VIRTUAL, + __bool__getValue__unsigned_int__unsigned_int__osg_Vec3_R1, + "", + ""); + I_Method3(bool, getValue, IN, unsigned int, i, IN, unsigned int, j, IN, osg::Vec4 &, value, + Properties::VIRTUAL, + __bool__getValue__unsigned_int__unsigned_int__osg_Vec4_R1, + "", + ""); I_SimpleProperty(osg::Image *, Image, __osg_Image_P1__getImage, __void__setImage__osg_Image_P1); @@ -163,6 +196,46 @@ BEGIN_OBJECT_REFLECTOR(osgTerrain::Layer) __C5_Locator_P1__getLocator, "", ""); + I_Method0(unsigned int, getNumColumns, + Properties::VIRTUAL, + __unsigned_int__getNumColumns, + "", + ""); + I_Method0(unsigned int, getNumRows, + Properties::VIRTUAL, + __unsigned_int__getNumRows, + "", + ""); + I_Method3(bool, getValue, IN, unsigned, int, IN, unsigned, int, IN, float &, x, + Properties::VIRTUAL, + __bool__getValue__unsigned__unsigned__float_R1, + "", + ""); + I_Method3(bool, getValue, IN, unsigned, int, IN, unsigned, int, IN, osg::Vec2 &, x, + Properties::VIRTUAL, + __bool__getValue__unsigned__unsigned__osg_Vec2_R1, + "", + ""); + I_Method3(bool, getValue, IN, unsigned, int, IN, unsigned, int, IN, osg::Vec3 &, x, + Properties::VIRTUAL, + __bool__getValue__unsigned__unsigned__osg_Vec3_R1, + "", + ""); + I_Method3(bool, getValue, IN, unsigned, int, IN, unsigned, int, IN, osg::Vec4 &, x, + Properties::VIRTUAL, + __bool__getValue__unsigned__unsigned__osg_Vec4_R1, + "", + ""); + I_Method6(void, computeIndices, IN, double, ndc_x, IN, double, ndc_y, IN, unsigned int &, i, IN, unsigned int &, j, IN, double &, ir, IN, double &, jr, + Properties::NON_VIRTUAL, + __void__computeIndices__double__double__unsigned_int_R1__unsigned_int_R1__double_R1__double_R1, + "", + ""); + I_Method3(bool, getInterpolatedValue, IN, double, ndc_x, IN, double, ndc_y, IN, float &, value, + Properties::NON_VIRTUAL, + __bool__getInterpolatedValue__double__double__float_R1, + "", + ""); I_Method0(osg::BoundingSphere, computeBound, Properties::VIRTUAL, __osg_BoundingSphere__computeBound,