Advertisement
Guest User

Untitled

a guest
Oct 18th, 2015
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.44 KB | None | 0 0
  1. //the glut "render" function
  2. void render()
  3. {
  4.     sceneManager.renderFrame(myCamera);
  5. }
  6.  
  7. void SceneManager::renderFrame(Camera *camera)
  8. {
  9.     //clear screen
  10.     _renderer.clearScreen();
  11.  
  12.     //static meshes. Ignore this because it isn't in use when I benchmark my app with skeletal animation
  13.     if(_meshProgram && !_staticMeshes.empty()) {
  14.         _renderer.beginRendering(camera, _meshProgram);
  15.         for(unsigned int i = 0; i < _staticMeshes.size(); i++)
  16.         {
  17.             std::vector<SceneNode*> &meshList = _staticMeshes[i]->list;
  18.  
  19.             for(unsigned int j = 0; j < meshList.size(); j++)
  20.                 _renderer.renderSceneNode(meshList[j], _meshProgram);
  21.         }
  22.         _renderer.endRendering(_meshProgram);
  23.     }
  24.  
  25.     //animated meshes:
  26.     //go through the list of
  27.     if(_skeletalAnimationProgram && !_animatedMeshes.empty()) {
  28.  
  29.         _animationController.updateAnimations();
  30.         _renderer.beginRendering(camera, _skeletalAnimationProgram);
  31.  
  32.         for(unsigned int i = 0; i < _animatedMeshes.size(); i++)
  33.         {
  34.             std::vector<SceneNode*> &meshList = _animatedMeshes[i]->list;
  35.             BoneList *boneList = _animatedMeshes[i]->boneList;
  36.  
  37.             boneList->clearBoneMatrices();
  38.             boneList->fillBoneMatrices();
  39.             _skeletalAnimationProgram->setUniform((GLint)bones_location, boneList->boneMatrices, MAX_BONES_IN_SHADER);
  40.            
  41.             //render the scene nodes with attached MeshDatas
  42.             for(unsigned int j = 0; j < meshList.size(); j++)
  43.                 _renderer.renderSceneNode(meshList[j], _skeletalAnimationProgram);
  44.         }
  45.         _renderer.endRendering(_skeletalAnimationProgram);
  46.     }
  47.  
  48.     //swap buffers
  49.     _renderer.swapBuffers();
  50. }
  51.  
  52. //this function consumes most of the time. Though I can't think of anything that could speed this more
  53. void AnimationController::updateAnimations()
  54. {
  55.     //iterate through active animations
  56.     for(unsigned int i = 0; i < _animations.size(); i++)
  57.     {
  58.         Animation *animation = _animations[i];
  59.  
  60.         //only process if the animation is running
  61.         if(animation->animationState != ANIMATION_STATE_RUNNING)
  62.             continue;
  63.  
  64.         //calculate delta time
  65.         double time = ((double)animation->timer.elapsed() / 1000.0) * (animation->animationSet->ticksPerSecond());
  66.         while(time > animation->animationSet->duration()) time -= animation->animationSet->duration();
  67.  
  68.         //update animation matrices (bone->nodeMatrix)
  69.         std::vector<Bone*> &bones= animation->boneList->bones;
  70.         for(unsigned int j = 0; j < bones.size(); j++)
  71.         {
  72.             Bone *bone = bones[j];
  73.  
  74.             _calculateNodeMatrix(bones[j], bone->nodeAnimationData(), time, &animation->lastKeyframeIndex);
  75.         }
  76.     }
  77.  
  78.     //set finalMatrix for each bone (using a recursive method)
  79.     for(unsigned int i = 0; i < _animatedMeshes->size(); i++) {
  80.         SceneNodeList *list = (*_animatedMeshes)[i];
  81.        
  82.         _updateFinalMatrices(list->sceneNode);
  83.     }
  84. }
  85.  
  86. void AnimationController::_calculateNodeMatrix(Bone *bone, const NodeAnimationData *animData, double time, unsigned int *lastIndex)
  87. {
  88.     unsigned int currIndex = _getAnimationKeyIndex(&animData->positionKeys, time, *lastIndex);
  89.     unsigned int nextIndex = currIndex + 1;
  90.     *lastIndex = currIndex;
  91.     double factor;
  92.  
  93.     //rotation
  94.     const QuatKey &currQuatKey = animData->quatKeys[currIndex];
  95.     const QuatKey &nextQuatKey = animData->quatKeys[nextIndex];
  96.    
  97.     factor = (time - currQuatKey.time) / (nextQuatKey.time - currQuatKey.time);
  98.     factor = glm::clamp(factor, 0.0, 1.0);
  99.    
  100.     glm::quat rotation_quat = glm::slerp(currQuatKey.quat, nextQuatKey.quat, (float)factor);
  101.     glm::mat4 rotation_mat = glm::mat4_cast(rotation_quat);
  102.    
  103.     //position
  104.     const VectorKey &currPosKey = animData->positionKeys[currIndex];
  105.     const VectorKey &nextPosKey = animData->positionKeys[nextIndex];
  106.    
  107.     factor = (time - currPosKey.time) / (nextPosKey.time - currPosKey.time);
  108.     factor = glm::clamp(factor, 0.0, 1.0);
  109.    
  110.     glm::vec3 position_vec = currPosKey.vec*(1.0f - (float)factor) + nextPosKey.vec * (float)factor;
  111.     glm::mat4 position_mat = glm::translate(position_vec);
  112.            
  113.     bone->setNodeMatrix(position_mat * rotation_mat);
  114. }
  115.  
  116.  
  117. void AnimationController::_updateFinalMatrices(SceneNode *sceneNode)
  118. {
  119.     Bone *bone = sceneNode->bone();
  120.     Bone *parentBone = sceneNode->parent()->bone(); //note there is no check for sceneNode->parent() because the root node isn't supposed to hold a Mesh. add an error check for this later
  121.  
  122.     if(bone) {
  123.         bone->setNodeMatrix( (parentBone ? parentBone->nodeMatrix() : glm::mat4(1))
  124.                              * bone->nodeMatrix() );
  125.         bone->calculateFinalMatrix();
  126.     }
  127.    
  128.     for(unsigned int i = 0; i < sceneNode->getNumChildren(); i++)
  129.         _updateFinalMatrices(sceneNode->getChildNode(i));
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement