Skip to content

Commit

Permalink
Initialize keyFrame struct properly. Go back to use inverseBindMatrices.
Browse files Browse the repository at this point in the history
  • Loading branch information
xissburg committed Jul 6, 2019
1 parent f8c192d commit f936ba3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 32 deletions.
44 changes: 16 additions & 28 deletions src/Ogre_glTF_skeletonImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,18 @@ void skeletonImporter::addChidren(const std::vector<int>& childs, Ogre::v1::OldB
auto bone = skeleton->getBone(nodeToJointMap[child]);
if(!bone) { throw InitError("could not get bone " + std::to_string(bone->getHandle())); }

if(!node.translation.empty())
bone->setPosition(node.translation[0], node.translation[1], node.translation[2]);
parent->addChild(bone);

if(!node.rotation.empty())
bone->setOrientation(node.rotation[3], node.rotation[0], node.rotation[1], node.rotation[2]);
auto bindMatrix = bindMatrices[nodeToJointMap[child]];

if(!node.scale.empty())
bone->setScale(node.scale[0], node.scale[1], node.scale[2]);
Ogre::Vector3 translation, scale;
Ogre::Quaternion rotation;

if(!node.matrix.empty())
{
std::array<Ogre::Real, 4 * 4> matrixArray { 0 };
internal_utils::container_double_to_real(node.matrix, matrixArray);
Ogre::Matrix4 matrix { matrixArray.data() };
Ogre::Vector3 position;
Ogre::Quaternion orientation;
Ogre::Vector3 scale;
matrix.transpose().decomposition(position, scale, orientation);
bone->setPosition(position);
bone->setOrientation(orientation);
bone->setScale(scale);
}
bindMatrix.decomposition(translation, scale, rotation);

parent->addChild(bone);
bone->setPosition(parent->convertWorldToLocalPosition(translation));
bone->setOrientation(parent->convertWorldToLocalOrientation(rotation));
bone->setScale(parent->_getDerivedScale() / scale);

addChidren(model.nodes[child].children, bone);
}
Expand Down Expand Up @@ -112,7 +100,7 @@ void skeletonImporter::loadTimepointFromSamplerToKeyFrame(int bone, int frameID,
animationFrame.timePoint = data;
else if(animationFrame.timePoint != data)
{
throw FileIOError("Missmatch of timecode while loading an animation keyframe for bone joint " + std::to_string(bone)
throw FileIOError("Mismatch of timecode while loading an animation keyframe for bone joint " + std::to_string(bone)
+ "\n"
"read from file : "
+ std::to_string(data) + " while animationFrame recorded " + std::to_string(animationFrame.timePoint));
Expand All @@ -128,7 +116,7 @@ void skeletonImporter::loadVector3FromSampler(int frameID, int& count, tinygltf:
unsigned char* dataStart = buffer.data.data() + bufferView.byteOffset + output.byteOffset;
const size_t byteStride = output.ByteStride(bufferView);

assert(output.type == TINYGLTF_TYPE_VEC3); //Need to be a 3D vectorsince it's a translation vector
assert(output.type == TINYGLTF_TYPE_VEC3); //Need to be a 3D vector since it's a translation vector

if(output.componentType == TINYGLTF_COMPONENT_TYPE_FLOAT) { vector = Ogre::Vector3(reinterpret_cast<float*>(dataStart + frameID * byteStride)); }
else if(output.componentType == TINYGLTF_COMPONENT_TYPE_DOUBLE) //need double to float conversion
Expand All @@ -152,7 +140,7 @@ void skeletonImporter::loadQuatFromSampler(int frameID, int& count, tinygltf::An
unsigned char* dataStart = buffer.data.data() + bufferView.byteOffset + output.byteOffset;
const size_t byteStride = output.ByteStride(bufferView);

assert(output.type == TINYGLTF_TYPE_VEC4); //Need to be a 4D vectorsince it's a translation vector
assert(output.type == TINYGLTF_TYPE_VEC4); //Need to be a 4D vector since it's a quaternion

if(output.componentType == TINYGLTF_COMPONENT_TYPE_FLOAT)
{
Expand All @@ -164,7 +152,7 @@ void skeletonImporter::loadQuatFromSampler(int frameID, int& count, tinygltf::An
std::array<Ogre::Real, 4> vectFloat {};
std::array<double, 4> vectDouble {};

memcpy(vectDouble.data(), reinterpret_cast<double*>(dataStart + frameID * byteStride), 3 * sizeof(double));
memcpy(vectDouble.data(), reinterpret_cast<double*>(dataStart + frameID * byteStride), 4 * sizeof(double));
internal_utils::container_double_to_real(vectDouble, vectFloat);

quat = Ogre::Quaternion(vectFloat[3], vectFloat[0], vectFloat[1], vectFloat[2]);
Expand Down Expand Up @@ -270,7 +258,7 @@ void skeletonImporter::loadSkeletonAnimations(const tinygltf::Skin skin, const s

std::unordered_map<tinygltfJointNodeIndex, keyFrameList> boneIndexedKeyFrames;

const auto getAnimationLenght = [](const keyFrameList& l) {
const auto getAnimationLength = [](const keyFrameList& l) {
if(l.empty()) return 0.0f;
return l.back().timePoint;
};
Expand All @@ -293,8 +281,8 @@ void skeletonImporter::loadSkeletonAnimations(const tinygltf::Skin skin, const s
for(auto& channel : animation.channels)
{
const auto joint = nodeToJointMap[channel.target_node];
const auto& boneRawAnnimationChannelIt = boneRawAnimationChannels.find(joint);
if(boneRawAnnimationChannelIt == boneRawAnimationChannels.end()) boneRawAnimationChannels[joint];
const auto& boneRawAnimationChannelIt = boneRawAnimationChannels.find(joint);
if(boneRawAnimationChannelIt == boneRawAnimationChannels.end()) boneRawAnimationChannels[joint];

boneRawAnimationChannels[joint].push_back(channel);
}
Expand All @@ -317,7 +305,7 @@ void skeletonImporter::loadSkeletonAnimations(const tinygltf::Skin skin, const s

//here, we have a list of all key frames for one bone
boneIndexedKeyFrames[bone] = keyFrames;
maxLen = getAnimationLenght(keyFrames);
maxLen = getAnimationLength(keyFrames);
}

//Create animation
Expand Down
10 changes: 6 additions & 4 deletions src/private_headers/Ogre_glTF_skeletonImporter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ namespace Ogre_glTF
///Represent a keyframe as laded from tinygltf, but converted to Ogre objects
struct keyFrame
{
float timePoint{ -1 };
float weights{};
Ogre::Quaternion rotation{};
Ogre::Vector3 position, scale{};
float timePoint;
float weights;
Ogre::Quaternion rotation;
Ogre::Vector3 position, scale;

keyFrame() : timePoint(-1), weights(0), position(0,0,0), scale(1,1,1) {}
};

///Vector of keyframes
Expand Down

0 comments on commit f936ba3

Please sign in to comment.