From fb3e705709245faf6485f17cc6d8b2cf36a10e28 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 27 Jun 2002 13:15:34 +0000 Subject: [PATCH] Converted the template DrawElements primitive class into three seperate non templated classes - UByteDrawElements, UShortDrawElements, UIntDrawElements. --- include/osg/Array | 83 ++++++++-------- include/osg/Geometry | 16 +-- include/osg/Primitive | 170 +++++++++++++++++++++----------- src/osg/Array.cpp | 8 +- src/osg/Geometry.cpp | 28 +++--- src/osg/Primitive.cpp | 54 +++++++--- src/osgPlugins/osg/Geometry.cpp | 46 ++++----- 7 files changed, 242 insertions(+), 163 deletions(-) diff --git a/include/osg/Array b/include/osg/Array index fd288d254..d4769843b 100644 --- a/include/osg/Array +++ b/include/osg/Array @@ -15,86 +15,87 @@ namespace osg { -enum ArrayType -{ - AttributeArrayType = 0, - ByteArrayType = 1, - ShortArrayType = 2, - IntArrayType = 3, - UByteArrayType = 4, - UShortArrayType = 5, - UIntArrayType = 6, - UByte4ArrayType = 7, - FloatArrayType = 8, - Vec2ArrayType = 9, - Vec3ArrayType = 10, - Vec4ArrayType = 11 -}; -class SG_EXPORT AttributeArray : public Object +class SG_EXPORT Array : public Object { public: - AttributeArray(ArrayType arrayType=AttributeArrayType,GLint dataSize=0,GLenum dataType=0): + enum Type + { + ArrayType = 0, + ByteArrayType = 1, + ShortArrayType = 2, + IntArrayType = 3, + UByteArrayType = 4, + UShortArrayType = 5, + UIntArrayType = 6, + UByte4ArrayType = 7, + FloatArrayType = 8, + Vec2ArrayType = 9, + Vec3ArrayType = 10, + Vec4ArrayType = 11 + }; + + Array(Type arrayType=ArrayType,GLint dataSize=0,GLenum dataType=0): _arrayType(arrayType), _dataSize(dataSize), _dataType(dataType) {} - AttributeArray(const AttributeArray& array,const CopyOp& copyop=CopyOp::SHALLOW_COPY): + Array(const Array& array,const CopyOp& copyop=CopyOp::SHALLOW_COPY): Object(array,copyop), _arrayType(array._arrayType), _dataSize(array._dataSize), _dataType(array._dataType) {} - virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast(obj)!=NULL; } + virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast(obj)!=NULL; } virtual const char* libraryName() const { return "osg"; } virtual const char* className() const; - ArrayType arrayType() const { return _arrayType; } + Type getType() const { return _arrayType; } GLint dataSize() const { return _dataSize; } GLenum dataType() const { return _dataType; } virtual const GLvoid* dataPointer() const = 0; protected: - virtual ~AttributeArray() {} + virtual ~Array() {} - ArrayType _arrayType; + Type _arrayType; GLint _dataSize; GLenum _dataType; }; -template -class TemplateArray : public AttributeArray, public std::vector +template +class TemplateArray : public Array, public std::vector { public: - TemplateArray() : AttributeArray(ARRAYTYPE,DataSize,DataType) {} + TemplateArray() : Array(ARRAYTYPE,DataSize,DataType) {} TemplateArray(const TemplateArray& ta,const CopyOp& copyop=CopyOp::SHALLOW_COPY): - AttributeArray(ta,copyop), + Array(ta,copyop), std::vector(ta) {} TemplateArray(unsigned int no) : - AttributeArray(ARRAYTYPE,DataSize,DataType), + Array(ARRAYTYPE,DataSize,DataType), std::vector(no) {} TemplateArray(unsigned int no,T* ptr) : - AttributeArray(ARRAYTYPE,DataSize,DataType), + Array(ARRAYTYPE,DataSize,DataType), std::vector(ptr,ptr+no) {} template TemplateArray(InputIterator first,InputIterator last) : - AttributeArray(ARRAYTYPE,DataSize,DataType), + Array(ARRAYTYPE,DataSize,DataType), std::vector(first,last) {} // TemplateArray(T* first,T* last) : -// AttributeArray(ARRAYTYPE,DataSize,DataType), +// Array(ARRAYTYPE,DataSize,DataType), // std::vector(first,last) {} virtual Object* cloneType() const { return osgNew TemplateArray(); } @@ -103,17 +104,17 @@ class TemplateArray : public AttributeArray, public std::vector virtual const GLvoid* dataPointer() const { if (!empty()) return &front(); else return 0; } }; -typedef TemplateArray ByteArray; -typedef TemplateArray ShortArray; -typedef TemplateArray IntArray; -typedef TemplateArray UByteArray; -typedef TemplateArray UShortArray; -typedef TemplateArray UIntArray; -typedef TemplateArray UByte4Array; -typedef TemplateArray FloatArray; -typedef TemplateArray Vec2Array; -typedef TemplateArray Vec3Array; -typedef TemplateArray Vec4Array; +typedef TemplateArray ByteArray; +typedef TemplateArray ShortArray; +typedef TemplateArray IntArray; +typedef TemplateArray UByteArray; +typedef TemplateArray UShortArray; +typedef TemplateArray UIntArray; +typedef TemplateArray UByte4Array; +typedef TemplateArray FloatArray; +typedef TemplateArray Vec2Array; +typedef TemplateArray Vec3Array; +typedef TemplateArray Vec4Array; } diff --git a/include/osg/Geometry b/include/osg/Geometry index 90fa01fdc..9586835ad 100644 --- a/include/osg/Geometry +++ b/include/osg/Geometry @@ -54,17 +54,17 @@ class SG_EXPORT Geometry : public Drawable void setColorBinding(AttributeBinding ab) { _colorBinding = ab; } AttributeBinding getColorBinding() const { return _colorBinding; } - void setColorArray(AttributeArray* array) { _colorArray = array; if (!_colorArray.valid()) _colorBinding=BIND_OFF; dirtyDisplayList(); } - AttributeArray* getColorArray() { return _colorArray.get(); } - const AttributeArray* getColorArray() const { return _colorArray.get(); } + void setColorArray(Array* array) { _colorArray = array; if (!_colorArray.valid()) _colorBinding=BIND_OFF; dirtyDisplayList(); } + Array* getColorArray() { return _colorArray.get(); } + const Array* getColorArray() const { return _colorArray.get(); } - typedef std::vector< ref_ptr > TexCoordArrayList; + typedef std::vector< ref_ptr > TexCoordArrayList; - void setTexCoordArray(unsigned int unit,AttributeArray*); - AttributeArray* getTexCoordArray(unsigned int unit); - const AttributeArray* getTexCoordArray(unsigned int unit) const; + void setTexCoordArray(unsigned int unit,Array*); + Array* getTexCoordArray(unsigned int unit); + const Array* getTexCoordArray(unsigned int unit) const; unsigned int getNumTexCoordArrays() const { return _texCoordList.size(); } TexCoordArrayList& getTexCoordArrayList() { return _texCoordList; } @@ -118,7 +118,7 @@ class SG_EXPORT Geometry : public Drawable ref_ptr _normalArray; AttributeBinding _colorBinding; - ref_ptr _colorArray; + ref_ptr _colorArray; TexCoordArrayList _texCoordList; diff --git a/include/osg/Primitive b/include/osg/Primitive index b80aebddf..69d327247 100644 --- a/include/osg/Primitive +++ b/include/osg/Primitive @@ -9,19 +9,20 @@ namespace osg { -enum PrimitiveType -{ - PrimitivePrimitiveType = 0, - DrawArraysPrimitiveType = 1, - UByteDrawElementsPrimitiveType = 2, - UShortDrawElementsPrimitiveType = 3, - UIntDrawElementsPrimitiveType = 4, -}; -class SG_EXPORT Primitive : public Object +class Primitive : public Object { public: + enum Type + { + PrimitiveType = 0, + DrawArraysPrimitiveType = 1, + UByteDrawElementsPrimitiveType = 2, + UShortDrawElementsPrimitiveType = 3, + UIntDrawElementsPrimitiveType = 4, + }; + enum Mode { POINTS = GL_POINTS, @@ -36,7 +37,7 @@ class SG_EXPORT Primitive : public Object POLYGON = GL_POLYGON }; - Primitive(PrimitiveType primType=PrimitivePrimitiveType,GLenum mode=0): + Primitive(Type primType=PrimitiveType,GLenum mode=0): _primitiveType(primType), _mode(mode) {} @@ -47,9 +48,9 @@ class SG_EXPORT Primitive : public Object virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast(obj)!=NULL; } virtual const char* libraryName() const { return "osg"; } - virtual const char* className() const; + virtual const char* className() const { return "Primitve"; } - PrimitiveType primitiveType() const { return _primitiveType; } + Type getType() const { return _primitiveType; } void setMode(GLenum mode) { _mode = mode; } GLenum getMode() const { return _mode; } @@ -60,16 +61,16 @@ class SG_EXPORT Primitive : public Object protected: - PrimitiveType _primitiveType; + Type _primitiveType; GLenum _mode; }; -class DrawArrays : public Primitive +class SG_EXPORT DrawArrays : public Primitive { public: DrawArrays(GLenum mode=0): - Primitive(DrawArraysPrimitiveType,0) + Primitive(DrawArraysPrimitiveType,mode) {} DrawArrays(GLenum mode, GLint first, GLsizei count): @@ -102,71 +103,122 @@ class DrawArrays : public Primitive void setCount(GLsizei count) { _count = count; } GLsizei getCount() const { return _count; } - virtual void draw() const - { - glDrawArrays(_mode,_first,_count); - } + virtual void draw() const; - virtual void applyPrimitiveOperation(Drawable::PrimitiveFunctor& functor) - { - functor.drawArrays(_mode,_first,_count); - } + virtual void applyPrimitiveOperation(Drawable::PrimitiveFunctor& functor); + + protected: GLint _first; GLsizei _count; }; -template -class DrawElements : public Primitive, public std::vector +class SG_EXPORT UByteDrawElements : public Primitive, public std::vector { public: - DrawElements(GLenum mode=0): - Primitive(PRIMTYPE,mode), - _dataType(DataType) {} + UByteDrawElements(GLenum mode=0): + Primitive(UByteDrawElementsPrimitiveType,mode) {} - DrawElements(const DrawElements& array,const CopyOp& copyop=CopyOp::SHALLOW_COPY): + UByteDrawElements(const UByteDrawElements& array,const CopyOp& copyop=CopyOp::SHALLOW_COPY): Primitive(array,copyop), - std::vector(array), - _dataType(array._dataType) {} + std::vector(array) {} - DrawElements(GLenum mode,unsigned int no,T* ptr) : - Primitive(PRIMTYPE,mode), - std::vector(ptr,ptr+no), - _dataType(DataType) {} + UByteDrawElements(GLenum mode,unsigned int no,unsigned char* ptr) : + Primitive(UByteDrawElementsPrimitiveType,mode), + std::vector(ptr,ptr+no) {} - DrawElements(GLenum mode,unsigned int no) : - Primitive(PRIMTYPE,mode), - std::vector(no), - _dataType(DataType) {} + UByteDrawElements(GLenum mode,unsigned int no) : + Primitive(UByteDrawElementsPrimitiveType,mode), + std::vector(no) {} template - DrawElements(GLenum mode, InputIterator first,InputIterator last) : - Primitive(PRIMTYPE,mode), - std::vector(first,last), - _dataType(DataType) {} + UByteDrawElements(GLenum mode, InputIterator first,InputIterator last) : + Primitive(UByteDrawElementsPrimitiveType,mode), + std::vector(first,last) {} - virtual Object* cloneType() const { return osgNew DrawElements(); } - virtual Object* clone(const CopyOp& copyop) const { return osgNew DrawElements(*this,copyop); } - virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast(obj)!=NULL; } + virtual Object* cloneType() const { return osgNew UByteDrawElements(); } + virtual Object* clone(const CopyOp& copyop) const { return osgNew UByteDrawElements(*this,copyop); } + virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast(obj)!=NULL; } virtual const char* libraryName() const { return "osg"; } + virtual const char* className() const { return "UByteDrawElements"; } - virtual void draw() const - { - glDrawElements(_mode,size(),_dataType,&front()); - } + virtual void draw() const ; - virtual void applyPrimitiveOperation(Drawable::PrimitiveFunctor& functor) - { - if (!empty()) functor.drawElements(_mode,size(),&front()); - } - - GLenum _dataType; + virtual void applyPrimitiveOperation(Drawable::PrimitiveFunctor& functor); +}; + +class SG_EXPORT UShortDrawElements : public Primitive, public std::vector +{ + public: + + UShortDrawElements(GLenum mode=0): + Primitive(UShortDrawElementsPrimitiveType,mode) {} + + UShortDrawElements(const UShortDrawElements& array,const CopyOp& copyop=CopyOp::SHALLOW_COPY): + Primitive(array,copyop), + std::vector(array) {} + + UShortDrawElements(GLenum mode,unsigned int no,unsigned short* ptr) : + Primitive(UShortDrawElementsPrimitiveType,mode), + std::vector(ptr,ptr+no) {} + + UShortDrawElements(GLenum mode,unsigned int no) : + Primitive(UShortDrawElementsPrimitiveType,mode), + std::vector(no) {} + + template + UShortDrawElements(GLenum mode, InputIterator first,InputIterator last) : + Primitive(UShortDrawElementsPrimitiveType,mode), + std::vector(first,last) {} + + virtual Object* cloneType() const { return osgNew UShortDrawElements(); } + virtual Object* clone(const CopyOp& copyop) const { return osgNew UShortDrawElements(*this,copyop); } + virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast(obj)!=NULL; } + virtual const char* libraryName() const { return "osg"; } + virtual const char* className() const { return "UShortDrawElements"; } + + virtual void draw() const; + + virtual void applyPrimitiveOperation(Drawable::PrimitiveFunctor& functor); +}; + +class UIntDrawElements : public Primitive, public std::vector +{ + public: + + UIntDrawElements(GLenum mode=0): + Primitive(UIntDrawElementsPrimitiveType,mode) {} + + UIntDrawElements(const UIntDrawElements& array,const CopyOp& copyop=CopyOp::SHALLOW_COPY): + Primitive(array,copyop), + std::vector(array) {} + + UIntDrawElements(GLenum mode,unsigned int no,unsigned int* ptr) : + Primitive(UIntDrawElementsPrimitiveType,mode), + std::vector(ptr,ptr+no) {} + + UIntDrawElements(GLenum mode,unsigned int no) : + Primitive(UIntDrawElementsPrimitiveType,mode), + std::vector(no) {} + + template + UIntDrawElements(GLenum mode, InputIterator first,InputIterator last) : + Primitive(UIntDrawElementsPrimitiveType,mode), + std::vector(first,last) {} + + virtual Object* cloneType() const { return osgNew UIntDrawElements(); } + virtual Object* clone(const CopyOp& copyop) const { return osgNew UIntDrawElements(*this,copyop); } + virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast(obj)!=NULL; } + virtual const char* libraryName() const { return "osg"; } + virtual const char* className() const { return "UIntDrawElements"; } + + virtual void draw() const; + + virtual void applyPrimitiveOperation(Drawable::PrimitiveFunctor& functor); + }; -typedef DrawElements UByteDrawElements; -typedef DrawElements UShortDrawElements; -typedef DrawElements UIntDrawElements; } #endif diff --git a/src/osg/Array.cpp b/src/osg/Array.cpp index 44e077174..131eb80b2 100644 --- a/src/osg/Array.cpp +++ b/src/osg/Array.cpp @@ -4,7 +4,7 @@ using namespace osg; static char* s_ArrayNames[] = { - "AttributeArray", // 0 + "Array", // 0 "ByteArray", // 1 "ShortArray", // 2 "IntArray", // 3 @@ -20,11 +20,11 @@ static char* s_ArrayNames[] = "Vec4Array", // 11 }; -const char* AttributeArray::className() const +const char* Array::className() const { - if (_arrayType>=AttributeArrayType && _arrayType<=Vec4ArrayType) + if (_arrayType>=ArrayType && _arrayType<=Vec4ArrayType) return s_ArrayNames[_arrayType]; else - return "UnkownAttributeArray"; + return "UnkownArray"; } diff --git a/src/osg/Geometry.cpp b/src/osg/Geometry.cpp index 0c5cfb10b..f60b0ac1f 100644 --- a/src/osg/Geometry.cpp +++ b/src/osg/Geometry.cpp @@ -24,7 +24,7 @@ Geometry::~Geometry() // no need to delete, all automatically handled by ref_ptr :-) } -void Geometry::setTexCoordArray(unsigned int unit,AttributeArray* array) +void Geometry::setTexCoordArray(unsigned int unit,Array* array) { if (_texCoordList.size()<=unit) _texCoordList.resize(unit+1,0); @@ -34,7 +34,7 @@ void Geometry::setTexCoordArray(unsigned int unit,AttributeArray* array) dirtyDisplayList(); } -AttributeArray* Geometry::getTexCoordArray(unsigned int unit) +Array* Geometry::getTexCoordArray(unsigned int unit) { if (unit<_texCoordList.size()) return _texCoordList[unit].get(); else return 0; @@ -52,7 +52,7 @@ void Geometry::drawImmediateMode(State& /*state*/) // set up texture coordinates. for(unsigned int i=0;i<_texCoordList.size();++i) { - AttributeArray* array = _texCoordList[i].get(); + Array* array = _texCoordList[i].get(); //glClientActiveTextureARB(GL_TEXTURE0_ARB+i); if (array) { @@ -94,25 +94,25 @@ void Geometry::drawImmediateMode(State& /*state*/) // Vec3, Vec4 or UByte4 Arrays. const unsigned char* colorPointer = 0; unsigned int colorStride = 0; - ArrayType colorType = AttributeArrayType; + Array::Type colorType = Array::ArrayType; if (_colorArray.valid()) { - colorType = _colorArray->arrayType(); + colorType = _colorArray->getType(); switch(colorType) { - case(UByte4ArrayType): + case(Array::UByte4ArrayType): { colorPointer = reinterpret_cast(_colorArray->dataPointer()); colorStride = 4; break; } - case(Vec3ArrayType): + case(Array::Vec3ArrayType): { colorPointer = reinterpret_cast(_colorArray->dataPointer()); colorStride = 12; break; } - case(Vec4ArrayType): + case(Array::Vec4ArrayType): { colorPointer = reinterpret_cast(_colorArray->dataPointer()); colorStride = 16; @@ -132,13 +132,13 @@ void Geometry::drawImmediateMode(State& /*state*/) { switch(colorType) { - case(UByte4ArrayType): + case(Array::UByte4ArrayType): glColor4ubv(reinterpret_cast(colorPointer)); break; - case(Vec3ArrayType): + case(Array::Vec3ArrayType): glColor3fv(reinterpret_cast(colorPointer)); break; - case(Vec4ArrayType): + case(Array::Vec4ArrayType): glColor4fv(reinterpret_cast(colorPointer)); break; } @@ -167,13 +167,13 @@ void Geometry::drawImmediateMode(State& /*state*/) { switch(colorType) { - case(UByte4ArrayType): + case(Array::UByte4ArrayType): glColor4ubv(reinterpret_cast(colorPointer)); break; - case(Vec3ArrayType): + case(Array::Vec3ArrayType): glColor3fv(reinterpret_cast(colorPointer)); break; - case(Vec4ArrayType): + case(Array::Vec4ArrayType): glColor4fv(reinterpret_cast(colorPointer)); break; } diff --git a/src/osg/Primitive.cpp b/src/osg/Primitive.cpp index 9b18293e1..b25d41282 100644 --- a/src/osg/Primitive.cpp +++ b/src/osg/Primitive.cpp @@ -2,20 +2,46 @@ using namespace osg; -static char* s_PrimitiveNames[] = +void DrawArrays::draw() const { - "Primitive", // 0 - "DrawArrays", // 1 - "UByteDrawElements", // 2 - "UShortDrawElements", // 3 - "UIntDrawElements" // 4 -}; - -const char* Primitive::className() const -{ - if (_primitiveType>=PrimitivePrimitiveType && _primitiveType<=UIntDrawElementsPrimitiveType) - return s_PrimitiveNames[_primitiveType]; - else - return "UnkownAttributeArray"; + glDrawArrays(_mode,_first,_count); } +void DrawArrays::applyPrimitiveOperation(Drawable::PrimitiveFunctor& functor) +{ + functor.drawArrays(_mode,_first,_count); +} + + +void UByteDrawElements::draw() const +{ + glDrawElements(_mode,size(),GL_UNSIGNED_BYTE,&front()); +} + +void UByteDrawElements::applyPrimitiveOperation(Drawable::PrimitiveFunctor& functor) +{ + if (!empty()) functor.drawElements(_mode,size(),&front()); +} + + +void UShortDrawElements::draw() const +{ + glDrawElements(_mode,size(),GL_UNSIGNED_SHORT,&front()); +} + +void UShortDrawElements::applyPrimitiveOperation(Drawable::PrimitiveFunctor& functor) +{ + if (!empty()) functor.drawElements(_mode,size(),&front()); +} + + + +void UIntDrawElements::draw() const +{ + glDrawElements(_mode,size(),GL_UNSIGNED_INT,&front()); +} + +void UIntDrawElements::applyPrimitiveOperation(Drawable::PrimitiveFunctor& functor) +{ + if (!empty()) functor.drawElements(_mode,size(),&front()); +} diff --git a/src/osgPlugins/osg/Geometry.cpp b/src/osgPlugins/osg/Geometry.cpp index 71109e11c..8fa4eac9d 100644 --- a/src/osgPlugins/osg/Geometry.cpp +++ b/src/osgPlugins/osg/Geometry.cpp @@ -17,7 +17,7 @@ const char* Geometry_getBindingTypeStr(Geometry::AttributeBinding mode); bool Geometry_matchPrimitiveModeStr(const char* str,GLenum& mode); const char* Geometry_getPrimitiveModeStr(GLenum mode); -AttributeArray* AttributeArray_readLocalData(Input& fr); +Array* Array_readLocalData(Input& fr); bool Primitve_readLocalData(Input& fr,osg::Geometry& geom); @@ -149,7 +149,7 @@ bool Geometry_readLocalData(Object& obj, Input& fr) if (fr.matchSequence("ColorArray %w %i {")) { ++fr; - AttributeArray* colors = AttributeArray_readLocalData(fr); + Array* colors = Array_readLocalData(fr); if (colors) { geom.setColorArray(colors); @@ -163,7 +163,7 @@ bool Geometry_readLocalData(Object& obj, Input& fr) fr[1].getInt(unit); fr+=2; - AttributeArray* texcoords = AttributeArray_readLocalData(fr); + Array* texcoords = Array_readLocalData(fr); if (texcoords) { geom.setTexCoordArray(unit,texcoords); @@ -176,7 +176,7 @@ bool Geometry_readLocalData(Object& obj, Input& fr) } -AttributeArray* AttributeArray_readLocalData(Input& fr) +Array* Array_readLocalData(Input& fr) { int entry = fr[0].getNoNestedBrackets(); @@ -396,11 +396,11 @@ void Array_writeLocalData(Output& fw, Iterator first, Iterator last) } -bool Array_writeLocalData(const AttributeArray& array,Output& fw) +bool Array_writeLocalData(const Array& array,Output& fw) { - switch(array.arrayType()) + switch(array.getType()) { - case(ByteArrayType): + case(Array::ByteArrayType): { const ByteArray& carray = static_cast(array); fw<(array); fw<(array); fw<(array); fw<(array); fw<(array); fw<(array); fw<(array); fw<(array); fw<(array); fw<(array); fw<(prim); fw<(prim); fw<(prim); fw<(prim); fw<