Advertisement
Guest User

Untitled

a guest
Oct 27th, 2011
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. typedef map<string, aiMatrix4x4> Pose;
  2. class RiggedModel : public ofxAssimpModelLoader {
  3. public:
  4.     Pose getPose(int which = 0) {
  5.         const aiMesh* mesh = modelMeshes[which].mesh;  
  6.         int n = mesh->mNumBones;
  7.         Pose pose;
  8.         for(int a = 0; a < n; a++) {
  9.             const aiBone* bone = mesh->mBones[a];
  10.             aiNode* node = scene->mRootNode->FindNode(bone->mName);
  11.             pose[node->mName.data] = aiMatrix4x4();
  12.         }
  13.         return pose;
  14.     }
  15.     void pose(Pose& pose, int which = 0) {
  16.         const aiMesh* mesh = modelMeshes[which].mesh;
  17.         int n = mesh->mNumBones;
  18.         vector<aiMatrix4x4> boneMatrices(n);
  19.         for(int a = 0; a < n; a++) {
  20.             const aiBone* bone = mesh->mBones[a];
  21.             aiNode* node = scene->mRootNode->FindNode(bone->mName);
  22.             boneMatrices[a] = bone->mOffsetMatrix;
  23.             const aiNode* tempNode = node;
  24.             while(tempNode) {
  25.                 aiMatrix4x4 curPose = pose[tempNode->mName.data];
  26.                 boneMatrices[a] = (tempNode->mTransformation * curPose) * boneMatrices[a];
  27.                 tempNode = tempNode->mParent;
  28.             }
  29.             modelMeshes[which].hasChanged = true;
  30.             modelMeshes[which].validCache = false;
  31.         }
  32.        
  33.         modelMeshes[which].animatedPos.assign(modelMeshes[which].animatedPos.size(),0);
  34.         if(mesh->HasNormals()){
  35.             modelMeshes[which].animatedNorm.assign(modelMeshes[which].animatedNorm.size(),0);
  36.         }
  37.        
  38.         // loop through all vertex weights of all bones
  39.         for(int a = 0; a < n; a++) {
  40.             const aiBone* bone = mesh->mBones[a];
  41.             const aiMatrix4x4& posTrafo = boneMatrices[a];
  42.             for( size_t b = 0; b < bone->mNumWeights; b++) {
  43.                 const aiVertexWeight& weight = bone->mWeights[b];
  44.                 size_t vertexId = weight.mVertexId;
  45.                 const aiVector3D& srcPos = mesh->mVertices[vertexId];
  46.                 modelMeshes[which].animatedPos[vertexId] += weight.mWeight * (posTrafo * srcPos);
  47.             }
  48.             if(mesh->HasNormals()) {
  49.                 // 3x3 matrix, contains the bone matrix without the translation, only with rotation and possibly scaling
  50.                 aiMatrix3x3 normTrafo = aiMatrix3x3( posTrafo);
  51.                 for( size_t b = 0; b < bone->mNumWeights; b++) {
  52.                     const aiVertexWeight& weight = bone->mWeights[b];
  53.                     size_t vertexId = weight.mVertexId;
  54.                     const aiVector3D& srcNorm = mesh->mNormals[vertexId];
  55.                     modelMeshes[which].animatedNorm[vertexId] += weight.mWeight * (normTrafo * srcNorm);
  56.                 }
  57.             }
  58.         }
  59.         updateGLResources();
  60.     }
  61. };
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement