fixed issues to get node animations working

pull/1024/head
Angelo Scandaliato 2016-10-06 02:34:11 -07:00
parent 0313328a92
commit 4ac57f1fab
3 changed files with 115 additions and 49 deletions

View File

@ -950,9 +950,13 @@ namespace glTF
Ref<Accessor> translation; //!< Accessor reference to a buffer storing a array of three-component floating-point vectors. Ref<Accessor> translation; //!< Accessor reference to a buffer storing a array of three-component floating-point vectors.
}; };
AnimChannel Channels[3]; //!< Connect the output values of the key-frame animation to a specific node in the hierarchy. // AnimChannel Channels[3]; //!< Connect the output values of the key-frame animation to a specific node in the hierarchy.
AnimParameters Parameters; //!< The samplers that interpolate between the key-frames. // AnimParameters Parameters; //!< The samplers that interpolate between the key-frames.
AnimSampler Samplers[3]; //!< The parameterized inputs representing the key-frame data. // AnimSampler Samplers[3]; //!< The parameterized inputs representing the key-frame data.
std::vector<AnimChannel> Channels; //!< Connect the output values of the key-frame animation to a specific node in the hierarchy.
AnimParameters Parameters; //!< The samplers that interpolate between the key-frames.
std::vector<AnimSampler> Samplers; //!< The parameterized inputs representing the key-frame data.
Animation() {} Animation() {}
void Read(Value& obj, Asset& r); void Read(Value& obj, Asset& r);

View File

@ -105,9 +105,9 @@ namespace glTF {
/****************** Channels *******************/ /****************** Channels *******************/
Value channels; Value channels;
channels.SetArray(); channels.SetArray();
channels.Reserve(3/*unsigned(a.Channels.size())*/, w.mAl); channels.Reserve(unsigned(a.Channels.size()), w.mAl);
for (size_t i = 0; i < 3/*a.Channels.size()*/; ++i) { for (size_t i = 0; i < unsigned(a.Channels.size()); ++i) {
Animation::AnimChannel& c = a.Channels[i]; Animation::AnimChannel& c = a.Channels[i];
Value valChannel; Value valChannel;
valChannel.SetObject(); valChannel.SetObject();
@ -124,24 +124,32 @@ namespace glTF {
} }
channels.PushBack(valChannel, w.mAl); channels.PushBack(valChannel, w.mAl);
} }
obj.AddMember("Channels", channels, w.mAl); obj.AddMember("channels", channels, w.mAl);
/****************** Parameters *******************/ /****************** Parameters *******************/
Value valParameters; Value valParameters;
valParameters.SetObject(); valParameters.SetObject();
{ {
valParameters.AddMember("TIME", StringRef(a.Parameters.TIME->id), w.mAl); if (a.Parameters.TIME) {
valParameters.AddMember("rotation", StringRef(a.Parameters.rotation->id), w.mAl); valParameters.AddMember("TIME", StringRef(a.Parameters.TIME->id), w.mAl);
valParameters.AddMember("scale", StringRef(a.Parameters.scale->id), w.mAl); }
valParameters.AddMember("translation", StringRef(a.Parameters.translation->id), w.mAl); if (a.Parameters.rotation) {
valParameters.AddMember("rotation", StringRef(a.Parameters.rotation->id), w.mAl);
}
if (a.Parameters.scale) {
valParameters.AddMember("scale", StringRef(a.Parameters.scale->id), w.mAl);
}
if (a.Parameters.translation) {
valParameters.AddMember("translation", StringRef(a.Parameters.translation->id), w.mAl);
}
} }
obj.AddMember("Parameters", valParameters, w.mAl); obj.AddMember("parameters", valParameters, w.mAl);
/****************** Samplers *******************/ /****************** Samplers *******************/
Value valSamplers; Value valSamplers;
valSamplers.SetObject(); valSamplers.SetObject();
for (size_t i = 0; i < 3/*a.Samplers.size()*/; ++i) { for (size_t i = 0; i < unsigned(a.Samplers.size()); ++i) {
Animation::AnimSampler& s = a.Samplers[i]; Animation::AnimSampler& s = a.Samplers[i];
Value valSampler; Value valSampler;
valSampler.SetObject(); valSampler.SetObject();
@ -152,7 +160,7 @@ namespace glTF {
} }
valSamplers.AddMember(StringRef(s.id), valSampler, w.mAl); valSamplers.AddMember(StringRef(s.id), valSampler, w.mAl);
} }
obj.AddMember("Samplers", valSamplers, w.mAl); obj.AddMember("samplers", valSamplers, w.mAl);
} }
inline void Write(Value& obj, Buffer& b, AssetWriter& w) inline void Write(Value& obj, Buffer& b, AssetWriter& w)

