From 15fea448d847eb2c74ca74093954be0f9402104b Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 21 Jun 2013 09:43:15 +0000 Subject: [PATCH] Added new osg::Array::Binding parameter to osg::Geometry::set*Array(osg::Array* array, osg::Array::Binding binding=osg::Array::BIND_UNDEFINED) methods to make it easier for the user to set the binding without the need to call the deprecated osg::Geometry::set*Binding(osg::Geometry::AttributeBinding) --- examples/osggeometry/osggeometry.cpp | 48 ++++------- include/osg/Geometry | 26 +++--- src/osg/Geometry.cpp | 114 +++++++++++++++------------ 3 files changed, 92 insertions(+), 96 deletions(-) diff --git a/examples/osggeometry/osggeometry.cpp b/examples/osggeometry/osggeometry.cpp index 5af88e367..81460cbbd 100644 --- a/examples/osggeometry/osggeometry.cpp +++ b/examples/osggeometry/osggeometry.cpp @@ -121,15 +121,13 @@ osg::Node* createScene() // pass the color array to points geometry, note the binding to tell the geometry // that only use one color for the whole object. - pointsGeom->setColorArray(colors); - pointsGeom->setColorBinding(osg::Geometry::BIND_OVERALL); + pointsGeom->setColorArray(colors, osg::Array::BIND_OVERALL); // set the normal in the same way color. osg::Vec3Array* normals = new osg::Vec3Array; normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f)); - pointsGeom->setNormalArray(normals); - pointsGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); + pointsGeom->setNormalArray(normals, osg::Array::BIND_OVERALL); // create and add a DrawArray Primitive (see include/osg/Primitive). The first @@ -169,15 +167,13 @@ osg::Node* createScene() // set the colors as before, plus using the above osg::Vec4Array* colors = new osg::Vec4Array; colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); - linesGeom->setColorArray(colors); - linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL); + linesGeom->setColorArray(colors, osg::Array::BIND_OVERALL); // set the normal in the same way color. osg::Vec3Array* normals = new osg::Vec3Array; normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f)); - linesGeom->setNormalArray(normals); - linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); + linesGeom->setNormalArray(normals, osg::Array::BIND_OVERALL); // This time we simply use primitive, and hardwire the number of coords to use @@ -211,15 +207,13 @@ osg::Node* createScene() // set the colors as before, plus using the above osg::Vec4Array* colors = new osg::Vec4Array; colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); - linesGeom->setColorArray(colors); - linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL); + linesGeom->setColorArray(colors, osg::Array::BIND_OVERALL); // set the normal in the same way color. osg::Vec3Array* normals = new osg::Vec3Array; normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f)); - linesGeom->setNormalArray(normals); - linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); + linesGeom->setNormalArray(normals, osg::Array::BIND_OVERALL); // This time we simply use primitive, and hardwire the number of coords to use @@ -258,15 +252,13 @@ osg::Node* createScene() // set the colors as before, plus using the above osg::Vec4Array* colors = new osg::Vec4Array; colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); - linesGeom->setColorArray(colors); - linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL); + linesGeom->setColorArray(colors, osg::Array::BIND_OVERALL); // set the normal in the same way color. osg::Vec3Array* normals = new osg::Vec3Array; normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f)); - linesGeom->setNormalArray(normals); - linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); + linesGeom->setNormalArray(normals, osg::Array::BIND_OVERALL); // This time we simply use primitive, and hardwire the number of coords to use @@ -332,13 +324,11 @@ osg::Node* createScene() polyGeom->setVertexArray(vertices); // use the shared color array. - polyGeom->setColorArray(shared_colors.get()); - polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); + polyGeom->setColorArray(shared_colors.get(), osg::Array::BIND_OVERALL); // use the shared normal array. - polyGeom->setNormalArray(shared_normals.get()); - polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); + polyGeom->setNormalArray(shared_normals.get(), osg::Array::BIND_OVERALL); // This time we simply use primitive, and hardwire the number of coords to use @@ -379,13 +369,11 @@ osg::Node* createScene() polyGeom->setVertexArray(vertices); // use the shared color array. - polyGeom->setColorArray(shared_colors.get()); - polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); + polyGeom->setColorArray(shared_colors.get(), osg::Array::BIND_OVERALL); // use the shared normal array. - polyGeom->setNormalArray(shared_normals.get()); - polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); + polyGeom->setNormalArray(shared_normals.get(), osg::Array::BIND_OVERALL); // This time we simply use primitive, and hardwire the number of coords to use @@ -428,13 +416,11 @@ osg::Node* createScene() polyGeom->setVertexArray(vertices); // use the shared color array. - polyGeom->setColorArray(shared_colors.get()); - polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); + polyGeom->setColorArray(shared_colors.get(), osg::Array::BIND_OVERALL); // use the shared normal array. - polyGeom->setNormalArray(shared_normals.get()); - polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); + polyGeom->setNormalArray(shared_normals.get(), osg::Array::BIND_OVERALL); // This time we simply use primitive, and hardwire the number of coords to use @@ -496,13 +482,11 @@ osg::Node* createScene() polyGeom->setVertexArray(vertices); // use the shared color array. - polyGeom->setColorArray(shared_colors.get()); - polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); + polyGeom->setColorArray(shared_colors.get(), osg::Array::BIND_OVERALL); // use the shared normal array. - polyGeom->setNormalArray(shared_normals.get()); - polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); + polyGeom->setNormalArray(shared_normals.get(), osg::Array::BIND_OVERALL); // This time we simply use primitive, and hardwire the number of coords to use diff --git a/include/osg/Geometry b/include/osg/Geometry index 09e4e3185..46a48b53c 100644 --- a/include/osg/Geometry +++ b/include/osg/Geometry @@ -50,29 +50,29 @@ class OSG_EXPORT Geometry : public Drawable const Array* getVertexArray() const { return _vertexArray.get(); } - void setNormalArray(Array* array); + void setNormalArray(Array* array, osg::Array::Binding binding=osg::Array::BIND_UNDEFINED); Array* getNormalArray() { return _normalArray.get(); } const Array* getNormalArray() const { return _normalArray.get(); } - - void setColorArray(Array* array); + + void setColorArray(Array* array, osg::Array::Binding binding=osg::Array::BIND_UNDEFINED); Array* getColorArray() { return _colorArray.get(); } const Array* getColorArray() const { return _colorArray.get(); } - void setSecondaryColorArray(Array* array); + void setSecondaryColorArray(Array* array, osg::Array::Binding binding=osg::Array::BIND_UNDEFINED); Array* getSecondaryColorArray() { return _secondaryColorArray.get(); } const Array* getSecondaryColorArray() const { return _secondaryColorArray.get(); } - void setFogCoordArray(Array* array); + void setFogCoordArray(Array* array, osg::Array::Binding binding=osg::Array::BIND_UNDEFINED); Array* getFogCoordArray() { return _fogCoordArray.get(); } const Array* getFogCoordArray() const { return _fogCoordArray.get(); } - void setTexCoordArray(unsigned int unit,Array*); + void setTexCoordArray(unsigned int unit,Array* array, osg::Array::Binding binding=osg::Array::BIND_UNDEFINED); Array* getTexCoordArray(unsigned int unit); const Array* getTexCoordArray(unsigned int unit) const; @@ -82,7 +82,7 @@ class OSG_EXPORT Geometry : public Drawable - void setVertexAttribArray(unsigned int index,Array* array); + void setVertexAttribArray(unsigned int index, Array* array, osg::Array::Binding binding=osg::Array::BIND_UNDEFINED); Array *getVertexAttribArray(unsigned int index); const Array *getVertexAttribArray(unsigned int index) const; @@ -214,12 +214,12 @@ class OSG_EXPORT Geometry : public Drawable osg::ref_ptr _fogCoordArray; ArrayList _texCoordList; ArrayList _vertexAttribList; - + bool _containsDeprecatedData; - + public: - - + + /** Return true if the deprecated use array indicies or BIND_PER_PRIMITIVE binding has been assigned to arrays.*/ bool containsDeprecatedData() const { return _containsDeprecatedData; } @@ -293,7 +293,7 @@ class OSG_EXPORT Geometry : public Drawable /** no longer supported.*/ inline void setVertexAttribIndices(unsigned int index,IndexArray* array); inline const IndexArray* getVertexAttribIndices(unsigned int index) const; -#endif +#endif }; /** Convenience function to be used for creating quad geometry with texture coords. @@ -392,7 +392,7 @@ inline const IndexArray* Geometry::getVertexAttribIndices(unsigned int index) co else return 0; } #endif - + } #endif diff --git a/src/osg/Geometry.cpp b/src/osg/Geometry.cpp index 66008fe52..36f99e35c 100644 --- a/src/osg/Geometry.cpp +++ b/src/osg/Geometry.cpp @@ -83,7 +83,7 @@ Geometry::~Geometry() // no need to delete, all automatically handled by ref_ptr :-) } -#define ARRAY_NOT_EMPTY(array) (array!=0 && array->getNumElements()!=0) +#define ARRAY_NOT_EMPTY(array) (array!=0 && array->getNumElements()!=0) bool Geometry::empty() const { @@ -100,6 +100,8 @@ bool Geometry::empty() const void Geometry::setVertexArray(Array* array) { + if (array && array->getBinding()==osg::Array::BIND_UNDEFINED) array->setBinding(osg::Array::BIND_PER_VERTEX); + _vertexArray = array; dirtyDisplayList(); @@ -108,8 +110,10 @@ void Geometry::setVertexArray(Array* array) if (_useVertexBufferObjects && array) addVertexBufferObjectIfRequired(array); } -void Geometry::setNormalArray(Array* array) +void Geometry::setNormalArray(Array* array, osg::Array::Binding binding) { + if (array && binding!=osg::Array::BIND_UNDEFINED) array->setBinding(binding); + _normalArray = array; dirtyDisplayList(); @@ -117,8 +121,10 @@ void Geometry::setNormalArray(Array* array) if (_useVertexBufferObjects && array) addVertexBufferObjectIfRequired(array); } -void Geometry::setColorArray(Array* array) +void Geometry::setColorArray(Array* array, osg::Array::Binding binding) { + if (array && binding!=osg::Array::BIND_UNDEFINED) array->setBinding(binding); + _colorArray = array; dirtyDisplayList(); @@ -126,8 +132,10 @@ void Geometry::setColorArray(Array* array) if (_useVertexBufferObjects && array) addVertexBufferObjectIfRequired(array); } -void Geometry::setSecondaryColorArray(Array* array) +void Geometry::setSecondaryColorArray(Array* array, osg::Array::Binding binding) { + if (array && binding!=osg::Array::BIND_UNDEFINED) array->setBinding(binding); + _secondaryColorArray = array; dirtyDisplayList(); @@ -135,8 +143,10 @@ void Geometry::setSecondaryColorArray(Array* array) if (_useVertexBufferObjects && array) addVertexBufferObjectIfRequired(array); } -void Geometry::setFogCoordArray(Array* array) +void Geometry::setFogCoordArray(Array* array, osg::Array::Binding binding) { + if (array && binding!=osg::Array::BIND_UNDEFINED) array->setBinding(binding); + _fogCoordArray = array; dirtyDisplayList(); @@ -154,18 +164,20 @@ void Geometry::setFogCoordArray(Array* array) if (array->getBinding() == static_cast(ab)) return; \ array->setBinding(static_cast(ab));\ if (ab==3 /*osg::Geometry::BIND_PER_PRIMITIVE*/) _containsDeprecatedData = true; - -#define GET_BINDING(array) (array!=0 ? static_cast(array->getBinding()) : BIND_OFF) + +#define GET_BINDING(array) (array!=0 ? static_cast(array->getBinding()) : BIND_OFF) -void Geometry::setTexCoordArray(unsigned int index,Array* array) +void Geometry::setTexCoordArray(unsigned int index,Array* array, osg::Array::Binding binding) { if (_texCoordList.size()<=index) _texCoordList.resize(index+1); + if (array && binding!=osg::Array::BIND_UNDEFINED) array->setBinding(binding); + _texCoordList[index] = array; // do we set to array BIND_PER_VERTEX? @@ -191,7 +203,7 @@ const Array* Geometry::getTexCoordArray(unsigned int index) const } -void Geometry::setVertexAttribArray(unsigned int index, Array* array) +void Geometry::setVertexAttribArray(unsigned int index, Array* array, osg::Array::Binding binding) { if (_vertexAttribList.size()<=index) _vertexAttribList.resize(index+1); @@ -696,7 +708,7 @@ void Geometry::drawImplementation(RenderInfo& renderInfo) const OSG_WARN<<"Geometry::drawImplementation() unable to render due to deprecated data, call geometry->fixDeprecatedData();"<push_back(0); elems->push_back(1); elems->push_back(2); - + elems->push_back(2); elems->push_back(3); elems->push_back(0); @@ -1102,7 +1114,7 @@ Geometry::AttributeBinding Geometry::getNormalBinding() const { return GET_BINDING(_normalArray.get()); } - + void Geometry::setColorBinding(AttributeBinding ab) { SET_BINDING(_colorArray.get(), ab) @@ -1114,7 +1126,7 @@ Geometry::AttributeBinding Geometry::getColorBinding() const { return GET_BINDING(_colorArray.get()); } - + void Geometry::setSecondaryColorBinding(AttributeBinding ab) { SET_BINDING(_secondaryColorArray.get(), ab) @@ -1126,7 +1138,7 @@ Geometry::AttributeBinding Geometry::getSecondaryColorBinding() const { return GET_BINDING(_secondaryColorArray.get()); } - + void Geometry::setFogCoordBinding(AttributeBinding ab) { SET_BINDING(_fogCoordArray.get(), ab) @@ -1146,7 +1158,7 @@ void Geometry::setVertexAttribBinding(unsigned int index,AttributeBinding ab) if (_vertexAttribList[index]->getBinding()==static_cast(ab)) return; _vertexAttribList[index]->setBinding(static_cast(ab)); - + dirtyDisplayList(); } else @@ -1166,7 +1178,7 @@ void Geometry::setVertexAttribNormalize(unsigned int index,GLboolean norm) if (index<_vertexAttribList.size() && _vertexAttribList[index].valid()) { _vertexAttribList[index]->setNormalize(norm); - + dirtyDisplayList(); } } @@ -1236,7 +1248,7 @@ void Geometry::duplicateSharedArrays() namespace GeometryUtilFunctions { - + inline osg::IndexArray* getIndexArray(osg::Array* array) { return array ? dynamic_cast(array->getUserData()) : 0; @@ -1259,7 +1271,7 @@ namespace GeometryUtilFunctions targetArray->setNormalize(sourceArray->getNormalize()); targetArray->setPreserveDataType(sourceArray->getPreserveDataType()); targetArray->resizeArray(indices->getNumElements()); - + unsigned int elementSize = sourceArray->getElementSize(); const char* sourcePtr = static_cast(sourceArray->getDataPointer()); char* targetPtr = const_cast(static_cast(targetArray->getDataPointer())); @@ -1296,17 +1308,17 @@ namespace GeometryUtilFunctions unsigned int elementSize; PtrData():source(0),target(0),elementSize(0) {} - + PtrData(osg::Array* s, osg::Array* t): source(const_cast(static_cast(s->getDataPointer()))), target(const_cast(static_cast(t->getDataPointer()))), elementSize(s->getElementSize()) {} - + PtrData(const PtrData& rhs): source(rhs.source), target(rhs.target), elementSize(rhs.elementSize) {} - + PtrData& operator = (const PtrData& rhs) { source = rhs.source; @@ -1315,13 +1327,13 @@ namespace GeometryUtilFunctions return *this; } }; - + } bool Geometry::checkForDeprecatedData() { _containsDeprecatedData = false; - + if (GeometryUtilFunctions::containsDeprecatedUsage(_vertexArray.get())) _containsDeprecatedData = true; if (GeometryUtilFunctions::containsDeprecatedUsage(_normalArray.get())) _containsDeprecatedData = true; @@ -1349,9 +1361,9 @@ bool Geometry::checkForDeprecatedData() void Geometry::fixDeprecatedData() { if (!_containsDeprecatedData) return; - + bool containsBindPerPrimitive = false; - + OSG_NOTICE<<"Geometry::fixDeprecatedData()"<getBinding()==osg::Array::BIND_PER_VERTEX) GeometryUtilFunctions::duplicateArray(perVertexArrays, _normalArray, numVertices); else if (_normalArray->getBinding()==3 /*osg::Array::BIND_PER_PRIMITIVE*/) GeometryUtilFunctions::duplicateArray(perPrimitiveArrays, _normalArray, numVertices); } - + if (_colorArray.valid()) { if (_colorArray->getBinding()==osg::Array::BIND_PER_VERTEX) GeometryUtilFunctions::duplicateArray(perVertexArrays, _colorArray, numVertices); else if (_colorArray->getBinding()==3 /*osg::Array::BIND_PER_PRIMITIVE*/) GeometryUtilFunctions::duplicateArray(perPrimitiveArrays, _colorArray, numVertices); } - + if (_secondaryColorArray.valid()) { if (_secondaryColorArray->getBinding()==osg::Array::BIND_PER_VERTEX) GeometryUtilFunctions::duplicateArray(perVertexArrays, _secondaryColorArray, numVertices); else if (_secondaryColorArray->getBinding()==3 /*osg::Array::BIND_PER_PRIMITIVE*/) GeometryUtilFunctions::duplicateArray(perPrimitiveArrays, _secondaryColorArray, numVertices); } - + if (_fogCoordArray.valid()) { if (_fogCoordArray->getBinding()==osg::Array::BIND_PER_VERTEX) GeometryUtilFunctions::duplicateArray(perVertexArrays, _fogCoordArray, numVertices); else if (_fogCoordArray->getBinding()==3 /*osg::Array::BIND_PER_PRIMITIVE*/) GeometryUtilFunctions::duplicateArray(perPrimitiveArrays, _fogCoordArray, numVertices); } - + for(ArrayList::iterator itr = _texCoordList.begin(); itr != _texCoordList.end(); ++itr) @@ -1488,7 +1500,7 @@ void Geometry::fixDeprecatedData() else if ((*itr)->getBinding()==3 /*osg::Array::BIND_PER_PRIMITIVE*/) GeometryUtilFunctions::duplicateArray(perPrimitiveArrays, *itr, numVertices); } } - + for(ArrayList::iterator itr = _vertexAttribList.begin(); itr != _vertexAttribList.end(); ++itr) @@ -1503,23 +1515,23 @@ void Geometry::fixDeprecatedData() typedef std::vector PtrList; PtrList perVertexPtrs; PtrList perPrimitivePtrs; - + for(GeometryUtilFunctions::ArrayPairs::iterator itr = perVertexArrays.begin(); itr != perVertexArrays.end(); ++itr) { perVertexPtrs.push_back(GeometryUtilFunctions::PtrData(itr->first.get(), itr->second.get())); } - + for(GeometryUtilFunctions::ArrayPairs::iterator itr = perPrimitiveArrays.begin(); itr != perPrimitiveArrays.end(); ++itr) { perPrimitivePtrs.push_back(GeometryUtilFunctions::PtrData(itr->first.get(), itr->second.get())); } - - - // start the primitiveNum at -1 as we increment it the first time through when + + + // start the primitiveNum at -1 as we increment it the first time through when // we start processing the primitive sets. int primitiveNum = -1; for(PrimitiveSetList::iterator itr = _primitives.begin(); @@ -1551,7 +1563,7 @@ void Geometry::fixDeprecatedData() const DrawArrays* drawArray = static_cast(primitiveset); if (primLength==0) primLength = drawArray->getCount(); - + unsigned int primCount=0; unsigned int indexEnd = drawArray->getFirst()+drawArray->getCount(); for(unsigned int vindex=drawArray->getFirst(); @@ -1559,7 +1571,7 @@ void Geometry::fixDeprecatedData() ++vindex, ++target_vindex, ++primCount) { if ((primCount%primLength)==0) ++source_pindex; - + // copy bind per primitive from primitiveNum for(PtrList::iterator itr = perVertexPtrs.begin(); itr != perVertexPtrs.end(); @@ -1573,7 +1585,7 @@ void Geometry::fixDeprecatedData() *target++ = *source++; } } - + // copy bind per vertex from vindex for(PtrList::iterator itr = perPrimitivePtrs.begin(); itr != perPrimitivePtrs.end(); @@ -1607,7 +1619,7 @@ void Geometry::fixDeprecatedData() ++vindex, ++target_vindex, ++primCount) { if ((primCount%localPrimLength)==0) ++source_pindex; - + // copy bind per primitive from primitiveNum for(PtrList::iterator itr = perVertexPtrs.begin(); itr != perVertexPtrs.end(); @@ -1621,7 +1633,7 @@ void Geometry::fixDeprecatedData() *target++ = *source++; } } - + // copy bind per vertex from vindex for(PtrList::iterator itr = perPrimitivePtrs.begin(); itr != perPrimitivePtrs.end(); @@ -1651,7 +1663,7 @@ void Geometry::fixDeprecatedData() { if ((primCount%primLength)==0) ++source_pindex; unsigned int vindex=*primItr; - + // copy bind per primitive from primitiveNum for(PtrList::iterator itr = perVertexPtrs.begin(); itr != perVertexPtrs.end(); @@ -1665,7 +1677,7 @@ void Geometry::fixDeprecatedData() *target++ = *source++; } } - + // copy bind per vertex from vindex for(PtrList::iterator itr = perPrimitivePtrs.begin(); itr != perPrimitivePtrs.end(); @@ -1708,7 +1720,7 @@ void Geometry::fixDeprecatedData() *target++ = *source++; } } - + // copy bind per vertex from vindex for(PtrList::iterator itr = perPrimitivePtrs.begin(); itr != perPrimitivePtrs.end(); @@ -1751,7 +1763,7 @@ void Geometry::fixDeprecatedData() *target++ = *source++; } } - + // copy bind per vertex from vindex for(PtrList::iterator itr = perPrimitivePtrs.begin(); itr != perPrimitivePtrs.end(); @@ -1774,6 +1786,6 @@ void Geometry::fixDeprecatedData() } } } - + _containsDeprecatedData = false; }