From eb59590d77bcb720e98a4fb87320065871b664db Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 28 Mar 2017 18:29:26 +0100 Subject: [PATCH] Added handling of large shapes --- examples/osgshape/osgshape.cpp | 50 ++++++++++++++++++++++++++-------- src/osg/Shape.cpp | 36 ++++++++++++++---------- 2 files changed, 61 insertions(+), 25 deletions(-) diff --git a/examples/osgshape/osgshape.cpp b/examples/osgshape/osgshape.cpp index 8fe695629..d82eb7821 100644 --- a/examples/osgshape/osgshape.cpp +++ b/examples/osgshape/osgshape.cpp @@ -33,7 +33,7 @@ // for the grid data.. #include "../osghangglide/terrain_coords.h" -osg::Geode* createShapes() +osg::Geode* createShapes(osg::ArgumentParser& arguments) { osg::Geode* geode = new osg::Geode(); @@ -70,17 +70,43 @@ osg::Geode* createShapes() geode->addDrawable(new osg::ShapeDrawable(new osg::Capsule(osg::Vec3(8.0f,0.0f,0.0f),radius,height),hints)); osg::HeightField* grid = new osg::HeightField; - grid->allocate(38,39); - grid->setXInterval(0.28f); - grid->setYInterval(0.28f); - - for(unsigned int r=0;r<39;++r) + if (arguments.read("--large")) { - for(unsigned int c=0;c<38;++c) + unsigned int numX = 512; + unsigned int numY = 512; + double sizeX = 10.0; + double sizeY = 10.0; + grid->allocate(numX,numY); + grid->setXInterval(sizeX/float(numX)); + grid->setYInterval(sizeY/float(numY)); + + for(unsigned int r=0;rsetHeight(c,r,vertex[r+c*39][2]); + for(unsigned int c=0;csetHeight(c, r, 2.0*sin(rx*ry*4.0*osg::PI)); + } } } + else + { + grid->allocate(38,39); + grid->setXInterval(0.28f); + grid->setYInterval(0.28f); + + for(unsigned int r=0;r<39;++r) + { + for(unsigned int c=0;c<38;++c) + { + grid->setHeight(c,r,vertex[r+c*39][2]); + } + } + } + + geode->addDrawable(new osg::ShapeDrawable(grid)); osg::ConvexHull* mesh = new osg::ConvexHull; @@ -109,13 +135,15 @@ osg::Geode* createShapes() return geode; } -int main(int, char **) +int main(int argc, char **argv) { + osg::ArgumentParser arguments(&argc,argv); + // construct the viewer. - osgViewer::Viewer viewer; + osgViewer::Viewer viewer(arguments); // add model to viewer. - viewer.setSceneData( createShapes() ); + viewer.setSceneData( createShapes(arguments) ); // add the state manipulator viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); diff --git a/src/osg/Shape.cpp b/src/osg/Shape.cpp index 67d4d71f8..e54dd8c2e 100644 --- a/src/osg/Shape.cpp +++ b/src/osg/Shape.cpp @@ -243,10 +243,15 @@ void BuildShapeGeometryVisitor::End() { if (_start_index>=_vertices->size()) return; + bool smallPrimitiveSet = _vertices->size() < 65536; + // OSG_NOTICE<<"BuildShapeGeometryVisitor::End() smallPrimitiveSet = "< primitives = new osg::DrawElementsUShort(GL_TRIANGLES); + osg::ref_ptr primitives = smallPrimitiveSet ? + static_cast(new osg::DrawElementsUShort(GL_TRIANGLES)) : + static_cast(new osg::DrawElementsUInt(GL_TRIANGLES)); + _geometry->addPrimitiveSet(primitives.get()); for(unsigned int i=_start_index; i<_vertices->size(); i+=4) @@ -256,18 +261,21 @@ void BuildShapeGeometryVisitor::End() unsigned int p2 = i+2; unsigned int p3 = i+3; - primitives->push_back(p0); - primitives->push_back(p3); - primitives->push_back(p1); + primitives->addElement(p0); + primitives->addElement(p3); + primitives->addElement(p1); - primitives->push_back(p1); - primitives->push_back(p3); - primitives->push_back(p2); + primitives->addElement(p1); + primitives->addElement(p3); + primitives->addElement(p2); } } else if (_mode==GL_QUAD_STRIP) { - osg::ref_ptr primitives = new osg::DrawElementsUShort(GL_TRIANGLES); + osg::ref_ptr primitives = smallPrimitiveSet ? + static_cast(new osg::DrawElementsUShort(GL_TRIANGLES)) : + static_cast(new osg::DrawElementsUInt(GL_TRIANGLES)); + _geometry->addPrimitiveSet(primitives.get()); for(unsigned int i=_start_index; i<_vertices->size()-2; i+=2) @@ -277,13 +285,13 @@ void BuildShapeGeometryVisitor::End() unsigned int p2 = i+2; unsigned int p3 = i+3; - primitives->push_back(p0); - primitives->push_back(p2); - primitives->push_back(p1); + primitives->addElement(p0); + primitives->addElement(p2); + primitives->addElement(p1); - primitives->push_back(p1); - primitives->push_back(p2); - primitives->push_back(p3); + primitives->addElement(p1); + primitives->addElement(p2); + primitives->addElement(p3); } } else