From 673885a6d330787cc208aaf0ce81176961b89a62 Mon Sep 17 00:00:00 2001 From: Alexandre Avenel Date: Fri, 21 Sep 2018 20:04:53 +0200 Subject: [PATCH 1/2] Add unit test for gltf2 export to obj --- test/unit/utglTF2ImportExport.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/unit/utglTF2ImportExport.cpp b/test/unit/utglTF2ImportExport.cpp index 9eb3ef5bd..ebf97a5eb 100644 --- a/test/unit/utglTF2ImportExport.cpp +++ b/test/unit/utglTF2ImportExport.cpp @@ -101,6 +101,24 @@ TEST_F( utglTF2ImportExport, importBinaryglTF2FromFileTest ) { EXPECT_TRUE( binaryImporterTest() ); } +#ifndef ASSIMP_BUILD_NO_EXPORT +TEST_F(utglTF2ImportExport, importglTF2AndExportToOBJ) { + Assimp::Importer importer; + Assimp::Exporter exporter; + const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured.gltf", aiProcess_ValidateDataStructure); + EXPECT_NE(nullptr, scene); + EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "obj", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF/BoxTextured_out.obj")); +} + +TEST_F(utglTF2ImportExport, importglTF2EmbeddedAndExportToOBJ) { + Assimp::Importer importer; + Assimp::Exporter exporter; + const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-Embedded/BoxTextured.gltf", aiProcess_ValidateDataStructure); + EXPECT_NE(nullptr, scene); + EXPECT_EQ(aiReturn_SUCCESS, exporter.Export(scene, "obj", ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTextured-glTF-Embedded/BoxTextured_out.obj")); +} +#endif // ASSIMP_BUILD_NO_EXPORT + TEST_F(utglTF2ImportExport, importglTF2PrimitiveModePointsWithoutIndices) { Assimp::Importer importer; //Points without indices From 504e5fd5c556e450c0ea09365ca28952dffa12c9 Mon Sep 17 00:00:00 2001 From: Alexandre Avenel Date: Fri, 21 Sep 2018 21:24:53 +0200 Subject: [PATCH 2/2] Use unique_ptr for gltf2 textures --- code/glTF2Asset.h | 4 ++-- code/glTF2Asset.inl | 20 ++++++++++---------- code/glTF2Importer.cpp | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/code/glTF2Asset.h b/code/glTF2Asset.h index b4545baa2..d1a70ae0a 100644 --- a/code/glTF2Asset.h +++ b/code/glTF2Asset.h @@ -659,7 +659,7 @@ namespace glTF2 int width, height; private: - uint8_t* mData; + std::unique_ptr mData; size_t mDataLength; public: @@ -674,7 +674,7 @@ namespace glTF2 { return mDataLength; } inline const uint8_t* GetData() const - { return mData; } + { return mData.get(); } inline uint8_t* StealData(); diff --git a/code/glTF2Asset.inl b/code/glTF2Asset.inl index 5a87715ce..96348d4f2 100644 --- a/code/glTF2Asset.inl +++ b/code/glTF2Asset.inl @@ -688,7 +688,6 @@ T Accessor::Indexer::GetValue(int i) inline Image::Image() : width(0) , height(0) - , mData(0) , mDataLength(0) { @@ -704,7 +703,9 @@ inline void Image::Read(Value& obj, Asset& r) if (ParseDataURI(uristr, uri->GetStringLength(), dataURI)) { mimeType = dataURI.mediaType; if (dataURI.base64) { - mDataLength = Util::DecodeBase64(dataURI.data, dataURI.dataLength, mData); + uint8_t *ptr = nullptr; + mDataLength = Util::DecodeBase64(dataURI.data, dataURI.dataLength, ptr); + mData.reset(ptr); } } else { @@ -717,8 +718,9 @@ inline void Image::Read(Value& obj, Asset& r) this->mDataLength = this->bufferView->byteLength; // maybe this memcpy could be avoided if aiTexture does not delete[] pcData at destruction. - this->mData = new uint8_t [this->mDataLength]; - memcpy(this->mData, buffer->GetPointer() + this->bufferView->byteOffset, this->mDataLength); + + this->mData.reset(new uint8_t[this->mDataLength]); + memcpy(this->mData.get(), buffer->GetPointer() + this->bufferView->byteOffset, this->mDataLength); if (Value* mtype = FindString(obj, "mimeType")) { this->mimeType = mtype->GetString(); @@ -729,10 +731,8 @@ inline void Image::Read(Value& obj, Asset& r) inline uint8_t* Image::StealData() { - uint8_t* data = mData; - mDataLength = 0; - mData = 0; - return data; + mDataLength = 0; + return mData.release(); } inline void Image::SetData(uint8_t* data, size_t length, Asset& r) @@ -747,8 +747,8 @@ inline void Image::SetData(uint8_t* data, size_t length, Asset& r) bufferView->byteOffset = b->AppendData(data, length); } else { // text file: will be stored as a data uri - this->mData = data; - this->mDataLength = length; + this->mData.reset(data); + this->mDataLength = length; } } diff --git a/code/glTF2Importer.cpp b/code/glTF2Importer.cpp index 8581d25fe..740935601 100755 --- a/code/glTF2Importer.cpp +++ b/code/glTF2Importer.cpp @@ -818,7 +818,7 @@ void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset& r) // Add the embedded textures for (size_t i = 0; i < r.images.Size(); ++i) { - Image img = r.images[i]; + Image &img = r.images[i]; if (!img.HasData()) continue; int idx = mScene->mNumTextures++;