From 476f931eaa1f85776656e04fb355464e3f062045 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 20 Jun 2002 19:54:08 +0000 Subject: [PATCH] Addd new experimental osg::Geometry Drawable which will eventually replace GeoSet. Currently doesn't draw anything, and is very much in the early design stages. --- VisualStudio/osg/osg.dsp | 8 ++ include/osg/Geometry | 200 ++++++++++++++++++++++++++++++++++ src/osg/Geometry.cpp | 230 +++++++++++++++++++++++++++++++++++++++ src/osg/Makefile | 1 + 4 files changed, 439 insertions(+) create mode 100644 include/osg/Geometry create mode 100644 src/osg/Geometry.cpp diff --git a/VisualStudio/osg/osg.dsp b/VisualStudio/osg/osg.dsp index 2cf22a5da..a10ae5563 100755 --- a/VisualStudio/osg/osg.dsp +++ b/VisualStudio/osg/osg.dsp @@ -197,6 +197,10 @@ SOURCE=..\..\src\osg\Geode.cpp # End Source File # Begin Source File +SOURCE=..\..\src\osg\Geometry.cpp +# End Source File +# Begin Source File + SOURCE=..\..\src\osg\GeoSet.cpp # End Source File # Begin Source File @@ -493,6 +497,10 @@ SOURCE=..\..\Include\Osg\Geode # End Source File # Begin Source File +SOURCE=..\..\Include\Osg\Geometry +# End Source File +# Begin Source File + SOURCE=..\..\Include\Osg\GeoSet # End Source File # Begin Source File diff --git a/include/osg/Geometry b/include/osg/Geometry new file mode 100644 index 000000000..c79892bb1 --- /dev/null +++ b/include/osg/Geometry @@ -0,0 +1,200 @@ +//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 { + + +class AttributeArray : public Object +{ + + public: + + AttributeArray(GLint dataSize=0,GLenum dataType=0):_dataSize(dataSize),_dataType(dataType) {} + + AttributeArray(const AttributeArray& array,const CopyOp& copyop=CopyOp::SHALLOW_COPY): + Object(array,copyop), + _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"; } + + + GLint dataSize() const { return _dataSize; } + GLenum dataType() const { return _dataType; } + virtual const GLvoid* dataPointer() const = 0; + + protected: + + virtual ~AttributeArray() {} + + GLint _dataSize; + GLenum _dataType; +}; + +static char* s_classNames[] = { + "ByteArray", // 0 + "ShortArray", // 1 + "IntArray", // 2 + "UByteArray", // 3 + "UShortArray", // 4 + "UIntArray", // 5 + "FloatArray", // 6 + "Vec2Array", // 7 + "Vec3Array", // 8 + "Vec4Array" // 9 + }; + +template +class TemplateArray : public AttributeArray, public std::vector +{ + public: + + TemplateArray() : TemplateArray(DataSize,DataType) {} + + TemplateArray(const T& t,const CopyOp& copyop=CopyOp::SHALLOW_COPY) : AttributeArray(t,copyop), std::vector(t) {} + + 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[ClassName]; } + + virtual const GLvoid* dataPointer() const { if (!empty()) return &(*begin()); else return 0; } +}; + +typedef TemplateArray ByteArray; +typedef TemplateArray ShortArray; +typedef TemplateArray IntArray; +typedef TemplateArray UByteArray; +typedef TemplateArray UShortArray; +typedef TemplateArray UIntArray; +typedef TemplateArray FloatArray; +typedef TemplateArray Vec2Array; +typedef TemplateArray Vec3Array; +typedef TemplateArray Vec4Array; + + +/** 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 PrimitiveType + { + 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 + }; + + enum AttributeType + { + PRIMITIVES, + COORDS, + NORMALS, + COLORS, + TEX_COORDS, + TEX_COORDS_0 = TEX_COORDS, + TEX_COORDS_1, + TEX_COORDS_2, + TEX_COORDS_3, + TEX_COORDS_4, + TEX_COORDS_5, + TEX_COORDS_6, + TEX_COORDS_7 + }; + + void setAttribute(AttributeType type,AttributeArray* array); + + void setAttribute(AttributeType type,AttributeArray* array,AttributeArray* indices); + + AttributeArray* getAttribute(AttributeType type); + + void setIndices(AttributeType type,AttributeArray* indices); + + AttributeArray* getIndices(AttributeType type); + + + /** 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; + + void setTexCoordArray(unsigned int pos,AttributeArray*); + AttributeArray* getTexCoordArray(unsigned int pos); + + void setTexCoordIndicesArray(unsigned int pos,AttributeArray*); + AttributeArray* getTexCoordIndicesArray(unsigned int pos); + + typedef std::vector< ref_ptr > TexCoordList; + + ref_ptr _primitives; + + ref_ptr _coords; + ref_ptr _coordIndices; + + ref_ptr _normals; + ref_ptr _normalIndices; + + ref_ptr _colors; + ref_ptr _colorIndices; + + TexCoordList _texCoordList; + TexCoordList _texCoordIndicesList; + +}; + + +} + +#endif diff --git a/src/osg/Geometry.cpp b/src/osg/Geometry.cpp new file mode 100644 index 000000000..ac6c80bf0 --- /dev/null +++ b/src/osg/Geometry.cpp @@ -0,0 +1,230 @@ +#include + +using namespace osg; + +Geometry::Geometry() +{ +} + +Geometry::Geometry(const Geometry& geometry,const CopyOp& copyop): + Drawable(geometry,copyop), + _coords(geometry._coords), + _coordIndices(geometry._coordIndices), + _normals(geometry._normals), + _normalIndices(geometry._normalIndices), + _colors(geometry._colors), + _colorIndices(geometry._colorIndices), + _texCoordList(geometry._texCoordList), + _texCoordIndicesList(geometry._texCoordIndicesList) +{ +} + +Geometry::~Geometry() +{ + // no need to delete, all automatically handled by ref_ptr :-) +} + +void Geometry::setTexCoordArray(unsigned int pos,AttributeArray* array) +{ + if (_texCoordList.size()<=pos) + _texCoordList.resize(pos+1,0); + + _texCoordList[pos] = array; +} + +AttributeArray* Geometry::getTexCoordArray(unsigned int pos) +{ + if (pos<_texCoordList.size()) return _texCoordList[pos].get(); + else return 0; +} + +void Geometry::setTexCoordIndicesArray(unsigned int pos,AttributeArray* array) +{ + if (_texCoordList.size()<=pos) + _texCoordList.resize(pos+1,0); + + _texCoordIndicesList[pos] = array; +} + +AttributeArray* Geometry::getTexCoordIndicesArray(unsigned int pos) +{ + if (pos<_texCoordIndicesList.size()) return _texCoordIndicesList[pos].get(); + else return 0; +} + +void Geometry::setAttribute(AttributeType type,AttributeArray* array) +{ + switch(type) + { + case(PRIMITIVES): + _primitives = array; + break; + case(COORDS): + _coords = array; + break; + case(NORMALS): + _normals = array; + break; + case(COLORS): + _colors = array; + break; + default: + if (type>=TEX_COORDS_0) + { + setTexCoordArray(type-TEX_COORDS_0,array); + } + break; + } +} + + +void Geometry::setAttribute(AttributeType type,AttributeArray* array,AttributeArray* indices) +{ + switch(type) + { + case(PRIMITIVES): + _primitives = array; + // indices not appropriate! + break; + case(COORDS): + _coords = array; + _coordIndices = indices; + break; + case(NORMALS): + _normals = array; + _normalIndices = indices; + break; + case(COLORS): + _colors = array; + _colorIndices = indices; + break; + default: + if (type>=TEX_COORDS_0) + { + setTexCoordArray(type-TEX_COORDS_0,array); + setTexCoordIndicesArray(type-TEX_COORDS_0,indices); + } + break; + } +} + + +AttributeArray* Geometry::getAttribute(AttributeType type) +{ + switch(type) + { + case(PRIMITIVES): + return _primitives.get(); + break; + case(COORDS): + return _coords.get(); + break; + case(NORMALS): + return _normals.get(); + break; + case(COLORS): + return _colors.get(); + break; + default: + if (type>=TEX_COORDS_0) + { + return getTexCoordArray(type-TEX_COORDS_0); + } + break; + } + return 0; +} + + +void Geometry::setIndices(AttributeType type,AttributeArray* indices) +{ + switch(type) + { + case(PRIMITIVES): + // indices not appropriate! + break; + case(COORDS): + _coordIndices = indices; + break; + case(NORMALS): + _normalIndices = indices; + break; + case(COLORS): + _colorIndices = indices; + break; + default: + if (type>=TEX_COORDS_0) + { + setTexCoordIndicesArray(type-TEX_COORDS_0,indices); + } + break; + } +} + + +AttributeArray* Geometry::getIndices(AttributeType type) +{ + switch(type) + { + case(COORDS): + return _coordIndices.get(); + break; + case(NORMALS): + return _normalIndices.get(); + break; + case(COLORS): + return _colorIndices.get(); + break; + default: + if (type>=TEX_COORDS_0) + { + return getTexCoordIndicesArray(type-TEX_COORDS_0); + } + break; + } + return 0; +} + + +void Geometry::drawImmediateMode(State& /*state*/) +{ +} + +/** Statistics collection for each drawable- 26.09.01 + */ +bool Geometry::getStats(Statistics &) +{ + return false; +} + +Drawable::AttributeBitMask Geometry::suppportsAttributeOperation() const +{ + return 0; +} + + +/** return the attributes successully applied in applyAttributeUpdate.*/ +Drawable::AttributeBitMask Geometry::applyAttributeOperation(AttributeFunctor& ) +{ + return 0; +} + +const bool Geometry::computeBound() const +{ + _bbox.init(); + + const Vec3Array* coords = dynamic_cast(_coords.get()); + if (coords) + { + for(Vec3Array::const_iterator itr=coords->begin(); + itr!=coords->end(); + ++itr) + { + _bbox.expandBy(*itr); + } + } + _bbox_computed = true; + + return _bbox.valid(); +} + diff --git a/src/osg/Makefile b/src/osg/Makefile index 0ae00f285..82791807c 100644 --- a/src/osg/Makefile +++ b/src/osg/Makefile @@ -28,6 +28,7 @@ CXXFILES =\ FrameStamp.cpp\ FrontFace.cpp\ GLExtensions.cpp\ + Geometry.cpp\ GeoSet.cpp\ GeoSet_ogl.cpp\ Geode.cpp\