From ddce3e0079e934bce13fd5350d9380a19ec15de2 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 30 Mar 2018 15:16:45 +0100 Subject: [PATCH] To osgclipe example added --text textstring and --simple command line options to test mixing text and clipping --- examples/osgclip/CMakeLists.txt | 5 +- examples/osgclip/osgclip.cpp | 100 ++++++++++++++++++++++++-------- 2 files changed, 79 insertions(+), 26 deletions(-) diff --git a/examples/osgclip/CMakeLists.txt b/examples/osgclip/CMakeLists.txt index acc0d137f..f657fb111 100644 --- a/examples/osgclip/CMakeLists.txt +++ b/examples/osgclip/CMakeLists.txt @@ -1,6 +1,7 @@ -#this file is automatically generated - +#this file is automatically generated SET(TARGET_SRC osgclip.cpp ) +SET(TARGET_ADDED_LIBRARIES osgText ) + #### end var setup ### SETUP_EXAMPLE(osgclip) diff --git a/examples/osgclip/osgclip.cpp b/examples/osgclip/osgclip.cpp index b6f8e5cb1..1e2f56691 100644 --- a/examples/osgclip/osgclip.cpp +++ b/examples/osgclip/osgclip.cpp @@ -38,6 +38,7 @@ #include #include +#include osg::ref_ptr decorate_with_clip_node(const osg::ref_ptr& subgraph) @@ -57,25 +58,6 @@ osg::ref_ptr decorate_with_clip_node(const osg::ref_ptr& s wireframe_subgraph->addChild(subgraph); rootnode->addChild(wireframe_subgraph); -/* - // simple approach to adding a clipnode above a subgraph. - - // create clipped part. - osg::ClipNode* clipped_subgraph = new osg::ClipNode; - - osg::BoundingSphere bs = subgraph->getBound(); - bs.radius()*= 0.4f; - - osg::BoundingBox bb; - bb.expandBy(bs); - - - clipped_subgraph->createClipBox(bb); - clipped_subgraph->addChild(subgraph); - rootnode->addChild(clipped_subgraph); -*/ - - // more complex approach to managing ClipNode, allowing // ClipNode node to be transformed independently from the subgraph // that it is clipping. @@ -110,34 +92,104 @@ osg::ref_ptr decorate_with_clip_node(const osg::ref_ptr& s } +osg::ref_ptr simple_decorate_with_clip_node(const osg::ref_ptr& subgraph) +{ + osg::ref_ptr rootnode = new osg::Group; + + + // more complex approach to managing ClipNode, allowing + // ClipNode node to be transformed independently from the subgraph + // that it is clipping. + + osg::MatrixTransform* transform= new osg::MatrixTransform; + + osg::NodeCallback* nc = new osg::AnimationPathCallback(subgraph->getBound().center(),osg::Vec3(0.0f,0.0f,1.0f),osg::inDegrees(45.0f)); + transform->setUpdateCallback(nc); + + osg::ClipNode* clipnode = new osg::ClipNode; + osg::BoundingSphere bs = subgraph->getBound(); + bs.radius()*= 0.4f; + + osg::BoundingBox bb; + bb.expandBy(bs); + + clipnode->createClipBox(bb); + clipnode->setCullingActive(false); + + transform->addChild(clipnode); + rootnode->addChild(transform); + + + // create clipped part. + osg::Group* clipped_subgraph = new osg::Group; + + clipped_subgraph->setStateSet(clipnode->getStateSet()); + clipped_subgraph->addChild(subgraph); + rootnode->addChild(clipped_subgraph); + + return rootnode; +} + int main( int argc, char **argv ) { // use an ArgumentParser object to manage the program arguments. osg::ArgumentParser arguments(&argc,argv); + osgViewer::Viewer viewer(arguments); + // load the nodes from the commandline arguments. + osg::ref_ptr scene = new osg::Group; + + std::string textString; + while(arguments.read("--text", textString)) + { + osg::ref_ptr text = new osgText::Text; + text->setFont("fonts/times.ttf"); + text->setAxisAlignment(osgText::Text::XZ_PLANE); + text->setDrawMode(osgText::Text::TEXT|osgText::Text::ALIGNMENT|osgText::Text::BOUNDINGBOX|osgText::Text::FILLEDBOUNDINGBOX); + text->setText(textString); + scene->addChild(text); + } + + osg::ref_ptr loadedModel = osgDB::readRefNodeFiles(arguments); + if (loadedModel.valid()) + { + scene->addChild(scene.get()); + } // if not loaded assume no arguments passed in, try use default mode instead. - if (!loadedModel) loadedModel = osgDB::readRefNodeFile("cow.osgt"); + if (scene->getNumChildren()==0) + { + loadedModel = osgDB::readRefNodeFile("cow.osgt"); + scene->addChild(loadedModel.get()); + } - if (!loadedModel) + if (scene->getNumChildren()==0) { osg::notify(osg::NOTICE)<<"Please specify a filename on the command line"< rootnode = decorate_with_clip_node(loadedModel); + osg::ref_ptr rootnode; + + + if (arguments.read("--simple")) + { + rootnode = simple_decorate_with_clip_node(scene); + } + else + { + rootnode = decorate_with_clip_node(scene); + } // run optimization over the scene graph osgUtil::Optimizer optimzer; optimzer.optimize(rootnode); - osgViewer::Viewer viewer; - // set the scene to render viewer.setSceneData(rootnode);