virtual void read(const aiScene *scene) { //Find channels, and the bones used in the channels for(unsigned int ca=0;ca<scene->mNumAnimations;ca++) { animations.emplace_back(); auto& animation=animations[ca]; animation.duration=scene->mAnimations[ca]->mDuration; animation.ticksPerSecond=scene->mAnimations[ca]->mTicksPerSecond; animation.channels.resize(scene->mAnimations[ca]->mNumChannels); for(unsigned int cc=0;cc<scene->mAnimations[ca]->mNumChannels;cc++) { animation.channels[cc].positions.resize(scene->mAnimations[ca]->mChannels[cc]->mNumPositionKeys); animation.channels[cc].scales.resize(scene->mAnimations[ca]->mChannels[cc]->mNumScalingKeys); animation.channels[cc].rotations.resize(scene->mAnimations[ca]->mChannels[cc]->mNumRotationKeys); for(unsigned int cp=0;cp<scene->mAnimations[ca]->mChannels[cc]->mNumPositionKeys;cp++) { animation.channels[cc].positions[cp]=scene->mAnimations[ca]->mChannels[cc]->mPositionKeys[cp]; } for(unsigned int cs=0;cs<scene->mAnimations[ca]->mChannels[cc]->mNumScalingKeys;cs++) { animation.channels[cc].scales[cs]=scene->mAnimations[ca]->mChannels[cc]->mScalingKeys[cs]; } for(unsigned int cr=0;cr<scene->mAnimations[ca]->mChannels[cc]->mNumRotationKeys;cr++) { animation.channels[cc].rotations[cr]=scene->mAnimations[ca]->mChannels[cc]->mRotationKeys[cr]; } const aiNode* node=scene->mRootNode->FindNode(scene->mAnimations[ca]->mChannels[cc]->mNodeName); animations[ca].channels[cc].boneId=getBoneId(node); } } //Find all the bones, and their parent bones, connected to the meshes for(unsigned int cm=0;cm<scene->mNumMeshes;cm++) { for(unsigned int cb=0;cb<scene->mMeshes[cm]->mNumBones;cb++) { const aiNode* node=scene->mRootNode->FindNode(scene->mMeshes[cm]->mBones[cb]->mName); this->meshes[cm].boneWeights.emplace_back(); unsigned int boneId=getBoneId(node); this->meshes[cm].boneWeights[cb].boneId=boneId; this->meshes[cm].boneWeights[cb].offsetMatrix=scene->mMeshes[cm]->mBones[cb]->mOffsetMatrix; for(unsigned int cw=0;cw<scene->mMeshes[cm]->mBones[cb]->mNumWeights;cw++) { this->meshes[cm].boneWeights[cb].weights.emplace_back(scene->mMeshes[cm]->mBones[cb]->mWeights[cw]); } if(!bones[boneId].hasParentBoneId) { //Populate Bone::parentBoneIds node=node->mParent; while(node!=scene->mRootNode) { unsigned int parentBoneId=getBoneId(node); bones[boneId].parentBoneId=parentBoneId; bones[boneId].hasParentBoneId=true; boneId=parentBoneId; node=node->mParent; } } } } }
void SkeletalAnimationModelLoader::read_Meshes_Channels_Bones(const aiScene *scene) { ////////////////////////////////////////////////////////////////// // Read vertices, normals, texture coordinates ////////////////////////////////////////////////////////////////// for (unsigned int cm = 0; cm < scene->mNumMeshes; cm++) { this->meshes.emplace_back(); const aiMesh *mesh = scene->mMeshes[cm]; meshes[cm].name = mesh->mName.C_Str(); meshes[cm].materialId = mesh->mMaterialIndex; meshes[cm].vertices.resize(mesh->mNumVertices); meshes[cm].normals.resize(mesh->mNumVertices); meshes[cm].tangents.resize(mesh->mNumVertices); meshes[cm].textureCoords.resize(mesh->mNumVertices); const aiVector3D aiZeroVector(0.0f, 0.0f, 0.0f); for (unsigned int cv = 0; cv < mesh->mNumVertices; cv++) { meshes[cm].vertices[cv] = mesh->mVertices[cv]; meshes[cm].normals[cv] = mesh->mNormals[cv]; meshes[cm].tangents[cv] = mesh->HasTangentsAndBitangents() ? mesh->mTangents[cv] : aiZeroVector; if (mesh->HasTextureCoords(0)) { meshes[cm].textureCoords[cv].x = mesh->mTextureCoords[0][cv].x; meshes[cm].textureCoords[cv].y = mesh->mTextureCoords[0][cv].y; } else meshes[cm].textureCoords[cv].x = meshes[cm].textureCoords[cv].y = 0.0; } meshes[cm].faces.resize(mesh->mNumFaces); for (unsigned int cf = 0; cf < mesh->mNumFaces; cf++) { meshes[cm].faces[cf] = mesh->mFaces[cf]; } } /////////////////////////////////////////////////////// // Find channels, and the bones used in the channels /////////////////////////////////////////////////////// for (unsigned int ca = 0; ca < scene->mNumAnimations; ca++) { animations.emplace_back(); auto &animation = animations[ca]; animation.duration = scene->mAnimations[ca]->mDuration; animation.ticksPerSecond = scene->mAnimations[ca]->mTicksPerSecond; animation.channels.resize(scene->mAnimations[ca]->mNumChannels); for (unsigned int cc = 0; cc < scene->mAnimations[ca]->mNumChannels; cc++) { animation.channels[cc].positions.resize(scene->mAnimations[ca]->mChannels[cc]->mNumPositionKeys); animation.channels[cc].scales.resize(scene->mAnimations[ca]->mChannels[cc]->mNumScalingKeys); animation.channels[cc].rotations.resize(scene->mAnimations[ca]->mChannels[cc]->mNumRotationKeys); for (unsigned int cp = 0; cp < scene->mAnimations[ca]->mChannels[cc]->mNumPositionKeys; cp++) { animation.channels[cc].positions[cp] = scene->mAnimations[ca]->mChannels[cc]->mPositionKeys[cp]; } for (unsigned int cs = 0; cs < scene->mAnimations[ca]->mChannels[cc]->mNumScalingKeys; cs++) { animation.channels[cc].scales[cs] = scene->mAnimations[ca]->mChannels[cc]->mScalingKeys[cs]; } for (unsigned int cr = 0; cr < scene->mAnimations[ca]->mChannels[cc]->mNumRotationKeys; cr++) { animation.channels[cc].rotations[cr] = scene->mAnimations[ca]->mChannels[cc]->mRotationKeys[cr]; } const aiNode *node = scene->mRootNode->FindNode(scene->mAnimations[ca]->mChannels[cc]->mNodeName); animations[ca].channels[cc].boneId = getBoneId(node); } } /////////////////////////////////////////////////////////////////////// // Find all the bones, and their parent bones, connected to the meshes /////////////////////////////////////////////////////////////////////// for (unsigned int cm = 0; cm < scene->mNumMeshes; cm++) { for (unsigned int cb = 0; cb < scene->mMeshes[cm]->mNumBones; cb++) { const aiNode *node = scene->mRootNode->FindNode(scene->mMeshes[cm]->mBones[cb]->mName); this->meshes[cm].boneWeights.emplace_back(); unsigned int boneId = getBoneId(node); this->meshes[cm].boneWeights[cb].boneId = boneId; this->meshes[cm].boneWeights[cb].offsetMatrix = scene->mMeshes[cm]->mBones[cb]->mOffsetMatrix; for (unsigned int cw = 0; cw < scene->mMeshes[cm]->mBones[cb]->mNumWeights; cw++) { this->meshes[cm].boneWeights[cb].weights.emplace_back(scene->mMeshes[cm]->mBones[cb]->mWeights[cw]); } if (!bones[boneId].hasParentBoneId) { //Populate Bone::parentBoneIds node = node->mParent; while (node != scene->mRootNode) { unsigned int parentBoneId = getBoneId(node); bones[boneId].parentBoneId = parentBoneId; bones[boneId].hasParentBoneId = true; boneId = parentBoneId; node = node->mParent; } } } } }