From d810d4a4c096a5515bf3c60fb7a69bb281567ea2 Mon Sep 17 00:00:00 2001 From: Julien Valentin Date: Sun, 30 Jul 2017 10:15:32 +0200 Subject: [PATCH] add a very simple example for mdi with basevertex --- examples/CMakeLists.txt | 1 + examples/osgsimpleMDI/CMakeLists.txt | 7 ++ examples/osgsimpleMDI/osgsimpleMDI.cpp | 160 +++++++++++++++++++++++++ 3 files changed, 168 insertions(+) create mode 100644 examples/osgsimpleMDI/CMakeLists.txt create mode 100644 examples/osgsimpleMDI/osgsimpleMDI.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index d695fe86f..9d54ac9ca 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -20,6 +20,7 @@ ELSE(ANDROID) IF(DYNAMIC_OPENSCENEGRAPH) + ADD_SUBDIRECTORY(osgsimpleMDI) ADD_SUBDIRECTORY(osg2cpp) ADD_SUBDIRECTORY(osganalysis) ADD_SUBDIRECTORY(osganimate) diff --git a/examples/osgsimpleMDI/CMakeLists.txt b/examples/osgsimpleMDI/CMakeLists.txt new file mode 100644 index 000000000..3c14efece --- /dev/null +++ b/examples/osgsimpleMDI/CMakeLists.txt @@ -0,0 +1,7 @@ +#this file is automatically generated + + +SET(TARGET_SRC osgsimpleMDI.cpp ) + +#### end var setup ### +SETUP_EXAMPLE(osgsimpleMDI) diff --git a/examples/osgsimpleMDI/osgsimpleMDI.cpp b/examples/osgsimpleMDI/osgsimpleMDI.cpp new file mode 100644 index 000000000..7166f973d --- /dev/null +++ b/examples/osgsimpleMDI/osgsimpleMDI.cpp @@ -0,0 +1,160 @@ +/* OpenSceneGraph example, osgtransformfeedback +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +/* file: examples/osgsimpleMDI/osgsimpleMDI.cpp +* author: Julien Valentin 2017-08-01 +* copyright: (C) 2013 +* license: OpenSceneGraph Public License (OSGPL) +* +* A simple example of mdi with basevertex +* +*/ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + + + + +/////////////////////////////////////////////////////////////////////////// +#define MAXX 100 +#define MAXY 100 + +int main( int argc, char**argv ) +{ + + osg::ArgumentParser arguments(&argc,argv); + + bool MDIenable=true; + if(arguments.read("--classic")) + MDIenable=false; + + osg::Geode* root( new osg::Geode ); + osg::BoundingBox bb; + + osg::Vec3 corner; + osg::ref_ptr 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::Vec3 myCoords[] = + { + osg::Vec3(0,0.0f,0.7f), + osg::Vec3(0,0.0f,0), + osg::Vec3(0.7,0.0f,0), + 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, + 1, + 3, + 2 + }; + 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); + } + + } + root->addChild(geom); + } + } + + if(MDIenable) { + ///override bounds + root->getDrawable(0)->setInitialBound(bb); + ///put it somewhere + root->setUserData( oldprHolder); + } + + osgViewer::Viewer viewer; + viewer.addEventHandler(new osgViewer::StatsHandler); + viewer.setSceneData( root ); + return viewer.run(); +}