Ok! I managed to do it by putting them in a osg:: Switch and calling 
morphsGrp-> setAllChildrenOff ();
But I have performance problems ... to manage various morphs I have followed 
this model:


Code:

std::vector<osg::ref_ptr<osg::Geometry>> geoms;
morphsGrp = new osg::Switch();
std::vector<osgAnimation::MorphGeometry*> morphGeometrys;
int morphsCont = 0;
for(int i=0;i<animations.size();i++)
{
     if(!animations[i].type.compare("animation")){
     ...
     }
     else if(!animations[i].type.compare("morph")){
          osg::notify(osg::WARN) <<".......reading morph "<< 
animations[i].callName << std::endl;
          osg::ref_ptr<osg::Node> shape0 = 
osgDB::readNodeFile(animations[i].loadDir);
         if (!shape0) {
             std::cerr << "can't read morphtarget" << std::endl;
         }
        else{
            osg::Geometry* geom = 
osgGenerals::findGeometryNode(shape0)->asGeode()->getDrawable(0)->asGeometry();
            if(!geom) {
                 std::cerr << "can't read geometry" << std::endl;
            }
            else{
                geoms.push_back(geom);
                osgAnimation::Animation* animation = new 
osgAnimation::Animation;
                osgAnimation::FloatLinearChannel* channel0 = new 
osgAnimation::FloatLinearChannel;                                             
      
channel0->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(0,0.0));
                                        
channel0->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(0.5,1.0));
                                        
channel0->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(1.0,0.0));
                                        
channel0->setTargetName(animations[i].callName);
                   channel0->setName("0");
                   animation->addChannel(channel0);
                   animation->setName(animations[i].callName);
                   animation->computeDuration();
                   animation->setPlayMode(osgAnimation::Animation::ONCE);
                    m_morphManager->registerAnimation(animation);
                    // initialize with the first shape
                   morphGeometrys.push_back(new 
osgAnimation::MorphGeometry(*geometryBase));
                                        
morphGeometrys[morphsCont]->addMorphTarget(geoms[morphsCont].get());
                   osg::Geode* geodeMorph = new osg::Geode ;
                   geodeMorph->setName(animations[i].callName);
                   geodeMorph->addDrawable( morphGeometrys[morphsCont]) ;
                    osgAnimation::UpdateMorph* updateMorph = new 
osgAnimation::UpdateMorph(animations[i].callName);
                    geodeMorph->setUpdateCallback(updateMorph) ;
                     morphsGrp->addChild(geodeMorph);

                     morphsCont++;
                }
          }
     }
 }
osgAnimation::Animation* animation = new osgAnimation::Animation;
osgAnimation::MorphGeometry* morphGeometry = new 
osgAnimation::MorphGeometry(*geometryBase);
morphGeometry->addMorphTarget(morphGeometrys[0]);
osgAnimation::FloatLinearChannel* channel0 = new 
osgAnimation::FloatLinearChannel;
        
channel0->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(0,0.0));
        
channel0->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(0.5,1.0));
        
channel0->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(1.0,0.0));
channel0->setTargetName("playMorphs");
channel0->setName("0");
animation->addChannel(channel0);
for(int i=0;i<morphGeometrys.size();i++){
     morphGeometry->addMorphTarget(morphGeometrys[i]);
     osgAnimation::FloatLinearChannel* channel0 = new 
osgAnimation::FloatLinearChannel;
                
channel0->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(0,0.0));
                
channel0->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(0.5,1.0));
                
channel0->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(1.0,0.0));
      channel0->setTargetName("playMorphs");
      channel0->setName("0");
     animation->addChannel(channel0);
}
        
animation->setName("playMorphs");
animation->computeDuration();
animation->setPlayMode(osgAnimation::Animation::ONCE);
m_morphManager->registerAnimation(animation);
        
osg::Geode* geodeMorph = new osg::Geode ;
geodeMorph->addDrawable( morphGeometry) ;
geodeMorph->setUpdateCallback( new osgAnimation::UpdateMorph("playMorphs") ) ;
        
morphsGrp->addChild(geodeMorph);
morphsGrp->setAllChildrenOff();
morphsGrp->setUpdateCallback(m_morphManager);

if(morphGeometry){
     geom0->setSourceGeometry( morphGeometry);
     modelNode->addChild( morphsGrp ) ;
     return true;
}




Now I load about 15 morphs and the performance is about 17 frames per second. 
Loading 3, the performance goes up twice. Any ideas on how to improve this?

Thank you!

Cheers,
Aitor

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=40552#40552





_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to