Re: [osg-users] Collect all the osg::Materials from a osg::Node using osg::NodeVisitor
Hi Robert Thanks for this great hint! Really good one! Cheers! Nick On Fri, Aug 26, 2016 at 6:27 PM, Robert Osfield wrote: > On 26 August 2016 at 17:13, Trajce Nikolov NICK > wrote: > > Ok :-) .. I will answer it too . > > > > I am working with older version of osg where the Geometry was still not > > inherited from Node so I had to apply a special case for Geodes ... Sorry > > for the noise ;-) > > Glad to hear you spotted the problem. > > One thing that jumped out at me when I read the code was that you call > getOrCreateStateSet(), this might simplify the code but is terribly > inefficient for both this traversal and any subsequent use of the > scene graph as it will force the creation of StateSet's for all nodes > in the scene graph. Most nodes in a scene graph should never need a > StateSet so you certainly don't want to go around assigning empty > ones. > > What a better check would be: > > void apply(osg::Node& node) > { > if (node.getStateSet()) apply(*node.getStateSet())l > } > > void apply(osg::StateSet& stateset) > { > osg::StateAttribute* attr = > stateset>getAttribute(osg::StateAttribute::MATERIAL); > if (attr) > { > std::cout << "ATTRIBUTE: " << attr->getName() << std::endl; > > osg::Material* material = dynamic_cast(attr); > if (material && (material->getName() != "@RootMaterial@")) > { > std::cout << "MATERIAL: " << material->getName() << > std::endl; > } > } > } > ___ > osg-users mailing list > osg-users@lists.openscenegraph.org > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org > -- trajce nikolov nick ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Collect all the osg::Materials from a osg::Node using osg::NodeVisitor
On 26 August 2016 at 17:13, Trajce Nikolov NICK wrote: > Ok :-) .. I will answer it too . > > I am working with older version of osg where the Geometry was still not > inherited from Node so I had to apply a special case for Geodes ... Sorry > for the noise ;-) Glad to hear you spotted the problem. One thing that jumped out at me when I read the code was that you call getOrCreateStateSet(), this might simplify the code but is terribly inefficient for both this traversal and any subsequent use of the scene graph as it will force the creation of StateSet's for all nodes in the scene graph. Most nodes in a scene graph should never need a StateSet so you certainly don't want to go around assigning empty ones. What a better check would be: void apply(osg::Node& node) { if (node.getStateSet()) apply(*node.getStateSet())l } void apply(osg::StateSet& stateset) { osg::StateAttribute* attr = stateset>getAttribute(osg::StateAttribute::MATERIAL); if (attr) { std::cout << "ATTRIBUTE: " << attr->getName() << std::endl; osg::Material* material = dynamic_cast(attr); if (material && (material->getName() != "@RootMaterial@")) { std::cout << "MATERIAL: " << material->getName() << std::endl; } } } ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Collect all the osg::Materials from a osg::Node using osg::NodeVisitor
Ok :-) .. I will answer it too . I am working with older version of osg where the Geometry was still not inherited from Node so I had to apply a special case for Geodes ... Sorry for the noise ;-) Cheers! Nick On Fri, Aug 26, 2016 at 5:58 PM, Trajce Nikolov NICK < trajce.nikolov.n...@gmail.com> wrote: > Hi Community, > > this is really simple, but for a reason it is not working on my end. I > have simple model with animation (a cube that scales over time). On load I > launch a NodeVisitor to collect all the Materials. I can see one Material > in the text file but the parser is not hitng it. Here is my Visitor code: > Any clue? And thanks a bunch as always! > > struct MaterialFinderVisitor : public osg::NodeVisitor > { > MaterialFinderVisitor() > : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) > { > } > > virtual void apply( osg::Node& node ) > { > osg::StateAttribute* attr = node.getOrCreateStateSet()-> > getAttribute(osg::StateAttribute::MATERIAL); > if (attr) > { > std::cout << "ATTRIBUTE: " << attr->getName() << std::endl; > > osg::Material* material = dynamic_cast(attr); > if (material && (material->getName() != "@RootMaterial@")) > { > std::cout << "MATERIAL: " << material->getName() << > std::endl; > } > } > traverse(node); > } > > > -- > trajce nikolov nick > -- trajce nikolov nick ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
[osg-users] Collect all the osg::Materials from a osg::Node using osg::NodeVisitor
Hi Community, this is really simple, but for a reason it is not working on my end. I have simple model with animation (a cube that scales over time). On load I launch a NodeVisitor to collect all the Materials. I can see one Material in the text file but the parser is not hitng it. Here is my Visitor code: Any clue? And thanks a bunch as always! struct MaterialFinderVisitor : public osg::NodeVisitor { MaterialFinderVisitor() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) { } virtual void apply( osg::Node& node ) { osg::StateAttribute* attr = node.getOrCreateStateSet()->getAttribute(osg::StateAttribute::MATERIAL); if (attr) { std::cout << "ATTRIBUTE: " << attr->getName() << std::endl; osg::Material* material = dynamic_cast(attr); if (material && (material->getName() != "@RootMaterial@")) { std::cout << "MATERIAL: " << material->getName() << std::endl; } } traverse(node); } -- trajce nikolov nick scaledanim4.osgt.tar.gz Description: GNU Zip compressed data ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org