root/OpenSceneGraph/trunk/src/osgAnimation/LinkVisitor.cpp @ 10518

Revision 10518, 2.6 kB (checked in by cedricpinson, 5 years ago)

From Cedric Pinson, Add UpdateMaterial? callback to animate material\nUpdate LinkVisitor? to traverse stateset\nUpdate ReaderWriter? to read osgAnimation::UpdateMaterial? Callback\nUpdate nathan.osg data file to demonstrate the MaterialAnimation?

RevLine 
[10498]1/*  -*-c++-*-
2 *  Copyright (C) 2009 Cedric Pinson <cedric.pinson@plopbyte.net>
3 *
4 * This library is open source and may be redistributed and/or modified under 
5 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
6 * (at your option) any later version.  The full license is in LICENSE file
7 * included with this distribution, and on the openscenegraph.org website.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * OpenSceneGraph Public License for more details.
13*/
14
15#include <osgAnimation/LinkVisitor>
16#include <osgAnimation/UpdateCallback>
17#include <osg/Notify>
[10518]18#include <osg/Geode>
[10498]19
20using namespace osgAnimation;
21
22LinkVisitor::LinkVisitor() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
23{
24    _nbLinkedTarget = 0;
25}
26
27void LinkVisitor::reset()
28{
29    _nbLinkedTarget = 0;
30}
31
32AnimationList& LinkVisitor::getAnimationList()
33{
34    return _animations;
35}
[10518]36void LinkVisitor::link(osgAnimation::AnimationUpdateCallbackBase* cb)
37{
38    int result = 0;
39    for (int i = 0; i < (int)_animations.size(); i++)
40    {
41        result += cb->link(_animations[i].get());
42        _nbLinkedTarget += result;
43    }
44    osg::notify(osg::NOTICE) << "LinkVisitor links " << result << " for \"" << cb->getName() << '"' << std::endl;
45}
[10498]46
[10518]47void LinkVisitor::handle_stateset(osg::StateSet* stateset)
48{
49    if (!stateset)
50        return;
51    osg::StateSet::AttributeList& attr = stateset->getAttributeList();
52    for (osg::StateSet::AttributeList::iterator it = attr.begin(); it != attr.end(); it++)
53    {
54        osg::StateAttribute* sattr = it->second.first.get();
55        osgAnimation::AnimationUpdateCallbackBase* cb = dynamic_cast<osgAnimation::AnimationUpdateCallbackBase*>(sattr->getUpdateCallback());
56        if (cb)
57            link(cb);
58    }
59}
60
[10498]61void LinkVisitor::apply(osg::Node& node)
62{
[10518]63    osg::StateSet* st = node.getStateSet();
64    if (st)
65        handle_stateset(st);
66
67    osg::NodeCallback* cb = node.getUpdateCallback();
68    while (cb)
[10498]69    {
[10518]70        osgAnimation::AnimationUpdateCallbackBase* cba = dynamic_cast<osgAnimation::AnimationUpdateCallbackBase*>(cb);
71        if (cba)
72            link(cba);
73        cb = cb->getNestedCallback();
[10498]74    }
75    traverse(node);
76}
[10518]77
78void LinkVisitor::apply(osg::Geode& node)
79{
80    for (unsigned int i = 0; i < node.getNumDrawables(); i++)
81    {
82        osg::Drawable* drawable = node.getDrawable(i);
83        if (drawable && drawable->getStateSet())
84            handle_stateset(drawable->getStateSet());
85    }
86    apply(static_cast<osg::Node&>(node));
87}
Note: See TracBrowser for help on using the browser.