From 79852de5ae0d11f974d28248712b90bdb47c1c76 Mon Sep 17 00:00:00 2001 From: Angelo Scandaliato Date: Wed, 5 Oct 2016 17:30:02 -0700 Subject: [PATCH] added skins and jointNames --- code/glTFAsset.h | 14 ++++++++++-- code/glTFAssetWriter.inl | 12 ++++++++++ code/glTFExporter.cpp | 59 +++++++++++++++++++++++++++++++++--------------- code/glTFExporter.h | 1 + 4 files changed, 66 insertions(+), 20 deletions(-) diff --git a/code/glTFAsset.h b/code/glTFAsset.h index dd94f2b..b7ea8fb 100644 --- a/code/glTFAsset.h +++ b/code/glTFAsset.h @@ -128,6 +128,7 @@ namespace glTF struct BufferView; // here due to cross-reference struct Texture; struct Light; + struct Skin; // Vec/matrix types, as raw float arrays @@ -806,6 +807,10 @@ namespace glTF Ref camera; Ref light; + std::vector< Ref > skeletons; //!< The ID of skeleton nodes. + Ref skin; //!< The ID of the skin referenced by this node. + std::string jointName; //!< Name used when this node is a joint in a skin. + Node() {} void Read(Value& obj, Asset& r); }; @@ -845,6 +850,11 @@ namespace glTF struct Skin : public Object { + Nullable bindShapeMatrix; //!< Floating-point 4x4 transformation matrix stored in column-major order. + Ref inverseBindMatrices; //!< The ID of the accessor containing the floating-point 4x4 inverse-bind matrices. + std::vector*/> jointNames; //!< Joint names of the joints (nodes with a jointName property) in this skin. + std::string name; //!< The user-defined name of this object. + Skin() {} void Read(Value& obj, Asset& r); }; @@ -1099,7 +1109,7 @@ namespace glTF LazyDict samplers; LazyDict scenes; //LazyDict shaders; - //LazyDict skins; + LazyDict skins; //LazyDict techniques; LazyDict textures; @@ -1124,7 +1134,7 @@ namespace glTF , samplers (*this, "samplers") , scenes (*this, "scenes") //, shaders (*this, "shaders") - //, skins (*this, "skins") + , skins (*this, "skins") //, techniques (*this, "techniques") , textures (*this, "textures") , lights (*this, "lights", "KHR_materials_common") diff --git a/code/glTFAssetWriter.inl b/code/glTFAssetWriter.inl index d79a64d..7aff77a 100644 --- a/code/glTFAssetWriter.inl +++ b/code/glTFAssetWriter.inl @@ -377,6 +377,10 @@ namespace glTF { AddRefsVector(obj, "children", n.children, w.mAl); AddRefsVector(obj, "meshes", n.meshes, w.mAl); + + if (!n.jointName.empty()) { + obj.AddMember("jointName", n.jointName, w.mAl); + } } inline void Write(Value& obj, Program& b, AssetWriter& w) @@ -412,7 +416,15 @@ namespace glTF { inline void Write(Value& obj, Skin& b, AssetWriter& w) { + /****************** jointNames *******************/ + Value vJointNames; + vJointNames.SetArray(); + vJointNames.Reserve(unsigned(b.jointNames.size()), w.mAl); + for (size_t i = 0; i < unsigned(b.jointNames.size()); ++i) { + vJointNames.PushBack(StringRef(b.jointNames[i]), w.mAl); + } + obj.AddMember("jointNames", vJointNames, w.mAl); } inline void Write(Value& obj, Technique& b, AssetWriter& w) diff --git a/code/glTFExporter.cpp b/code/glTFExporter.cpp index 686846b..3d1aa99 100644 --- a/code/glTFExporter.cpp +++ b/code/glTFExporter.cpp @@ -126,8 +126,6 @@ glTFExporter::glTFExporter(const char* filename, IOSystem* pIOSystem, const aiSc ExportMetadata(); - //for (unsigned int i = 0; i < pScene->mNumAnimations; ++i) {} - //for (unsigned int i = 0; i < pScene->mNumCameras; ++i) {} //for (unsigned int i = 0; i < pScene->mNumLights; ++i) {} @@ -148,6 +146,8 @@ glTFExporter::glTFExporter(const char* filename, IOSystem* pIOSystem, const aiSc ExportAnimations(); + ExportSkins(); + glTF::AssetWriter writer(*mAsset); if (isBinary) { @@ -701,8 +701,7 @@ inline void ExtractAnimationData(Asset& mAsset, std::string& animId, Ref timeData; timeData.resize(nodeChannel->mNumPositionKeys); for (size_t i = 0; i < nodeChannel->mNumPositionKeys; ++i) { - // timeData[i] = uint16_t(nodeChannel->mPositionKeys[i].mTime); - timeData[i] = nodeChannel->mPositionKeys[i].mTime; + timeData[i] = nodeChannel->mPositionKeys[i].mTime; // Check if we have to cast type here. e.g. uint16_t() } Ref timeAccessor = ExportAnimationData(mAsset, animId, buffer, nodeChannel->mNumPositionKeys, &timeData[0], AttribType::SCALAR, AttribType::SCALAR, ComponentType_FLOAT); @@ -773,7 +772,6 @@ void glTFExporter::ExportAnimations() // // Setup to output buffer data // //-------------------------- - // aiString aiName; std::cout<<"mNumAnimations " << mScene->mNumAnimations << "\n"; for (unsigned int i = 0; i < mScene->mNumAnimations; ++i) { const aiAnimation* anim = mScene->mAnimations[i]; @@ -793,25 +791,15 @@ void glTFExporter::ExportAnimations() name = mAsset->FindUniqueID(name, "animation"); Ref animRef = mAsset->animations.Create(name); - // Loop over the data and check to see if it exactly matches an existing buffer. - // If yes, then reference the existing corresponding accessor. - // Otherwise, add to the buffer and create a new accessor. - /******************* Parameters ********************/ // If compression is used then you need parameters of uncompressed region: begin and size. At this step "begin" is stored. // if(comp_allow) idx_srcdata_begin = bufferRef->byteLength; + // Loop over the data and check to see if it exactly matches an existing buffer. + // If yes, then reference the existing corresponding accessor. + // Otherwise, add to the buffer and create a new accessor. ExtractAnimationData(*mAsset, name, animRef, bufferRef, nodeChannel); - // FAKE DATA FOR NOW!!!!! - // These are accessors to bufferviews to buffer data. - // Ref acc = mAsset->accessors.Get(unsigned (0)); - // animRef->Parameters.TIME = acc; - // animRef->Parameters.rotation = acc; - // animRef->Parameters.scale = acc; - // animRef->Parameters.translation = acc; - - for (unsigned int j = 0; j < 3; ++j) { std::string channelType; switch (j) { @@ -848,6 +836,41 @@ void glTFExporter::ExportAnimations() +void glTFExporter::ExportSkins() +{ + for (unsigned int idx_mesh = 0; idx_mesh < mScene->mNumMeshes; ++idx_mesh) { + const aiMesh* aim = mScene->mMeshes[idx_mesh]; + + if(!aim->HasBones()) { continue; } // skip to next mesh if no bones. + + std::string skinName = aim->mName.C_Str(); + skinName = mAsset->FindUniqueID(skinName, "skin"); + Ref skinRef = mAsset->skins.Create(skinName); + skinRef->name = skinName; + + for (unsigned int idx_bone = 0; idx_bone < aim->mNumBones; ++idx_bone) { + const aiBone* aib = aim->mBones[idx_bone]; + + Ref nodeRef = mAsset->nodes.Get(aib->mName.C_Str()); + nodeRef->jointName = "joint_" + std::to_string(idx_bone); + + skinRef->jointNames.push_back("joint_" + std::to_string(idx_bone)); + + std::cout << "Node->id " << nodeRef->id << "\n"; + + // skinRef->bindShapeMatrix; + // skinRef->inverseBindMatrices; + + // aib->mNumWeights; + // aib->mOffsetMatrix; + // aib->mWeights; + + } // End: for-loop mNumMeshes + + } // End: for-loop mNumMeshes +} + + #endif // ASSIMP_BUILD_NO_GLTF_EXPORTER diff --git a/code/glTFExporter.h b/code/glTFExporter.h index 6e95230..a661183 100644 --- a/code/glTFExporter.h +++ b/code/glTFExporter.h @@ -102,6 +102,7 @@ namespace Assimp unsigned int ExportNode(const aiNode* node); void ExportScene(); void ExportAnimations(); + void ExportSkins(); }; } -- 2.7.4