From 5afd32b2d9960eb7b686f5b25b50e16f157c84d8 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 19 Dec 2017 09:57:57 +0000 Subject: [PATCH] Introduced dedicated VertexArrayStateList class to wrap up the VertexArrayState container with convinience methods to help initialize them. Using the new VertexArrayStateList class fixed bug associated with setting new setTexCoord() array when a VertexArrayState is already assigned. --- include/osg/Drawable | 2 - include/osg/VertexArrayState | 57 ++++++++++++++++- src/osg/Geometry.cpp | 52 ++++++++++++--- src/osg/VertexArrayState.cpp | 119 +++++++++++++++++++++++++++++++++-- 4 files changed, 212 insertions(+), 18 deletions(-) diff --git a/include/osg/Drawable b/include/osg/Drawable index 71f52cfc0..c223ce254 100644 --- a/include/osg/Drawable +++ b/include/osg/Drawable @@ -321,8 +321,6 @@ class OSG_EXPORT Drawable : public Node /** Implementaion of Craeate tje VertexArrayState object.*/ virtual VertexArrayState* createVertexArrayStateImplementation(RenderInfo& renderInfo) const; - typedef buffered_object< osg::ref_ptr > VertexArrayStateList; - void setVertexArrayStateList(VertexArrayStateList& vasl) { _vertexArrayStateList = vasl; } VertexArrayStateList& getVertexArrayStateList() { return _vertexArrayStateList; } diff --git a/include/osg/VertexArrayState b/include/osg/VertexArrayState index ce5956315..6673d40cb 100644 --- a/include/osg/VertexArrayState +++ b/include/osg/VertexArrayState @@ -23,7 +23,7 @@ namespace osg { class OSG_EXPORT VertexArrayState : public osg::Referenced { -public: + public: VertexArrayState(osg::State* state); @@ -176,7 +176,7 @@ public: void release(); -public: + public: // osg::GLBufferObject* getGLBufferObject(osg::Array* array); @@ -208,6 +208,59 @@ public: }; + +class OSG_EXPORT VertexArrayStateList +{ + public: + + VertexArrayStateList(); + + VertexArrayStateList& operator = (const VertexArrayStateList& rhs); + + inline void clear() { _array.clear(); } + + inline bool empty() const { return _array.empty(); } + + inline unsigned int size() const { return _array.size(); } + + inline void resize(unsigned int newSize) { _array.resize(newSize); } + + inline ref_ptr& operator[] (unsigned int pos) + { + // automatically resize array. + if (_array.size()<=pos) + _array.resize(pos+1,0); + + return _array[pos]; + } + + inline const ref_ptr& operator[] (unsigned int pos) const + { + // automatically resize array. + if (_array.size()<=pos) + _array.resize(pos+1,0); + + return _array[pos]; + } + + void assignAllDispatchers(); + + void assignVertexArrayDispatcher(); + void assignNormalArrayDispatcher(); + void assignColorArrayDispatcher(); + void assignSecondaryColorArrayDispatcher(); + void assignFogCoordArrayDispatcher(); + void assignTexCoordArrayDispatcher(unsigned int numUnits); + void assignVertexAttribArrayDispatcher(unsigned int numUnits); + +protected: + + typedef std::vector< osg::ref_ptr > Array; + mutable Array _array; +}; + + + inline void VertexArrayState::lazyDisablingOfVertexAttributes() { _activeDispatchers.swap(_previous_activeDispatchers); diff --git a/src/osg/Geometry.cpp b/src/osg/Geometry.cpp index c0c699391..c79a2851d 100644 --- a/src/osg/Geometry.cpp +++ b/src/osg/Geometry.cpp @@ -162,7 +162,12 @@ void Geometry::setVertexArray(Array* array) dirtyGLObjects(); dirtyBound(); - if (/*_useVertexBufferObjects && */array) addVertexBufferObjectIfRequired(array); + if (/*_useVertexBufferObjects && */array) + { + _vertexArrayStateList.assignVertexArrayDispatcher(); + + addVertexBufferObjectIfRequired(array); + } } void Geometry::setNormalArray(Array* array, osg::Array::Binding binding) @@ -173,7 +178,12 @@ void Geometry::setNormalArray(Array* array, osg::Array::Binding binding) dirtyGLObjects(); - if (/*_useVertexBufferObjects && */array) addVertexBufferObjectIfRequired(array); + if (/*_useVertexBufferObjects && */array) + { + _vertexArrayStateList.assignNormalArrayDispatcher(); + + addVertexBufferObjectIfRequired(array); + } } void Geometry::setColorArray(Array* array, osg::Array::Binding binding) @@ -184,7 +194,12 @@ void Geometry::setColorArray(Array* array, osg::Array::Binding binding) dirtyGLObjects(); - if (/*_useVertexBufferObjects && */array) addVertexBufferObjectIfRequired(array); + if (/*_useVertexBufferObjects && */array) + { + _vertexArrayStateList.assignColorArrayDispatcher(); + + addVertexBufferObjectIfRequired(array); + } } void Geometry::setSecondaryColorArray(Array* array, osg::Array::Binding binding) @@ -195,7 +210,12 @@ void Geometry::setSecondaryColorArray(Array* array, osg::Array::Binding binding) dirtyGLObjects(); - if (/*_useVertexBufferObjects && */array) addVertexBufferObjectIfRequired(array); + if (/*_useVertexBufferObjects && */array) + { + _vertexArrayStateList.assignSecondaryColorArrayDispatcher(); + + addVertexBufferObjectIfRequired(array); + } } void Geometry::setFogCoordArray(Array* array, osg::Array::Binding binding) @@ -206,7 +226,12 @@ void Geometry::setFogCoordArray(Array* array, osg::Array::Binding binding) dirtyGLObjects(); - if (/*_useVertexBufferObjects && */array) addVertexBufferObjectIfRequired(array); + if (/*_useVertexBufferObjects && */array) + { + _vertexArrayStateList.assignFogCoordArrayDispatcher(); + + addVertexBufferObjectIfRequired(array); + } } @@ -228,6 +253,8 @@ void Geometry::setTexCoordArray(unsigned int index,Array* array, osg::Array::Bin if (/*_useVertexBufferObjects && */array) { + _vertexArrayStateList.assignTexCoordArrayDispatcher(_texCoordList.size()); + addVertexBufferObjectIfRequired(array); } } @@ -250,8 +277,10 @@ void Geometry::setTexCoordArrayList(const ArrayList& arrayList) dirtyGLObjects(); - /*if (_useVertexBufferObjects)*/ + if (!_texCoordList.empty()) { + _vertexArrayStateList.assignTexCoordArrayDispatcher(_texCoordList.size()); + for(ArrayList::iterator itr = _texCoordList.begin(); itr != _texCoordList.end(); ++itr) @@ -272,7 +301,12 @@ void Geometry::setVertexAttribArray(unsigned int index, Array* array, osg::Array dirtyGLObjects(); - if (/*_useVertexBufferObjects && */array) addVertexBufferObjectIfRequired(array); + if (/*_useVertexBufferObjects && */array) + { + _vertexArrayStateList.assignVertexAttribArrayDispatcher(_vertexAttribList.size()); + + addVertexBufferObjectIfRequired(array); + } } Array *Geometry::getVertexAttribArray(unsigned int index) @@ -293,8 +327,10 @@ void Geometry::setVertexAttribArrayList(const ArrayList& arrayList) dirtyGLObjects(); - /*if (_useVertexBufferObjects)*/ + if (!_vertexAttribList.empty()) { + _vertexArrayStateList.assignVertexAttribArrayDispatcher(_vertexAttribList.size()); + for(ArrayList::iterator itr = _vertexAttribList.begin(); itr != _vertexAttribList.end(); ++itr) diff --git a/src/osg/VertexArrayState.cpp b/src/osg/VertexArrayState.cpp index f0bc391d1..f39391a97 100644 --- a/src/osg/VertexArrayState.cpp +++ b/src/osg/VertexArrayState.cpp @@ -546,6 +546,8 @@ void VertexArrayState::deleteVertexArrayObject() void VertexArrayState::assignVertexArrayDispatcher() { + if (_vertexArray.valid()) return; + #ifdef OSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE if (!_state->getUseVertexAttributeAliasing()) { @@ -561,6 +563,8 @@ void VertexArrayState::assignVertexArrayDispatcher() void VertexArrayState::assignNormalArrayDispatcher() { + if (_normalArray.valid()) return; + #ifdef OSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE if (!_state->getUseVertexAttributeAliasing()) { @@ -576,6 +580,8 @@ void VertexArrayState::assignNormalArrayDispatcher() void VertexArrayState::assignColorArrayDispatcher() { + if (_colorArray.valid()) return; + #ifdef OSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE if (!_state->getUseVertexAttributeAliasing()) { @@ -591,6 +597,8 @@ void VertexArrayState::assignColorArrayDispatcher() void VertexArrayState::assignSecondaryColorArrayDispatcher() { + if (_secondaryColorArray.valid()) return; + #ifdef OSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE if (!_state->getUseVertexAttributeAliasing()) { @@ -605,6 +613,8 @@ void VertexArrayState::assignSecondaryColorArrayDispatcher() void VertexArrayState::assignFogCoordArrayDispatcher() { + if (_fogCoordArray.valid()) return; + #ifdef OSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE if (!_state->getUseVertexAttributeAliasing()) { @@ -622,8 +632,7 @@ void VertexArrayState::assignTexCoordArrayDispatcher(unsigned int numUnits) #ifdef OSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE if (!_state->getUseVertexAttributeAliasing()) { - _texCoordArrays.clear(); - for(unsigned int i=0; igetTexCoordAliasList()[i]._location="<<_state->getTexCoordAliasList()[i]._location<getTexCoordAliasList()[i]._location) ); @@ -642,8 +650,7 @@ void VertexArrayState::assignTexCoordArrayDispatcher(unsigned int numUnits) void VertexArrayState::assignVertexAttribArrayDispatcher(unsigned int numUnits) { - _vertexAttribArrays.clear(); - for(unsigned int i=0; igetMaxNumberOfGraphicsContexts()) +{ +} + + +VertexArrayStateList& VertexArrayStateList::operator = (const VertexArrayStateList& rhs) +{ + if (&rhs==this) return *this; + + _array = rhs._array; + + return *this; +} + +void VertexArrayStateList::assignAllDispatchers() +{ + for(Array::iterator itr = _array.begin(); + itr != _array.end(); + ++itr) + { + if (itr->valid()) (*itr)->assignAllDispatchers(); + } +} + +void VertexArrayStateList::assignVertexArrayDispatcher() +{ + for(Array::iterator itr = _array.begin(); + itr != _array.end(); + ++itr) + { + if (itr->valid()) (*itr)->assignVertexArrayDispatcher(); + } +} + +void VertexArrayStateList::assignNormalArrayDispatcher() +{ + for(Array::iterator itr = _array.begin(); + itr != _array.end(); + ++itr) + { + if (itr->valid()) (*itr)->assignNormalArrayDispatcher(); + } +} + +void VertexArrayStateList::assignColorArrayDispatcher() +{ + for(Array::iterator itr = _array.begin(); + itr != _array.end(); + ++itr) + { + if (itr->valid()) (*itr)->assignColorArrayDispatcher(); + } +} + +void VertexArrayStateList::assignSecondaryColorArrayDispatcher() +{ + for(Array::iterator itr = _array.begin(); + itr != _array.end(); + ++itr) + { + if (itr->valid()) (*itr)->assignSecondaryColorArrayDispatcher(); + } +} + +void VertexArrayStateList::assignFogCoordArrayDispatcher() +{ + for(Array::iterator itr = _array.begin(); + itr != _array.end(); + ++itr) + { + if (itr->valid()) (*itr)->assignFogCoordArrayDispatcher(); + } +} + +void VertexArrayStateList::assignTexCoordArrayDispatcher(unsigned int numUnits) +{ + for(Array::iterator itr = _array.begin(); + itr != _array.end(); + ++itr) + { + if (itr->valid()) + { + (*itr)->assignTexCoordArrayDispatcher(numUnits); + } + } +} + +void VertexArrayStateList::assignVertexAttribArrayDispatcher(unsigned int numUnits) +{ + for(Array::iterator itr = _array.begin(); + itr != _array.end(); + ++itr) + { + if (itr->valid()) (*itr)->assignVertexAttribArrayDispatcher(numUnits); + } +}