From 6771cc31e1c39928c30ea880e088b407e15e254d Mon Sep 17 00:00:00 2001 From: sam Date: Thu, 16 Feb 2017 16:03:55 -0800 Subject: [PATCH] Adding the reading path --- libraries/gpu/src/gpu/Texture.h | 2 +- libraries/gpu/src/gpu/Texture_ktx.cpp | 16 ++++++++++-- libraries/model/src/model/TextureMap.cpp | 32 +++++++++++++++++++++--- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/libraries/gpu/src/gpu/Texture.h b/libraries/gpu/src/gpu/Texture.h index fba4c7079f..acf063e365 100755 --- a/libraries/gpu/src/gpu/Texture.h +++ b/libraries/gpu/src/gpu/Texture.h @@ -488,7 +488,7 @@ public: ExternalUpdates getUpdates() const; static ktx::KTXUniquePointer serialize(const Texture& texture); - static TexturePointer unserialize(const ktx::KTXUniquePointer& srcData); + static Texture* unserialize(const ktx::KTXUniquePointer& srcData); protected: const TextureUsageType _usageType; diff --git a/libraries/gpu/src/gpu/Texture_ktx.cpp b/libraries/gpu/src/gpu/Texture_ktx.cpp index d06454b933..6380b23903 100644 --- a/libraries/gpu/src/gpu/Texture_ktx.cpp +++ b/libraries/gpu/src/gpu/Texture_ktx.cpp @@ -34,9 +34,21 @@ ktx::KTXUniquePointer Texture::serialize(const Texture& texture) { auto ktxBuffer = ktx::KTX::create(header, images); return ktxBuffer; } -TexturePointer Texture::unserialize(const ktx::KTXUniquePointer& srcData) { +Texture* Texture::unserialize(const ktx::KTXUniquePointer& srcData) { + if (!srcData) { + return nullptr; + } const auto& header = *srcData->getHeader(); - return nullptr; + Format pixelFormat = Format::COLOR_RGBA_32; + + auto tex = Texture::create2D(pixelFormat, header.getPixelWidth(), header.getPixelHeight()); + uint16_t level = 0; + for (auto& image : srcData->_images) { + tex->assignStoredMip(level, pixelFormat, image._imageSize, image._bytes); + level++; + } + + return tex; } \ No newline at end of file diff --git a/libraries/model/src/model/TextureMap.cpp b/libraries/model/src/model/TextureMap.cpp index cadd985c22..4f8a43412e 100755 --- a/libraries/model/src/model/TextureMap.cpp +++ b/libraries/model/src/model/TextureMap.cpp @@ -290,12 +290,36 @@ gpu::Texture* TextureUsage::process2DTextureColorFromImage(const QImage& srcImag filename += std::to_string((size_t) theTexture); filename += ".ktx"; - FILE* file = fopen (filename.c_str(),"wb"); - if (file != nullptr) { - fwrite(theKTX->_storage->data(), 1, theKTX->_storage->size(), file); - fclose (file); + { + FILE* file = fopen (filename.c_str(),"wb"); + if (file != nullptr) { + fwrite(theKTX->_storage->data(), 1, theKTX->_storage->size(), file); + fclose (file); + } } + { + FILE* file = fopen (filename.c_str(),"rb"); + if (file != nullptr) { + // obtain file size: + fseek (file , 0 , SEEK_END); + auto size = ftell(file); + rewind(file); + + std::unique_ptr storage(new ktx::Storage(size)); + fread(storage->_bytes, 1, storage->_size, file); + fclose (file); + + //then create a new texture out of the ktx + auto theNewTexure = Texture::unserialize(ktx::KTX::create(storage)); + + if (theNewTexure) { + auto srcTexture = theTexture; + theTexture = theNewTexure; + delete srcTexture; + } + } + } } }