Browse Source

stop passing around data_dir everywhere

testing
cinnaboot 6 years ago
parent
commit
584fd98da1
  1. 4
      examples/assimp_loading/main.cpp
  2. 2
      include/entity.h
  3. 4
      include/mesh.h
  4. 2
      include/util.h
  5. 2
      include/util_image.h
  6. 20
      src/entity.cpp
  7. 16
      src/mesh.cpp
  8. 15
      src/util.cpp
  9. 10
      src/util_image.cpp

4
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";

2
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);

4
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);

2
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);

2
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);

20
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;
}

16
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;
}
}

15
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

10
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;

Loading…
Cancel
Save