diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-04-06 12:17:56 +1100 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-04-06 12:17:56 +1100 |
commit | 76de359be82154193d1ed13f30c34019a92318b7 (patch) | |
tree | a1528ff6b499fc9a7390626398f6e08ccb3a4fbc | |
parent | ef264da91e1e7efc209bd49320fc5907f62312a7 (diff) |
wip: get integers
-rw-r--r-- | examples/skinned_animation/ex_skinned_animation.c | 102 | ||||
-rw-r--r-- | src/renderer/render_types.h | 8 | ||||
-rw-r--r-- | src/resources/gltf.c | 28 | ||||
-rw-r--r-- | xmake.lua | 7 |
4 files changed, 143 insertions, 2 deletions
diff --git a/examples/skinned_animation/ex_skinned_animation.c b/examples/skinned_animation/ex_skinned_animation.c new file mode 100644 index 0000000..9efffc6 --- /dev/null +++ b/examples/skinned_animation/ex_skinned_animation.c @@ -0,0 +1,102 @@ +#include <glfw3.h> + +#include "../example_scene.h" +#include "animation.h" +#include "camera.h" +#include "core.h" +#include "input.h" +#include "keys.h" +#include "log.h" +#include "maths.h" +#include "maths_types.h" +#include "primitives.h" +#include "render.h" +#include "render_backend.h" +#include "render_types.h" + +typedef struct game_state { + camera camera; + vec3 camera_euler; + bool first_mouse_update; // so the camera doesnt lurch when you run the first + // process_camera_rotation +} game_state; + +void update_camera_rotation(input_state* input, game_state* game, camera* cam); + +int main() { + double currentFrame = glfwGetTime(); + double lastFrame = currentFrame; + double deltaTime; + + core* core = core_bringup(); + + model_handle animated_cube_handle = + model_load_gltf(core, "assets/models/gltf/SimpleSkin/glTF/SimpleSkin.gltf", false); + model* cube = &core->models->data[animated_cube_handle.raw]; + model_upload_meshes(&core->renderer, cube); + + scene our_scene = make_default_scene(); + + vec3 cam_pos = vec3_create(5, 5, 5); + game_state game = { + .camera = camera_create(cam_pos, vec3_negate(cam_pos), VEC3_Y, deg_to_rad(45.0)), + .camera_euler = vec3_create(90, 0, 0), + .first_mouse_update = true, + }; + + print_vec3(game.camera.front); + + // Main loop + const f32 camera_lateral_speed = 0.2; + const f32 camera_zoom_speed = 0.15; + + // animation + // animation_clip track = cube->animations->data[0]; + // f64 total_time = 0.0; + + while (!should_exit(core)) { + input_update(&core->input); + + currentFrame = glfwGetTime(); + deltaTime = currentFrame - lastFrame; + lastFrame = currentFrame; + // total_time += deltaTime; + // printf("delta time %f\n", deltaTime); + // f64 t = fmod(total_time, 1.0); + // INFO("Total time: %f", t); + + vec3 translation = VEC3_ZERO; + if (key_is_pressed(KEYCODE_W) || key_is_pressed(KEYCODE_KEY_UP)) { + translation = vec3_mult(game.camera.front, camera_zoom_speed); + } else if (key_is_pressed(KEYCODE_S) || key_is_pressed(KEYCODE_KEY_DOWN)) { + translation = vec3_mult(game.camera.front, -camera_zoom_speed); + } else if (key_is_pressed(KEYCODE_A) || key_is_pressed(KEYCODE_KEY_LEFT)) { + vec3 lateral = vec3_normalise(vec3_cross(game.camera.front, game.camera.up)); + translation = vec3_mult(lateral, -camera_lateral_speed); + } else if (key_is_pressed(KEYCODE_D) || key_is_pressed(KEYCODE_KEY_RIGHT)) { + vec3 lateral = vec3_normalise(vec3_cross(game.camera.front, game.camera.up)); + translation = vec3_mult(lateral, camera_lateral_speed); + } + game.camera.position = vec3_add(game.camera.position, translation); + + render_frame_begin(&core->renderer); + + mat4 model = mat4_translation(VEC3_ZERO); + // quat rot = animation_sample(track.rotation, t).rotation; + quat rot = quat_ident(); + transform tf = transform_create(VEC3_ZERO, rot, 1.0); + + draw_model(&core->renderer, &game.camera, cube, tf, &our_scene); + + // gfx_backend_draw_frame(&core->renderer, &game.camera, model, NULL); + + render_frame_end(&core->renderer); + } + + INFO("Shutting down"); + model_destroy(cube); + + core_shutdown(core); + + return 0; +} diff --git a/src/renderer/render_types.h b/src/renderer/render_types.h index 8d12183..377cdc7 100644 --- a/src/renderer/render_types.h +++ b/src/renderer/render_types.h @@ -118,6 +118,14 @@ typedef struct vertex { vec2 uv; } vertex; +typedef struct skinned_vertex { + vec3 position; + vec3 normal; + vec2 uv; + vec4i joints; /** @brief 4 indices of joints that influence vectors position */ + vec4 weights; /** @brief weight (0,1) of each joint */ +} skinned_vertex; + #ifndef TYPED_VERTEX_ARRAY KITC_DECL_TYPED_ARRAY(vertex) // creates "vertex_darray" #define TYPED_VERTEX_ARRAY diff --git a/src/resources/gltf.c b/src/resources/gltf.c index eade8e6..ae4fd48 100644 --- a/src/resources/gltf.c +++ b/src/resources/gltf.c @@ -7,6 +7,7 @@ #include "file.h" #include "loaders.h" #include "log.h" +#include "maths_types.h" #include "mem.h" #include "path.h" #include "render.h" @@ -26,6 +27,7 @@ typedef struct face face; KITC_DECL_TYPED_ARRAY(vec3) KITC_DECL_TYPED_ARRAY(vec2) KITC_DECL_TYPED_ARRAY(u32) +KITC_DECL_TYPED_ARRAY(vec4i) KITC_DECL_TYPED_ARRAY(face) bool model_load_gltf_str(const char *file_string, const char *filepath, str8 relative_path, @@ -82,6 +84,7 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel vec3_darray *tmp_normals = vec3_darray_new(1000); vec2_darray *tmp_uvs = vec2_darray_new(1000); face_darray *tmp_faces = face_darray_new(1000); + vec4i_darray *tmp_joints = face_darray_new(1000); cgltf_options options = { 0 }; cgltf_data *data = NULL; @@ -95,6 +98,21 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel cgltf_load_buffers(&options, data, filepath); DEBUG("loaded buffers"); + // --- Skin + size_t num_skins = data->skins_count; + bool is_skinned = false; + if (num_skins == 1) { + is_skinned = true; + } else if (num_skins > 1) { + WARN("GLTF files with more than 1 skin are not supported"); + return false; + } + + { + cgltf_skin* gltf_skin = data->skins; + TRACE("loading skin %s", gltf_skin->name); + } + // --- Materials TRACE("Num materials %d", data->materials_count); size_t num_materials = data->materials_count; @@ -139,6 +157,7 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel 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", pri) for (int a = 0; a < data->meshes[m].primitives[0].attributes_count; a++) { cgltf_attribute attribute = data->meshes[m].primitives[0].attributes[a]; @@ -184,7 +203,12 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel } } else if (attribute.type == cgltf_attribute_type_joints) { cgltf_accessor *accessor = attribute.data; - assert(accessor->component_type == cgltf_component_type_r_32u); + assert(accessor->component_type == cgltf_component_type_r_16u); + u16 joint_indices[4]; + // ERROR("Access count %d", accessor.); + for (cgltf_size v = 0; v < accessor->count; ++v) { + // cgltf_accessor_read_uint(accessor, v, cgltf_uint *out, cgltf_size element_size) + } } else { WARN("Unhandled cgltf_attribute_type: %s. skipping..", attribute.name); } @@ -322,7 +346,7 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel 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); + // printf("Quat %f %f %f %f\n", rot.x, rot.y, rot.z, rot.w); keyframes.values[v].rotation = rot; break; } @@ -159,6 +159,13 @@ target("animation") add_files("examples/property_animation/ex_property_animation.c") set_rundir("$(projectdir)") +target("skinned") + set_kind("binary") + set_group("examples") + add_deps("core_shared") + add_files("examples/skinned_animation/ex_skinned_animation.c") + set_rundir("$(projectdir)") + target("input") set_kind("binary") set_group("examples") |