Show
Ignore:
Timestamp:
10/27/09 16:37:13 (5 years ago)
Author:
cedricpinson
Message:

From Cedric Pinson, The following commit include:
* Refactore of RigGeometry? to support hardware skinning
* Refactore of Timeline to split Action in differents files
* Add example how to use hardware skinning

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/src/osgAnimation/Action.cpp

    r10561 r10693  
    1515#include <osgAnimation/Action> 
    1616 
    17 osgAnimation::Action::Action() 
     17using namespace osgAnimation; 
     18 
     19Action::Action() 
    1820{ 
    1921    _numberFrame = 25; 
     
    2224    _loop = 1; 
    2325} 
    24 osgAnimation::Action::Action(const Action&,const osg::CopyOp&) {} 
    25 osgAnimation::Action::Callback* osgAnimation::Action::getFrameCallback(unsigned int frame) 
     26Action::Action(const Action&,const osg::CopyOp&) {} 
     27Action::Callback* Action::getFrameCallback(unsigned int frame) 
    2628{ 
    2729    if (_framesCallback.find(frame) != _framesCallback.end()) 
     
    3234} 
    3335 
    34 osgAnimation::Action::Callback* osgAnimation::Action::getFrameCallback(double time) 
     36void Action::removeCallback(Callback* cb) 
     37{ 
     38    std::vector<unsigned int> keyToRemove; 
     39    for (FrameCallback::iterator it = _framesCallback.begin(); it != _framesCallback.end(); it++)  
     40    { 
     41        if (it->second.get()) 
     42        { 
     43            if (it->second.get() == cb)  
     44            { 
     45                it->second = it->second->getNestedCallback(); 
     46                if (!it->second.valid()) 
     47                    keyToRemove.push_back(it->first); 
     48            } 
     49            else  
     50            { 
     51                it->second->removeCallback(cb); 
     52            } 
     53        } 
     54    } 
     55    for (std::vector<unsigned int>::iterator it = keyToRemove.begin(); it != keyToRemove.end(); it++) 
     56        _framesCallback.erase(*it); 
     57} 
     58 
     59Action::Callback* Action::getFrameCallback(double time) 
    3560{ 
    3661    unsigned int frame = static_cast<unsigned int>(floor(time * _fps)); 
     
    5782    return true; 
    5883} 
    59  
    60  
    61 osgAnimation::BlendIn::BlendIn(Animation* animation, double duration, double weight) 
    62 { 
    63     _animation = animation; 
    64     _weight = weight; 
    65     float d = duration * _fps; 
    66     setNumFrames(static_cast<unsigned int>(floor(d)) + 1); 
    67     setName("BlendIn"); 
    68 } 
    69  
    70 void osgAnimation::BlendIn::computeWeight(unsigned int frame) 
    71 { 
    72  
    73     // frame + 1 because the start is 0 and we want to start the blend in at the first 
    74     // frame. 
    75     double ratio = ( (frame+1) * 1.0 / (getNumFrames()) ); 
    76     double w = _weight * ratio; 
    77  
    78     osg::notify(osg::DEBUG_INFO) << getName() << " BlendIn frame " << frame  << " weight " << w << std::endl; 
    79     _animation->setWeight(w); 
    80 } 
    81  
    82  
    83 osgAnimation::BlendOut::BlendOut(Animation* animation, double duration) 
    84 { 
    85     _animation = animation; 
    86     float d = duration * _fps; 
    87     setNumFrames(static_cast<unsigned int>(floor(d) + 1)); 
    88     _weight = 1.0; 
    89     setName("BlendOut"); 
    90 } 
    91  
    92 void osgAnimation::BlendOut::computeWeight(unsigned int frame) 
    93 { 
    94     double ratio = ( (frame+1) * 1.0 / (getNumFrames()) ); 
    95     double w = _weight * (1.0-ratio); 
    96     osg::notify(osg::DEBUG_INFO) << getName() << " BlendOut frame " << frame  << " weight " << w << std::endl; 
    97     _animation->setWeight(w); 
    98 } 
    99  
    100  
    101 osgAnimation::ActionAnimation::ActionAnimation(Animation* animation) : _animation(animation) 
    102 { 
    103     Action::setDuration(animation->getDuration()); 
    104     setName(animation->getName()); 
    105 } 
    106 void osgAnimation::ActionAnimation::updateAnimation(unsigned int frame, int priority) 
    107 { 
    108     _animation->update(frame * 1.0/_fps, priority); 
    109 } 
    110  
    111  
    112  
    113  
    114  
    115 osgAnimation::StripAnimation::StripAnimation(const StripAnimation& a, const osg::CopyOp& c) : Action(a,c)  
    116 { 
    117     _animation = a._animation; 
    118     _blendIn = a._blendIn; 
    119     _blendOut = a._blendOut; 
    120 } 
    121  
    122 osgAnimation::StripAnimation::StripAnimation(Animation* animation, double blendInDuration, double blendOutDuration, double blendInWeightTarget) 
    123 { 
    124     _blendIn = new BlendIn(animation, blendInDuration, blendInWeightTarget); 
    125     _animation = new ActionAnimation(animation); 
    126     unsigned int start = static_cast<unsigned int>(floor((_animation->getDuration() - blendOutDuration) * _fps)); 
    127     _blendOut = FrameBlendOut(start, new BlendOut(animation, blendOutDuration)); 
    128     setName(animation->getName() + "_Strip"); 
    129     _blendIn->setName(_animation->getName() + "_" + _blendIn->getName()); 
    130     _blendOut.second->setName(_animation->getName() + "_" + _blendOut.second->getName()); 
    131     setDuration(animation->getDuration()); 
    132 } 
    133  
    134  
    135 void osgAnimation::StripAnimation::setLoop(unsigned int loop) 
    136 { 
    137     _animation->setLoop(loop); 
    138     if (!loop) 
    139         setDuration(-1); 
    140     else 
    141         setDuration(loop * _animation->getDuration()); 
    142  
    143     // duration changed re evaluate the blendout duration 
    144     unsigned int start = static_cast<unsigned int>(floor((getDuration() - _blendOut.second->getDuration()) * _fps)); 
    145     _blendOut = FrameBlendOut(start, _blendOut.second); 
    146 } 
    147  
    148 void osgAnimation::StripAnimation::traverse(ActionVisitor& visitor) 
    149 { 
    150     if (_blendIn.valid()) 
    151     { 
    152         unsigned int f = visitor.getStackedFrameAction().back().first; 
    153         visitor.pushFrameActionOnStack(FrameAction(f,_blendIn.get())); 
    154         _blendIn->accept(visitor); 
    155         visitor.popFrameAction(); 
    156     } 
    157     if (_blendOut.second.valid()) 
    158     { 
    159         unsigned int f = visitor.getStackedFrameAction().back().first; 
    160         visitor.pushFrameActionOnStack(FrameAction(f + _blendOut.first,_blendOut.second.get())); 
    161         _blendOut.second.get()->accept(visitor); 
    162         visitor.popFrameAction(); 
    163     } 
    164  
    165     if (_animation.valid()) 
    166     { 
    167         unsigned int f = visitor.getStackedFrameAction().back().first; 
    168         visitor.pushFrameActionOnStack(FrameAction(f,_animation.get())); 
    169         _animation->accept(visitor); 
    170         visitor.popFrameAction(); 
    171     } 
    172 }