From 3c75a574b998aeb32d977cf95405275b36cd6ede Mon Sep 17 00:00:00 2001 From: Trevor Hollmann <thollmann@uni-koblenz.de> Date: Sun, 18 Jul 2021 08:16:40 +0200 Subject: [PATCH] [#79] Create new function specifically for texture loading. --- .../src/vkcv/asset/asset_loader.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/modules/asset_loader/src/vkcv/asset/asset_loader.cpp b/modules/asset_loader/src/vkcv/asset/asset_loader.cpp index e965edfb..a948de07 100644 --- a/modules/asset_loader/src/vkcv/asset/asset_loader.cpp +++ b/modules/asset_loader/src/vkcv/asset/asset_loader.cpp @@ -76,6 +76,33 @@ enum IndexType getIndexType(const enum fx::gltf::Accessor::ComponentType &type) } } + +int loadTexture(const Scene &scene, Texture &tex) +{ + if (tex.uri < 0 || tex.uri >= scene.uris.size()) { + vkcv_log(LogLevel::ERROR, "URI index out of range: %d", tex.uri); + } + const std::string &uri = scene.uris[tex.uri]; + int w, h, ch; + uint8_t *data = NULL; + if (!(data = stbi_load(uri.c_str(), &w, &h, &ch, 4))) { + vkcv_log(LogLevel::ERROR, "Failed to load image data from %s", + uri.c_str()); + tex.w = tex.h = tex.channels = 0; + return ASSET_ERROR; + } + tex.channels = 4; + tex.w = static_cast<uint16_t>(w); + tex.h = static_cast<uint16_t>(h); + + tex.data.resize(tex.w * tex.h * tex.channels); + memcpy(tex.data.data(), data, tex.data.size()); + free(data); + + return ASSET_SUCCESS; +} + + /** * This function loads all the textures of a Scene described by the texture- * and image- array of an fx::gltf::Document. -- GitLab