From 532a32416f062f8b59f300e516a65d58e88fb869 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 27 Jun 2002 10:50:19 +0000 Subject: [PATCH] Moved the AttributeArray and Primitive classes into their own header and source files. --- VisualStudio/osg/osg.dsp | 16 ++ include/osg/Array | 120 +++++++++++++++ include/osg/Geometry | 274 +---------------------------------- include/osg/Primitive | 172 ++++++++++++++++++++++ src/Demos/hangglide/base.cpp | 1 - src/osg/Array.cpp | 30 ++++ src/osg/Geometry.cpp | 43 ------ src/osg/Makefile | 2 + src/osg/Primitive.cpp | 21 +++ 9 files changed, 363 insertions(+), 316 deletions(-) create mode 100644 include/osg/Array create mode 100644 include/osg/Primitive create mode 100644 src/osg/Array.cpp create mode 100644 src/osg/Primitive.cpp diff --git a/VisualStudio/osg/osg.dsp b/VisualStudio/osg/osg.dsp index a10ae5563..da1402c9d 100755 --- a/VisualStudio/osg/osg.dsp +++ b/VisualStudio/osg/osg.dsp @@ -101,6 +101,10 @@ SOURCE=..\..\src\osg\AnimationPath.cpp # End Source File # Begin Source File +SOURCE=..\..\src\osg\Array.cpp +# End Source File +# Begin Source File + SOURCE=..\..\src\osg\Billboard.cpp # End Source File # Begin Source File @@ -309,6 +313,10 @@ SOURCE=..\..\src\osg\PositionAttitudeTransform.cpp # End Source File # Begin Source File +SOURCE=..\..\src\osg\Primitive.cpp +# End Source File +# Begin Source File + SOURCE=..\..\src\osg\Projection.cpp # End Source File # Begin Source File @@ -393,6 +401,10 @@ SOURCE=..\..\Include\Osg\AnimationPath # End Source File # Begin Source File +SOURCE=..\..\Include\Osg\Array +# End Source File +# Begin Source File + SOURCE=..\..\Include\Osg\Billboard # End Source File # Begin Source File @@ -637,6 +649,10 @@ SOURCE=..\..\Include\Osg\Projection # End Source File # Begin Source File +SOURCE=..\..\Include\Osg\Primitive +# End Source File +# Begin Source File + SOURCE=..\..\Include\Osg\Quat # End Source File # Begin Source File diff --git a/include/osg/Array b/include/osg/Array new file mode 100644 index 000000000..fd288d254 --- /dev/null +++ b/include/osg/Array @@ -0,0 +1,120 @@ +//C++ header - Open Scene Graph - Copyright (C) 1998-2001 Robert Osfield +//Distributed under the terms of the GNU Library General Public License (LGPL) +//as published by the Free Software Foundation. + +#ifndef OSG_ARRAY +#define OSG_ARRAY 1 + +#include +#include +#include +#include +#include + +#include + +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 +{ + + public: + + AttributeArray(ArrayType arrayType=AttributeArrayType,GLint dataSize=0,GLenum dataType=0): + _arrayType(arrayType), + _dataSize(dataSize), + _dataType(dataType) {} + + AttributeArray(const AttributeArray& 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 const char* libraryName() const { return "osg"; } + virtual const char* className() const; + + + ArrayType arrayType() const { return _arrayType; } + GLint dataSize() const { return _dataSize; } + GLenum dataType() const { return _dataType; } + virtual const GLvoid* dataPointer() const = 0; + + protected: + + virtual ~AttributeArray() {} + + ArrayType _arrayType; + GLint _dataSize; + GLenum _dataType; +}; + + + +template +class TemplateArray : public AttributeArray, public std::vector +{ + public: + + TemplateArray() : AttributeArray(ARRAYTYPE,DataSize,DataType) {} + + TemplateArray(const TemplateArray& ta,const CopyOp& copyop=CopyOp::SHALLOW_COPY): + AttributeArray(ta,copyop), + std::vector(ta) {} + + TemplateArray(unsigned int no) : + AttributeArray(ARRAYTYPE,DataSize,DataType), + std::vector(no) {} + + TemplateArray(unsigned int no,T* ptr) : + AttributeArray(ARRAYTYPE,DataSize,DataType), + std::vector(ptr,ptr+no) {} + + + template + TemplateArray(InputIterator first,InputIterator last) : + AttributeArray(ARRAYTYPE,DataSize,DataType), + std::vector(first,last) {} + +// TemplateArray(T* first,T* last) : +// AttributeArray(ARRAYTYPE,DataSize,DataType), +// std::vector(first,last) {} + + virtual Object* cloneType() const { return osgNew TemplateArray(); } + virtual Object* clone(const CopyOp& copyop) const { return osgNew TemplateArray(*this,copyop); } + + 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; + +} + +#endif diff --git a/include/osg/Geometry b/include/osg/Geometry index e543fe2f8..90fa01fdc 100644 --- a/include/osg/Geometry +++ b/include/osg/Geometry @@ -9,281 +9,11 @@ #include #include #include +#include +#include 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 -{ - - public: - - AttributeArray(ArrayType arrayType=AttributeArrayType,GLint dataSize=0,GLenum dataType=0): - _arrayType(arrayType), - _dataSize(dataSize), - _dataType(dataType) {} - - AttributeArray(const AttributeArray& 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 const char* libraryName() const { return "osg"; } - virtual const char* className() const; - - - ArrayType arrayType() const { return _arrayType; } - GLint dataSize() const { return _dataSize; } - GLenum dataType() const { return _dataType; } - virtual const GLvoid* dataPointer() const = 0; - - protected: - - virtual ~AttributeArray() {} - - ArrayType _arrayType; - GLint _dataSize; - GLenum _dataType; -}; - - - -template -class TemplateArray : public AttributeArray, public std::vector -{ - public: - - TemplateArray() : AttributeArray(ARRAYTYPE,DataSize,DataType) {} - - TemplateArray(const TemplateArray& ta,const CopyOp& copyop=CopyOp::SHALLOW_COPY): - AttributeArray(ta,copyop), - std::vector(ta) {} - - TemplateArray(unsigned int no) : - AttributeArray(ARRAYTYPE,DataSize,DataType), - std::vector(no) {} - - TemplateArray(unsigned int no,T* ptr) : - AttributeArray(ARRAYTYPE,DataSize,DataType), - std::vector(ptr,ptr+no) {} - - -#ifdef __STL_MEMBER_TEMPLATES - template - TemplateArray(InputIterator first,InputIterator last) : - AttributeArray(ARRAYTYPE,DataSize,DataType), - std::vector(first,last) {} -#else - TemplateArray(T* first,T* last) : - AttributeArray(ARRAYTYPE,DataSize,DataType), - std::vector(first,last) {} -#endif - - virtual Object* cloneType() const { return osgNew TemplateArray(); } - virtual Object* clone(const CopyOp& copyop) const { return osgNew TemplateArray(*this,copyop); } - - 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; - -/* ******************************************************************************************************************* */ - -enum PrimitiveType -{ - PrimitivePrimitiveType = 0, - DrawArraysPrimitiveType = 1, - UByteDrawElementsPrimitiveType = 2, - UShortDrawElementsPrimitiveType = 3, - UIntDrawElementsPrimitiveType = 4, -}; - -class SG_EXPORT Primitive : public Object -{ - public: - - enum Mode - { - POINTS = GL_POINTS, - LINES = GL_LINES, - LINE_STRIP = GL_LINE_STRIP, - LINE_LOOP = GL_LINE_LOOP, - TRIANGLES = GL_TRIANGLES, - TRIANGLE_STRIP = GL_TRIANGLE_STRIP, - TRIANGLE_FAN = GL_TRIANGLE_FAN, - QUADS = GL_QUADS, - QUAD_STRIP = GL_QUAD_STRIP, - POLYGON = GL_POLYGON - }; - - Primitive(PrimitiveType primType=PrimitivePrimitiveType,GLenum mode=0): - _primitiveType(primType), - _mode(mode) {} - - Primitive(const Primitive& prim,const CopyOp& copyop=CopyOp::SHALLOW_COPY): - Object(prim,copyop), - _primitiveType(prim._primitiveType), - _mode(prim._mode) {} - - virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast(obj)!=NULL; } - virtual const char* libraryName() const { return "osg"; } - virtual const char* className() const; - - PrimitiveType primitiveType() const { return _primitiveType; } - - void setMode(GLenum mode) { _mode = mode; } - GLenum getMode() const { return _mode; } - - virtual void draw() const = 0; - - virtual void applyPrimitiveOperation(Drawable::PrimitiveFunctor&) {} - - protected: - - PrimitiveType _primitiveType; - GLenum _mode; -}; - -class DrawArrays : public Primitive -{ - public: - - DrawArrays(): - Primitive(DrawArraysPrimitiveType,0) - {} - - DrawArrays(GLenum mode, GLint first, GLsizei count): - Primitive(DrawArraysPrimitiveType,mode), - _first(first), - _count(count) {} - - DrawArrays(const DrawArrays& da,const CopyOp& copyop=CopyOp::SHALLOW_COPY): - Primitive(da,copyop), - _first(da._first), - _count(da._count) {} - - virtual Object* cloneType() const { return osgNew DrawArrays(); } - virtual Object* clone(const CopyOp& copyop) const { return osgNew DrawArrays(*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 "DrawArrays"; } - - - void set(GLenum mode,GLint first, GLsizei count) - { - _mode = mode; - _first = first; - _count = count; - } - - void setFirst(GLint first) { _first = first; } - GLint getFirst() const { return _first; } - - void setCount(GLsizei count) { _count = count; } - GLsizei getCount() const { return _count; } - - virtual void draw() const - { - glDrawArrays(_mode,_first,_count); - } - - virtual void applyPrimitiveOperation(Drawable::PrimitiveFunctor& functor) - { - functor.drawArrays(_mode,_first,_count); - } - - GLint _first; - GLsizei _count; -}; - -template -class DrawElements : public Primitive, public std::vector -{ - public: - - DrawElements(GLenum mode=0): - Primitive(PRIMTYPE,mode), - _dataType(DataType) {} - - DrawElements(const DrawElements& array,const CopyOp& copyop=CopyOp::SHALLOW_COPY): - Primitive(array,copyop), - std::vector(array), - _dataType(array._dataType) {} - - DrawElements(GLenum mode,unsigned int no,T* ptr) : - Primitive(PRIMTYPE,mode), - std::vector(ptr,ptr+no), - _dataType(DataType) {} - - DrawElements(GLenum mode,unsigned int no) : - Primitive(PRIMTYPE,mode), - std::vector(no), - _dataType(DataType) {} - -#ifdef __STL_MEMBER_TEMPLATES - template - DrawElements(GLenum mode, InputIterator first,InputIterator last) : - Primitive(PRIMTYPE,mode), - std::vector(first,last), - _dataType(DataType) {} -#else - DrawElements(GLenum mode, T* first,T* last) : - Primitive(PRIMTYPE,mode), - std::vector(first,last), - _dataType(DataType) {} -#endif - 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 const char* libraryName() const { return "osg"; } - - virtual void draw() const - { - glDrawElements(_mode,size(),_dataType,&front()); - } - - virtual void applyPrimitiveOperation(Drawable::PrimitiveFunctor& functor) - { - if (!empty()) functor.drawElements(_mode,size(),&front()); - } - - GLenum _dataType; -}; - -typedef DrawElements UByteDrawElements; -typedef DrawElements UShortDrawElements; -typedef DrawElements UIntDrawElements; - - -/* ******************************************************************************************************************* */ - class SG_EXPORT Geometry : public Drawable { diff --git a/include/osg/Primitive b/include/osg/Primitive new file mode 100644 index 000000000..b80aebddf --- /dev/null +++ b/include/osg/Primitive @@ -0,0 +1,172 @@ +//C++ header - Open Scene Graph - Copyright (C) 1998-2001 Robert Osfield +//Distributed under the terms of the GNU Library General Public License (LGPL) +//as published by the Free Software Foundation. + +#ifndef OSG_PRIMTIVE +#define OSG_PRIMTIVE 1 + +#include + +namespace osg { + +enum PrimitiveType +{ + PrimitivePrimitiveType = 0, + DrawArraysPrimitiveType = 1, + UByteDrawElementsPrimitiveType = 2, + UShortDrawElementsPrimitiveType = 3, + UIntDrawElementsPrimitiveType = 4, +}; + +class SG_EXPORT Primitive : public Object +{ + public: + + enum Mode + { + POINTS = GL_POINTS, + LINES = GL_LINES, + LINE_STRIP = GL_LINE_STRIP, + LINE_LOOP = GL_LINE_LOOP, + TRIANGLES = GL_TRIANGLES, + TRIANGLE_STRIP = GL_TRIANGLE_STRIP, + TRIANGLE_FAN = GL_TRIANGLE_FAN, + QUADS = GL_QUADS, + QUAD_STRIP = GL_QUAD_STRIP, + POLYGON = GL_POLYGON + }; + + Primitive(PrimitiveType primType=PrimitivePrimitiveType,GLenum mode=0): + _primitiveType(primType), + _mode(mode) {} + + Primitive(const Primitive& prim,const CopyOp& copyop=CopyOp::SHALLOW_COPY): + Object(prim,copyop), + _primitiveType(prim._primitiveType), + _mode(prim._mode) {} + + virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast(obj)!=NULL; } + virtual const char* libraryName() const { return "osg"; } + virtual const char* className() const; + + PrimitiveType primitiveType() const { return _primitiveType; } + + void setMode(GLenum mode) { _mode = mode; } + GLenum getMode() const { return _mode; } + + virtual void draw() const = 0; + + virtual void applyPrimitiveOperation(Drawable::PrimitiveFunctor&) {} + + protected: + + PrimitiveType _primitiveType; + GLenum _mode; +}; + +class DrawArrays : public Primitive +{ + public: + + DrawArrays(GLenum mode=0): + Primitive(DrawArraysPrimitiveType,0) + {} + + DrawArrays(GLenum mode, GLint first, GLsizei count): + Primitive(DrawArraysPrimitiveType,mode), + _first(first), + _count(count) {} + + DrawArrays(const DrawArrays& da,const CopyOp& copyop=CopyOp::SHALLOW_COPY): + Primitive(da,copyop), + _first(da._first), + _count(da._count) {} + + virtual Object* cloneType() const { return osgNew DrawArrays(); } + virtual Object* clone(const CopyOp& copyop) const { return osgNew DrawArrays(*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 "DrawArrays"; } + + + void set(GLenum mode,GLint first, GLsizei count) + { + _mode = mode; + _first = first; + _count = count; + } + + void setFirst(GLint first) { _first = first; } + GLint getFirst() const { return _first; } + + void setCount(GLsizei count) { _count = count; } + GLsizei getCount() const { return _count; } + + virtual void draw() const + { + glDrawArrays(_mode,_first,_count); + } + + virtual void applyPrimitiveOperation(Drawable::PrimitiveFunctor& functor) + { + functor.drawArrays(_mode,_first,_count); + } + + GLint _first; + GLsizei _count; +}; + +template +class DrawElements : public Primitive, public std::vector +{ + public: + + DrawElements(GLenum mode=0): + Primitive(PRIMTYPE,mode), + _dataType(DataType) {} + + DrawElements(const DrawElements& array,const CopyOp& copyop=CopyOp::SHALLOW_COPY): + Primitive(array,copyop), + std::vector(array), + _dataType(array._dataType) {} + + DrawElements(GLenum mode,unsigned int no,T* ptr) : + Primitive(PRIMTYPE,mode), + std::vector(ptr,ptr+no), + _dataType(DataType) {} + + DrawElements(GLenum mode,unsigned int no) : + Primitive(PRIMTYPE,mode), + std::vector(no), + _dataType(DataType) {} + + template + DrawElements(GLenum mode, InputIterator first,InputIterator last) : + Primitive(PRIMTYPE,mode), + std::vector(first,last), + _dataType(DataType) {} + + 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 const char* libraryName() const { return "osg"; } + + virtual void draw() const + { + glDrawElements(_mode,size(),_dataType,&front()); + } + + virtual void applyPrimitiveOperation(Drawable::PrimitiveFunctor& functor) + { + if (!empty()) functor.drawElements(_mode,size(),&front()); + } + + GLenum _dataType; +}; + +typedef DrawElements UByteDrawElements; +typedef DrawElements UShortDrawElements; +typedef DrawElements UIntDrawElements; +} + +#endif diff --git a/src/Demos/hangglide/base.cpp b/src/Demos/hangglide/base.cpp index ce79e9a92..2041c09dc 100644 --- a/src/Demos/hangglide/base.cpp +++ b/src/Demos/hangglide/base.cpp @@ -20,7 +20,6 @@ Node *makeBase( void ) Vec3Array *coords = new Vec3Array(19); Vec2Array *tcoords = new Vec2Array(19); Vec4Array *colors = new Vec4Array(1); - int *lengths = new int[1]; (*colors)[0].set(1.0f,1.0f,1.0f,1.0f); diff --git a/src/osg/Array.cpp b/src/osg/Array.cpp new file mode 100644 index 000000000..44e077174 --- /dev/null +++ b/src/osg/Array.cpp @@ -0,0 +1,30 @@ +#include + +using namespace osg; + +static char* s_ArrayNames[] = +{ + "AttributeArray", // 0 + "ByteArray", // 1 + "ShortArray", // 2 + "IntArray", // 3 + + "UByteArray", // 4 + "UShortArray", // 5 + "UIntArray", // 6 + "UByte4Array", // 7 + + "FloatArray", // 8 + "Vec2Array", // 9 + "Vec3Array", // 10 + "Vec4Array", // 11 +}; + +const char* AttributeArray::className() const +{ + if (_arrayType>=AttributeArrayType && _arrayType<=Vec4ArrayType) + return s_ArrayNames[_arrayType]; + else + return "UnkownAttributeArray"; +} + diff --git a/src/osg/Geometry.cpp b/src/osg/Geometry.cpp index 955a5b2fc..0c5cfb10b 100644 --- a/src/osg/Geometry.cpp +++ b/src/osg/Geometry.cpp @@ -2,49 +2,6 @@ using namespace osg; -static char* s_ArrayNames[] = -{ - "AttributeArray", // 0 - "ByteArray", // 1 - "ShortArray", // 2 - "IntArray", // 3 - - "UByteArray", // 4 - "UShortArray", // 5 - "UIntArray", // 6 - "UByte4Array", // 7 - - "FloatArray", // 8 - "Vec2Array", // 9 - "Vec3Array", // 10 - "Vec4Array", // 11 -}; - -const char* AttributeArray::className() const -{ - if (_arrayType>=AttributeArrayType && _arrayType<=Vec4ArrayType) - return s_ArrayNames[_arrayType]; - else - return "UnkownAttributeArray"; -} - -static char* s_PrimitiveNames[] = -{ - "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"; -} - Geometry::Geometry() { _normalBinding = BIND_OFF; diff --git a/src/osg/Makefile b/src/osg/Makefile index 82791807c..788d102a4 100644 --- a/src/osg/Makefile +++ b/src/osg/Makefile @@ -4,6 +4,7 @@ include $(TOPDIR)/Make/makedefs CXXFILES =\ AlphaFunc.cpp\ AnimationPath.cpp\ + Array.cpp\ Billboard.cpp\ BoundingBox.cpp\ BoundingSphere.cpp\ @@ -56,6 +57,7 @@ CXXFILES =\ PolygonMode.cpp\ PolygonOffset.cpp\ PositionAttitudeTransform.cpp\ + Primitive.cpp\ Projection.cpp\ Quat.cpp\ ShadeModel.cpp\ diff --git a/src/osg/Primitive.cpp b/src/osg/Primitive.cpp new file mode 100644 index 000000000..9b18293e1 --- /dev/null +++ b/src/osg/Primitive.cpp @@ -0,0 +1,21 @@ +#include + +using namespace osg; + +static char* s_PrimitiveNames[] = +{ + "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"; +} +