From 480b452e3824578a763458a4e13920c6c81a4070 Mon Sep 17 00:00:00 2001 From: Julien Valentin Date: Sat, 13 Jan 2018 21:29:02 +0100 Subject: [PATCH] improve example to get command arguments --- examples/osgsampler/osgSampler.cpp | 218 ++++++++++++++++------------- 1 file changed, 122 insertions(+), 96 deletions(-) diff --git a/examples/osgsampler/osgSampler.cpp b/examples/osgsampler/osgSampler.cpp index c789bb7c9..1ab2104fa 100644 --- a/examples/osgsampler/osgSampler.cpp +++ b/examples/osgsampler/osgSampler.cpp @@ -1,5 +1,5 @@ /* -*-c++-*- - * Copyright (C) 2017 Julien Valentin + * Copyright (C) 2018 Julien Valentin * * This library is open source and may be redistributed and/or modified under * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or @@ -23,6 +23,7 @@ #include #include +#include /// add two composite texture color static const char* fragSource = @@ -37,12 +38,12 @@ static const char* fragSource = }; -class KeyboardEventHandler : public osgGA::GUIEventHandler +class SamplersKeyboardEventHandler : public osgGA::GUIEventHandler { public: - KeyboardEventHandler(osg::Sampler* tessInnerU, osg::Sampler* tessOuterU,osg::StateSet* ss): + SamplersKeyboardEventHandler(osg::Sampler* tessInnerU, osg::Sampler* tessOuterU): _sampler1(tessInnerU), - _sampler2(tessOuterU),_ss(ss){} + _sampler2(tessOuterU){} virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& gaa) { @@ -63,7 +64,6 @@ public: _sampler2->setFilter(osg::Texture::MAG_FILTER,newone); _sampler2->setFilter(osg::Texture::MIN_FILTER,newone); return true; - } } return osgGA::GUIEventHandler::handle(ea, gaa); @@ -72,10 +72,21 @@ public: private: osg::Sampler* _sampler1; osg::Sampler* _sampler2; - osg::StateSet* _ss; }; +class StateCollec: public osg::NodeVisitor{ +public: + StateCollec():osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN){} + virtual void apply(osg::Node& node){ + osg::StateSet *ss=node.getStateSet(); + if(ss) _statesets.insert(ss); + traverse(node); + } + std::set _statesets; + std::set & getStateSets(){return _statesets;} +}; + int main(int argc, char* argv[]) { osg::ArgumentParser arguments(&argc,argv); @@ -92,116 +103,131 @@ int main(int argc, char* argv[]) osg::ref_ptr loadedModel = osgDB::readRefNodeFiles(arguments); osg::ref_ptr geode ; - if (!loadedModel) + + osg::ref_ptr sampler1, sampler2; + if (loadedModel) { - geode = new osg::Geode; - ((osg::Geode*)geode.get())->addDrawable(osg::createTexturedQuadGeometry(osg::Vec3(0,0,0),osg::Vec3(1,0,0),osg::Vec3(0,0,1) )); + ///ensure loaded have Sampler + geode=loadedModel; + StateCollec sc; + geode->accept(sc); + for(std::set::iterator it=sc.getStateSets().begin();it != sc.getStateSets().end(); ++it) + { + osg::Sampler::generateSamplerObjects(*(*it)); + if(!sampler1.valid()) + sampler1 = sampler2 = (osg::Sampler*)(*it)->getTextureAttribute(0, osg::StateAttribute::SAMPLER); + else + sampler2=(osg::Sampler*)(*it)->getTextureAttribute(0, osg::StateAttribute::SAMPLER); + } + if(sampler1.valid()&&sampler2.valid()){ + OSG_WARN<<"2samplers manipulator setted"<addDrawable(osg::createTexturedQuadGeometry(osg::Vec3(0,0,0),osg::Vec3(1,0,0),osg::Vec3(0,0,1) )); - osg::ref_ptr< osg::Texture2D> tex1, tex2; - osg::ref_ptr sampler1, sampler2; + osg::ref_ptr< osg::Texture2D> tex1, tex2; - tex1=new osg::Texture2D(); - tex2=new osg::Texture2D(); + tex1=new osg::Texture2D(); + tex2=new osg::Texture2D(); - sampler1=new osg::Sampler(); - sampler2=new osg::Sampler(); + sampler1=new osg::Sampler(); + sampler2=new osg::Sampler(); - osg::Vec2ui resolution(4,4); - { - ///first texture//NO RED - unsigned char *data = new unsigned char[(resolution.x()* resolution.y()) * 4]; - unsigned char * ptr=data; - unsigned int sw=0,cptx=0; - while (ptr != data + sizeof(unsigned char)*(resolution.x()* resolution.y()) * 4) + osg::Vec2ui resolution(4,4); { - if(sw==1) + ///first texture//NO RED + unsigned char *data = new unsigned char[(resolution.x()* resolution.y()) * 4]; + unsigned char * ptr=data; + unsigned int sw=0, cptx=0; + while (ptr != data + sizeof(unsigned char)*(resolution.x()* resolution.y()) * 4) { - *ptr++=0; - *ptr++=0xff; - *ptr++=0xff; - *ptr++=0xff; + if(sw==1) + { + *ptr++=0; *ptr++=0xff; *ptr++=0xff; *ptr++=0xff; + } + else + { + *ptr++=0; *ptr++=0; *ptr++=0; *ptr++=0xff; + } + if(++cptxim = new osg::Image(); + im->setImage(resolution.x(), resolution.y(), 1,GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, data, osg::Image::USE_NEW_DELETE); + im->dirty(); + tex1->setImage(im); } - osg::ref_ptrim = new osg::Image(); - im->setImage(resolution.x(), resolution.y(), 1,GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, data, osg::Image::USE_NEW_DELETE); - im->dirty(); - tex1->setImage(im); - } - { - ///second texture// RED ONLY - unsigned char *data = new unsigned char[(resolution.x()* resolution.y()) * 4]; - unsigned char * ptr=data; - unsigned int sw=0,cptx=0; - while (ptr != data + sizeof(unsigned char)*(resolution.x()* resolution.y()) * 4) { - if(sw==1) + ///second texture// RED ONLY + unsigned char *data = new unsigned char[(resolution.x()* resolution.y()) * 4]; + unsigned char * ptr=data; + unsigned int sw=0,cptx=0; + while (ptr != data + sizeof(unsigned char)*(resolution.x()* resolution.y()) * 4) { - *ptr++=255; - *ptr++=0; - *ptr++=0; - *ptr++=255; + if(sw==1) + { + *ptr++=255; *ptr++=0; *ptr++=0; *ptr++=255; + } + else + { + *ptr++=0; *ptr++=0; *ptr++=0; *ptr++=255; + } + if(++cptxim = new osg::Image(); + im->setImage(resolution.x(), resolution.y(), 1,GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, data, osg::Image::USE_NEW_DELETE); + im->dirty(); + tex2->setImage(im); } - osg::ref_ptrim = new osg::Image(); - im->setImage(resolution.x(), resolution.y(), 1,GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, data, osg::Image::USE_NEW_DELETE); - im->dirty(); - tex2->setImage(im); + ///Overrided Filtering setup + tex1->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST); + tex1->setFilter(osg::Texture::MIN_FILTER,osg::Texture::NEAREST); + + tex2->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST); + tex2->setFilter(osg::Texture::MIN_FILTER,osg::Texture::NEAREST); + + ///Filter Override samplers setup + sampler1->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR); + sampler1->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); + + sampler2->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR); + sampler2->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); + + + osg::StateSet *ss; + ss = geode->getOrCreateStateSet(); + + ss->setTextureAttribute(0, tex1,osg::StateAttribute::ON); + ss->setTextureAttribute(1, tex2,osg::StateAttribute::ON); + //#define TEST_SAMPLER_OBJECT_AUTO_PORT 1 + #ifdef TEST_SAMPLER_OBJECT_AUTO_PORT + + osg::Sampler::generateSamplerObjects(*ss) + if( + (sampler1= static_cast(ss->getTextureAttribute(0,osg::StateAttribute::SAMPLER)))&& + (sampler2= static_cast(ss->getTextureAttribute(1,osg::StateAttribute::SAMPLER))) + ) + OSG_WARN<<"both samplers are generated"<setTextureAttribute(0,sampler1,osg::StateAttribute::ON); + ss->setTextureAttribute(1,sampler2,osg::StateAttribute::ON); + #endif + ss->addUniform(new osg::Uniform("tex1",(int)0)); + ss->addUniform(new osg::Uniform("tex2",(int)1)); + ss->setAttribute(program.get()); + + viewer.addEventHandler(new SamplersKeyboardEventHandler(sampler1, sampler2)); } - ///Overrided Filtering setup - tex1->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST); - tex1->setFilter(osg::Texture::MIN_FILTER,osg::Texture::NEAREST); - tex2->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST); - tex2->setFilter(osg::Texture::MIN_FILTER,osg::Texture::NEAREST); - - ///Filter Override samplers setup - sampler1->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR); - sampler1->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); - - sampler2->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR); - sampler2->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); - - - osg::StateSet *ss; - ss = geode->getOrCreateStateSet(); - - ss->setTextureAttribute(0,tex1,osg::StateAttribute::ON); - ss->setTextureAttribute(1,tex2,osg::StateAttribute::ON); - ss->setTextureAttribute(0,sampler1,osg::StateAttribute::ON); - ss->setTextureAttribute(1,sampler2,osg::StateAttribute::ON); - - ss->addUniform(new osg::Uniform("tex1",(int)0)); - ss->addUniform(new osg::Uniform("tex2",(int)1)); - ss->setAttribute(program.get()); - - - viewer.addEventHandler(new KeyboardEventHandler(sampler1, sampler2,ss)); viewer.addEventHandler(new osgViewer::StatsHandler); - viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage())); + viewer.addEventHandler(new osgViewer::WindowSizeHandler()); viewer.setSceneData(geode.get()); return viewer.run();