From 6b4caa0f12ead97d29c4eea401fda1ccfa5e2917 Mon Sep 17 00:00:00 2001 From: wuxiaoqian Date: Thu, 26 Apr 2018 10:56:56 +0800 Subject: [PATCH 1/2] fix export gltf2, The JOINTS_0 componentType is incorrect --- code/glTF2Asset.h | 1 + code/glTF2Asset.inl | 23 +++++++++++++++++++++++ code/glTF2Exporter.cpp | 20 ++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/code/glTF2Asset.h b/code/glTF2Asset.h index cece307d9..dd27398a4 100644 --- a/code/glTF2Asset.h +++ b/code/glTF2Asset.h @@ -601,6 +601,7 @@ namespace glTF2 /// \param [in] pReplace_Count - count of bytes in new data. /// \return true - if successfully replaced, false if input arguments is out of range. bool ReplaceData(const size_t pBufferData_Offset, const size_t pBufferData_Count, const uint8_t* pReplace_Data, const size_t pReplace_Count); + bool ReplaceData_joint(const size_t pBufferData_Offset, const size_t pBufferData_Count, const uint8_t* pReplace_Data, const size_t pReplace_Count); size_t AppendData(uint8_t* data, size_t length); void Grow(size_t amount); diff --git a/code/glTF2Asset.inl b/code/glTF2Asset.inl index 549df747e..e51f234c1 100644 --- a/code/glTF2Asset.inl +++ b/code/glTF2Asset.inl @@ -481,6 +481,29 @@ uint8_t* new_data; return true; } +inline bool Buffer::ReplaceData_joint(const size_t pBufferData_Offset, const size_t pBufferData_Count, const uint8_t* pReplace_Data, const size_t pReplace_Count) +{ +const size_t new_data_size = byteLength + pReplace_Count - pBufferData_Count; + +uint8_t* new_data; + + if((pBufferData_Count == 0) || (pReplace_Count == 0) || (pReplace_Data == nullptr)) return false; + + new_data = new uint8_t[new_data_size]; + // Copy data which place before replacing part. + memcpy(new_data, mData.get(), pBufferData_Offset); + // Copy new data. + memcpy(&new_data[pBufferData_Offset], pReplace_Data, pReplace_Count); + // Copy data which place after replacing part. + memcpy(&new_data[pBufferData_Offset + pReplace_Count], &mData.get()[pBufferData_Offset + pBufferData_Count] + , new_data_size - (pBufferData_Offset + pReplace_Count) + ); + // Apply new data + mData.reset(new_data, std::default_delete()); + byteLength = new_data_size; + + return true; +} inline size_t Buffer::AppendData(uint8_t* data, size_t length) { diff --git a/code/glTF2Exporter.cpp b/code/glTF2Exporter.cpp index c1a803c1f..3f4fe9cd0 100644 --- a/code/glTF2Exporter.cpp +++ b/code/glTF2Exporter.cpp @@ -623,6 +623,26 @@ void ExportSkin(Asset& mAsset, const aiMesh* aimesh, Ref& meshRef, Refprimitives.back(); Ref vertexJointAccessor = ExportData(mAsset, skinRef->id, bufferRef, aimesh->mNumVertices, vertexJointData, AttribType::VEC4, AttribType::VEC4, ComponentType_FLOAT); if ( vertexJointAccessor ) { + unsigned int offset = vertexJointAccessor->bufferView->byteOffset; + unsigned int bytesLen = vertexJointAccessor->bufferView->byteLength; + unsigned int s_bytesPerComp= ComponentTypeSize(ComponentType_UNSIGNED_SHORT); + unsigned int bytesPerComp = ComponentTypeSize(vertexJointAccessor->componentType); + unsigned int s_bytesLen = bytesLen * s_bytesPerComp / bytesPerComp; + Ref buf = vertexJointAccessor->bufferView->buffer; + uint8_t* arrys = new uint8_t[s_bytesLen]; + unsigned int i = 0; + for ( unsigned int j = 0; j <= bytesLen; j += bytesPerComp ){ + size_t len_p = offset + j; + float f_value = *(float *)&buf->GetPointer()[len_p]; + unsigned short c = static_cast(f_value); + uint8_t* data = new uint8_t[s_bytesPerComp]; + data = (uint8_t*)&c; + memcpy(&arrys[i*s_bytesPerComp], data, s_bytesPerComp); + ++i; + } + buf->ReplaceData_joint(offset, bytesLen, arrys, s_bytesLen); + vertexJointAccessor->componentType = ComponentType_UNSIGNED_SHORT; + p.attributes.joint.push_back( vertexJointAccessor ); } From dd7d0943f6bfd4502b0285518917f3a8093a931b Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Thu, 26 Apr 2018 14:24:00 +0200 Subject: [PATCH 2/2] Update glTF2Asset.inl Move creation of vars to avoid useless creation in case of an error. --- code/glTF2Asset.inl | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/code/glTF2Asset.inl b/code/glTF2Asset.inl index e132765ff..d96db6d74 100644 --- a/code/glTF2Asset.inl +++ b/code/glTF2Asset.inl @@ -461,36 +461,38 @@ inline void Buffer::EncodedRegion_SetCurrent(const std::string& pID) throw DeadlyImportError("GLTF: EncodedRegion with ID: \"" + pID + "\" not found."); } -inline bool Buffer::ReplaceData(const size_t pBufferData_Offset, const size_t pBufferData_Count, const uint8_t* pReplace_Data, const size_t pReplace_Count) +inline +bool Buffer::ReplaceData(const size_t pBufferData_Offset, const size_t pBufferData_Count, const uint8_t* pReplace_Data, const size_t pReplace_Count) { -const size_t new_data_size = byteLength + pReplace_Count - pBufferData_Count; -uint8_t* new_data; + if((pBufferData_Count == 0) || (pReplace_Count == 0) || (pReplace_Data == nullptr)) { + return false; + } - if((pBufferData_Count == 0) || (pReplace_Count == 0) || (pReplace_Data == nullptr)) return false; - - new_data = new uint8_t[new_data_size]; + const size_t new_data_size = byteLength + pReplace_Count - pBufferData_Count; + uint8_t *new_data = new uint8_t[new_data_size]; // Copy data which place before replacing part. - memcpy(new_data, mData.get(), pBufferData_Offset); + ::memcpy(new_data, mData.get(), pBufferData_Offset); // Copy new data. - memcpy(&new_data[pBufferData_Offset], pReplace_Data, pReplace_Count); + ::memcpy(&new_data[pBufferData_Offset], pReplace_Data, pReplace_Count); // Copy data which place after replacing part. - memcpy(&new_data[pBufferData_Offset + pReplace_Count], &mData.get()[pBufferData_Offset + pBufferData_Count], pBufferData_Offset); + ::memcpy(&new_data[pBufferData_Offset + pReplace_Count], &mData.get()[pBufferData_Offset + pBufferData_Count], pBufferData_Offset); // Apply new data mData.reset(new_data, std::default_delete()); byteLength = new_data_size; return true; } -inline bool Buffer::ReplaceData_joint(const size_t pBufferData_Offset, const size_t pBufferData_Count, const uint8_t* pReplace_Data, const size_t pReplace_Count) + +inline +bool Buffer::ReplaceData_joint(const size_t pBufferData_Offset, const size_t pBufferData_Count, const uint8_t* pReplace_Data, const size_t pReplace_Count) { -const size_t new_data_size = byteLength + pReplace_Count - pBufferData_Count; + if((pBufferData_Count == 0) || (pReplace_Count == 0) || (pReplace_Data == nullptr)) { + return false; + } -uint8_t* new_data; - - if((pBufferData_Count == 0) || (pReplace_Count == 0) || (pReplace_Data == nullptr)) return false; - - new_data = new uint8_t[new_data_size]; + const size_t new_data_size = byteLength + pReplace_Count - pBufferData_Count; + uint8_t* new_data = new uint8_t[new_data_size]; // Copy data which place before replacing part. memcpy(new_data, mData.get(), pBufferData_Offset); // Copy new data.