From 14424312813b55c62e44ef5c2152e27301733497 Mon Sep 17 00:00:00 2001 From: Omniscient Date: Tue, 5 Mar 2024 23:03:20 +1100 Subject: pass mesh and material darrays for happy path. we will probably swap to an arena allocator for the temp darrays soon but not right yet --- src/renderer/render.c | 1 + src/resources/loaders.h | 1 + src/resources/obj.c | 38 +++++++++++++++++++++----------------- src/std/containers/darray.h | 14 +++++++++++--- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/renderer/render.c b/src/renderer/render.c index f12954f..a0cc559 100644 --- a/src/renderer/render.c +++ b/src/renderer/render.c @@ -96,6 +96,7 @@ texture texture_data_load(const char* path, bool invert_y) { } void texture_data_upload(texture* tex) { + printf("Texture name %s\n", tex->name); TRACE("Upload texture data"); u32 texture_id; glGenTextures(1, &texture_id); diff --git a/src/resources/loaders.h b/src/resources/loaders.h index 8904655..858e4d1 100644 --- a/src/resources/loaders.h +++ b/src/resources/loaders.h @@ -1,6 +1,7 @@ #pragma once #include "defines.h" +#include "render_types.h" struct core; diff --git a/src/resources/obj.c b/src/resources/obj.c index fca6b0e..5fbfcdd 100644 --- a/src/resources/obj.c +++ b/src/resources/obj.c @@ -39,12 +39,16 @@ bool model_load_obj_str(const char *file_string, model *out_model, bool invert_t model_handle model_load_obj(core *core, const char *path, bool invert_textures_y) { TRACE("Loading model at Path %s\n", path); const char *file_string = string_from_file(path); - model model; + + model model = { 0 }; + model.meshes = mesh_darray_new(1); + model.materials = material_darray_new(1); bool success = model_load_obj_str(file_string, &model, invert_textures_y); if (!success) { - exit(-1); // FIXME: Return some sort of result type? + FATAL("Couldnt load OBJ file at path %s", path); + ERROR_EXIT("Load fails are considered crash-worthy right now. This will change later.\n"); } u32 index = model_darray_len(core->models); @@ -61,9 +65,6 @@ bool model_load_obj_str(const char *file_string, model *out_model, bool invert_t vec2_darray *tmp_uvs = vec2_darray_new(1000); face_darray *tmp_faces = face_darray_new(1000); - mesh_darray *meshes = mesh_darray_new(1); - material_darray *materials = material_darray_new(1); - // Other state bool object_set = false; bool material_loaded = false; @@ -101,16 +102,16 @@ bool model_load_obj_str(const char *file_string, model *out_model, bool invert_t if (!object_set) { object_set = true; } else { - create_submesh(meshes, tmp_positions, tmp_normals, tmp_uvs, tmp_faces, materials, - material_loaded, current_material_name); + create_submesh(out_model->meshes, tmp_positions, tmp_normals, tmp_uvs, tmp_faces, + out_model->materials, material_loaded, current_material_name); object_set = false; } } else if (strcmp(line_header, "v") == 0) { // special logic: if we went from faces back to vertices trigger a mesh output. // PS: I hate OBJ if (last_char_type == 'f') { - create_submesh(meshes, tmp_positions, tmp_normals, tmp_uvs, tmp_faces, materials, - material_loaded, current_material_name); + create_submesh(out_model->meshes, tmp_positions, tmp_normals, tmp_uvs, tmp_faces, + out_model->materials, material_loaded, current_material_name); object_set = false; } @@ -163,8 +164,9 @@ bool model_load_obj_str(const char *file_string, model *out_model, bool invert_t } else if (strcmp(line_header, "mtllib") == 0) { char path[1024] = "assets/"; sscanf(pch + offset, "%s", path + 7); - if (!load_material_lib(path, materials)) { + if (!load_material_lib(path, out_model->materials)) { ERROR("couldnt load material lib"); + return false; } } else if (strcmp(line_header, "usemtl") == 0) { material_loaded = true; @@ -178,8 +180,8 @@ bool model_load_obj_str(const char *file_string, model *out_model, bool invert_t // last mesh or if one wasnt created with 'o' directive if (face_darray_len(tmp_faces) > 0) { TRACE("Last leftover mesh"); - create_submesh(meshes, tmp_positions, tmp_normals, tmp_uvs, tmp_faces, materials, - material_loaded, current_material_name); + create_submesh(out_model->meshes, tmp_positions, tmp_normals, tmp_uvs, tmp_faces, + out_model->materials, material_loaded, current_material_name); } // Free data @@ -190,16 +192,16 @@ bool model_load_obj_str(const char *file_string, model *out_model, bool invert_t face_darray_free(tmp_faces); TRACE("Freed temporary OBJ loading data"); - memset(out_model, 0, sizeof(model)); - if (mesh_darray_len(meshes) > 256) { - printf("num meshes: %ld\n", mesh_darray_len(meshes)); + /* memset(out_model, 0, sizeof(model)); */ + if (mesh_darray_len(out_model->meshes) > 256) { + printf("num meshes: %ld\n", mesh_darray_len(out_model->meshes)); } - out_model->meshes = meshes; + /* out_model->meshes = meshes; */ // TODO: bounding box calculation for each mesh // TODO: bounding box calculation for model - out_model->materials = materials; + /* out_model->materials = materials; */ return true; } @@ -347,6 +349,8 @@ bool load_material_lib(const char *path, material_darray *materials) { pch = strtok_r(NULL, "\n", &saveptr); } + TRACE("end load material lib"); + // last mesh or if one wasnt created with 'o' directive // TRACE("Last leftover material"); material_darray_push(materials, current_material); diff --git a/src/std/containers/darray.h b/src/std/containers/darray.h index 729b4cf..85e8048 100644 --- a/src/std/containers/darray.h +++ b/src/std/containers/darray.h @@ -33,14 +33,22 @@ #define PREFIX static + /* if (arena != NULL) {\ */ + /* d = arena_alloc(arena, sizeof(T##_darray));\ */ + /* data = arena_alloc(arena, starting_capacity * sizeof(T));\ */ + /* } else {\ */ + /* }\ */ + #define KITC_DECL_TYPED_ARRAY(T) \ typedef typed_array(T) T##_darray; \ typedef typed_array_iterator(T) T##_darray_iter; \ \ /* Create a new one growable array */ \ - PREFIX T##_darray *T##_darray_new(size_t starting_capacity) { \ - T##_darray *d = malloc(sizeof(T##_darray)); \ - T *data = malloc(starting_capacity * sizeof(T)); \ + PREFIX T##_darray *T##_darray_new(size_t starting_capacity) { \ + T##_darray *d;\ + T *data ; \ + d = malloc(sizeof(T##_darray)); \ + data = malloc(starting_capacity * sizeof(T));\ \ d->len = 0; \ d->capacity = starting_capacity; \ -- cgit v1.2.3-70-g09d2