⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mixer.cpp

📁 Cal3D实现虚拟角色 Cal3D实现虚拟角色
💻 CPP
📖 第 1 页 / 共 2 页
字号:
  // update all active animation actions of this model  std::list<CalAnimationAction *>::iterator iteratorAnimationAction;  iteratorAnimationAction = m_listAnimationAction.begin();  while(iteratorAnimationAction != m_listAnimationAction.end())  {    // find the specified action and remove it    if((*iteratorAnimationAction)->getCoreAnimation() == pCoreAnimation )    {        // found, so remove      (*iteratorAnimationAction)->completeCallbacks(m_pModel);      delete (*iteratorAnimationAction);      iteratorAnimationAction = m_listAnimationAction.erase(iteratorAnimationAction);      return true;    }    iteratorAnimationAction++;  }  return false;} /*****************************************************************************//** Updates all active animations.  *  * This function updates all active animations of the mixer instance for a  * given amount of time.  *  * @param deltaTime The elapsed time in seconds since the last update.  *****************************************************************************/void CalMixer::updateAnimation(float deltaTime){  // update the current animation time  if(m_animationDuration == 0.0f)  {    m_animationTime = 0.0f;  }  else  {    m_animationTime += deltaTime * m_timeFactor;    if(m_animationTime >= m_animationDuration || m_animationTime<0)    {      m_animationTime = (float) fmod(m_animationTime, m_animationDuration);    }	if (m_animationTime < 0)      m_animationTime += m_animationDuration;  }  // update all active animation actions of this model  std::list<CalAnimationAction *>::iterator iteratorAnimationAction;  iteratorAnimationAction = m_listAnimationAction.begin();  while(iteratorAnimationAction != m_listAnimationAction.end())  {    // update and check if animation action is still active    if((*iteratorAnimationAction)->update(deltaTime))    {      (*iteratorAnimationAction)->checkCallbacks((*iteratorAnimationAction)->getTime(),m_pModel);      ++iteratorAnimationAction;    }    else    {      // animation action has ended, destroy and remove it from the animation list      (*iteratorAnimationAction)->completeCallbacks(m_pModel);      delete (*iteratorAnimationAction);      iteratorAnimationAction = m_listAnimationAction.erase(iteratorAnimationAction);    }  }  // todo: update all active animation poses of this model  // update the weight of all active animation cycles of this model  std::list<CalAnimationCycle *>::iterator iteratorAnimationCycle;  iteratorAnimationCycle = m_listAnimationCycle.begin();  float accumulatedWeight, accumulatedDuration;  accumulatedWeight = 0.0f;  accumulatedDuration = 0.0f;  while(iteratorAnimationCycle != m_listAnimationCycle.end())  {    // update and check if animation cycle is still active    if((*iteratorAnimationCycle)->update(deltaTime))    {      // check if it is in sync. if yes, update accumulated weight and duration      if((*iteratorAnimationCycle)->getState() == CalAnimation::STATE_SYNC)      {        accumulatedWeight += (*iteratorAnimationCycle)->getWeight();        accumulatedDuration += (*iteratorAnimationCycle)->getWeight() * (*iteratorAnimationCycle)->getCoreAnimation()->getDuration();      }      (*iteratorAnimationCycle)->checkCallbacks(m_animationTime,m_pModel);      ++iteratorAnimationCycle;    }    else    {      // animation cycle has ended, destroy and remove it from the animation list      (*iteratorAnimationCycle)->completeCallbacks(m_pModel);      delete (*iteratorAnimationCycle);      iteratorAnimationCycle = m_listAnimationCycle.erase(iteratorAnimationCycle);    }  }  // adjust the global animation cycle duration  if(accumulatedWeight > 0.0f)  {    m_animationDuration = accumulatedDuration / accumulatedWeight;  }  else  {    m_animationDuration = 0.0f;  }}void CalMixer::updateSkeleton(){  // get the skeleton we need to update  CalSkeleton *pSkeleton;  pSkeleton = m_pModel->getSkeleton();  if(pSkeleton == 0) return;  // clear the skeleton state  pSkeleton->clearState();  // get the bone vector of the skeleton  std::vector<CalBone *>& vectorBone = pSkeleton->getVectorBone();  // loop through all animation actions  std::list<CalAnimationAction *>::iterator iteratorAnimationAction;  for(iteratorAnimationAction = m_listAnimationAction.begin(); iteratorAnimationAction != m_listAnimationAction.end(); ++iteratorAnimationAction)  {    // get the core animation instance    CalCoreAnimation *pCoreAnimation;    pCoreAnimation = (*iteratorAnimationAction)->getCoreAnimation();    // get the list of core tracks of above core animation    std::list<CalCoreTrack *>& listCoreTrack = pCoreAnimation->getListCoreTrack();    // loop through all core tracks of the core animation    std::list<CalCoreTrack *>::iterator iteratorCoreTrack;    for(iteratorCoreTrack = listCoreTrack.begin(); iteratorCoreTrack != listCoreTrack.end(); ++iteratorCoreTrack)    {      // get the appropriate bone of the track      CalBone *pBone;      pBone = vectorBone[(*iteratorCoreTrack)->getCoreBoneId()];      // get the current translation and rotation      CalVector translation;      CalQuaternion rotation;      (*iteratorCoreTrack)->getState((*iteratorAnimationAction)->getTime(), translation, rotation);      // blend the bone state with the new state      pBone->blendState((*iteratorAnimationAction)->getWeight(), translation, rotation);    }  }  // lock the skeleton state  pSkeleton->lockState();  // loop through all animation cycles  std::list<CalAnimationCycle *>::iterator iteratorAnimationCycle;  for(iteratorAnimationCycle = m_listAnimationCycle.begin(); iteratorAnimationCycle != m_listAnimationCycle.end(); ++iteratorAnimationCycle)  {    // get the core animation instance    CalCoreAnimation *pCoreAnimation;    pCoreAnimation = (*iteratorAnimationCycle)->getCoreAnimation();    // calculate adjusted time    float animationTime;    if((*iteratorAnimationCycle)->getState() == CalAnimation::STATE_SYNC)    {      if(m_animationDuration == 0.0f)      {        animationTime = 0.0f;      }      else      {        animationTime = m_animationTime * pCoreAnimation->getDuration() / m_animationDuration;      }    }    else    {      animationTime = (*iteratorAnimationCycle)->getTime();    }    // get the list of core tracks of above core animation    std::list<CalCoreTrack *>& listCoreTrack = pCoreAnimation->getListCoreTrack();    // loop through all core tracks of the core animation    std::list<CalCoreTrack *>::iterator iteratorCoreTrack;    for(iteratorCoreTrack = listCoreTrack.begin(); iteratorCoreTrack != listCoreTrack.end(); ++iteratorCoreTrack)    {      // get the appropriate bone of the track      CalBone *pBone;      pBone = vectorBone[(*iteratorCoreTrack)->getCoreBoneId()];      // get the current translation and rotation      CalVector translation;      CalQuaternion rotation;      (*iteratorCoreTrack)->getState(animationTime, translation, rotation);      // blend the bone state with the new state      pBone->blendState((*iteratorAnimationCycle)->getWeight(), translation, rotation);    }  }  // lock the skeleton state  pSkeleton->lockState();  // let the skeleton calculate its final state  pSkeleton->calculateState();}/*****************************************************************************//** Returns the animation time.  *  * This function returns the animation time of the mixer instance.  *  * @return The animation time in seconds.  *****************************************************************************/float CalMixer::getAnimationTime(){	return m_animationTime;}/*****************************************************************************//** Returns the animation duration.  *  * This function returns the animation duration of the mixer instance.  *  * @return The animation duration in seconds.  *****************************************************************************/float CalMixer::getAnimationDuration(){	return m_animationDuration;}/*****************************************************************************//** Sets the animation time.  *  * This function sets the animation time of the mixer instance.  *  *****************************************************************************/void CalMixer::setAnimationTime(float animationTime){	m_animationTime=animationTime;}/*****************************************************************************//** Set the time factor.  *   * This function sets the time factor of the mixer instance.  * this time factor affect only sync animation  *  *****************************************************************************/void CalMixer::setTimeFactor(float timeFactor){    m_timeFactor = timeFactor;}/*****************************************************************************//** Get the time factor.  *   * This function return the time factor of the mixer instance.  *  *****************************************************************************/float CalMixer::getTimeFactor(){    return m_timeFactor;}/*****************************************************************************//** Get the model.  *   * This function return the CalModel of the mixer instance.  *  *****************************************************************************/CalModel *CalMixer::getCalModel() {     return m_pModel; } /*****************************************************************************//** Get the animation vector.  *   * This function return the animation vector of the mixer instance.  *  *****************************************************************************/std::vector<CalAnimation *> & CalMixer::getAnimationVector() {     return m_vectorAnimation; } /*****************************************************************************//** Get the list of the action animation.  *   * This function return the list of the action animation of the mixer instance.  *  *****************************************************************************/std::list<CalAnimationAction *> & CalMixer::getAnimationActionList() {     return m_listAnimationAction; }/*****************************************************************************//** Get the list of the cycle animation.  *   * This function return the list of the cycle animation of the mixer instance.  *  *****************************************************************************/std::list<CalAnimationCycle *> & CalMixer::getAnimationCycle() {     return m_listAnimationCycle; } //****************************************************************************//

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -