Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef map<string, aiMatrix4x4> Pose;
- class RiggedModel : public ofxAssimpModelLoader {
- public:
- Pose getPose(int which = 0) {
- const aiMesh* mesh = modelMeshes[which].mesh;
- int n = mesh->mNumBones;
- Pose pose;
- for(int a = 0; a < n; a++) {
- const aiBone* bone = mesh->mBones[a];
- aiNode* node = scene->mRootNode->FindNode(bone->mName);
- pose[node->mName.data] = aiMatrix4x4();
- }
- return pose;
- }
- void pose(Pose& pose, int which = 0) {
- const aiMesh* mesh = modelMeshes[which].mesh;
- int n = mesh->mNumBones;
- vector<aiMatrix4x4> boneMatrices(n);
- for(int a = 0; a < n; a++) {
- const aiBone* bone = mesh->mBones[a];
- aiNode* node = scene->mRootNode->FindNode(bone->mName);
- boneMatrices[a] = bone->mOffsetMatrix;
- const aiNode* tempNode = node;
- while(tempNode) {
- aiMatrix4x4 curPose = pose[tempNode->mName.data];
- boneMatrices[a] = (tempNode->mTransformation * curPose) * boneMatrices[a];
- tempNode = tempNode->mParent;
- }
- modelMeshes[which].hasChanged = true;
- modelMeshes[which].validCache = false;
- }
- modelMeshes[which].animatedPos.assign(modelMeshes[which].animatedPos.size(),0);
- if(mesh->HasNormals()){
- modelMeshes[which].animatedNorm.assign(modelMeshes[which].animatedNorm.size(),0);
- }
- // loop through all vertex weights of all bones
- for(int a = 0; a < n; a++) {
- const aiBone* bone = mesh->mBones[a];
- const aiMatrix4x4& posTrafo = boneMatrices[a];
- for( size_t b = 0; b < bone->mNumWeights; b++) {
- const aiVertexWeight& weight = bone->mWeights[b];
- size_t vertexId = weight.mVertexId;
- const aiVector3D& srcPos = mesh->mVertices[vertexId];
- modelMeshes[which].animatedPos[vertexId] += weight.mWeight * (posTrafo * srcPos);
- }
- if(mesh->HasNormals()) {
- // 3x3 matrix, contains the bone matrix without the translation, only with rotation and possibly scaling
- aiMatrix3x3 normTrafo = aiMatrix3x3( posTrafo);
- for( size_t b = 0; b < bone->mNumWeights; b++) {
- const aiVertexWeight& weight = bone->mWeights[b];
- size_t vertexId = weight.mVertexId;
- const aiVector3D& srcNorm = mesh->mNormals[vertexId];
- modelMeshes[which].animatedNorm[vertexId] += weight.mWeight * (normTrafo * srcNorm);
- }
- }
- }
- updateGLResources();
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement