diff options
-rw-r--r-- | bindgen/rust/examples/main_loop.rs | 58 | ||||
-rw-r--r-- | examples/game_demo/game_demo.c | 22 | ||||
-rw-r--r-- | src/core/core.c | 4 | ||||
-rw-r--r-- | src/defines.h | 2 | ||||
-rw-r--r-- | src/log.c | 4 | ||||
-rw-r--r-- | src/log.h | 25 | ||||
-rw-r--r-- | src/maths/maths.h | 15 | ||||
-rw-r--r-- | src/resources/gltf.c | 611 | ||||
-rw-r--r-- | src/systems/terrain.c | 2 |
9 files changed, 409 insertions, 334 deletions
diff --git a/bindgen/rust/examples/main_loop.rs b/bindgen/rust/examples/main_loop.rs index a74e11c..8da3e9a 100644 --- a/bindgen/rust/examples/main_loop.rs +++ b/bindgen/rust/examples/main_loop.rs @@ -1,32 +1,44 @@ use celeritas::*; unsafe fn run_game() { - // init - Core_Bringup(); - - // let mut cube_geo = Geo_CreateCuboid(Vec3 { x: 1.0, y: 1.0, z: 1.0 }); - // let cube = Mesh_Create(&mut cube_geo, false); - - let camera_pos = Vec3 { x: 0.0, y: 2.0, z: -3.0 }; - let pos_y = Vec3 { x: 0., y: 1.0, z: 0. }; - let camera = Camera_Create(camera_pos, vec3_normalise(vec3_negate(camera_pos)), pos_y, 45.0); - SetCamera(camera); - // let camera = Camera_Create(camera_pos, vec3_normalise(vec3_negate(camera_pos)), VEC3_Y, 45.0); - // SetCamera(cam); // update the camera in RenderScene - - let whatever = Vec3 { x: 1.0, y: 1.0, z: 1.0 }; - let sun = DirectionalLight { direction: whatever, ambient: whatever, diffuse: whatever, specular: whatever }; - SetMainLight(sun); - - // Skybox skybox = Skybox_Create(faces, 6); - let skybox = Skybox_Create(face_paths, 6); - + // init + Core_Bringup(); + + let camera_pos = Vec3 { + x: 0.0, + y: 2.0, + z: -3.0, + }; + let camera = Camera_Create( + camera_pos, + vec3_normalise(vec3_negate(camera_pos)), + VEC3_Y, + 45.0, + ); + SetCamera(camera); + + let whatever = Vec3 { + x: 1.0, + y: 1.0, + z: 1.0, + }; + let sun = DirectionalLight { + direction: whatever, + ambient: whatever, + diffuse: whatever, + specular: whatever, + }; + SetMainLight(sun); + + let skybox = Skybox_Create(face_paths, 6); + + // main loop while !ShouldExit() { - Frame_Begin(); - + Frame_Begin(); + Skybox_Draw(&mut skybox, camera); - Frame_End(); + Frame_End(); } } diff --git a/examples/game_demo/game_demo.c b/examples/game_demo/game_demo.c index 7f3496a..9688808 100644 --- a/examples/game_demo/game_demo.c +++ b/examples/game_demo/game_demo.c @@ -8,6 +8,7 @@ #include "core.h" #include "input.h" #include "keys.h" +#include "loaders.h" #include "maths.h" #include "primitives.h" #include "ral_types.h" @@ -32,8 +33,8 @@ int main() { // TODO: Move camera with model // --- Render Scene - Vec3 camera_pos = vec3(-8.0, 10.0, 0.); - Camera cam = Camera_Create(camera_pos, vec3_normalise(vec3_negate(camera_pos)), VEC3_Y, 45.0); + Vec3 camera_pos = vec3(0.0, 1.3, 3.0); + Camera cam = Camera_Create(camera_pos, VEC3_NEG_Z, VEC3_Y, 45.0); SetCamera(cam); // update the camera in RenderScene DirectionalLight sun = { @@ -72,11 +73,19 @@ int main() { .pbr_normal_map = normal_map, .pbr_ao_map = ao_map }; + // ModelHandle cube_handle = ModelLoad_gltf("assets/models/gltf/Cube/glTF/Cube.gltf", false); + ModelHandle cube_handle = ModelLoad_gltf("../../assets/prototyper/prototyper_m.gltf", false); + Model* Cube = MODEL_GET(cube_handle); + RenderEnt cube_r = { .mesh = &Cube->meshes->data[0], + .material = &Cube->materials->data[0], + .affine = mat4_ident(), + .casts_shadows = true }; + RenderEnt crate_renderable = { .mesh = &crate_mesh, .material = &crate_mat, .affine = mat4_scale(3.0), .casts_shadows = true }; - RenderEnt entities[] = { crate_renderable }; + RenderEnt entities[] = { cube_r, crate_renderable }; size_t entity_count = 1; // --- Transforms @@ -84,10 +93,6 @@ int main() { // TODO: parent camera to model - to start with I can just manually update it every frame // TODO: query joints of the gltf to get the hand bone to parent a sword to - // RenderEnt player_r = { .model = player_model, .affine = mat4_ident(), .casts_shadows = true }; - - // RenderEnt entities[] = { player_r }; - bool draw_debug = true; while (!ShouldExit()) { @@ -103,7 +108,6 @@ int main() { Shadow_Run(entities, entity_count); - /* if (draw_debug) { // draw the player model with shadows Render_RenderEntities(entities, entity_count); @@ -112,7 +116,7 @@ int main() { } else { Shadow_DrawDebugQuad(); } - */ + Terrain_Draw(terrain); // END Draw calls diff --git a/src/core/core.c b/src/core/core.c index fcb03a1..38777ba 100644 --- a/src/core/core.c +++ b/src/core/core.c @@ -45,8 +45,8 @@ void Core_Bringup() { arena model_arena = arena_create(malloc(model_data_max), model_data_max); Model_pool model_pool = Model_pool_create(&model_arena, 256, sizeof(Model)); - // g_core.models = model_pool; - // INFO("Created model pool allocator"); + g_core.models = model_pool; + INFO("Created model pool allocator"); // INFO("Creating default scene"); // scene_init(&g_core.default_scene); diff --git a/src/defines.h b/src/defines.h index 19ffa98..b147327 100644 --- a/src/defines.h +++ b/src/defines.h @@ -78,4 +78,4 @@ CORE_DEFINE_HANDLE( // #define CEL_REND_BACKEND_METAL 1 #define CEL_REND_BACKEND_OPENGL 1 // #define CEL_REND_BACKEND_VULKAN 1 -#endif +#endif
\ No newline at end of file @@ -50,7 +50,7 @@ void log_output(log_level level, const char *message, ...) { } void report_assertion_failure(const char *expression, const char *message, const char *file, - i32 line) { + int line) { log_output(LOG_LEVEL_FATAL, "Assertion failure: %s, message: '%s', in file: %s, on line %d\n", expression, message, file, line); -}
\ No newline at end of file +} @@ -53,4 +53,27 @@ void log_output(log_level level, const char* message, ...); #define TRACE(message, ...) log_output(LOG_LEVEL_TRACE, message, ##__VA_ARGS__) #else #define TRACE(message, ...) -#endif
\ No newline at end of file +#endif + +// TODO: Move this to an asserts file + +void report_assertion_failure(const char *expression, const char *message, const char *file, + int line); + +#define CASSERT(expr) \ + { \ + if (expr) { \ + } else { \ + report_assertion_failure(#expr, "", __FILE__, __LINE__); \ + __builtin_trap(); \ + } \ + } + +#define CASSERT_MSG(expr, msg) \ + { \ + if (expr) { \ + } else { \ + report_assertion_failure(#expr, msg, __FILE__, __LINE__); \ + __builtin_trap(); \ + } \ + }
\ No newline at end of file diff --git a/src/maths/maths.h b/src/maths/maths.h index ec6e90a..59e304c 100644 --- a/src/maths/maths.h +++ b/src/maths/maths.h @@ -42,13 +42,14 @@ c_static_inline Vec3 vec3_cross(Vec3 a, Vec3 b) { Vec3){ .x = a.y * b.z - a.z * b.y, .y = a.z * b.x - a.x * b.z, .z = a.x * b.y - a.y * b.x }; } -#define VEC3_ZERO ((Vec3){ .x = 0.0, .y = 0.0, .z = 0.0 }) -#define VEC3_X ((Vec3){ .x = 1.0, .y = 0.0, .z = 0.0 }) -#define VEC3_NEG_X ((Vec3){ .x = -1.0, .y = 0.0, .z = 0.0 }) -#define VEC3_Y ((Vec3){ .x = 0.0, .y = 1.0, .z = 0.0 }) -#define VEC3_NEG_Y ((Vec3){ .x = 0.0, .y = -1.0, .z = 0.0 }) -#define VEC3_Z ((Vec3){ .x = 0.0, .y = 0.0, .z = 1.0 }) -#define VEC3_NEG_Z ((Vec3){ .x = 0.0, .y = 0.0, .z = -1.0 }) +static const Vec3 VEC3_X = vec3(1.0, 0.0, 0.0); +static const Vec3 VEC3_NEG_X = vec3(-1.0, 0.0, 0.0); +static const Vec3 VEC3_Y = vec3(0.0, 1.0, 0.0); +static const Vec3 VEC3_NEG_Y = vec3(0.0, -1.0, 0.0); +static const Vec3 VEC3_Z = vec3(0.0, 0.0, 1.0); +static const Vec3 VEC3_NEG_Z = vec3(0.0, 0.0, -1.0); +static const Vec3 VEC3_ZERO = vec3(0.0, 0.0, 0.0); +static const Vec3 VEC3_ONES = vec3(1.0, 1.0, 1.0); c_static_inline void print_vec3(Vec3 v) { printf("{ x: %f, y: %f, z: %f )\n", (f64)v.x, (f64)v.y, (f64)v.z); diff --git a/src/resources/gltf.c b/src/resources/gltf.c index 56d5660..eb2647d 100644 --- a/src/resources/gltf.c +++ b/src/resources/gltf.c @@ -14,7 +14,6 @@ #include "path.h" #include "ral_types.h" #include "render.h" -// #include "render_backend.h" #include "render_types.h" #include "str.h" @@ -23,6 +22,9 @@ extern Core g_core; +/* GLTF Loading Pipeline + ===================== */ + struct face { cgltf_uint indices[3]; }; @@ -38,8 +40,8 @@ KITC_DECL_TYPED_ARRAY(face) bool model_load_gltf_str(const char *file_string, const char *filepath, Str8 relative_path, Model *out_model, bool invert_textures_y); -ModelHandle model_load_gltf(const char *path, bool invert_texture_y) { - size_t arena_size = 1024; +ModelHandle ModelLoad_gltf(const char *path, bool invert_texture_y) { + size_t arena_size = MB(1); arena scratch = arena_create(malloc(arena_size), arena_size); TRACE("Loading model at Path %s\n", path); @@ -55,16 +57,16 @@ ModelHandle model_load_gltf(const char *path, bool invert_texture_y) { model->meshes = Mesh_darray_new(1); model->materials = Material_darray_new(1); - // bool success = - // model_load_gltf_str(file_string, path, relative_path.path, model, invert_texture_y); + bool success = + model_load_gltf_str(file_string, path, relative_path.path, model, invert_texture_y); - // if (!success) { - // FATAL("Couldnt load GLTF file at path %s", path); - // ERROR_EXIT("Load fails are considered crash-worthy right now. This will change later.\n"); - // } + if (!success) { + FATAL("Couldnt load GLTF file at path %s", path); + ERROR_EXIT("Load fails are considered crash-worthy right now. This will change later.\n"); + } - // arena_free_all(&scratch); - // arena_free_storage(&scratch); + arena_free_all(&scratch); + arena_free_storage(&scratch); return handle; } @@ -78,38 +80,72 @@ void assert_path_type_matches_component_type(cgltf_animation_path_type target_pa // TODO: Brainstorm how I can make this simpler and break it up into more testable pieces -/* -typedef struct model { - str8 name; - mesh* meshes; - u32 mesh_count; -} model; -*/ +void load_position_components(Vec3_darray *positions, cgltf_accessor *accessor) { + TRACE("Loading %d vec3 position components", accessor->count); + CASSERT_MSG(accessor->component_type == cgltf_component_type_r_32f, + "Positions components are floats"); + CASSERT_MSG(accessor->type == cgltf_type_vec3, "Vertex positions should be a vec3"); + + for (cgltf_size v = 0; v < accessor->count; ++v) { + Vec3 pos; + cgltf_accessor_read_float(accessor, v, &pos.x, 3); + Vec3_darray_push(positions, pos); + } +} + +void load_normal_components(Vec3_darray *normals, cgltf_accessor *accessor) { + TRACE("Loading %d vec3 normal components", accessor->count); + CASSERT_MSG(accessor->component_type == cgltf_component_type_r_32f, + "Normal vector components are floats"); + CASSERT_MSG(accessor->type == cgltf_type_vec3, "Vertex normals should be a vec3"); + + for (cgltf_size v = 0; v < accessor->count; ++v) { + Vec3 pos; + cgltf_accessor_read_float(accessor, v, &pos.x, 3); + Vec3_darray_push(normals, pos); + } +} + +void load_texcoord_components(Vec2_darray *texcoords, cgltf_accessor *accessor) { + TRACE("Load texture coordinates from accessor"); + CASSERT(accessor->component_type == cgltf_component_type_r_32f); + CASSERT_MSG(accessor->type == cgltf_type_vec2, "Texture coordinates should be a vec2"); + + for (cgltf_size v = 0; v < accessor->count; ++v) { + Vec2 tex; + bool success = cgltf_accessor_read_float(accessor, v, &tex.x, 2); + if (!success) { + ERROR("Error loading tex coord"); + } + Vec2_darray_push(texcoords, tex); + } +} + bool model_load_gltf_str(const char *file_string, const char *filepath, Str8 relative_path, Model *out_model, bool invert_textures_y) { - // TRACE("Load GLTF from string"); - - // // Setup temps - // vec3_darray *tmp_positions = vec3_darray_new(1000); - // vec3_darray *tmp_normals = vec3_darray_new(1000); - // vec2_darray *tmp_uvs = vec2_darray_new(1000); - // vec4u_darray *tmp_joint_indices = vec4u_darray_new(1000); - // vec4_darray *tmp_weights = vec4_darray_new(1000); - // // FIXME - // // joint_darray *tmp_joints = joint_darray_new(256); - // // vertex_bone_data_darray *tmp_vertex_bone_data = vertex_bone_data_darray_new(1000); - - // cgltf_options options = { 0 }; + TRACE("Load GLTF from string"); + + // Setup temps + Vec3_darray *tmp_positions = Vec3_darray_new(1000); + Vec3_darray *tmp_normals = Vec3_darray_new(1000); + Vec2_darray *tmp_uvs = Vec2_darray_new(1000); + Vec4u_darray *tmp_joint_indices = Vec4u_darray_new(1000); + Vec4_darray *tmp_weights = Vec4_darray_new(1000); + // FIXME + // joint_darray *tmp_joints = joint_darray_new(256); + // vertex_bone_data_darray *tmp_vertex_bone_data = vertex_bone_data_darray_new(1000); + + cgltf_options options = { 0 }; cgltf_data *data = NULL; - // cgltf_result result = cgltf_parse_file(&options, filepath, &data); - // if (result != cgltf_result_success) { - // WARN("gltf load failed"); - // // TODO: cleanup arrays(allocate all from arena ?) - // return false; - // } + cgltf_result result = cgltf_parse_file(&options, filepath, &data); + if (result != cgltf_result_success) { + WARN("gltf load failed"); + // TODO: cleanup arrays(allocate all from arena ?) + return false; + } - // cgltf_load_buffers(&options, data, filepath); - // DEBUG("loaded buffers"); + cgltf_load_buffers(&options, data, filepath); + DEBUG("loaded buffers"); // // --- Skin // size_t num_skins = data->skins_count; @@ -170,85 +206,72 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, Str8 rel // we will use base color texture like blinn phong cgltf_texture_view albedo_tex_view = pbr.base_color_texture; // albedo char albedo_map_path[1024]; - snprintf(albedo_map_path, sizeof(albedo_map_path), "%s/%s", relative_path.buf, - albedo_tex_view.texture->image->uri); + if (albedo_tex_view.texture != NULL) { + snprintf(albedo_map_path, sizeof(albedo_map_path), "%s/%s", relative_path.buf, + albedo_tex_view.texture->image->uri); + } else { + WARN("GLTF model has no albedo map"); + } cgltf_texture_view metal_rough_tex_view = pbr.metallic_roughness_texture; char metal_rough_map_path[1024]; - snprintf(metal_rough_map_path, sizeof(metal_rough_map_path), "%s/%s", relative_path.buf, - metal_rough_tex_view.texture->image->uri); + if (metal_rough_tex_view.texture != NULL) { + snprintf(metal_rough_map_path, sizeof(metal_rough_map_path), "%s/%s", relative_path.buf, + metal_rough_tex_view.texture->image->uri); + } else { + WARN("GLTF model has no metal/rougnness map"); + } cgltf_texture_view normal_tex_view = gltf_material.normal_texture; char normal_map_path[1024]; - snprintf(normal_map_path, sizeof(normal_map_path), "%s/%s", relative_path.buf, - normal_tex_view.texture->image->uri); + if (normal_tex_view.texture != NULL) { + snprintf(normal_map_path, sizeof(normal_map_path), "%s/%s", relative_path.buf, + normal_tex_view.texture->image->uri); + } else { + WARN("GLTF model has no normal map"); + } + + TextureHandle albedo_map = TextureLoadFromFile(albedo_map_path); + TextureHandle metal_roughness_map = TextureLoadFromFile(metal_rough_map_path); + TextureHandle normal_map = TextureLoadFromFile(normal_map_path); // material our_material = // pbr_material_load(albedo_map_path, normal_map_path, true, metal_rough_map_path, NULL, // NULL); + Material our_material = { + .kind = MAT_PBR, + .metal_roughness_combined = true, + .pbr_albedo_map = albedo_map, + .pbr_metallic_map = metal_roughness_map, + .pbr_normal_map = normal_map, + }; // our_material.name = malloc(strlen(gltf_material.name) + 1); - // strcpy(our_material.name, gltf_material.name); + u32 string_length = strlen(gltf_material.name) + 1; + assert(string_length < 64); + strcpy(our_material.name, gltf_material.name); - // material_darray_push(out_model->materials, our_material); + Material_darray_push(out_model->materials, our_material); } - // TEMP - u32 mat_idx = 9999; - // --- Meshes size_t num_meshes = data->meshes_count; TRACE("Num meshes %d", num_meshes); for (size_t m = 0; m < num_meshes; m++) { cgltf_primitive primitive = data->meshes[m].primitives[0]; DEBUG("Found %d attributes", primitive.attributes_count); - // DEBUG("Number of this primitive %d", primitive.) for (cgltf_size a = 0; a < data->meshes[m].primitives[0].attributes_count; a++) { cgltf_attribute attribute = data->meshes[m].primitives[0].attributes[a]; if (attribute.type == cgltf_attribute_type_position) { - TRACE("Load positions from accessor"); - cgltf_accessor *accessor = attribute.data; - assert(accessor->component_type == cgltf_component_type_r_32f); - // CASSERT_MSG(accessor->type == cgltf_type_vec3, "Vertex positions should be a vec3"); - - TRACE("Loading %d vec3 components", accessor->count); - - // for (cgltf_size v = 0; v < accessor->count; ++v) { - // vec3 pos; - // cgltf_accessor_read_float(accessor, v, &pos.x, 3); - // vec3_darray_push(tmp_positions, pos); - // } - + load_position_components(tmp_positions, accessor); } else if (attribute.type == cgltf_attribute_type_normal) { - TRACE("Load normals from accessor"); - cgltf_accessor *accessor = attribute.data; - assert(accessor->component_type == cgltf_component_type_r_32f); - // CASSERT_MSG(accessor->type == cgltf_type_vec3, "Normal vectors should be a vec3"); - - // for (cgltf_size v = 0; v < accessor->count; ++v) { - // vec3 pos; - // cgltf_accessor_read_float(accessor, v, &pos.x, 3); - // vec3_darray_push(tmp_normals, pos); - // } - + load_normal_components(tmp_normals, accessor); } else if (attribute.type == cgltf_attribute_type_texcoord) { - TRACE("Load texture coordinates from accessor"); cgltf_accessor *accessor = attribute.data; - assert(accessor->component_type == cgltf_component_type_r_32f); - // CASSERT_MSG(accessor->type == cgltf_type_vec2, "Texture coordinates should be a - // vec2"); - - for (cgltf_size v = 0; v < accessor->count; ++v) { - // vec2 tex; - // bool success = cgltf_accessor_read_float(accessor, v, &tex.x, 2); - // if (!success) { - // ERROR("Error loading tex coord"); - // } - // vec2_darray_push(tmp_uvs, tex); - } + load_texcoord_components(tmp_uvs, accessor); } else if (attribute.type == cgltf_attribute_type_joints) { // FIXME: joints // TRACE("Load joint indices from accessor"); @@ -289,19 +312,21 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, Str8 rel } } // mesh.vertex_bone_data = vertex_bone_data_darray_new(1); + i32 mat_idx = -1; + if (primitive.material != NULL) { + DEBUG("Primitive Material %s", primitive.material->name); + for (u32 i = 0; i < Material_darray_len(out_model->materials); i++) { + printf("%s vs %s \n", primitive.material->name, out_model->materials->data[i].name); + if (strcmp(primitive.material->name, out_model->materials->data[i].name) == 0) { + INFO("Found material"); + mat_idx = i; + // mesh.material_index = i; + break; + } + } + } - // if (primitive.material != NULL) { - // ERROR("Primitive Material %s", primitive.material->name); - // for (u32 i = 0; i < material_darray_len(out_model->materials); i++) { - // printf("%s vs %s \n", primitive.material->name, out_model->materials->data[i].name); - // if (strcmp(primitive.material->name, out_model->materials->data[i].name) == 0) { - // INFO("Found material"); - // mat_idx = i; - // // mesh.material_index = i; - // break; - // } - // } - // } + TRACE("Vertex data has been loaded"); // // FIXME // // if (is_skinned) { @@ -327,196 +352,204 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, Str8 rel u32_darray *geo_indices = u32_darray_new(0); // Store vertices - // printf("Positions %d Normals %d UVs %d\n", tmp_positions->len, tmp_normals->len, - // tmp_uvs->len); assert(tmp_positions->len == tmp_normals->len); assert(tmp_normals->len == - // tmp_uvs->len); for (u32 v_i = 0; v_i < tmp_positions->len; v_i++) { - // vertex v = { .static_3d = { - // .position = tmp_positions->data[v_i], - // .normal = tmp_normals->data[v_i], - // .tex_coords = tmp_uvs->data[v_i], - // } }; - // vertex_darray_push(geo_vertices, v); - } - - // Store indices - // cgltf_accessor *indices = primitive.indices; - // if (primitive.indices > 0) { - // WARN("indices! %d", indices->count); - // has_indices = true; - - // // store indices - // for (cgltf_size i = 0; i < indices->count; ++i) { - // cgltf_uint ei; - // cgltf_accessor_read_uint(indices, i, &ei, 1); - // u32_darray_push(geo_indices, ei); - // } - - // fetch and store vertices for each index - // for (cgltf_size i = 0; i < indices->count; ++i) { - // vertex vert; - // cgltf_uint index = mesh.indices[i]; - // vert.position = tmp_positions->data[index]; - // vert.normal = tmp_normals->data[index]; - // vert.uv = tmp_uvs->data[index]; - // vertex_darray_push(mesh.vertices, vert); - - // if (is_skinned) { - // vertex_bone_data vbd = tmp_vertex_bone_data->data[index]; // create a copy - // vertex_bone_data_darray_push(mesh.vertex_bone_data, vbd); - // } - // // for each vertex do the bone data - // } - // } else { - // has_indices = false; - // return false; // TODO: handle this - // } - - // geometry_data *geometry = malloc(sizeof(geometry_data)); - // geometry->format = VERTEX_STATIC_3D; - // geometry->colour = (rgba){ 1, 1, 1, 1 }; - // geometry->vertices = geo_vertices; - // geometry->indices = geo_indices; - // geometry->has_indices = has_indices; - - // mesh m = mesh_create(geometry, true); - // m.material_index = (u32_opt){ .has_value = mat_idx == 9999, .value = mat_idx }; - - // mesh_darray_push(out_model->meshes, m); - // } - - // // clear data for each mesh - // vec3_darray_clear(tmp_positions); - // vec3_darray_clear(tmp_normals); - // vec2_darray_free(tmp_uvs); - // vec4u_darray_clear(tmp_joint_indices); - // vec4_darray_clear(tmp_weights); - // joint_darray_clear(tmp_joints); - // } + printf("Positions %d Normals %d UVs %d\n", tmp_positions->len, tmp_normals->len, tmp_uvs->len); + assert(tmp_positions->len == tmp_normals->len); + assert(tmp_normals->len == tmp_uvs->len); + for (u32 v_i = 0; v_i < tmp_positions->len; v_i++) { + Vertex v = { .static_3d = { + .position = tmp_positions->data[v_i], + .normal = tmp_normals->data[v_i], + .tex_coords = tmp_uvs->data[v_i], + } }; + Vertex_darray_push(geo_vertices, v); + } - // for (int i = 0; i < out_model->meshes->len; i++) { - // u32 mat_idx = out_model->meshes->data[i].material_index; - // printf("Mesh %d Mat index %d Mat name %s\n", i, mat_idx, - // out_model->materials->data[mat_idx].name); - // } + // Store indices + cgltf_accessor *indices = primitive.indices; + if (primitive.indices > 0) { + WARN("indices! %d", indices->count); + has_indices = true; + + // store indices + for (cgltf_size i = 0; i < indices->count; ++i) { + cgltf_uint ei; + cgltf_accessor_read_uint(indices, i, &ei, 1); + u32_darray_push(geo_indices, ei); + } - // // Animations - // TRACE("Num animations %d", data->animations_count); - // size_t num_animations = data->animations_count; - // if (num_animations > 0) { - // // Create an arena for all animation related data - // #define ANIMATION_STORAGE_ARENA_SIZE (1024 * 1024 * 1024) - // char *animation_backing_storage = malloc(ANIMATION_STORAGE_ARENA_SIZE); - // // We'll store data on this arena so we can easily free it all at once later - // out_model->animation_data_arena = - // arena_create(animation_backing_storage, ANIMATION_STORAGE_ARENA_SIZE); - // arena *arena = &out_model->animation_data_arena; - - // if (!out_model->animations) { - // out_model->animations = animation_clip_darray_new(num_animations); - // } + // fetch and store vertices for each index + // for (cgltf_size i = 0; i < indices->count; ++i) { + // Vertex vert; + // cgltf_uint index = mesh.indices[i]; + // vert.position = tmp_positions->data[index]; + // vert.normal = tmp_normals->data[index]; + // vert.uv = tmp_uvs->data[index]; + // vertex_darray_push(mesh.vertices, vert); + + // if (is_skinned) { + // vertex_bone_data vbd = tmp_vertex_bone_data->data[index]; // create a copy + // vertex_bone_data_darray_push(mesh.vertex_bone_data, vbd); + // } + // for each vertex do the bone data + // } + // } else { + // has_indices = false; + // return false; // TODO: handle this + // } + + Geometry *geometry = malloc(sizeof(Geometry)); + geometry->format = VERTEX_STATIC_3D; + geometry->has_indices = true; + geometry->vertices = geo_vertices; + geometry->indices = geo_indices; + // geometry->format = VERTEX_STATIC_3D; + // geometry->colour = (rgba){ 1, 1, 1, 1 }; + // geometry->vertices = geo_vertices; + // geometry->indices = geo_indices; + // geometry->has_indices = has_indices; + + // mesh m = mesh_create(geometry, true); + // m.material_index = (u32_opt){ .has_value = mat_idx == 9999, .value = mat_idx }; + + Mesh m = Mesh_Create(geometry, false); + m.material_index = mat_idx; + Mesh_darray_push(out_model->meshes, m); + } - // for (int anim_idx = 0; anim_idx < data->animations_count; anim_idx++) { - // cgltf_animation animation = data->animations[anim_idx]; - // animation_clip clip = { 0 }; - - // for (size_t c = 0; c < animation.channels_count; c++) { - // cgltf_animation_channel channel = animation.channels[c]; - - // animation_sampler *sampler = arena_alloc(arena, sizeof(animation_sampler)); - - // animation_sampler **target_property; - // keyframe_kind data_type; - - // switch (channel.target_path) { - // case cgltf_animation_path_type_rotation: - // target_property = &clip.rotation; - // data_type = KEYFRAME_ROTATION; - // break; - // case cgltf_animation_path_type_translation: - // target_property = &clip.translation; - // data_type = KEYFRAME_TRANSLATION; - // break; - // case cgltf_animation_path_type_scale: - // target_property = &clip.scale; - // data_type = KEYFRAME_SCALE; - // break; - // case cgltf_animation_path_type_weights: - // target_property = &clip.weights; - // data_type = KEYFRAME_WEIGHTS; - // WARN("Morph target weights arent supported yet"); - // return false; - // default: - // WARN("unsupported animation type"); - // return false; - // } - // *target_property = sampler; - - // sampler->current_index = 0; - // printf("1 %d index\n", sampler->current_index); - // sampler->animation.interpolation = INTERPOLATION_LINEAR; - - // // keyframe times - // size_t n_frames = channel.sampler->input->count; - // assert(channel.sampler->input->component_type == cgltf_component_type_r_32f); - // // FIXME: CASSERT_MSG function "Expected animation sampler input component to be type - // f32 - // // (keyframe times)"); - // f32 *times = arena_alloc(arena, n_frames * sizeof(f32)); - // sampler->animation.n_timestamps = n_frames; - // sampler->animation.timestamps = times; - // cgltf_accessor_unpack_floats(channel.sampler->input, times, n_frames); - - // assert_path_type_matches_component_type(channel.target_path, channel.sampler->output); - - // // keyframe values - // size_t n_values = channel.sampler->output->count; - // assert(n_frames == n_values); - - // keyframes keyframes = { 0 }; - // keyframes.kind = KEYFRAME_ROTATION; - // keyframes.count = n_values; - // keyframes.values = arena_alloc(arena, n_values * sizeof(keyframe)); - // for (cgltf_size v = 0; v < channel.sampler->output->count; ++v) { - // switch (data_type) { - // case KEYFRAME_ROTATION: { - // quat rot; - // cgltf_accessor_read_float(channel.sampler->output, v, &rot.x, 4); - // // printf("Quat %f %f %f %f\n", rot.x, rot.y, rot.z, rot.w); - // keyframes.values[v].rotation = rot; - // break; - // } - // case KEYFRAME_TRANSLATION: { - // vec3 trans; - // cgltf_accessor_read_float(channel.sampler->output, v, &trans.x, 3); - // keyframes.values[v].translation = trans; - // break; - // } - // case KEYFRAME_SCALE: { - // vec3 scale; - // cgltf_accessor_read_float(channel.sampler->output, v, &scale.x, 3); - // keyframes.values[v].scale = scale; - // break; - // } - // case KEYFRAME_WEIGHTS: { - // // TODO - // break; - // } - // } - // } - // sampler->animation.values = keyframes; + // // clear data for each mesh + // vec3_darray_clear(tmp_positions); + // vec3_darray_clear(tmp_normals); + // vec2_darray_free(tmp_uvs); + // vec4u_darray_clear(tmp_joint_indices); + // vec4_darray_clear(tmp_weights); + // joint_darray_clear(tmp_joints); + // } - // sampler->min = channel.sampler->input->min[0]; - // sampler->max = channel.sampler->input->max[0]; + // for (int i = 0; i < out_model->meshes->len; i++) { + // u32 mat_idx = out_model->meshes->data[i].material_index; + // printf("Mesh %d Mat index %d Mat name %s\n", i, mat_idx, + // out_model->materials->data[mat_idx].name); + // } - // // clip.rotation = sampler; - // // printf("%d timestamps\n", sampler->animation.n_timestamps); - // // printf("%d index\n", sampler->current_index); - // } + // // Animations + // TRACE("Num animations %d", data->animations_count); + // size_t num_animations = data->animations_count; + // if (num_animations > 0) { + // // Create an arena for all animation related data + // #define ANIMATION_STORAGE_ARENA_SIZE (1024 * 1024 * 1024) + // char *animation_backing_storage = malloc(ANIMATION_STORAGE_ARENA_SIZE); + // // We'll store data on this arena so we can easily free it all at once later + // out_model->animation_data_arena = + // arena_create(animation_backing_storage, ANIMATION_STORAGE_ARENA_SIZE); + // arena *arena = &out_model->animation_data_arena; + + // if (!out_model->animations) { + // out_model->animations = animation_clip_darray_new(num_animations); + // } - // WARN("stuff %ld", clip.rotation->animation.n_timestamps); - // animation_clip_darray_push(out_model->animations, clip); - // } - // } + // for (int anim_idx = 0; anim_idx < data->animations_count; anim_idx++) { + // cgltf_animation animation = data->animations[anim_idx]; + // animation_clip clip = { 0 }; + + // for (size_t c = 0; c < animation.channels_count; c++) { + // cgltf_animation_channel channel = animation.channels[c]; + + // animation_sampler *sampler = arena_alloc(arena, sizeof(animation_sampler)); + + // animation_sampler **target_property; + // keyframe_kind data_type; + + // switch (channel.target_path) { + // case cgltf_animation_path_type_rotation: + // target_property = &clip.rotation; + // data_type = KEYFRAME_ROTATION; + // break; + // case cgltf_animation_path_type_translation: + // target_property = &clip.translation; + // data_type = KEYFRAME_TRANSLATION; + // break; + // case cgltf_animation_path_type_scale: + // target_property = &clip.scale; + // data_type = KEYFRAME_SCALE; + // break; + // case cgltf_animation_path_type_weights: + // target_property = &clip.weights; + // data_type = KEYFRAME_WEIGHTS; + // WARN("Morph target weights arent supported yet"); + // return false; + // default: + // WARN("unsupported animation type"); + // return false; + // } + // *target_property = sampler; + + // sampler->current_index = 0; + // printf("1 %d index\n", sampler->current_index); + // sampler->animation.interpolation = INTERPOLATION_LINEAR; + + // // keyframe times + // size_t n_frames = channel.sampler->input->count; + // assert(channel.sampler->input->component_type == cgltf_component_type_r_32f); + // // FIXME: CASSERT_MSG function "Expected animation sampler input component to be + // type f32 + // // (keyframe times)"); + // f32 *times = arena_alloc(arena, n_frames * sizeof(f32)); + // sampler->animation.n_timestamps = n_frames; + // sampler->animation.timestamps = times; + // cgltf_accessor_unpack_floats(channel.sampler->input, times, n_frames); + + // assert_path_type_matches_component_type(channel.target_path, + // channel.sampler->output); + + // // keyframe values + // size_t n_values = channel.sampler->output->count; + // assert(n_frames == n_values); + + // keyframes keyframes = { 0 }; + // keyframes.kind = KEYFRAME_ROTATION; + // keyframes.count = n_values; + // keyframes.values = arena_alloc(arena, n_values * sizeof(keyframe)); + // for (cgltf_size v = 0; v < channel.sampler->output->count; ++v) { + // switch (data_type) { + // case KEYFRAME_ROTATION: { + // quat rot; + // cgltf_accessor_read_float(channel.sampler->output, v, &rot.x, 4); + // // printf("Quat %f %f %f %f\n", rot.x, rot.y, rot.z, rot.w); + // keyframes.values[v].rotation = rot; + // break; + // } + // case KEYFRAME_TRANSLATION: { + // vec3 trans; + // cgltf_accessor_read_float(channel.sampler->output, v, &trans.x, 3); + // keyframes.values[v].translation = trans; + // break; + // } + // case KEYFRAME_SCALE: { + // vec3 scale; + // cgltf_accessor_read_float(channel.sampler->output, v, &scale.x, 3); + // keyframes.values[v].scale = scale; + // break; + // } + // case KEYFRAME_WEIGHTS: { + // // TODO + // break; + // } + // } + // } + // sampler->animation.values = keyframes; + + // sampler->min = channel.sampler->input->min[0]; + // sampler->max = channel.sampler->input->max[0]; + + // // clip.rotation = sampler; + // // printf("%d timestamps\n", sampler->animation.n_timestamps); + // // printf("%d index\n", sampler->current_index); + // } + + // WARN("stuff %ld", clip.rotation->animation.n_timestamps); + // animation_clip_darray_push(out_model->animations, clip); + // } + } return true; } @@ -530,8 +563,8 @@ bool model_load_gltf(const char *path, model *out_model) { kitc_darray *tmp_normals = kitc_darray_new(sizeof(vec3), 1000); kitc_darray *tmp_uvs = kitc_darray_new(sizeof(vec2), 1000); - // may as well just init with max capacity as we're just gonna free at end of this function anyway - bh_material_darray *materials = bh_material_darray_new(MAX_MATERIALS); + // may as well just init with max capacity as we're just gonna free at end of this function +anyway bh_material_darray *materials = bh_material_darray_new(MAX_MATERIALS); CASSERT(materials->len == 0); cgltf_options options = {0}; @@ -592,7 +625,8 @@ bool model_load_gltf(const char *path, model *out_model) { our_track.keyframe_times = times; CASSERT_MSG(channel.sampler->input->component_type == cgltf_component_type_r_32f, "Expected animation sampler input component to be type f32 (keyframe times)"); - cgltf_accessor_unpack_floats(channel.sampler->input, times, channel.sampler->input->count); + cgltf_accessor_unpack_floats(channel.sampler->input, times, +channel.sampler->input->count); // printf("keyframe times[\n"); // for (int i = 0; i < n_frames; i++) { @@ -755,7 +789,8 @@ bool model_load_gltf(const char *path, model *out_model) { } } - // mesh.material_index = 0; // TODO: make sure DEFAULT_MATERIAL is added at material index 0 + // mesh.material_index = 0; // TODO: make sure DEFAULT_MATERIAL is added at material index +0 // TODO: material handling mesh.material_index = bh_material_darray_len(materials) - 1; diff --git a/src/systems/terrain.c b/src/systems/terrain.c index 404c7ea..c19ba33 100644 --- a/src/systems/terrain.c +++ b/src/systems/terrain.c @@ -90,7 +90,7 @@ void Terrain_LoadHeightmap(Terrain_Storage* storage, Heightmap hmap, f32 grid_sc u8* bytes = hmap.image_data; u8 channel = bytes[position]; float value = (float)channel / 255.0; - printf("(%d, %d) %d : %f \n", i, j, channel, value); + // printf("(%d, %d) %d : %f \n", i, j, channel, value); assert(index < num_vertices); f32 height = Heightmap_HeightXZ(&hmap, i, j); |