diff --git a/examples/assimp_loading/main.cpp b/examples/assimp_loading/main.cpp index abf58b8..4dc3adc 100644 --- a/examples/assimp_loading/main.cpp +++ b/examples/assimp_loading/main.cpp @@ -59,8 +59,6 @@ main() Entity spaceship; // TODO: this should be handled in entInit - // TODO: utilCopyCStr() can fail, and returns false - utilCopyCStr(spaceship.model_filename, "../data/spaceship.glb", 256); entSetWorldPosition(spaceship, 0, 0, 0); // set entity scale, rotation, world_transform to defaults :( ... @@ -71,7 +69,7 @@ main() glm::vec3 cam_pos(0, -100, 100); cameraInitPerspective(rs->cam, cam_pos, spaceship.translation, rs->cam.world_up); - if (entInit(spaceship, "../data", rs->default_shader)) { + if (entInit(spaceship, "../data/spaceship.glb", rs->default_shader)) { doRenderLoop(rs, doFrameCallback, &spaceship, 1); } else { LOG(Error) << "Error initializing entity, exiting\n"; diff --git a/include/entity.h b/include/entity.h index 3605345..4665102 100644 --- a/include/entity.h +++ b/include/entity.h @@ -32,7 +32,7 @@ entScale(Entity& e, glm::vec3 v) e.world_transform = glm::scale(e.world_transform, v); } -bool entInit(Entity& e, const char* data_dir, rg_shader_program& shader); +bool entInit(Entity& e, const char* model_path, rg_shader_program& shader); void entFree(Entity& e); diff --git a/include/mesh.h b/include/mesh.h index 276eb82..fe0afb6 100644 --- a/include/mesh.h +++ b/include/mesh.h @@ -36,9 +36,7 @@ struct meMeshGroup bool meInitAssimp(); -// NOTE: data_dir gets passed to an internal function, filename is a fullpath, wtfs/min++ -// TODO: maybe pass data_dir in to init function and store as a static variable? -bool meLoadFromFile(meMeshGroup& mesh_group, const char* data_dir, const char* filename); +bool meLoadFromFile(meMeshGroup& mesh_group, const char* filepath); void meFreeMeshGroup(meMeshGroup& mesh_group); diff --git a/include/util.h b/include/util.h index b8323bf..859d20e 100644 --- a/include/util.h +++ b/include/util.h @@ -90,7 +90,7 @@ bool utilCopyCStr(char* dest, const char* src, uint max_len); // NOTE: returns new string with '/' between // NOTE: max_len should be the size of return buffer. -bool utilConcatPath(char* out, const char* base_dir, const char* file_name, uint max_len); +char* utilConcatPath(char* out, const char* base_dir, const char* file_name, uint max_len); const char* utilBaseName(const char* path_str); diff --git a/include/util_image.h b/include/util_image.h index da11604..15b9b2d 100644 --- a/include/util_image.h +++ b/include/util_image.h @@ -24,7 +24,7 @@ struct util_RGBA real32 A; }; -util_image utilLoadImage(const char* base_dir, const char* filename); +util_image utilLoadImage(const char* full_path); void utilFreeImage(util_image image); diff --git a/src/entity.cpp b/src/entity.cpp index ca96a16..f306f28 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -8,24 +8,18 @@ bool initEntityROs(Entity& e); bool convertMeshInfo(meMeshInfo* mesh, render_object* ro, bool use_normals, bool use_texture); bool -entInit(Entity& e, const char* data_dir, rg_shader_program& shader) +entInit(Entity& e, const char* model_path, rg_shader_program& shader) { e.world_transform = glm::mat4(1.0); entScale(e, e.scale); - uint max_len = 256; // TODO: define max_len for property strings - char full_path[max_len]; - if (utilConcatPath(full_path, data_dir, e.model_filename, max_len)) { - if (meLoadFromFile(e.mesh_group, data_dir, full_path)) { - initEntityRG(e, shader, e.mesh_group.use_normals); - entSetWorldPosition(e, e.translation.x, e.translation.y, e.translation.z); - - if (!initEntityROs(e)) - goto cleanup; - } else { + + if (meLoadFromFile(e.mesh_group, model_path)) { + initEntityRG(e, shader, e.mesh_group.use_normals); + entSetWorldPosition(e, e.translation.x, e.translation.y, e.translation.z); + + if (!initEntityROs(e)) goto cleanup; - } } else { - LOG(Error) << data_dir << " + " << e.model_filename << " is too long\n"; goto cleanup; } diff --git a/src/mesh.cpp b/src/mesh.cpp index 2669684..14d6fb8 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -19,7 +19,7 @@ meMeshInfo* allocateMeshInfo(uint num_vertices, uint num_indices, bool has_norma void freeMesh(meMeshInfo* mesh); inline glm::vec3 copyVector(aiVector3D v_in, glm::vec3& v_out); meMeshInfo* copyMeshInfo(const aiScene* scene, aiMesh* mesh); -bool loadDiffuseTexture(const char* data_dir, const aiScene* scene, aiMesh* mesh, meMeshInfo* mi); +bool loadDiffuseTexture(const aiScene* scene, aiMesh* mesh, meMeshInfo* mi); // interface @@ -35,13 +35,13 @@ meInitAssimp() } bool -meLoadFromFile(meMeshGroup& mesh_group, const char* data_dir, const char* filename) +meLoadFromFile(meMeshGroup& mesh_group, const char* filepath) { - LOG(Info) << "Loading file: " << filename << "\n"; - const aiScene* scene = aiImportFile(filename, aiProcessPreset_TargetRealtime_MaxQuality); + LOG(Info) << "Loading file: " << filepath << "\n"; + const aiScene* scene = aiImportFile(filepath, aiProcessPreset_TargetRealtime_MaxQuality); if (!scene) { - LOG(Error) << "Error loading file: " << filename << "\n"; + LOG(Error) << "Error loading file: " << filepath << "\n"; return false; } @@ -59,7 +59,7 @@ meLoadFromFile(meMeshGroup& mesh_group, const char* data_dir, const char* filena meMeshInfo* mi = copyMeshInfo(scene, mesh); if (mesh->HasTextureCoords(0)) { - if (!loadDiffuseTexture(data_dir, scene, mesh, mi)) { + if (!loadDiffuseTexture(scene, mesh, mi)) { LOG(Error) << "Error loading texture, cleaning up import\n"; freeMesh(mi); aiReleaseImport(scene); @@ -138,7 +138,7 @@ copyVector(aiVector3D v_in, glm::vec3& v_out) } bool -loadDiffuseTexture(const char* data_dir, const aiScene* scene, aiMesh* mesh, meMeshInfo* mi) +loadDiffuseTexture(const aiScene* scene, aiMesh* mesh, meMeshInfo* mi) { aiMaterial* mat = scene->mMaterials[mesh->mMaterialIndex]; aiString file_name; @@ -162,7 +162,7 @@ loadDiffuseTexture(const char* data_dir, const aiScene* scene, aiMesh* mesh, meM } else { LOG(Info) << "Loading texture file: " << file_name.C_Str() << "\n"; - mi->diffuse_texture = utilLoadImage(data_dir, file_name.C_Str()); + mi->diffuse_texture = utilLoadImage(file_name.C_Str()); mi->use_texture = true; } } diff --git a/src/util.cpp b/src/util.cpp index 406b434..d40e79a 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -38,22 +38,17 @@ utilCopyCStr(char* dest, const char* src, uint max_len) return true; } -bool +char* utilConcatPath(char* out, const char* base_dir, const char* file_name, uint max_len) { size_t l1 = std::strlen(base_dir); size_t l2 = std::strlen(file_name); size_t padded = l1 + l2 + 2; // NOTE: null term + '/' - if (padded <= MAX_STRING_LENGTH && padded <= max_len) { - int c = std::snprintf(out, padded, "%s/%s", base_dir, file_name); - if (c > 0) - return true; - else - return false; - } - - return false; + assert(padded <= MAX_STRING_LENGTH && padded <= max_len); + int c = std::snprintf(out, padded, "%s/%s", base_dir, file_name); + assert(c > 0); + return out; } bool diff --git a/src/util_image.cpp b/src/util_image.cpp index 0bf4b34..ff33ef5 100644 --- a/src/util_image.cpp +++ b/src/util_image.cpp @@ -9,16 +9,8 @@ util_image -utilLoadImage(const char* base_dir, const char* filename) +utilLoadImage(const char* full_path) { - const uint max_path = 256; // NOTE: util_image.file_path is char[256] - const char* base_name = utilBaseName(filename); - // NOTE: +1 for '/' char, +1 for null - uint l = std::strlen(base_dir) + std::strlen(base_name) + 2; - 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;