diff --git a/include/osg/Primitive b/include/osg/Primitive index 2f5aeabd7..3adae0213 100644 --- a/include/osg/Primitive +++ b/include/osg/Primitive @@ -126,6 +126,8 @@ class Primitive : public Object virtual void accept(Drawable::PrimitiveFunctor&) {} virtual void offsetIndices(int offset) = 0; + + virtual unsigned int getNumIndices() = 0; protected: @@ -178,6 +180,8 @@ class SG_EXPORT DrawArrays : public Primitive virtual void offsetIndices(int offset) { _first += offset; } + virtual unsigned int getNumIndices() { return _count; } + protected: GLint _first; @@ -229,6 +233,8 @@ class SG_EXPORT DrawArrayLengths : public Primitive, public VectorSizei virtual void offsetIndices(int offset) { _first += offset; } + virtual unsigned int getNumIndices(); + protected: GLint _first; @@ -269,6 +275,8 @@ class SG_EXPORT DrawElementsUByte : public Primitive, public VectorUByte virtual void accept(Drawable::PrimitiveFunctor& functor); virtual void offsetIndices(int offset); + + virtual unsigned int getNumIndices() { return size(); } }; @@ -307,6 +315,8 @@ class SG_EXPORT DrawElementsUShort : public Primitive, public VectorUShort virtual void accept(Drawable::PrimitiveFunctor& functor); virtual void offsetIndices(int offset); + + virtual unsigned int getNumIndices() { return size(); } }; class SG_EXPORT DrawElementsUInt : public Primitive, public VectorUInt @@ -344,6 +354,8 @@ class SG_EXPORT DrawElementsUInt : public Primitive, public VectorUInt virtual void accept(Drawable::PrimitiveFunctor& functor); virtual void offsetIndices(int offset); + + virtual unsigned int getNumIndices() { return size(); } }; // backwards compatibility with first incarnation of DrawElements nameing convention. diff --git a/src/osg/Primitive.cpp b/src/osg/Primitive.cpp index b505d66bb..0bd0260c5 100644 --- a/src/osg/Primitive.cpp +++ b/src/osg/Primitive.cpp @@ -36,6 +36,17 @@ void DrawArrayLengths::accept(Drawable::PrimitiveFunctor& functor) } } +unsigned int DrawArrayLengths::getNumIndices() +{ + unsigned int count = 0; + for(VectorSizei::iterator itr=begin(); + itr!=end(); + ++itr) + { + count += *itr; + } + return count; +} void DrawElementsUByte::draw() const diff --git a/src/osgPlugins/lwo/ReaderWriterLWO.cpp b/src/osgPlugins/lwo/ReaderWriterLWO.cpp index 231bebaa5..3e8e7f2af 100644 --- a/src/osgPlugins/lwo/ReaderWriterLWO.cpp +++ b/src/osgPlugins/lwo/ReaderWriterLWO.cpp @@ -198,7 +198,30 @@ osgDB::ReaderWriter::ReadResult ReaderWriterLWO::readNode_LWO1(const std::string { GeometryCollection& gc = mtgcm[face.material]; - gc._geom->addPrimitive(osgNew osg::DrawArrays(osg::Primitive::POLYGON,gc._coordCount,face.index_cnt)); + osg::Primitive::Mode mode; + switch(face.index_cnt) + { + case(0): + mode = osg::Primitive::POINTS; + break; + case(1): + mode = osg::Primitive::POINTS; + break; + case(2): + mode = osg::Primitive::LINES; + break; + case(3): + mode = osg::Primitive::TRIANGLES; + break; + case(4): + mode = osg::Primitive::QUADS; + break; + default: + mode = osg::Primitive::POLYGON; + break; + } + + gc._geom->addPrimitive(osgNew osg::DrawArrays(mode,gc._coordCount,face.index_cnt)); gc._coordCount += face.index_cnt; // From the spec_low.lxt : @@ -239,7 +262,7 @@ osgDB::ReaderWriter::ReadResult ReaderWriterLWO::readNode_LWO1(const std::string if (gc._geom) { - //tesselator.retesselatePolygons(*gc._geom); + tesselator.retesselatePolygons(*gc._geom); smoother.smooth(*gc._geom); diff --git a/src/osgUtil/Optimizer.cpp b/src/osgUtil/Optimizer.cpp index 1b4a6cf4b..79aec781f 100644 --- a/src/osgUtil/Optimizer.cpp +++ b/src/osgUtil/Optimizer.cpp @@ -64,20 +64,12 @@ void Optimizer::optimize(osg::Node* node, unsigned int options) if (options & SHARE_DUPLICATE_STATE) { -// #if (defined(_MSC_VER) && _MSC_VER<1300 && !defined(_STLPORT_VERSION)) -// osg::notify(osg::NOTICE)<<"Warning: this application was built with VisualStudio 6.0's native STL,"<accept(osv); osv.optimize(); MergeGeometryVisitor mgv; node->accept(mgv); -// #endif } @@ -1139,49 +1131,80 @@ struct LessGeometryPrimitiveType bool Optimizer::MergeGeometryVisitor::mergeGeode(osg::Geode& geode) { - if (geode.getNumDrawables()<2) return false; + if (geode.getNumDrawables()>=2) + { - typedef std::vector DuplicateList; - typedef std::map GeometryDuplicateMap; + typedef std::vector DuplicateList; + typedef std::map GeometryDuplicateMap; - GeometryDuplicateMap geometryDuplicateMap; - + GeometryDuplicateMap geometryDuplicateMap; + + unsigned int i; + for(i=0;i(geode.getDrawable(i)); + if (geom) + { + geom->computeCorrectBindingsAndArraySizes(); + + geometryDuplicateMap[geom].push_back(geom); + } + } + + for(GeometryDuplicateMap::iterator itr=geometryDuplicateMap.begin(); + itr!=geometryDuplicateMap.end(); + ++itr) + { + if (itr->second.size()>1) + { + std::sort(itr->second.begin(),itr->second.end(),LessGeometryPrimitiveType()); + osg::Geometry* lhs = itr->second[0]; + for(DuplicateList::iterator dupItr=itr->second.begin()+1; + dupItr!=itr->second.end(); + ++dupItr) + { + osg::Geometry* rhs = *dupItr; + if (mergeGeometry(*lhs,*rhs)) + { + geode.removeDrawable(rhs); + + static int co = 0; + osg::notify(osg::INFO)<<"merged and removed Geometry "<<++co<(geode.getDrawable(i)); if (geom) { - geom->computeCorrectBindingsAndArraySizes(); - - geometryDuplicateMap[geom].push_back(geom); - } - } - - for(GeometryDuplicateMap::iterator itr=geometryDuplicateMap.begin(); - itr!=geometryDuplicateMap.end(); - ++itr) - { - if (itr->second.size()>1) - { - std::sort(itr->second.begin(),itr->second.end(),LessGeometryPrimitiveType()); - osg::Geometry* lhs = itr->second[0]; - for(DuplicateList::iterator dupItr=itr->second.begin()+1; - dupItr!=itr->second.end(); - ++dupItr) + osg::Geometry::PrimitiveList& primitives = geom->getPrimitiveList(); + for(osg::Geometry::PrimitiveList::iterator itr=primitives.begin(); + itr!=primitives.end(); + ++itr) { - osg::Geometry* rhs = *dupItr; - if (mergeGeometry(*lhs,*rhs)) + osg::Primitive* prim = itr->get(); + if (prim->getMode()==osg::Primitive::POLYGON) { - geode.removeDrawable(rhs); - - static int co = 0; - osg::notify(osg::INFO)<<"merged and removed Geometry "<<++co<getNumIndices()==3) + { + prim->setMode(osg::Primitive::TRIANGLES); + } + else if (prim->getNumIndices()==4) + { + prim->setMode(osg::Primitive::QUADS); + } } } } } + // now merge any compatible primtives. for(i=0;igetType()) { case(osg::Primitive::DrawArraysPrimitiveType): diff --git a/src/osgUtil/Tesselator.cpp b/src/osgUtil/Tesselator.cpp index d9256fc96..0a76c66f7 100644 --- a/src/osgUtil/Tesselator.cpp +++ b/src/osgUtil/Tesselator.cpp @@ -38,6 +38,7 @@ void Tesselator::beginContour() if (_tobj) { gluTessBeginContour(_tobj); +// cout<<"begin"<_v,vertex); +// cout<<"\t"<<*vertex<