diff --git a/examples/osgcluster/osgcluster.cpp b/examples/osgcluster/osgcluster.cpp index 27ba3db2b..6bbc22f29 100644 --- a/examples/osgcluster/osgcluster.cpp +++ b/examples/osgcluster/osgcluster.cpp @@ -170,6 +170,9 @@ int main( int argc, char **argv ) // objects for managing the broadcasting and recieving of camera packets. Broadcaster bc; Receiver rc; + + bc.setPort(socketNumber); + rc.setPort(socketNumber); while( !viewer.done() ) { diff --git a/include/osg/Drawable b/include/osg/Drawable index 782369d35..8898df8a4 100644 --- a/include/osg/Drawable +++ b/include/osg/Drawable @@ -250,12 +250,19 @@ class SG_EXPORT Drawable : public Object static void flushDeletedDisplayLists(unsigned int contextID); - enum AttributeType + typedef unsigned int AttributeType; + + enum AttributeTypes { - VERTICES, - NORMALS, - COLORS, - TEXTURE_COORDS, + VERTICES = 0, + WEIGHTS = 1, + NORMALS = 2, + COLORS = 3, + SECONDARY_COLORS = 4, + FOG_COORDS = 5, + ATTIBUTE_6 = 6, + ATTIBUTE_7 = 7, + TEXTURE_COORDS = 8, TEXTURE_COORDS_0 = TEXTURE_COORDS, TEXTURE_COORDS_1 = TEXTURE_COORDS_0+1, TEXTURE_COORDS_2 = TEXTURE_COORDS_0+2, diff --git a/include/osg/Geometry b/include/osg/Geometry index 59bb9676c..1a4f4be99 100644 --- a/include/osg/Geometry +++ b/include/osg/Geometry @@ -127,6 +127,49 @@ class SG_EXPORT Geometry : public Drawable const TexCoordArrayList& getTexCoordArrayList() const { return _texCoordList; } +#ifdef COMPILE_POSSIBLE_NEW_ARRAY_METHODS + + void setArray(AttributeType type, Array* array); + Array* getArray(AttributeType type); + const Array* getArray(AttributeType type) const; + + void setIndices(AttributeType type, IndexArray* indices); + IndexArray* getIndices(AttributeType type); + const IndexArray* getIndices(AttributeType type) const; + + void setNormalize(AttributeType type, GLboolean normalize); + GLboolean getNormalize(AttributeType type) const; + + void setBinding(AttributeType type,AttributeBinding binding); + AttributeBinding getBinding(AttributeType type) const; + + struct AttributeData + { + AttributeData(): + _normalize(GL_FALSE), + _binding(BIND_OFF) {} + + ref_ptr _array; + ref_ptr _indices; + GLboolean _normalize; + AttributeBinding _binding; + }; + + unsigned int getNumArrays() const { return _attributeList.size(); } + + AttributeData& getAttributeData(unsigned int type) { return _attributeList[type]; } + + const AttributeData& getAttributeData(unsigned int type) const { return _attributeList[type]; } + + typedef std::vector AttributeList; + + void setAttributeList(AttributeList& al) { _attributeList = al; } + + AttributeList& getAttributeList() { return _attributeList; } + + const AttributeList& getAttributeList() const { return _attributeList; } + +#endif typedef std::pair< ref_ptr, ref_ptr > VertexAttribArrayPair; typedef std::pair< GLboolean, VertexAttribArrayPair > VertexAttribNormArrayPair; @@ -320,6 +363,9 @@ class SG_EXPORT Geometry : public Drawable PrimitiveSetList _primitives; +#ifdef COMPILE_POSSIBLE_NEW_ARRAY_METHODS + AttributeList _attributeList; +#endif ref_ptr _vertexArray; ref_ptr _vertexIndices; diff --git a/include/osg/State b/include/osg/State index c10b9bb77..cbd42df1b 100644 --- a/include/osg/State +++ b/include/osg/State @@ -17,7 +17,6 @@ #include #include #include -#include #include #include @@ -537,85 +536,18 @@ class SG_EXPORT State : public Referenced * note, only updates values that change.*/ bool setActiveTextureUnit( unsigned int unit ); - - typedef void (APIENTRY * VertexAttribPointerProc) (unsigned int, GLint, GLenum, GLboolean normalized, GLsizei stride, const GLvoid *pointer); - typedef void (APIENTRY * EnableVertexAttribProc) (unsigned int); - typedef void (APIENTRY * DisableVertexAttribProc) (unsigned int); - /** wrapper around glEnableVertexAttribArrayARB(index);glVertexAttribPointerARB(..); * note, only updates values that change.*/ - inline void setVertexAttribPointer( unsigned int index, - GLint size, GLenum type, GLboolean normalized, - GLsizei stride, const GLvoid *ptr ) - { - static VertexAttribPointerProc s_glVertexAttribPointer = - (VertexAttribPointerProc) osg::getGLExtensionFuncPtr("glVertexAttribPointer","glVertexAttribPointerARB"); - - static EnableVertexAttribProc s_glEnableVertexAttribArray = - (EnableVertexAttribProc) osg::getGLExtensionFuncPtr("glEnableVertexAttribArray","glEnableVertexAttribArrayARB"); - - if( s_glVertexAttribPointer ) - { - if ( index >= _vertexAttribArrayList.size()) _vertexAttribArrayList.resize(index+1); - EnabledArrayPair& eap = _vertexAttribArrayList[index]; - - if (!eap._enabled || eap._dirty) - { - eap._enabled = true; - s_glEnableVertexAttribArray( index ); - } - if (eap._pointer != ptr || eap._normalized!=normalized || eap._dirty) - { - s_glVertexAttribPointer( index, size, type, normalized, stride, ptr ); - eap._pointer = ptr; - eap._normalized = normalized; - } - eap._dirty = false; - } - } + void setVertexAttribPointer( unsigned int index, + GLint size, GLenum type, GLboolean normalized, + GLsizei stride, const GLvoid *ptr ); /** wrapper around DisableVertexAttribArrayARB(index); * note, only updates values that change.*/ - inline void disableVertexAttribPointer( unsigned int index ) - { - static DisableVertexAttribProc s_glDisableVertexAttribArray = - (DisableVertexAttribProc) osg::getGLExtensionFuncPtr("glDisableVertexAttribArray","glDisableVertexAttribArrayARB"); - - if (s_glDisableVertexAttribArray) - { - if ( index >= _vertexAttribArrayList.size()) _vertexAttribArrayList.resize(index+1); - EnabledArrayPair& eap = _vertexAttribArrayList[index]; - - if (eap._enabled || eap._dirty) - { - eap._enabled = false; - eap._dirty = false; - s_glDisableVertexAttribArray( index ); - } - } - } - - inline void disableVertexAttribPointersAboveAndIncluding( unsigned int index ) - { - static DisableVertexAttribProc s_glDisableVertexAttribArray = - (DisableVertexAttribProc) osg::getGLExtensionFuncPtr("glDisableVertexAttribArray","glDisableVertexAttribArrayARB"); - - if (s_glDisableVertexAttribArray) - { - while (index<_vertexAttribArrayList.size()) - { - EnabledArrayPair& eap = _vertexAttribArrayList[index]; - if (eap._enabled || eap._dirty) - { - eap._enabled = false; - eap._dirty = false; - s_glDisableVertexAttribArray( index ); - } - ++index; - } - } - } - + void disableVertexAttribPointer( unsigned int index ); + + void disableVertexAttribPointersAboveAndIncluding( unsigned int index ); + inline void dirtyVertexAttribPointersAboveAndIncluding( unsigned int index ) { while (index<_vertexAttribArrayList.size()) diff --git a/src/osg/AutoTransform.cpp b/src/osg/AutoTransform.cpp index 9d035c362..f9dcb208f 100644 --- a/src/osg/AutoTransform.cpp +++ b/src/osg/AutoTransform.cpp @@ -16,10 +16,10 @@ using namespace osg; AutoTransform::AutoTransform(): - _scale(1.0f,1.0f,1.0f), _autoUpdateEyeMovementTolerance(0.0f), _autoRotateToScreen(false), _autoScaleToScreen(false), + _scale(1.0f,1.0f,1.0f), _firstTimeToInitEyePoint(true), _matrixDirty(true) { @@ -30,8 +30,8 @@ AutoTransform::AutoTransform(const AutoTransform& pat,const CopyOp& copyop): Transform(pat,copyop), _position(pat._position), _pivotPoint(pat._pivotPoint), - _scale(pat._scale), - _rotation(pat._rotation) + _rotation(pat._rotation), + _scale(pat._scale) { // setNumChildrenRequiringUpdateTraversal(getNumChildrenRequiringUpdateTraversal()+1); } diff --git a/src/osg/Geometry.cpp b/src/osg/Geometry.cpp index 3d2eca10d..a16aca250 100644 --- a/src/osg/Geometry.cpp +++ b/src/osg/Geometry.cpp @@ -83,51 +83,51 @@ public: _normalized(normalized), _extensions(extensions), _attribcoords(attribcoords), - _indices(indices) {} + _indices(indices) {;} - void operator () (unsigned int pos) - { - if (_indices) _attribcoords->accept(_indices->index(pos),*this); - else _attribcoords->accept(pos,*this); - } + void operator () (unsigned int pos) + { + if (_indices) _attribcoords->accept(_indices->index(pos),*this); + else _attribcoords->accept(pos,*this); + } - virtual void apply(const GLshort& s) - { - _extensions->glVertexAttrib1s( _index, s ); - } - virtual void apply(const GLfloat& f) - { - _extensions->glVertexAttrib1f( _index, f ); - } - virtual void apply(const UByte4& v) - { - if( _normalized ) - { - _extensions->glVertexAttrib4Nubv( _index, v.ptr() ); - } - else - { - _extensions->glVertexAttrib4ubv( _index, v.ptr() ); - } - } - virtual void apply(const Vec2& v) - { - _extensions->glVertexAttrib2fv( _index, v.ptr() ); - } - virtual void apply(const Vec3& v) - { - _extensions->glVertexAttrib3fv( _index, v.ptr() ); - } - virtual void apply(const Vec4& v) - { - _extensions->glVertexAttrib4fv( _index, v.ptr() ); - } + virtual void apply(const GLshort& s) + { + _extensions->glVertexAttrib1s( _index, s ); + } + virtual void apply(const GLfloat& f) + { + _extensions->glVertexAttrib1f( _index, f ); + } + virtual void apply(const UByte4& v) + { + if( _normalized ) + { + _extensions->glVertexAttrib4Nubv( _index, v.ptr() ); + } + else + { + _extensions->glVertexAttrib4ubv( _index, v.ptr() ); + } + } + virtual void apply(const Vec2& v) + { + _extensions->glVertexAttrib2fv( _index, v.ptr() ); + } + virtual void apply(const Vec3& v) + { + _extensions->glVertexAttrib3fv( _index, v.ptr() ); + } + virtual void apply(const Vec4& v) + { + _extensions->glVertexAttrib4fv( _index, v.ptr() ); + } - unsigned int _index; - GLboolean _normalized; - const Geometry::Extensions* _extensions; - const Array* _attribcoords; - const IndexArray* _indices; + const Geometry::Extensions *_extensions; + const Array* _attribcoords; + const IndexArray* _indices; + GLboolean _normalized; + unsigned int _index; }; class DrawTexCoord : public osg::Referenced, public osg::ConstValueVisitor @@ -247,6 +247,9 @@ Geometry::Geometry() Geometry::Geometry(const Geometry& geometry,const CopyOp& copyop): Drawable(geometry,copyop), +#ifdef COMPILE_POSSIBLE_NEW_ARRAY_METHODS + _attributeList(geometry._attributeList), +#endif _vertexArray(dynamic_cast(copyop(geometry._vertexArray.get()))), _normalBinding(geometry._normalBinding), _normalArray(dynamic_cast(copyop(geometry._normalArray.get()))), @@ -331,6 +334,89 @@ const IndexArray* Geometry::getTexCoordIndices(unsigned int unit) const else return 0; } + +#ifdef COMPILE_POSSIBLE_NEW_ARRAY_METHODS + +void Geometry::setArray(AttributeType type,Array* array) +{ + if (_attributeList.size()<=type) + _attributeList.resize(type+1); + + _attributeList[type]._array = array; + + dirtyDisplayList(); +} + +Array* Geometry::getArray(AttributeType type) +{ + if (type<_attributeList.size()) return _attributeList[type]._array.get(); + else return 0; +} + +const Array* Geometry::getArray(AttributeType type) const +{ + if (type<_attributeList.size()) return _attributeList[type]._array.get(); + else return 0; +} + + +void Geometry::setIndices(AttributeType type,IndexArray* array) +{ + if (_attributeList.size()<=type) + _attributeList.resize(type+1); + + _attributeList[type]._indices = array; + + dirtyDisplayList(); +} + +IndexArray* Geometry::getIndices(AttributeType type) +{ + if (type<_attributeList.size()) return _attributeList[type]._indices.get(); + else return 0; +} + +const IndexArray* Geometry::getIndices(AttributeType type) const +{ + if (type<_attributeList.size()) return _attributeList[type]._indices.get(); + else return 0; +} + + +void Geometry::setNormalize(AttributeType type,GLboolean normalize) +{ + if (_attributeList.size()<=type) + _attributeList.resize(type+1); + + _attributeList[type]._normalize = normalize; + + dirtyDisplayList(); +} + +GLboolean Geometry::getNormalize(AttributeType type) const +{ + if (type<_attributeList.size()) return _attributeList[type]._normalize; + else return GL_FALSE; +} + +void Geometry::setBinding(AttributeType type,AttributeBinding binding) +{ + if (_attributeList.size()<=type) + _attributeList.resize(type+1); + + _attributeList[type]._binding = binding; + + dirtyDisplayList(); +} + +Geometry::AttributeBinding Geometry::getBinding(AttributeType type) const +{ + if (type<_attributeList.size()) return _attributeList[type]._binding; + else return BIND_OFF; +} + +#endif + void Geometry::setVertexAttribArray(unsigned int index,bool normalize,Array* array,AttributeBinding ab) { if (_vertexAttribList.size()<=index) @@ -342,7 +428,15 @@ void Geometry::setVertexAttribArray(unsigned int index,bool normalize,Array* arr _vertexAttribList[index].first = normalize; _vertexAttribList[index].second.first = array; - _vertexAttribBindingList[index] = ab; + if( index == 0 ) + { + // Force bind per vertex + _vertexAttribBindingList[index] = BIND_PER_VERTEX; + } + else + { + _vertexAttribBindingList[index] = ab; + } _fastPathComputed = false; dirtyDisplayList(); @@ -683,8 +777,6 @@ void Geometry::drawImplementation(State& state) const // if(!extensions->isVertexProgramSupported()) { - notify(WARN) << "Error: VertexProgram not supported by OpenGL driver" << std::endl; - for( unsigned int va = 0; va < _vertexAttribBindingList.size(); ++va ) { if (_vertexAttribBindingList[va]!=BIND_OFF) @@ -720,7 +812,10 @@ void Geometry::drawImplementation(State& state) const // fast path. // - state.setVertexPointer(3,GL_FLOAT,0,_vertexArray->getDataPointer()); + if( _vertexArray.valid() ) + state.setVertexPointer(3,GL_FLOAT,0,_vertexArray->getDataPointer()); + else + state.disableVertexPointer(); if (_normalBinding==BIND_PER_VERTEX) state.setNormalPointer(GL_FLOAT,0,_normalArray->getDataPointer()); @@ -759,34 +854,29 @@ void Geometry::drawImplementation(State& state) const for( index = 0; index < _vertexAttribList.size(); ++index ) { const Array* array = _vertexAttribList[index].second.first.get(); - const IndexArray* indexArray = _vertexAttribList[index].second.second.get(); + const AttributeBinding ab = _vertexAttribBindingList[index]; - if( _vertexAttribBindingList[index] == BIND_PER_VERTEX ) + if( ab == BIND_PER_VERTEX && array ) { - if( array ) - { - state.setVertexAttribPointer( index, array->getDataSize(), array->getDataType(), - _vertexAttribList[index].first, 0, array->getDataPointer() ); - } - else - { - state.disableVertexAttribPointer( index ); - } + state.setVertexAttribPointer( index, array->getDataSize(), array->getDataType(), + _vertexAttribList[index].first, 0, array->getDataPointer() ); } else { - if( indexArray ) + if( array ) { - if( indexArray->getNumElements() > 0 ) + const IndexArray* indexArray = _vertexAttribList[index].second.second.get(); + + if( indexArray && indexArray->getNumElements() > 0 ) { - drawVertexAttribMap[_vertexAttribBindingList[index]].push_back( + drawVertexAttribMap[ab].push_back( new DrawVertexAttrib(extensions,index,_vertexAttribList[index].first,array,indexArray) ); } - } - else - { - drawVertexAttribMap[_vertexAttribBindingList[index]].push_back( - new DrawVertexAttrib(extensions,index,_vertexAttribList[index].first,array,0) ); + else + { + drawVertexAttribMap[ab].push_back( + new DrawVertexAttrib(extensions,index,_vertexAttribList[index].first,array,0) ); + } } state.disableVertexAttribPointer( index ); @@ -908,24 +998,25 @@ void Geometry::drawImplementation(State& state) const if( extensions->isVertexProgramSupported() ) { unsigned int index; - for( index = 0; index < _vertexAttribList.size(); ++index ) + for( index = 1; index < _vertexAttribList.size(); ++index ) { const Array* array = _vertexAttribList[index].second.first.get(); - const IndexArray* indexArray = _vertexAttribList[index].second.second.get(); - if( indexArray ) + if( array && array->getNumElements() > 0 ) { - if( indexArray->getNumElements() > 0 ) + const IndexArray* indexArray = _vertexAttribList[index].second.second.get(); + + if( indexArray && indexArray->getNumElements() > 0 ) { drawVertexAttribMap[_vertexAttribBindingList[index]].push_back( new DrawVertexAttrib(extensions,index,_vertexAttribList[index].first,array,indexArray) ); } + else + { + drawVertexAttribMap[_vertexAttribBindingList[index]].push_back( + new DrawVertexAttrib(extensions,index,_vertexAttribList[index].first,array,0) ); + } } - else - { - drawVertexAttribMap[_vertexAttribBindingList[index]].push_back( - new DrawVertexAttrib(extensions,index,_vertexAttribList[index].first,array,0) ); - } } } @@ -955,6 +1046,18 @@ void Geometry::drawImplementation(State& state) const // set up vertex functor. DrawVertex drawVertex(_vertexArray.get(),_vertexIndices.get()); + bool useVertexAttrib = _vertexAttribList.size() > 0 && + _vertexAttribList[0].second.first.valid() && + _vertexAttribList[0].second.first->getNumElements(); + + ref_ptr drawVertexAttribZero; + if( useVertexAttrib ) + { + drawVertexAttribZero = new DrawVertexAttrib(extensions,0, + _vertexAttribList[0].first,_vertexAttribList[0].second.first.get(), + _vertexAttribList[0].second.second.get()); + } + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // draw the primitives themselves. @@ -1048,7 +1151,14 @@ void Geometry::drawImplementation(State& state) const } if (drawTextCoord.valid()) (*drawTextCoord)(vindex); - drawVertex(vindex); + if( useVertexAttrib ) + { + (*drawVertexAttribZero)(vindex); + } + else + { + drawVertex(vindex); + } } glEnd(); @@ -1109,8 +1219,15 @@ void Geometry::drawImplementation(State& state) const } if (drawTextCoord.valid()) (*drawTextCoord)(vindex); - drawVertex(vindex); - + if( useVertexAttrib ) + { + (*drawVertexAttribZero)(vindex); + } + else + { + drawVertex(vindex); + } + ++vindex; } @@ -1173,7 +1290,14 @@ void Geometry::drawImplementation(State& state) const } if (drawTextCoord.valid()) (*drawTextCoord)(vindex); - drawVertex(vindex); + if( useVertexAttrib ) + { + (*drawVertexAttribZero)(vindex); + } + else + { + drawVertex(vindex); + } } glEnd(); @@ -1233,7 +1357,14 @@ void Geometry::drawImplementation(State& state) const } if (drawTextCoord.valid()) (*drawTextCoord)(vindex); - drawVertex(vindex); + if( useVertexAttrib ) + { + (*drawVertexAttribZero)(vindex); + } + else + { + drawVertex(vindex); + } } glEnd(); @@ -1293,7 +1424,14 @@ void Geometry::drawImplementation(State& state) const } if (drawTextCoord.valid()) (*drawTextCoord)(vindex); - drawVertex(vindex); + if( useVertexAttrib ) + { + (*drawVertexAttribZero)(vindex); + } + else + { + drawVertex(vindex); + } } glEnd(); diff --git a/src/osg/State.cpp b/src/osg/State.cpp index 5fe68e1f3..e17186005 100644 --- a/src/osg/State.cpp +++ b/src/osg/State.cpp @@ -13,6 +13,8 @@ #include #include #include +#include + using namespace osg; @@ -614,6 +616,84 @@ void State::setSecondaryColorPointer( GLint size, GLenum type, } } +typedef void (APIENTRY * VertexAttribPointerProc) (unsigned int, GLint, GLenum, GLboolean normalized, GLsizei stride, const GLvoid *pointer); +typedef void (APIENTRY * EnableVertexAttribProc) (unsigned int); +typedef void (APIENTRY * DisableVertexAttribProc) (unsigned int); + +/** wrapper around glEnableVertexAttribArrayARB(index);glVertexAttribPointerARB(..); +* note, only updates values that change.*/ +void State::setVertexAttribPointer( unsigned int index, + GLint size, GLenum type, GLboolean normalized, + GLsizei stride, const GLvoid *ptr ) +{ + static VertexAttribPointerProc s_glVertexAttribPointer = + (VertexAttribPointerProc) osg::getGLExtensionFuncPtr("glVertexAttribPointer","glVertexAttribPointerARB"); + + static EnableVertexAttribProc s_glEnableVertexAttribArray = + (EnableVertexAttribProc) osg::getGLExtensionFuncPtr("glEnableVertexAttribArray","glEnableVertexAttribArrayARB"); + + if( s_glVertexAttribPointer ) + { + if ( index >= _vertexAttribArrayList.size()) _vertexAttribArrayList.resize(index+1); + EnabledArrayPair& eap = _vertexAttribArrayList[index]; + + if (!eap._enabled || eap._dirty) + { + eap._enabled = true; + s_glEnableVertexAttribArray( index ); + } + if (eap._pointer != ptr || eap._normalized!=normalized || eap._dirty) + { + s_glVertexAttribPointer( index, size, type, normalized, stride, ptr ); + eap._pointer = ptr; + eap._normalized = normalized; + } + eap._dirty = false; + } +} + +/** wrapper around DisableVertexAttribArrayARB(index); +* note, only updates values that change.*/ +void State::disableVertexAttribPointer( unsigned int index ) +{ + static DisableVertexAttribProc s_glDisableVertexAttribArray = + (DisableVertexAttribProc) osg::getGLExtensionFuncPtr("glDisableVertexAttribArray","glDisableVertexAttribArrayARB"); + + if (s_glDisableVertexAttribArray) + { + if ( index >= _vertexAttribArrayList.size()) _vertexAttribArrayList.resize(index+1); + EnabledArrayPair& eap = _vertexAttribArrayList[index]; + + if (eap._enabled || eap._dirty) + { + eap._enabled = false; + eap._dirty = false; + s_glDisableVertexAttribArray( index ); + } + } +} + +void State::disableVertexAttribPointersAboveAndIncluding( unsigned int index ) +{ + static DisableVertexAttribProc s_glDisableVertexAttribArray = + (DisableVertexAttribProc) osg::getGLExtensionFuncPtr("glDisableVertexAttribArray","glDisableVertexAttribArrayARB"); + + if (s_glDisableVertexAttribArray) + { + while (index<_vertexAttribArrayList.size()) + { + EnabledArrayPair& eap = _vertexAttribArrayList[index]; + if (eap._enabled || eap._dirty) + { + eap._enabled = false; + eap._dirty = false; + s_glDisableVertexAttribArray( index ); + } + ++index; + } + } +} + bool State::computeSecondaryColorSupported() const { _isSecondColorSupportResolved = true;