diff --git a/src/mesh.cpp b/src/mesh.cpp index 8f4a928..9225cc3 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -1,5 +1,4 @@ #include -#include #include #include @@ -158,12 +157,10 @@ copyMeshInfo(const char* data_dir, const aiScene* scene, aiMesh* mesh) if (has_tex && mat->GetTextureCount(aiTextureType_DIFFUSE) > 0) { aiString file_name; if (AI_SUCCESS != mat->GetTexture(aiTextureType_DIFFUSE, 0, &file_name, NULL, NULL, NULL, NULL, NULL)) { - LOG(ERROR) << "Some Assimp-type-error\n"; + LOG(ERROR) << "No diffuse texture from assimp\n"; } else { - std::string file_path; - file_path.append(data_dir).append("/").append(file_name.C_Str()); LOG(INFO) << "Loading texture file: " << file_name.C_Str() << "\n"; - mi->diffuse_texture = utilLoadImage(file_path.c_str()); + mi->diffuse_texture = utilLoadImage(data_dir, file_name.C_Str()); mi->use_texture = true; } } diff --git a/src/util.cpp b/src/util.cpp index 8f8c773..4631620 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -13,6 +13,18 @@ const uint MAX_FILESIZE = 2 * 1024 * 1024; // 2MB const uint MAX_STRING_LENGTH = 1024; + +const char* +utilBaseName(const char* path_str) +{ + assert(std::strlen(path_str) < MAX_STRING_LENGTH); + const char* output = std::strrchr(path_str, '/'); + if (output) + return output; + else + return path_str; +} + // TODO: don't use ftell() to get filesize // https://wiki.sei.cmu.edu/confluence/display/c/FIO19-C.+Do+not+use+fseek%28%29+and+ftell%28%29+to+compute+the+size+of+a+regular+file char * @@ -64,15 +76,21 @@ void utilSafeFree(const void* mem) } util_image -utilLoadImage(const char* filename) +utilLoadImage(const char* base_dir, const char* filename) { - LOG(INFO) << "Loading Image: " << filename << "\n"; - assert(std::strlen(filename) < 256); + const uint max_path = 256; // NOTE: util_image.file_path is char[256] + const char* base_name = utilBaseName(filename); + uint l = std::strlen(base_dir) + std::strlen(base_name) + 1; // NOTE: +1 for '/' char + assert(l < max_path); + char full_path[l]; + std::memcpy(full_path, base_dir, std::strlen(base_dir) + 1); + std::strcat(std::strcat(full_path, "/"), base_name); + LOG(INFO) << "Loading Image: " << full_path << "\n"; util_image image; - std::memcpy(image.file_path, filename, std::strlen(filename) + 1); + std::memcpy(image.file_path, full_path, std::strlen(full_path) + 1); stbi_set_flip_vertically_on_load(1); - image.pixels = stbi_load(filename, &image.w, &image.h, &image.num_channels, 0); + image.pixels = stbi_load(full_path, &image.w, &image.h, &image.num_channels, 0); image.data_len = image.w * image.h * image.num_channels; // NOTE: assumes 8 bits per channel if (image.pixels == 0) { diff --git a/src/util.h b/src/util.h index eadd418..f4338b0 100644 --- a/src/util.h +++ b/src/util.h @@ -86,13 +86,15 @@ utilConvertColor(GLfloat buf[3], uint32 color) buf[2] = (GLfloat) ((color >> 8) & 0xFF) / (GLfloat) 255; } +const char* utilBaseName(const char* path_str); + // NOTE: Wrapper for calloc that will send error message on out of memory #define UTIL_ALLOC(len, type) (type *) utilLogAlloc((len), sizeof(type), __FILE__, __LINE__) -void * utilLogAlloc(uint item_count, uint type_size, const char* file_name, const int line); +void* utilLogAlloc(uint item_count, uint type_size, const char* file_name, const int line); -char * utilDumpTextFile(const char* filename); +char* utilDumpTextFile(const char* filename); -util_image utilLoadImage(const char* filename); +util_image utilLoadImage(const char* base_dir, const char* filename); void utilFreeImage(util_image image);