View File

@ -146,7 +146,7 @@ glTFExporter::glTFExporter(const char* filename, IOSystem* pIOSystem, const aiSc
ExportAnimations(); ExportAnimations();
ExportSkins(); // ExportSkins();
glTF::AssetWriter writer(*mAsset); glTF::AssetWriter writer(*mAsset);
@ -586,7 +586,9 @@ void glTFExporter::ExportMeshes()
unsigned int glTFExporter::ExportNode(const aiNode* n) unsigned int glTFExporter::ExportNode(const aiNode* n)
{ {
std::cout<< "n->mName.C_Str() " << n->mName.C_Str() << "\n";
Ref<Node> node = mAsset->nodes.Create(mAsset->FindUniqueID(n->mName.C_Str(), "node")); Ref<Node> node = mAsset->nodes.Create(mAsset->FindUniqueID(n->mName.C_Str(), "node"));
std::cout<< "node->id " << node->id << "\n";
if (!n->mTransformation.IsIdentity()) { if (!n->mTransformation.IsIdentity()) {
node->matrix.isPresent = true; node->matrix.isPresent = true;
@ -705,46 +707,54 @@ inline void ExtractAnimationData(Asset& mAsset, std::string& animId, Ref<Animati
//------------------------------------------------------- //-------------------------------------------------------
// Extract TIME parameter data. // Extract TIME parameter data.
// Check if the timeStamps are the same for mPositionKeys, mRotationKeys, and mScalingKeys. // Check if the timeStamps are the same for mPositionKeys, mRotationKeys, and mScalingKeys.
typedef float TimeType; if(nodeChannel->mNumPositionKeys > 0) {
std::vector<TimeType> timeData; std::cout<< "Parameters.TIME\n";
timeData.resize(nodeChannel->mNumPositionKeys); typedef float TimeType;
for (size_t i = 0; i < nodeChannel->mNumPositionKeys; ++i) { std::vector<TimeType> timeData;
timeData[i] = nodeChannel->mPositionKeys[i].mTime; // Check if we have to cast type here. e.g. uint16_t() timeData.resize(nodeChannel->mNumPositionKeys);
} for (size_t i = 0; i < nodeChannel->mNumPositionKeys; ++i) {
timeData[i] = nodeChannel->mPositionKeys[i].mTime; // Check if we have to cast type here. e.g. uint16_t()
}
Ref<Accessor> timeAccessor = ExportAnimationData(mAsset, animId, buffer, nodeChannel->mNumPositionKeys, &timeData[0], AttribType::SCALAR, AttribType::SCALAR, ComponentType_FLOAT); Ref<Accessor> timeAccessor = ExportAnimationData(mAsset, animId, buffer, nodeChannel->mNumPositionKeys, &timeData[0], AttribType::SCALAR, AttribType::SCALAR, ComponentType_FLOAT);
if (timeAccessor) animRef->Parameters.TIME = timeAccessor; if (timeAccessor) animRef->Parameters.TIME = timeAccessor;
}
//------------------------------------------------------- //-------------------------------------------------------
// Extract translation parameter data // Extract translation parameter data
C_STRUCT aiVector3D* translationData = new aiVector3D[nodeChannel->mNumPositionKeys]; if(nodeChannel->mNumPositionKeys > 0) {
for (size_t i = 0; i < nodeChannel->mNumPositionKeys; ++i) { C_STRUCT aiVector3D* translationData = new aiVector3D[nodeChannel->mNumPositionKeys];
translationData[i] = nodeChannel->mPositionKeys[i].mValue; for (size_t i = 0; i < nodeChannel->mNumPositionKeys; ++i) {
} translationData[i] = nodeChannel->mPositionKeys[i].mValue;
}
Ref<Accessor> tranAccessor = ExportAnimationData(mAsset, animId, buffer, nodeChannel->mNumPositionKeys, translationData, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT); Ref<Accessor> tranAccessor = ExportAnimationData(mAsset, animId, buffer, nodeChannel->mNumPositionKeys, translationData, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
if (tranAccessor) animRef->Parameters.translation = tranAccessor; if (tranAccessor) animRef->Parameters.translation = tranAccessor;
}
//------------------------------------------------------- //-------------------------------------------------------
// Extract scale parameter data // Extract scale parameter data
C_STRUCT aiVector3D* scaleData = new aiVector3D[nodeChannel->mNumScalingKeys]; if(nodeChannel->mNumScalingKeys > 0) {
for (size_t i = 0; i < nodeChannel->mNumScalingKeys; ++i) { C_STRUCT aiVector3D* scaleData = new aiVector3D[nodeChannel->mNumScalingKeys];
scaleData[i] = nodeChannel->mScalingKeys[i].mValue; for (size_t i = 0; i < nodeChannel->mNumScalingKeys; ++i) {
} scaleData[i] = nodeChannel->mScalingKeys[i].mValue;
}
Ref<Accessor> scaleAccessor = ExportAnimationData(mAsset, animId, buffer, nodeChannel->mNumScalingKeys, scaleData, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT); Ref<Accessor> scaleAccessor = ExportAnimationData(mAsset, animId, buffer, nodeChannel->mNumScalingKeys, scaleData, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
if (scaleAccessor) animRef->Parameters.scale = scaleAccessor; if (scaleAccessor) animRef->Parameters.scale = scaleAccessor;
}
//------------------------------------------------------- //-------------------------------------------------------
// Extract rotation parameter data // Extract rotation parameter data
C_STRUCT aiQuaternion* rotationData = new aiQuaternion[nodeChannel->mNumRotationKeys]; if(nodeChannel->mNumRotationKeys > 0) {
for (size_t i = 0; i < nodeChannel->mNumRotationKeys; ++i) { C_STRUCT aiQuaternion* rotationData = new aiQuaternion[nodeChannel->mNumRotationKeys];
rotationData[i] = nodeChannel->mRotationKeys[i].mValue; for (size_t i = 0; i < nodeChannel->mNumRotationKeys; ++i) {
rotationData[i] = nodeChannel->mRotationKeys[i].mValue;
}
Ref<Accessor> rotAccessor = ExportAnimationData(mAsset, animId, buffer, nodeChannel->mNumRotationKeys, rotationData, AttribType::VEC4, AttribType::VEC4, ComponentType_FLOAT);
if (rotAccessor) animRef->Parameters.rotation = rotAccessor;
} }
Ref<Accessor> rotAccessor = ExportAnimationData(mAsset, animId, buffer, nodeChannel->mNumRotationKeys, rotationData, AttribType::VEC4, AttribType::VEC4, ComponentType_FLOAT);
if (rotAccessor) animRef->Parameters.rotation = rotAccessor;
} }
@ -784,7 +794,7 @@ void glTFExporter::ExportAnimations()
for (unsigned int i = 0; i < mScene->mNumAnimations; ++i) { for (unsigned int i = 0; i < mScene->mNumAnimations; ++i) {
const aiAnimation* anim = mScene->mAnimations[i]; const aiAnimation* anim = mScene->mAnimations[i];
std::string nameAnim; std::string nameAnim = "anim";
if (anim->mName.length > 0) { if (anim->mName.length > 0) {
nameAnim = anim->mName.C_Str(); nameAnim = anim->mName.C_Str();
} }
@ -798,6 +808,7 @@ void glTFExporter::ExportAnimations()
std::string name = nameAnim + "_" + std::to_string(channelIndex); std::string name = nameAnim + "_" + std::to_string(channelIndex);
name = mAsset->FindUniqueID(name, "animation"); name = mAsset->FindUniqueID(name, "animation");
Ref<Animation> animRef = mAsset->animations.Create(name); Ref<Animation> animRef = mAsset->animations.Create(name);
std::cout<<"channelName " << name << "\n";
/******************* Parameters ********************/ /******************* Parameters ********************/
// If compression is used then you need parameters of uncompressed region: begin and size. At this step "begin" is stored. // If compression is used then you need parameters of uncompressed region: begin and size. At this step "begin" is stored.
@ -808,30 +819,73 @@ void glTFExporter::ExportAnimations()
// Otherwise, add to the buffer and create a new accessor. // Otherwise, add to the buffer and create a new accessor.
ExtractAnimationData(*mAsset, name, animRef, bufferRef, nodeChannel); ExtractAnimationData(*mAsset, name, animRef, bufferRef, nodeChannel);
// for (unsigned int j = 0; j < 3; ++j) {
// std::string channelType;
// switch (j) {
// case 0:
// channelType = "rotation";
// break;
// case 1:
// channelType = "scale";
// break;
// case 2:
// channelType = "translation";
// break;
// }
// animRef->Channels[j].sampler = name + "_" + channelType;
// animRef->Channels[j].target.path = channelType;
// animRef->Samplers[j].output = channelType;
// animRef->Samplers[j].id = name + "_" + channelType;
// animRef->Channels[j].target.id = mAsset->nodes.Get(nodeChannel->mNodeName.C_Str());
// animRef->Samplers[j].input = "TIME";
// animRef->Samplers[j].interpolation = "LINEAR";
// }
for (unsigned int j = 0; j < 3; ++j) { for (unsigned int j = 0; j < 3; ++j) {
std::string channelType; std::string channelType;
int channelSize;
switch (j) { switch (j) {
case 0: case 0:
channelType = "rotation"; channelType = "rotation";
channelSize = nodeChannel->mNumRotationKeys;
break; break;
case 1: case 1:
channelType = "scale"; channelType = "scale";
channelSize = nodeChannel->mNumScalingKeys;
break; break;
case 2: case 2:
channelType = "translation"; channelType = "translation";
channelSize = nodeChannel->mNumPositionKeys;
break; break;
} }
animRef->Channels[j].sampler = name + "_" + channelType; if (channelSize < 1) { continue; }
animRef->Channels[j].target.path = channelType;
animRef->Samplers[j].output = channelType;
animRef->Samplers[j].id = name + "_" + channelType;
animRef->Channels[j].target.id = mAsset->nodes.Get(nodeChannel->mNodeName.C_Str()); std::cout<<"channelType " << channelType << "\n";
animRef->Samplers[j].input = "TIME"; Animation::AnimChannel tmpAnimChannel;
animRef->Samplers[j].interpolation = "LINEAR"; Animation::AnimSampler tmpAnimSampler;
tmpAnimChannel.sampler = name + "_" + channelType;
tmpAnimChannel.target.path = channelType;
tmpAnimSampler.output = channelType;
tmpAnimSampler.id = name + "_" + channelType;
std::cout<<"nodeChannel->mNodeName.C_Str() " << nodeChannel->mNodeName.C_Str() << "\n";
tmpAnimChannel.target.id = mAsset->nodes.Get(nodeChannel->mNodeName.C_Str());
std::cout<<"tmpAnimChannel.target.id " << tmpAnimChannel.target.id << "\n";
tmpAnimSampler.input = "TIME";
tmpAnimSampler.interpolation = "LINEAR";
animRef->Channels.push_back(tmpAnimChannel);
animRef->Samplers.push_back(tmpAnimSampler);
} }
} }
std::cout<<"mNumMeshChannels " << anim->mNumMeshChannels << "\n"; std::cout<<"mNumMeshChannels " << anim->mNumMeshChannels << "\n";