//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_GEOMETRY #define OSG_GEOMETRY 1 #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 }; static char* s_classNames[] = { "AttributeArray" // 0 "ByteArray", // 1 "ShortArray", // 2 "IntArray", // 3 "UByteArray", // 4 "UShortArray", // 5 "UIntArray", // 6 "UByte4Array", // 7 "FloatArray", // 8 "Vec2Array", // 9 "Vec3Array", // 10 "Vec4Array" // 11 }; class 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 { return "AttributeArray"; } 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,T* ptr) : AttributeArray(ARRAYTYPE,DataSize,DataType), std::vector(ptr,ptr+no) {} 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 char* libraryName() const { return "osg"; } virtual const char* className() const { return s_classNames[ARRAYTYPE]; } 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, }; static char* s_PrimitiveNames[] = { "Primitive", // 0 "DrawArrays", // 1 "UByteDrawElements", // 2 "UShortDrawElements", // 3 "UIntDrawElements" // 4 }; class Primitive : public Object { public: enum PrimitiveMode { 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):_primitiveType(primType) {} Primitive(const Primitive& prim,const CopyOp& copyop=CopyOp::SHALLOW_COPY): Object(prim,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 "Primitive"; } PrimitiveType primitiveType; virtual void draw() const = 0; PrimitiveType _primitiveType; }; class DrawArrays : public Primitive { public: DrawArrays(): Primitive(DrawArraysPrimitiveType) {} DrawArrays(GLenum mode, GLint first, GLsizei count): Primitive(DrawArraysPrimitiveType), _mode(mode), _first(first), _count(count) {} DrawArrays(const DrawArrays& da,const CopyOp& copyop=CopyOp::SHALLOW_COPY): Primitive(da,copyop), _mode(da._mode), _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"; } virtual void draw() const { glDrawArrays(_mode,_first,_count); } GLenum _mode; GLint _first; GLsizei _count; }; template class DrawElements : public Primitive, public std::vector { public: DrawElements(GLenum mode=0): Primitive(PRIMTYPE), _mode(mode), _dataType(DataType) {} DrawElements(const DrawElements& array,const CopyOp& copyop=CopyOp::SHALLOW_COPY): Primitive(array,copyop), std::vector(array), _mode(array._mode), _dataType(array._dataType) {} DrawElements(GLenum mode,unsigned int no,T* ptr) : Primitive(PRIMTYPE), std::vector(ptr,ptr+no), _mode(mode), _dataType(DataType) {} DrawElements(GLenum mode, T* first,T* last) : Primitive(PRIMTYPE), std::vector(first,last), _mode(mode), _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 const char* className() const { return s_PrimitiveNames[PRIMTYPE]; } virtual void draw() const { glDrawElements(_mode,size(),_dataType,&front()); } GLenum _mode; GLenum _dataType; }; typedef DrawElements UByteDrawElements; typedef DrawElements UShortDrawElements; typedef DrawElements UIntDrawElements; /* ******************************************************************************************************************* */ /** Experiemntal replacement for GeoSet. */ class SG_EXPORT Geometry : public Drawable { public: Geometry(); /** Copy constructor using CopyOp to manage deep vs shallow copy.*/ Geometry(const Geometry& Geometry,const CopyOp& copyop=CopyOp::SHALLOW_COPY); virtual Object* cloneType() const { return osgNew Geometry(); } virtual Object* clone(const CopyOp& copyop) const { return osgNew Geometry(*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 "Geometry"; } enum AttributeBinding { BIND_OFF=0, BIND_OVERALL, BIND_PER_PRIMITIVE, BIND_PER_VERTEX, }; void setVertexArray(Vec3Array* array) { _vertexArray = array; } Vec3Array* getVertexArray() { return _vertexArray.get(); } const Vec3Array* getVertexArray() const { return _vertexArray.get(); } void setNormalBinding(AttributeBinding ab) { _normalBinding = ab; } AttributeBinding getNormalBinding() const { return _normalBinding; } void setNormalArray(Vec3Array* array) { _normalArray = array; if (!_normalArray.valid()) _normalBinding=BIND_OFF; } Vec3Array* getNormalArray() { return _normalArray.get(); } const Vec3Array* getNormalArray() const { return _normalArray.get(); } void setColorBinding(AttributeBinding ab) { _colorBinding = ab; } AttributeBinding getColorBinding() const { return _colorBinding; } void setColorArray(AttributeArray* array) { _colorArray = array; if (!_colorArray.valid()) _colorBinding=BIND_OFF; } AttributeArray* getColorArray() { return _colorArray.get(); } const AttributeArray* getColorArray() const { return _colorArray.get(); } typedef std::vector< ref_ptr > TexCoordList; void setTexCoordArray(unsigned int unit,AttributeArray*); AttributeArray* getTexCoordArray(unsigned int unit); const AttributeArray* getTexCoordArray(unsigned int unit) const; typedef std::vector< ref_ptr > PrimitiveList; void setPrimitiveList(const PrimitiveList& primitives) { _primitives = primitives; } PrimitiveList& getPrimitiveList() { return _primitives; } const PrimitiveList& getPrimitiveList() const { return _primitives; } void addPrimitive(Primitive* primitive) { if (primitive) _primitives.push_back(primitive); } /** draw Geometry directly ignoring an OpenGL display list which could be attached. * This is the internal draw method which does the drawing itself, * and is the method to override when deriving from Geometry for user-drawn objects. */ virtual void drawImmediateMode(State& state); /** Statistics collection for each drawable- 26.09.01 */ bool getStats(Statistics &); /** return the attributes supported by applyAttrbuteUpdate() as an AttributeBitMask.*/ virtual AttributeBitMask suppportsAttributeOperation() const; /** return the attributes successully applied in applyAttributeUpdate.*/ virtual AttributeBitMask applyAttributeOperation(AttributeFunctor& auf); protected: Geometry& operator = (const Geometry&) { return *this;} virtual ~Geometry(); virtual const bool computeBound() const; PrimitiveList _primitives; ref_ptr _vertexArray; AttributeBinding _normalBinding; ref_ptr _normalArray; AttributeBinding _colorBinding; ref_ptr _colorArray; TexCoordList _texCoordList; }; } #endif