From 16291da3b023616d6b79259a61dbde9384d4a3dc Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 9 Oct 2002 15:43:56 +0000 Subject: [PATCH] Fixes to smoothing visitor and tesselator to handle indexed Geometry. --- include/osg/PrimitiveSet | 58 ++++++++++++++++++++++++++------ src/osg/PrimitiveSet.cpp | 5 ++- src/osgUtil/SmoothingVisitor.cpp | 1 + src/osgUtil/Tesselator.cpp | 27 ++++++++++++++- 4 files changed, 76 insertions(+), 15 deletions(-) diff --git a/include/osg/PrimitiveSet b/include/osg/PrimitiveSet index 6c1cd9189..e73edf430 100644 --- a/include/osg/PrimitiveSet +++ b/include/osg/PrimitiveSet @@ -125,9 +125,27 @@ class PrimitiveSet : public Object virtual void accept(Drawable::PrimitiveFunctor&) {} + virtual unsigned int index(unsigned int pos) const = 0; + virtual unsigned int getNumIndices() const = 0; virtual void offsetIndices(int offset) = 0; - - virtual unsigned int getNumIndices() = 0; + + virtual unsigned int getNumPrimitives() const + { + switch(_mode) + { + case(POINTS): return getNumIndices(); + case(LINES): return getNumIndices()/2; + case(TRIANGLES): return getNumIndices()/3; + case(QUADS): return getNumIndices()/4; + case(LINE_STRIP): + case(LINE_LOOP): + case(TRIANGLE_STRIP): + case(TRIANGLE_FAN): + case(QUAD_STRIP): + case(POLYGON): return 1; + } + return 0; + } protected: @@ -183,10 +201,10 @@ class SG_EXPORT DrawArrays : public PrimitiveSet virtual void accept(Drawable::PrimitiveFunctor& functor); + virtual unsigned int getNumIndices() const { return _count; } + virtual unsigned int index(unsigned int pos) const { return _first+pos; } virtual void offsetIndices(int offset) { _first += offset; } - virtual unsigned int getNumIndices() { return _count; } - protected: GLint _first; @@ -236,9 +254,27 @@ class SG_EXPORT DrawArrayLengths : public PrimitiveSet, public VectorSizei virtual void accept(Drawable::PrimitiveFunctor& functor); + virtual unsigned int getNumIndices() const; + virtual unsigned int index(unsigned int pos) const { return _first+pos; } virtual void offsetIndices(int offset) { _first += offset; } - virtual unsigned int getNumIndices(); + virtual unsigned int getNumPrimitives() const + { + switch(_mode) + { + case(POINTS): return getNumIndices(); + case(LINES): return getNumIndices()/2; + case(TRIANGLES): return getNumIndices()/3; + case(QUADS): return getNumIndices()/4; + case(LINE_STRIP): + case(LINE_LOOP): + case(TRIANGLE_STRIP): + case(TRIANGLE_FAN): + case(QUAD_STRIP): + case(POLYGON): return size(); + } + return 0; + } protected: @@ -279,9 +315,9 @@ class SG_EXPORT DrawElementsUByte : public PrimitiveSet, public VectorUByte virtual void accept(Drawable::PrimitiveFunctor& functor); + virtual unsigned int getNumIndices() const { return size(); } + virtual unsigned int index(unsigned int pos) const { return (*this)[pos]; } virtual void offsetIndices(int offset); - - virtual unsigned int getNumIndices() { return size(); } }; @@ -319,9 +355,9 @@ class SG_EXPORT DrawElementsUShort : public PrimitiveSet, public VectorUShort virtual void accept(Drawable::PrimitiveFunctor& functor); + virtual unsigned int getNumIndices() const { return size(); } + virtual unsigned int index(unsigned int pos) const { return (*this)[pos]; } virtual void offsetIndices(int offset); - - virtual unsigned int getNumIndices() { return size(); } }; class SG_EXPORT DrawElementsUInt : public PrimitiveSet, public VectorUInt @@ -358,9 +394,9 @@ class SG_EXPORT DrawElementsUInt : public PrimitiveSet, public VectorUInt virtual void accept(Drawable::PrimitiveFunctor& functor); + virtual unsigned int getNumIndices() const { return size(); } + virtual unsigned int index(unsigned int pos) const { return (*this)[pos]; } virtual void offsetIndices(int offset); - - virtual unsigned int getNumIndices() { return size(); } }; } diff --git a/src/osg/PrimitiveSet.cpp b/src/osg/PrimitiveSet.cpp index 96d7c8d7a..d6da45d3c 100644 --- a/src/osg/PrimitiveSet.cpp +++ b/src/osg/PrimitiveSet.cpp @@ -36,10 +36,10 @@ void DrawArrayLengths::accept(Drawable::PrimitiveFunctor& functor) } } -unsigned int DrawArrayLengths::getNumIndices() +unsigned int DrawArrayLengths::getNumIndices() const { unsigned int count = 0; - for(VectorSizei::iterator itr=begin(); + for(VectorSizei::const_iterator itr=begin(); itr!=end(); ++itr) { @@ -48,7 +48,6 @@ unsigned int DrawArrayLengths::getNumIndices() return count; } - void DrawElementsUByte::draw() const { glDrawElements(_mode,size(),GL_UNSIGNED_BYTE,&front()); diff --git a/src/osgUtil/SmoothingVisitor.cpp b/src/osgUtil/SmoothingVisitor.cpp index d5cc49618..3e2c82fd1 100644 --- a/src/osgUtil/SmoothingVisitor.cpp +++ b/src/osgUtil/SmoothingVisitor.cpp @@ -121,6 +121,7 @@ void SmoothingVisitor::smooth(osg::Geometry& geom) } geom.setNormalArray( normals ); + geom.setNormalIndices( geom.getVertexIndices() ); geom.setNormalBinding(osg::Geometry::BIND_PER_VERTEX); diff --git a/src/osgUtil/Tesselator.cpp b/src/osgUtil/Tesselator.cpp index fea633efc..0ae6e3eda 100644 --- a/src/osgUtil/Tesselator.cpp +++ b/src/osgUtil/Tesselator.cpp @@ -137,11 +137,26 @@ void Tesselator::retesselatePolygons(osg::Geometry& geom) Vec3Array* vertices = geom.getVertexArray(); if (!vertices || vertices->empty() || geom.getPrimitiveSetList().empty()) return; + + // we currently don't handle geometry which use indices... + if (geom.getVertexIndices() || + geom.getNormalIndices() || + geom.getColorIndices() || + geom.getSecondaryColorIndices() || + geom.getFogCoordIndices()) return; + + // not even text coord indices don't handle geometry which use indices... + for(unsigned int unit=0;unitgetMode()==osg::PrimitiveSet::POLYGON) + if (primitive->getMode()==osg::PrimitiveSet::POLYGON && primitive->getNumIndices()>3) { beginTesselation(); beginContour(); @@ -225,6 +240,16 @@ void Tesselator::retesselatePolygons(osg::Geometry& geom) arrays.push_back(geom.getColorArray()); } + if (geom.getSecondaryColorBinding()==osg::Geometry::BIND_PER_VERTEX) + { + arrays.push_back(geom.getSecondaryColorArray()); + } + + if (geom.getFogCoordBinding()==osg::Geometry::BIND_PER_VERTEX) + { + arrays.push_back(geom.getFogCoordArray()); + } + osg::Geometry::TexCoordArrayList& tcal = geom.getTexCoordArrayList(); for(osg::Geometry::TexCoordArrayList::iterator tcalItr=tcal.begin(); tcalItr!=tcal.end();