Example #1
0
void createAnimation(SimpleXMLTransfer *animation, ssgEntity* model)
{
  ssgEntity *node;
  
  if (animation->getName() != "animation")
  {
    std::cerr << "creatAnimation: invalid child ";
    std::cerr << animation->getName() << std::endl;
  }
  else
  {
    std::string node_name = animation->getString("object.name", "default");
    std::string type      = animation->getString("type", "default");

    node = SSGUtil::findNamedNode(model, node_name.c_str());
    if (node != NULL)
    {
      CRRCAnimation *anim = NULL;
      std::cout << "createAnimation: found animation node ";
      std::cout << node_name << ", type " << type << std::endl; 
      
      if (type == "ControlSurface")
      {
        anim = new CRRCControlSurfaceAnimation(animation);
      }
      else
      {
        std::cerr << "createAnimation: unknown animation type '"
                  << type << "'" << std::cerr;
      }
      
      if (anim != NULL)
      {
        if (anim->getBranch() == NULL)
        {
          std::cerr << "createAnimation: defunct animation class (animation branch is <NULL>)\n";
          exit(EXIT_FAILURE);
        }
        else
        {
          SSGUtil::spliceBranch(anim->getBranch(), node);
          anim->init();
          anim->setName("Animation");
          anim->getBranch()->setUserData(anim);
          anim->getBranch()->setTravCallback(SSG_CALLBACK_PRETRAV, animation_callback);
        }
      }
      
    }
    else
    {
      std::cerr << "createAnimation: node '" << node_name << "' not found in 3D model" << std::endl;
    }
  }
}
/** \brief Add animations to a model
 *
 *  This method reads animation description tags from a model file
 *  and tries to add the corresponding animations to the 3D model.
 *
 *  \todo Right now there's only one type of animation: movable control
 *  surfaces. Therefore this method receives a pointer to the control
 *  input class. If animations are added that need a different kind of
 *  input for their update() method, we need to decide how to pass all
 *  this stuff to initAnimations().
 *
 *  \param  model_file    XML model description file
 *  \param  model         scenegraph of the 3D model
 *  \param  fInputs       pointer to the control input class
 *  \param  anim_list     list of all created CRRCAnimation objects
 */
void initAnimations(SimpleXMLTransfer *model_file, ssgEntity* model, 
                    TSimInputs *fInput, std::vector<CRRCAnimation*>& anim_list)
{
  SimpleXMLTransfer *animations = model_file->getChild("animations", true);
  int num_anims = animations->getChildCount();
  fprintf(stdout, "initAnimations: found %d children\n", num_anims);
  
  for (int i = 0; i < num_anims; i++)
  {
    SimpleXMLTransfer *animation = animations->getChildAt(i);
    ssgEntity *node;
    
    if (animation->getName() != "animation")
    {
      fprintf(stderr, "initAnimations: invalid child <%s>\n", animation->getName().c_str());
    }
    else
    {
      std::string node_name = animation->getString("object.name", "default");
      std::string type      = animation->getString("type", "default");

      node = SSGUtil::findNamedNode(model, node_name.c_str());
      if (node != NULL)
      {
        CRRCAnimation *anim = NULL;
        printf("initAnimations: found animation node %s, type %s\n", 
                node_name.c_str(), type.c_str());
        
        if (type == "ControlSurface")
        {
          anim = new CRRCControlSurfaceAnimation(animation, fInput);
        }
        else
        {
          fprintf(stderr, "initAnimations: unknown animation type '%s'\n", type.c_str());
        }
        
        if (anim != NULL)
        {
          if (anim->getBranch() == NULL)
          {
            fprintf(stderr, "initAnimations: defunct animation class (animation branch is <NULL>)\n");
            exit(0);
          }
          else
          {
            SSGUtil::spliceBranch(anim->getBranch(), node);
            anim->init();
            anim->setName("Animation");
            anim->getBranch()->setUserData(anim);
            anim->getBranch()->setTravCallback(SSG_CALLBACK_PRETRAV, animation_callback);
            anim_list.push_back(anim);
          }
        }
        
      }
      else
      {
        fprintf(stderr, "initAnimations: node '%s' not found in 3D model\n", node_name.c_str());
      }
    }
  }
}