From b50f0db0a9c04fe14d031ec7be3f825dde6ae957 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 27 Aug 2004 08:01:17 +0000 Subject: [PATCH] Added handling of DrawArrayLengths PrimitiveSet. --- include/osgUtil/Tesselator | 1 + src/osgUtil/Tesselator.cpp | 195 ++++++++++++++++++++++--------------- 2 files changed, 120 insertions(+), 76 deletions(-) diff --git a/include/osgUtil/Tesselator b/include/osgUtil/Tesselator index 2d98592d9..273ef5d56 100644 --- a/include/osgUtil/Tesselator +++ b/include/osgUtil/Tesselator @@ -115,6 +115,7 @@ class OSGUTIL_EXPORT Tesselator : public osg::Referenced void collectTesselation(osg::Geometry &cxgeom); typedef std::map VertexPtrToIndexMap; + void addContour(GLenum mode, unsigned int first, unsigned int last, osg::Vec3Array* vertices); void addContour(osg::PrimitiveSet* primitive, osg::Vec3Array* vertices); void handleNewVertices(osg::Geometry& geom,VertexPtrToIndexMap &vertexPtrToIndexMap); diff --git a/src/osgUtil/Tesselator.cpp b/src/osgUtil/Tesselator.cpp index 390081c03..2f4a842e7 100644 --- a/src/osgUtil/Tesselator.cpp +++ b/src/osgUtil/Tesselator.cpp @@ -208,14 +208,38 @@ void Tesselator::retesselatePolygons(osg::Geometry &geom) for(int primNo=0;primNo primitive = _Contours[primNo].get(); - if (_ttype==TESS_TYPE_POLYGONS || _ttype==TESS_TYPE_DRAWABLE) { - if (primitive->getMode()==osg::PrimitiveSet::POLYGON ||_ttype==TESS_TYPE_DRAWABLE) { - beginTesselation(); - addContour(primitive.get(), vertices); - endTesselation(); + if (_ttype==TESS_TYPE_POLYGONS || _ttype==TESS_TYPE_DRAWABLE) + { + if (primitive->getMode()==osg::PrimitiveSet::POLYGON || _ttype==TESS_TYPE_DRAWABLE) + { - collectTesselation(geom); - } else { // copy the contour primitive as it is not being tesselated + if (primitive->getType()==osg::PrimitiveSet::DrawArrayLengthsPrimitiveType) + { + osg::DrawArrayLengths* drawArrayLengths = static_cast(primitive.get()); + unsigned int first = drawArrayLengths->getFirst(); + for(osg::DrawArrayLengths::iterator itr=drawArrayLengths->begin(); + itr!=drawArrayLengths->end(); + ++itr) + { + beginTesselation(); + unsigned int last = first + *itr; + addContour(primitive->getMode(),first,last,vertices); + first = last; + endTesselation(); + collectTesselation(geom); + } + } + else + { + beginTesselation(); + addContour(primitive.get(), vertices); + endTesselation(); + collectTesselation(geom); + } + + } + else + { // copy the contour primitive as it is not being tesselated geom.addPrimitiveSet(primitive.get()); } } else { @@ -241,10 +265,78 @@ void Tesselator::retesselatePolygons(osg::Geometry &geom) } } +void Tesselator::addContour(GLenum mode, unsigned int first, unsigned int last, osg::Vec3Array* vertices) +{ + beginContour(); + + unsigned int idx=0; + unsigned int nperprim=0; // number of vertices per primitive + if (mode==osg::PrimitiveSet::QUADS) nperprim=4; + else if (mode==osg::PrimitiveSet::TRIANGLES) nperprim=3; + + unsigned int i; + switch (mode) + { + case osg::PrimitiveSet::QUADS: + case osg::PrimitiveSet::TRIANGLES: + case osg::PrimitiveSet::POLYGON: + case osg::PrimitiveSet::LINE_LOOP: + case osg::PrimitiveSet::TRIANGLE_FAN: + { + for(i=first;i0 && i=first;i-=2) + { // ...5,3,1 + addVertex(&((*vertices)[i])); + } + } + break; + case osg::PrimitiveSet::TRIANGLE_STRIP: + { + for( i=first;ifirst&& i0 && igetMode()==osg::PrimitiveSet::QUADS) nperprim=4; if (primitive->getMode()==osg::PrimitiveSet::TRIANGLES) nperprim=3; @@ -254,73 +346,19 @@ void Tesselator::addContour(osg::PrimitiveSet* primitive, osg::Vec3Array* vertic { case(osg::PrimitiveSet::DrawArraysPrimitiveType): { - unsigned int i; osg::DrawArrays* drawArray = static_cast(primitive); unsigned int first = drawArray->getFirst(); unsigned int last = first+drawArray->getCount(); - - switch (primitive->getMode()) { - case osg::PrimitiveSet::QUADS: - case osg::PrimitiveSet::TRIANGLES: - case osg::PrimitiveSet::POLYGON: - case osg::PrimitiveSet::LINE_LOOP: - case osg::PrimitiveSet::TRIANGLE_FAN: - { - for(i=first;i0 && i=first;i-=2) - { // ...5,3,1 - addVertex(&((*vertices)[i])); - } - } - break; - case osg::PrimitiveSet::TRIANGLE_STRIP: - { - for( i=first;ifirst&& i0 && igetMode(),first,last,vertices); break; - } + } case(osg::PrimitiveSet::DrawElementsUBytePrimitiveType): { + beginContour(); osg::DrawElementsUByte* drawElements = static_cast(primitive); for(osg::DrawElementsUByte::iterator indexItr=drawElements->begin(); - indexItr!=drawElements->end(); - ++indexItr, idx++) + indexItr!=drawElements->end(); + ++indexItr, idx++) { addVertex(&((*vertices)[*indexItr])); if (nperprim>0 && indexItr!=drawElements->end() && idx%nperprim==nperprim-1) { @@ -328,14 +366,16 @@ void Tesselator::addContour(osg::PrimitiveSet* primitive, osg::Vec3Array* vertic beginContour(); } } + endContour(); break; } case(osg::PrimitiveSet::DrawElementsUShortPrimitiveType): { + beginContour(); osg::DrawElementsUShort* drawElements = static_cast(primitive); for(osg::DrawElementsUShort::iterator indexItr=drawElements->begin(); - indexItr!=drawElements->end(); - ++indexItr, idx++) + indexItr!=drawElements->end(); + ++indexItr, idx++) { addVertex(&((*vertices)[*indexItr])); if (nperprim>0 && indexItr!=drawElements->end() && idx%nperprim==nperprim-1) { @@ -343,14 +383,16 @@ void Tesselator::addContour(osg::PrimitiveSet* primitive, osg::Vec3Array* vertic beginContour(); } } + endContour(); break; } case(osg::PrimitiveSet::DrawElementsUIntPrimitiveType): { + beginContour(); osg::DrawElementsUInt* drawElements = static_cast(primitive); for(osg::DrawElementsUInt::iterator indexItr=drawElements->begin(); - indexItr!=drawElements->end(); - ++indexItr, idx++) + indexItr!=drawElements->end(); + ++indexItr, idx++) { addVertex(&((*vertices)[*indexItr])); if (nperprim>0 && indexItr!=drawElements->end() && idx%nperprim==nperprim-1) { @@ -358,13 +400,14 @@ void Tesselator::addContour(osg::PrimitiveSet* primitive, osg::Vec3Array* vertic beginContour(); } } + endContour(); break; } default: + osg::notify(osg::NOTICE)<<"Tesselator::addContour(primitive, vertices) : Primitive type "<getType()<<" not handled"<array.valid()) { @@ -410,8 +453,8 @@ void Tesselator::handleNewVertices(osg::Geometry& geom,VertexPtrToIndexMap &vert // now add any new vertices that are required. for(NewVertexList::iterator itr=_newVertexList.begin(); - itr!=_newVertexList.end(); - ++itr) + itr!=_newVertexList.end(); + ++itr) { NewVertex& newVertex = (*itr); osg::Vec3* vertex = newVertex._vpos;