From fbeb8dbaa54fd38624c3260fd4780bfc3ed4ffb0 Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Fri, 23 Oct 2020 15:32:20 -0400 Subject: [PATCH] add path for binary texture data in util_image --- include/util_image.h | 4 +++- src/mesh.cpp | 16 +++++++++------- src/util_image.cpp | 43 ++++++++++++++++++++++++++++++------------- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/include/util_image.h b/include/util_image.h index 15b9b2d..6bb7e83 100644 --- a/include/util_image.h +++ b/include/util_image.h @@ -24,7 +24,9 @@ struct util_RGBA real32 A; }; -util_image utilLoadImage(const char* full_path); +util_image utilLoadImagePath(const char* full_path); + +util_image utilLoadImageBytes(const unsigned char* bytes, uint length); void utilFreeImage(util_image image); diff --git a/src/mesh.cpp b/src/mesh.cpp index 14d6fb8..6cf565f 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -152,18 +152,20 @@ loadDiffuseTexture(const aiScene* scene, aiMesh* mesh, meMeshInfo* mi) LOG(Error) << "No diffuse texture from assimp\n"; return false; } else { + mi->use_texture = true; const aiTexture* tex = scene->GetEmbeddedTexture(file_name.C_Str()); + if (tex != nullptr) { LOG(Info) << "has embedded texture\n"; - - // TODO: load binary texture data with stb_image - LOG(Info) << "format hint: " << tex->achFormatHint << "\n"; - - + mi->diffuse_texture = utilLoadImageBytes((const uint8*) tex->pcData, tex->mWidth); } else { LOG(Info) << "Loading texture file: " << file_name.C_Str() << "\n"; - mi->diffuse_texture = utilLoadImage(file_name.C_Str()); - mi->use_texture = true; + mi->diffuse_texture = utilLoadImagePath(file_name.C_Str()); + } + + if (mi->diffuse_texture.pixels == nullptr) { + LOG(Error) << "Error loading texture\n"; + return false; } } diff --git a/src/util_image.cpp b/src/util_image.cpp index d8d8e52..64c2e57 100644 --- a/src/util_image.cpp +++ b/src/util_image.cpp @@ -8,14 +8,42 @@ #include "util_image.h" +util_image parseSTBResult(util_image img); + + util_image -utilLoadImage(const char* full_path) +utilLoadImagePath(const char* full_path) { LOG(Info) << "Loading Image: " << full_path << "\n"; util_image image; stbi_set_flip_vertically_on_load(1); - image.pixels = stbi_load(full_path, &image.w, &image.h, &image.num_channels, 0); + return parseSTBResult(image); +} + +util_image +utilLoadImageBytes(const uint8* bytes, uint length) +{ + LOG(Info) << "Loading binary image data\n"; + util_image image; + stbi_set_flip_vertically_on_load(1); + image.pixels = stbi_load_from_memory(bytes, length, &image.w, &image.h, &image.num_channels, 0); + return parseSTBResult(image); +} + +void +utilFreeImage(util_image image) +{ + image.w = image.h = image.data_len = 0; + image.bits_per_channel = 8; + image.num_channels = 4; + std::memset(image.file_path, 0, std::strlen(image.file_path) + 1); + utilSafeFree(image.pixels); +} + +util_image +parseSTBResult(util_image image) +{ image.bits_per_channel = 8; image.data_len = image.w * image.h * image.num_channels; // NOTE: assumes 8 bits per channel @@ -30,14 +58,3 @@ utilLoadImage(const char* full_path) return image; } - -void -utilFreeImage(util_image image) -{ - image.w = image.h = image.data_len = 0; - image.bits_per_channel = 8; - image.num_channels = 4; - std::memset(image.file_path, 0, std::strlen(image.file_path) + 1); - utilSafeFree(image.pixels); -} -