SkeletalAnimationModelLoader::SkeletalAnimationModelLoader(const std::string file, glm::quat rotation, float scal) : transformation() { //if(rotation){ aiVector3D scale(scal); aiVector3D position(0.0); const aiQuaternion rotat(rotation.w, rotation.x, rotation.y, rotation.z); transformation = aiMatrix4x4Compose(scale, rotat, position); //} ///////////////////////////////////*/ m_fileName = file; Assimp::Importer importer; importer.SetIOHandler(new CustomIOSystem()); const aiScene *scene = importer.ReadFile(file, aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs | aiProcess_CalcTangentSpace); //*/ if (!scene) { log_err("Failed to load mesh: %s", file.c_str()); } else { MeshLoader::loadScene(scene); read_Meshes_Channels_Bones(scene); } }
void SkeletalAnimationModelLoader::createFrame(unsigned int animationId, double time, bool loop) { if (animationId < animations.size()) { for (auto &channel : animations[animationId].channels) { aiVector3D scale = animations[animationId].interpolate(channel.scales, time, true); aiQuaternion rotation = animations[animationId].interpolate(channel.rotations, time, true); aiVector3D position = animations[animationId].interpolate(channel.positions, time, true); bones[channel.boneId].transformation = aiMatrix4x4Compose(scale, rotation, position); } } }
//Draw the animation frame given time in seconds void drawFrame(double time) { model.createFrame(0, time); unsigned int boneId=model.boneName2boneId.at("head"); aiVector3D oldScale; aiQuaternion oldRotation; aiVector3D oldPosition; model.bones[boneId].transformation.Decompose(oldScale, oldRotation, oldPosition); aiMatrix3x3 newRotation; aiMatrix3x3::Rotation(cos(time*10.0), aiVector3D(0, 0, 1), newRotation); model.bones[boneId].transformation=aiMatrix4x4Compose(oldScale, aiQuaternion(newRotation)*oldRotation, oldPosition); for(auto& mesh: model.meshes) { auto meshFrame=model.getMeshFrame(mesh); model.drawMeshFrame(meshFrame); } }