From aac146c7bc7da1558645e488a8e8239c05c51063 Mon Sep 17 00:00:00 2001 From: Julien Valentin Date: Mon, 31 Jul 2017 02:43:50 +0200 Subject: [PATCH 1/3] correct a bug and make it more conservative --- examples/osgsimpleMDI/osgsimpleMDI.cpp | 95 +++++++++----------------- 1 file changed, 34 insertions(+), 61 deletions(-) diff --git a/examples/osgsimpleMDI/osgsimpleMDI.cpp b/examples/osgsimpleMDI/osgsimpleMDI.cpp index 7166f973d..e7814f2c3 100644 --- a/examples/osgsimpleMDI/osgsimpleMDI.cpp +++ b/examples/osgsimpleMDI/osgsimpleMDI.cpp @@ -38,7 +38,7 @@ #include #include -#include +#include #include #include @@ -51,7 +51,7 @@ /////////////////////////////////////////////////////////////////////////// #define MAXX 100 #define MAXY 100 - +//#define VIAINTERFACE 1 int main( int argc, char**argv ) { @@ -59,23 +59,25 @@ int main( int argc, char**argv ) bool MDIenable=true; if(arguments.read("--classic")) - MDIenable=false; + { MDIenable=false; + OSG_WARN<<"disabling MDI"< vertbo=new osg::VertexBufferObject; - osg::ref_ptr texbo=new osg::VertexBufferObject; osg::ref_ptr ebo=new osg::ElementBufferObject; ///create empty mdi osg::MultiDrawElementsIndirectUShort* mdi=new osg::MultiDrawElementsIndirectUShort(osg::PrimitiveSet::TRIANGLE_STRIP); osg::DefaultIndirectCommandDrawElements* mdicommands= new osg::DefaultIndirectCommandDrawElements(); mdi->setIndirectCommandArray(mdicommands); - mdi->setBufferObject(ebo); - osg::ref_ptr oldprHolder=new osg::Geometry(); + osg::ref_ptr geom=new osg::Geometry(); + geom->setUseVertexBufferObjects(true); + osg::BoundingBox bb; + bb.set(0,0,0,MAXX,0,MAXY); + //set bounds by hand cause of the lack of support of basevertex in PrimitiveFunctors + geom->setInitialBound(bb); osg::Vec3 myCoords[] = { @@ -85,13 +87,6 @@ int main( int argc, char**argv ) osg::Vec3(0.7f,0.0f,0.7f) }; - osg::Vec2 myTexCoords[] = - { - osg::Vec2(0,1), - osg::Vec2(0,0), - osg::Vec2(1,0), - osg::Vec2(1,1) - }; unsigned short myIndices[] = { 0, @@ -99,60 +94,38 @@ int main( int argc, char**argv ) 3, 2 }; + osg::Vec3Array * verts=new osg::Vec3Array(); + + for(int j =0 ; jsetUseVertexBufferObjects(true); - osg::Vec3Array * verts=new osg::Vec3Array(4,myCoords); - for(int z=0; z<4; z++)(*verts)[z]+=corner; - geom->setVertexArray(verts); - geom->setTexCoordArray(0,new osg::Vec2Array(4,myTexCoords)); - geom->addPrimitiveSet(new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLE_STRIP,4,myIndices)); - ///end generate indexed quad - - bb.expandBy(geom->getBoundingBox()); - - if(MDIenable) { - ///setup bos - geom->getVertexArray()->setBufferObject(vertbo); - geom->getTexCoordArray(0)->setBufferObject(texbo); - osg::DrawElementsUShort *dre = static_cast(geom->getPrimitiveSet(0)); - dre->setBufferObject(ebo); - - ///keep old pr alive for indices upload purpose - oldprHolder->addPrimitiveSet(dre); - geom->removePrimitiveSet(0,1); - - ///create indirect command - osg::DrawElementsIndirectCommand cmd; - cmd.count=4; - cmd.instanceCount=1; - cmd.firstIndex=root->getNumChildren()*4; - cmd.baseVertex=root->getNumChildren()*4; - mdicommands->push_back(cmd); - - if(cmd.firstIndex==0) { - ///only first geom have a mdi primset - geom->addPrimitiveSet(mdi); - ///avoid bound computation to fail because of the lack of support of basevertex in functors - geom->setComputeBoundingBoxCallback(new osg::Drawable::ComputeBoundingBoxCallback); - } + ///create indirect command + osg::DrawElementsIndirectCommand cmd; + cmd.count=4; + cmd.instanceCount=1; + cmd.firstIndex=verts->size(); + cmd.baseVertex=verts->size(); + mdicommands->push_back(cmd); + for(int z=0; z<4; z++) { + verts->push_back(osg::Vec3(i,0,j)+myCoords[z]); + mdi->addElement(myIndices[z]); } - root->addChild(geom); } } - + geom->setVertexArray(verts); if(MDIenable) { - ///override bounds - root->getDrawable(0)->setInitialBound(bb); - ///put it somewhere - root->setUserData( oldprHolder); - } + geom->addPrimitiveSet(mdi); + } else + for(int i=0; isetElementBufferObject(ebo); + geom->addPrimitiveSet(dre); + } + root->addChild(geom); osgViewer::Viewer viewer; viewer.addEventHandler(new osgViewer::StatsHandler); viewer.setSceneData( root ); From 3537460b0eb3408047faa2f9a56d7529c2677bc6 Mon Sep 17 00:00:00 2001 From: Julien Valentin Date: Mon, 31 Jul 2017 03:08:52 +0200 Subject: [PATCH 2/3] Add arguments, command line usage and use DrawElementsUInt for classic case --- examples/osgsimpleMDI/osgsimpleMDI.cpp | 38 +++++++++++++++++--------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/examples/osgsimpleMDI/osgsimpleMDI.cpp b/examples/osgsimpleMDI/osgsimpleMDI.cpp index e7814f2c3..13ca86dbb 100644 --- a/examples/osgsimpleMDI/osgsimpleMDI.cpp +++ b/examples/osgsimpleMDI/osgsimpleMDI.cpp @@ -38,7 +38,7 @@ #include #include -#include +#include #include #include @@ -49,13 +49,29 @@ /////////////////////////////////////////////////////////////////////////// -#define MAXX 100 -#define MAXY 100 -//#define VIAINTERFACE 1 +//#define MAXX 1000 +//#define MAXY 1000 + int main( int argc, char**argv ) { osg::ArgumentParser arguments(&argc,argv); + arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates Multi Indirect Draw with basevertex"); + arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] "); + arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); + arguments.getApplicationUsage()->addCommandLineOption("--numX","square count on X"); + arguments.getApplicationUsage()->addCommandLineOption("--numY","square count on Y"); + arguments.getApplicationUsage()->addCommandLineOption("--classic","disable MDI and use classic DrawElements"); + + if (arguments.read("-h") || arguments.read("--help")) + { + arguments.getApplicationUsage()->write(std::cout); + return 1; + } + + int MAXX=200;int MAXY=200; + arguments.read("--numX",MAXX); + arguments.read("--numY",MAXY); bool MDIenable=true; if(arguments.read("--classic")) @@ -87,13 +103,9 @@ int main( int argc, char**argv ) osg::Vec3(0.7f,0.0f,0.7f) }; - unsigned short myIndices[] = - { - 0, - 1, - 3, - 2 - }; + unsigned short myIndices[] = { 0, 1, 3, 2 }; + unsigned int myIndicesUI[] = { 0, 1, 3, 2 }; + osg::Vec3Array * verts=new osg::Vec3Array(); @@ -120,8 +132,8 @@ int main( int argc, char**argv ) } else for(int i=0; isetElementBufferObject(ebo); geom->addPrimitiveSet(dre); } From 9e9d3cd2491f1e9aa57c4e6b492fe28c4406b8d9 Mon Sep 17 00:00:00 2001 From: Julien Valentin Date: Mon, 31 Jul 2017 03:15:03 +0200 Subject: [PATCH 3/3] fix a bug : indices pre incremented should be post --- examples/osgsimpleMDI/osgsimpleMDI.cpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/examples/osgsimpleMDI/osgsimpleMDI.cpp b/examples/osgsimpleMDI/osgsimpleMDI.cpp index 13ca86dbb..be7bd2c31 100644 --- a/examples/osgsimpleMDI/osgsimpleMDI.cpp +++ b/examples/osgsimpleMDI/osgsimpleMDI.cpp @@ -47,11 +47,6 @@ - -/////////////////////////////////////////////////////////////////////////// -//#define MAXX 1000 -//#define MAXY 1000 - int main( int argc, char**argv ) { @@ -69,9 +64,10 @@ int main( int argc, char**argv ) return 1; } - int MAXX=200;int MAXY=200; - arguments.read("--numX",MAXX); - arguments.read("--numY",MAXY); + int MAXX=200; + int MAXY=200; + arguments.read("--numX",MAXX); + arguments.read("--numY",MAXY); bool MDIenable=true; if(arguments.read("--classic")) @@ -132,10 +128,10 @@ int main( int argc, char**argv ) } else for(int i=0; isetElementBufferObject(ebo); geom->addPrimitiveSet(dre); + for(int z=0; z<4; z++)myIndicesUI[z]+=4; } root->addChild(geom); osgViewer::Viewer viewer;