From fb3e055ff500e530e9ec46317441688bf4d07e13 Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Mon, 1 Apr 2024 01:57:11 +1100 Subject: image / textures ! --- assets/shaders/object.frag | 14 ++++++++++---- assets/shaders/object.vert | 11 +++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) (limited to 'assets') diff --git a/assets/shaders/object.frag b/assets/shaders/object.frag index 11b33e1..8466c1d 100644 --- a/assets/shaders/object.frag +++ b/assets/shaders/object.frag @@ -1,9 +1,15 @@ #version 450 #extension GL_ARB_separate_shader_objects : enable -layout(location = 0) in vec3 in_normal; -layout(location = 1) in vec3 in_position; +layout(location = 0) in vec3 in_position; +layout(location = 1) in vec3 in_normal; +layout(location = 2) in vec2 in_tex_coord; -layout(location = 0) out vec4 out_colour; +layout(binding = 1) uniform sampler2D texSampler; -void main() { out_colour = vec4(in_normal, 1.0); } \ No newline at end of file +layout(location = 0) out vec4 out_color; + +void main() { + // out_color = vec4(1.0); + out_color = texture(texSampler, in_tex_coord); +} \ No newline at end of file diff --git a/assets/shaders/object.vert b/assets/shaders/object.vert index a7237bb..23c0ffb 100644 --- a/assets/shaders/object.vert +++ b/assets/shaders/object.vert @@ -3,9 +3,11 @@ layout(location = 0) in vec3 in_position; layout(location = 1) in vec3 in_normal; +layout(location = 2) in vec2 in_tex_coord; -layout(location = 0) out vec3 out_normal; -layout(location = 1) out vec3 out_position; +layout(location = 0) out vec3 out_position; +layout(location = 1) out vec3 out_normal; +layout(location = 2) out vec2 out_tex_coord; layout(set = 0, binding = 0) uniform global_object_uniform { mat4 projection; @@ -19,8 +21,9 @@ layout(push_constant) uniform push_constants { u_push_constants; void main() { - out_position = in_position; gl_Position = global_ubo.projection * global_ubo.view * u_push_constants.model * vec4(in_position.x, in_position.y, in_position.z, 1.0); - out_normal = in_normal; // forward normal vectors + out_position = in_position; + out_normal = in_normal; + out_tex_coord = in_tex_coord; } \ No newline at end of file -- cgit v1.2.3-70-g09d2 From 6a95b047998c0e0dcfdf60d17cf2cd0bd0bfee12 Mon Sep 17 00:00:00 2001 From: omnisci3nce <17525998+omnisci3nce@users.noreply.github.com> Date: Wed, 3 Apr 2024 19:54:57 +1100 Subject: make set explicit --- assets/shaders/object.frag | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'assets') diff --git a/assets/shaders/object.frag b/assets/shaders/object.frag index 8466c1d..fa50fcf 100644 --- a/assets/shaders/object.frag +++ b/assets/shaders/object.frag @@ -5,7 +5,7 @@ layout(location = 0) in vec3 in_position; layout(location = 1) in vec3 in_normal; layout(location = 2) in vec2 in_tex_coord; -layout(binding = 1) uniform sampler2D texSampler; +layout(set = 0, binding = 1) uniform sampler2D texSampler; layout(location = 0) out vec4 out_color; -- cgit v1.2.3-70-g09d2 From af97c4330a27a92d2362e30b70990e3aa5c9954a Mon Sep 17 00:00:00 2001 From: omnisci3nce <17525998+omnisci3nce@users.noreply.github.com> Date: Thu, 4 Apr 2024 20:54:45 +1100 Subject: add AnimatedCube --- assets/models/gltf/AnimatedCube/LICENSE.md | 15 ++ assets/models/gltf/AnimatedCube/README.body.md | 3 + assets/models/gltf/AnimatedCube/README.md | 27 +++ .../models/gltf/AnimatedCube/glTF/AnimatedCube.bin | Bin 0 -> 1860 bytes .../gltf/AnimatedCube/glTF/AnimatedCube.gltf | 262 +++++++++++++++++++++ .../AnimatedCube/glTF/AnimatedCube_BaseColor.png | Bin 0 -> 891995 bytes .../glTF/AnimatedCube_MetallicRoughness.png | Bin 0 -> 319 bytes assets/models/gltf/AnimatedCube/metadata.json | 25 ++ .../gltf/AnimatedCube/screenshot/screenshot.gif | Bin 0 -> 517169 bytes .../property_animation/ex_property_animation.c | 103 ++++++++ src/defines.h | 4 +- src/renderer/backends/backend_opengl.c | 6 + src/resources/gltf.c | 3 +- 13 files changed, 445 insertions(+), 3 deletions(-) create mode 100644 assets/models/gltf/AnimatedCube/LICENSE.md create mode 100644 assets/models/gltf/AnimatedCube/README.body.md create mode 100644 assets/models/gltf/AnimatedCube/README.md create mode 100644 assets/models/gltf/AnimatedCube/glTF/AnimatedCube.bin create mode 100644 assets/models/gltf/AnimatedCube/glTF/AnimatedCube.gltf create mode 100644 assets/models/gltf/AnimatedCube/glTF/AnimatedCube_BaseColor.png create mode 100644 assets/models/gltf/AnimatedCube/glTF/AnimatedCube_MetallicRoughness.png create mode 100644 assets/models/gltf/AnimatedCube/metadata.json create mode 100644 assets/models/gltf/AnimatedCube/screenshot/screenshot.gif (limited to 'assets') diff --git a/assets/models/gltf/AnimatedCube/LICENSE.md b/assets/models/gltf/AnimatedCube/LICENSE.md new file mode 100644 index 0000000..c001f8f --- /dev/null +++ b/assets/models/gltf/AnimatedCube/LICENSE.md @@ -0,0 +1,15 @@ +# LICENSE file for the model: Animated Cube + +All files in this directory tree are licensed as indicated below. + +* All files directly associated with the model including all text, image and binary files: + + * [CC0 1.0 Universal]("https://creativecommons.org/publicdomain/zero/1.0/legalcode") [SPDX license identifier: "CC0-1.0"] + +* This file and all other metadocumentation files including "metadata.json": + + * [Creative Commons Attribtution 4.0 International]("https://creativecommons.org/licenses/by/4.0/legalcode") [SPDX license identifier: "CC-BY-4.0"] + +Full license text of these licenses are available at the links above + +#### Generated by modelmetadata \ No newline at end of file diff --git a/assets/models/gltf/AnimatedCube/README.body.md b/assets/models/gltf/AnimatedCube/README.body.md new file mode 100644 index 0000000..1efdf8c --- /dev/null +++ b/assets/models/gltf/AnimatedCube/README.body.md @@ -0,0 +1,3 @@ +## Screenshot + +![screenshot](screenshot/screenshot.gif) diff --git a/assets/models/gltf/AnimatedCube/README.md b/assets/models/gltf/AnimatedCube/README.md new file mode 100644 index 0000000..d5cf0a3 --- /dev/null +++ b/assets/models/gltf/AnimatedCube/README.md @@ -0,0 +1,27 @@ +# Animated Cube + +## Tags + +[core](../../Models-core.md), [testing](../../Models-testing.md) + +## Summary + +Same as 'Cube', but having a linear rotation animation. + +## Operations + +* [Display](https://github.khronos.org/glTF-Sample-Viewer-Release/?model=https://raw.GithubUserContent.com/KhronosGroup/glTF-Sample-Assets/main/./Models/AnimatedCube/glTF/AnimatedCube.gltf) in SampleViewer +* [Model Directory](./) + +## Screenshot + +![screenshot](screenshot/screenshot.gif) + + +## Legal + +© 2017, UX3D. [CC0 1.0 Universal](https://creativecommons.org/publicdomain/zero/1.0/legalcode) + + - Norbert Nopper for Everything + +#### Assembled by modelmetadata \ No newline at end of file diff --git a/assets/models/gltf/AnimatedCube/glTF/AnimatedCube.bin b/assets/models/gltf/AnimatedCube/glTF/AnimatedCube.bin new file mode 100644 index 0000000..72f7d2d Binary files /dev/null and b/assets/models/gltf/AnimatedCube/glTF/AnimatedCube.bin differ diff --git a/assets/models/gltf/AnimatedCube/glTF/AnimatedCube.gltf b/assets/models/gltf/AnimatedCube/glTF/AnimatedCube.gltf new file mode 100644 index 0000000..ff117b0 --- /dev/null +++ b/assets/models/gltf/AnimatedCube/glTF/AnimatedCube.gltf @@ -0,0 +1,262 @@ +{ + "accessors" : [ + { + "bufferView" : 0, + "byteOffset" : 0, + "componentType" : 5126, + "count" : 3, + "max" : [ + 2.000000 + ], + "min" : [ + 0.000000 + ], + "type" : "SCALAR" + }, + { + "bufferView" : 1, + "byteOffset" : 0, + "componentType" : 5126, + "count" : 3, + "max" : [ + 0.000000, + 1.000000, + 0.000000, + 1.000000 + ], + "min" : [ + 0.000000, + -8.742278e-008, + 0.000000, + -1.000000 + ], + "type" : "VEC4" + }, + { + "bufferView" : 2, + "byteOffset" : 0, + "componentType" : 5123, + "count" : 36, + "max" : [ + 35 + ], + "min" : [ + 0 + ], + "type" : "SCALAR" + }, + { + "bufferView" : 3, + "byteOffset" : 0, + "componentType" : 5126, + "count" : 36, + "max" : [ + 1.000000, + 1.000000, + 1.000001 + ], + "min" : [ + -1.000000, + -1.000000, + -1.000000 + ], + "type" : "VEC3" + }, + { + "bufferView" : 4, + "byteOffset" : 0, + "componentType" : 5126, + "count" : 36, + "max" : [ + 1.000000, + 1.000000, + 1.000000 + ], + "min" : [ + -1.000000, + -1.000000, + -1.000000 + ], + "type" : "VEC3" + }, + { + "bufferView" : 5, + "byteOffset" : 0, + "componentType" : 5126, + "count" : 36, + "max" : [ + 1.000000, + -0.000000, + -0.000000, + 1.000000 + ], + "min" : [ + 0.000000, + -0.000000, + -1.000000, + -1.000000 + ], + "type" : "VEC4" + }, + { + "bufferView" : 6, + "byteOffset" : 0, + "componentType" : 5126, + "count" : 36, + "max" : [ + 1.000000, + 1.000000 + ], + "min" : [ + -1.000000, + -1.000000 + ], + "type" : "VEC2" + } + ], + "animations" : [ + { + "channels" : [ + { + "sampler" : 0, + "target" : { + "node" : 0, + "path" : "rotation" + } + } + ], + "name" : "animation_AnimatedCube", + "samplers" : [ + { + "input" : 0, + "interpolation" : "LINEAR", + "output" : 1 + } + ] + } + ], + "asset" : { + "generator" : "VKTS glTF 2.0 exporter", + "version" : "2.0" + }, + "bufferViews" : [ + { + "buffer" : 0, + "byteLength" : 12, + "byteOffset" : 0 + }, + { + "buffer" : 0, + "byteLength" : 48, + "byteOffset" : 12 + }, + { + "buffer" : 0, + "byteLength" : 72, + "byteOffset" : 60, + "target" : 34963 + }, + { + "buffer" : 0, + "byteLength" : 432, + "byteOffset" : 132, + "target" : 34962 + }, + { + "buffer" : 0, + "byteLength" : 432, + "byteOffset" : 564, + "target" : 34962 + }, + { + "buffer" : 0, + "byteLength" : 576, + "byteOffset" : 996, + "target" : 34962 + }, + { + "buffer" : 0, + "byteLength" : 288, + "byteOffset" : 1572, + "target" : 34962 + } + ], + "buffers" : [ + { + "byteLength" : 1860, + "uri" : "AnimatedCube.bin" + } + ], + "images" : [ + { + "uri" : "AnimatedCube_BaseColor.png" + }, + { + "uri" : "AnimatedCube_MetallicRoughness.png" + } + ], + "materials" : [ + { + "name" : "AnimatedCube", + "pbrMetallicRoughness" : { + "baseColorTexture" : { + "index" : 0 + }, + "metallicRoughnessTexture" : { + "index" : 1 + } + } + } + ], + "meshes" : [ + { + "name" : "AnimatedCube", + "primitives" : [ + { + "attributes" : { + "NORMAL" : 4, + "POSITION" : 3, + "TANGENT" : 5, + "TEXCOORD_0" : 6 + }, + "indices" : 2, + "material" : 0, + "mode" : 4 + } + ] + } + ], + "nodes" : [ + { + "mesh" : 0, + "name" : "AnimatedCube", + "rotation" : [ + 0.000000, + -1.000000, + 0.000000, + 0.000000 + ] + } + ], + "samplers" : [ + {} + ], + "scene" : 0, + "scenes" : [ + { + "nodes" : [ + 0 + ] + } + ], + "textures" : [ + { + "sampler" : 0, + "source" : 0 + }, + { + "sampler" : 0, + "source" : 1 + } + ] +} \ No newline at end of file diff --git a/assets/models/gltf/AnimatedCube/glTF/AnimatedCube_BaseColor.png b/assets/models/gltf/AnimatedCube/glTF/AnimatedCube_BaseColor.png new file mode 100644 index 0000000..5e5cb20 Binary files /dev/null and b/assets/models/gltf/AnimatedCube/glTF/AnimatedCube_BaseColor.png differ diff --git a/assets/models/gltf/AnimatedCube/glTF/AnimatedCube_MetallicRoughness.png b/assets/models/gltf/AnimatedCube/glTF/AnimatedCube_MetallicRoughness.png new file mode 100644 index 0000000..efd2026 Binary files /dev/null and b/assets/models/gltf/AnimatedCube/glTF/AnimatedCube_MetallicRoughness.png differ diff --git a/assets/models/gltf/AnimatedCube/metadata.json b/assets/models/gltf/AnimatedCube/metadata.json new file mode 100644 index 0000000..3af6fdb --- /dev/null +++ b/assets/models/gltf/AnimatedCube/metadata.json @@ -0,0 +1,25 @@ +{ + "version": 2, + "legal": [ + { + "license": "CC0", + "licenseUrl": "https://creativecommons.org/publicdomain/zero/1.0/legalcode", + "artist": "Norbert Nopper", + "year": "2017", + "owner": "UX3D", + "what": "Everything", + "text": "CC0 1.0 Universal", + "spdx": "CC0-1.0", + "icon": "https://licensebuttons.net/p/zero/1.0/88x31.png" + } + ], + "tags": [ + "core", + "testing" + ], + "screenshot": "screenshot/screenshot.gif", + "name": "Animated Cube", + "path": "./Models/AnimatedCube", + "summary": "Same as 'Cube', but having a linear rotation animation.", + "createReadme": true +} \ No newline at end of file diff --git a/assets/models/gltf/AnimatedCube/screenshot/screenshot.gif b/assets/models/gltf/AnimatedCube/screenshot/screenshot.gif new file mode 100644 index 0000000..eea74ee Binary files /dev/null and b/assets/models/gltf/AnimatedCube/screenshot/screenshot.gif differ diff --git a/examples/property_animation/ex_property_animation.c b/examples/property_animation/ex_property_animation.c index e69de29..38bafd5 100644 --- a/examples/property_animation/ex_property_animation.c +++ b/examples/property_animation/ex_property_animation.c @@ -0,0 +1,103 @@ +#include + +#include "camera.h" +#include "core.h" +#include "input.h" +#include "keys.h" +#include "maths.h" +#include "maths_types.h" +#include "primitives.h" +#include "render.h" +#include "render_backend.h" +#include "render_types.h" + +const vec3 pointlight_positions[4] = { + { 0.7, 0.2, 2.0 }, + { 2.3, -3.3, -4.0 }, + { -4.0, 2.0, -12.0 }, + { 0.0, 0.0, -3.0 }, +}; +point_light point_lights[4]; + +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() { + core* core = core_bringup(); + + model_handle animated_cube_handle = + model_load_gltf(core, "assets/models/gltf/AnimatedCube/glTF/AnimatedCube.gltf", false); + model* cube = &core->models->data[animated_cube_handle.raw]; + model_upload_meshes(&core->renderer, cube); + + directional_light dir_light = { .direction = (vec3){ -0.2, -1.0, -0.3 }, + .ambient = (vec3){ 0.2, 0.2, 0.2 }, + .diffuse = (vec3){ 0.5, 0.5, 0.5 }, + .specular = (vec3){ 1.0, 1.0, 1.0 } }; + + for (int i = 0; i < 4; i++) { + point_lights[i].position = pointlight_positions[i]; + point_lights[i].ambient = (vec3){ 0.05, 0.05, 0.05 }; + point_lights[i].diffuse = (vec3){ 0.8, 0.8, 0.8 }; + point_lights[i].specular = (vec3){ 1.0, 1.0, 1.0 }; + point_lights[i].constant = 1.0; + point_lights[i].linear = 0.09; + point_lights[i].quadratic = 0.032; + } + scene our_scene = { .dir_light = dir_light, .n_point_lights = 4 }; + memcpy(&our_scene.point_lights, &point_lights, sizeof(point_light[4])); + + 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; + + while (!should_exit(core)) { + input_update(&core->input); + + 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); + + // UNUSED: threadpool_process_results(&core->threadpool, 1); + + render_frame_begin(&core->renderer); + + mat4 model = mat4_translation(VEC3_ZERO); + transform tf = transform_create(VEC3_ZERO, quat_ident(), 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); + } + + core_shutdown(core); + + return 0; +} diff --git a/src/defines.h b/src/defines.h index 4459e1a..52aa7b0 100644 --- a/src/defines.h +++ b/src/defines.h @@ -66,8 +66,8 @@ Renderer backend defines: // Platform will inform renderer backend (unless user overrides) #if defined(CEL_PLATFORM_LINUX) || defined(CEL_PLATFORM_WINDOWS) -// #define CEL_REND_BACKEND_OPENGL 1 -#define CEL_REND_BACKEND_VULKAN 1 +#define CEL_REND_BACKEND_OPENGL 1 +// #define CEL_REND_BACKEND_VULKAN 1 #endif #if defined(CEL_PLATFORM_MAC) diff --git a/src/renderer/backends/backend_opengl.c b/src/renderer/backends/backend_opengl.c index e3a4fb6..8df0933 100644 --- a/src/renderer/backends/backend_opengl.c +++ b/src/renderer/backends/backend_opengl.c @@ -1,4 +1,5 @@ #include +#include "camera.h" #define CEL_PLATFORM_LINUX #include "defines.h" @@ -40,6 +41,11 @@ bool gfx_backend_init(renderer *ren) { return true; } + +void gfx_backend_draw_frame(renderer* ren, camera* cam, mat4 model, texture* tex) { + +} + void gfx_backend_shutdown(renderer *ren) {} void uniform_vec3f(u32 program_id, const char *uniform_name, vec3 *value) { diff --git a/src/resources/gltf.c b/src/resources/gltf.c index b269fcd..261c96f 100644 --- a/src/resources/gltf.c +++ b/src/resources/gltf.c @@ -211,7 +211,8 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel if (primitive.material != NULL) { for (int i = 0; i < material_darray_len(out_model->materials); i++) { - if (strcmp(primitive.material->name, out_model->materials->data[i].name)) { + 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) { TRACE("Found material"); mesh.material_index = i; break; -- cgit v1.2.3-70-g09d2 From ef264da91e1e7efc209bd49320fc5907f62312a7 Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sat, 6 Apr 2024 11:51:33 +1100 Subject: add a skinned model example from gltf sample models. --- .gitignore | 1 + assets/models/gltf/SimpleSkin/LICENSE.md | 15 +++ assets/models/gltf/SimpleSkin/README.body.md | 18 +++ assets/models/gltf/SimpleSkin/README.md | 42 +++++++ .../gltf/SimpleSkin/glTF-Embedded/SimpleSkin.gltf | 131 +++++++++++++++++++++ assets/models/gltf/SimpleSkin/glTF/SimpleSkin.gltf | 131 +++++++++++++++++++++ .../gltf/SimpleSkin/glTF/inverseBindMatrices.bin | Bin 0 -> 128 bytes .../models/gltf/SimpleSkin/glTF/skinAnimation.bin | Bin 0 -> 240 bytes .../models/gltf/SimpleSkin/glTF/skinGeometry.bin | Bin 0 -> 168 bytes .../models/gltf/SimpleSkin/glTF/skinningData.bin | Bin 0 -> 320 bytes assets/models/gltf/SimpleSkin/metadata.json | 26 ++++ .../SimpleSkin/screenshot/inverseBindMatrices.png | Bin 0 -> 43880 bytes .../gltf/SimpleSkin/screenshot/screenshot.gif | Bin 0 -> 2000860 bytes .../gltf/SimpleSkin/screenshot/skinAnimation.png | Bin 0 -> 100791 bytes .../gltf/SimpleSkin/screenshot/skinGeometry.png | Bin 0 -> 74566 bytes .../gltf/SimpleSkin/screenshot/skinningData.png | Bin 0 -> 86339 bytes examples/example_scene.h | 31 +++++ .../property_animation/ex_property_animation.c | 34 ++---- src/animation.c | 18 +-- src/resources/gltf.c | 77 ++++++++---- 20 files changed, 466 insertions(+), 58 deletions(-) create mode 100644 assets/models/gltf/SimpleSkin/LICENSE.md create mode 100644 assets/models/gltf/SimpleSkin/README.body.md create mode 100644 assets/models/gltf/SimpleSkin/README.md create mode 100644 assets/models/gltf/SimpleSkin/glTF-Embedded/SimpleSkin.gltf create mode 100644 assets/models/gltf/SimpleSkin/glTF/SimpleSkin.gltf create mode 100644 assets/models/gltf/SimpleSkin/glTF/inverseBindMatrices.bin create mode 100644 assets/models/gltf/SimpleSkin/glTF/skinAnimation.bin create mode 100644 assets/models/gltf/SimpleSkin/glTF/skinGeometry.bin create mode 100644 assets/models/gltf/SimpleSkin/glTF/skinningData.bin create mode 100644 assets/models/gltf/SimpleSkin/metadata.json create mode 100644 assets/models/gltf/SimpleSkin/screenshot/inverseBindMatrices.png create mode 100644 assets/models/gltf/SimpleSkin/screenshot/screenshot.gif create mode 100644 assets/models/gltf/SimpleSkin/screenshot/skinAnimation.png create mode 100644 assets/models/gltf/SimpleSkin/screenshot/skinGeometry.png create mode 100644 assets/models/gltf/SimpleSkin/screenshot/skinningData.png create mode 100644 examples/example_scene.h (limited to 'assets') diff --git a/.gitignore b/.gitignore index f082afc..605c5e9 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,6 @@ _temp .vscode .cache .clangd +.DS_Store compile_commands.json node_modules/ \ No newline at end of file diff --git a/assets/models/gltf/SimpleSkin/LICENSE.md b/assets/models/gltf/SimpleSkin/LICENSE.md new file mode 100644 index 0000000..e602203 --- /dev/null +++ b/assets/models/gltf/SimpleSkin/LICENSE.md @@ -0,0 +1,15 @@ +# LICENSE file for the model: Simple Skin + +All files in this directory tree are licensed as indicated below. + +* All files directly associated with the model including all text, image and binary files: + + * [CC0 1.0 Universal]("https://creativecommons.org/publicdomain/zero/1.0/legalcode") [SPDX license identifier: "CC0-1.0"] + +* This file and all other metadocumentation files including "metadata.json": + + * [Creative Commons Attribtution 4.0 International]("https://creativecommons.org/licenses/by/4.0/legalcode") [SPDX license identifier: "CC-BY-4.0"] + +Full license text of these licenses are available at the links above + +#### Generated by modelmetadata \ No newline at end of file diff --git a/assets/models/gltf/SimpleSkin/README.body.md b/assets/models/gltf/SimpleSkin/README.body.md new file mode 100644 index 0000000..00cd41e --- /dev/null +++ b/assets/models/gltf/SimpleSkin/README.body.md @@ -0,0 +1,18 @@ +## Screenshot + +![screenshot](screenshot/screenshot.gif) + +## Notes + +Details about skinning using this particular model are explained in the +[skinning section of the glTF tutorial](https://github.com/javagl/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_019_SimpleSkin.md). + +## Data layout + +The following images show the data layout of this sample: + +![skinGeometry](screenshot/skinGeometry.png) +![skinAnimation](screenshot/skinAnimation.png) +![inverseBindMatrices](screenshot/inverseBindMatrices.png) +![skinningData](screenshot/skinningData.png) + diff --git a/assets/models/gltf/SimpleSkin/README.md b/assets/models/gltf/SimpleSkin/README.md new file mode 100644 index 0000000..ec08353 --- /dev/null +++ b/assets/models/gltf/SimpleSkin/README.md @@ -0,0 +1,42 @@ +# Simple Skin + +## Tags + +[core](../../Models-core.md), [testing](../../Models-testing.md), [written](../../Models-written.md) + +## Summary + +A simple example of vertex skinning in glTF + +## Operations + +* [Display](https://github.khronos.org/glTF-Sample-Viewer-Release/?model=https://raw.GithubUserContent.com/KhronosGroup/glTF-Sample-Assets/main/./Models/SimpleSkin/glTF/SimpleSkin.gltf) in SampleViewer +* [Model Directory](./) + +## Screenshot + +![screenshot](screenshot/screenshot.gif) + +## Notes + +Details about skinning using this particular model are explained in the +[skinning section of the glTF tutorial](https://github.com/javagl/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_019_SimpleSkin.md). + +## Data layout + +The following images show the data layout of this sample: + +![skinGeometry](screenshot/skinGeometry.png) +![skinAnimation](screenshot/skinAnimation.png) +![inverseBindMatrices](screenshot/inverseBindMatrices.png) +![skinningData](screenshot/skinningData.png) + + + +## Legal + +© 2017, Public. [CC0 1.0 Universal](https://creativecommons.org/publicdomain/zero/1.0/legalcode) + + - javagl for Everything + +#### Assembled by modelmetadata \ No newline at end of file diff --git a/assets/models/gltf/SimpleSkin/glTF-Embedded/SimpleSkin.gltf b/assets/models/gltf/SimpleSkin/glTF-Embedded/SimpleSkin.gltf new file mode 100644 index 0000000..e9f4e4d --- /dev/null +++ b/assets/models/gltf/SimpleSkin/glTF-Embedded/SimpleSkin.gltf @@ -0,0 +1,131 @@ +{ + "scene" : 0, + "scenes" : [ { + "nodes" : [ 0, 1 ] + } ], + + "nodes" : [ { + "skin" : 0, + "mesh" : 0 + }, { + "children" : [ 2 ] + }, { + "translation" : [ 0.0, 1.0, 0.0 ], + "rotation" : [ 0.0, 0.0, 0.0, 1.0 ] + } ], + + "meshes" : [ { + "primitives" : [ { + "attributes" : { + "POSITION" : 1, + "JOINTS_0" : 2, + "WEIGHTS_0" : 3 + }, + "indices" : 0 + } ] + } ], + + "skins" : [ { + "inverseBindMatrices" : 4, + "joints" : [ 1, 2 ] + } ], + + "animations" : [ { + "channels" : [ { + "sampler" : 0, + "target" : { + "node" : 2, + "path" : "rotation" + } + } ], + "samplers" : [ { + "input" : 5, + "interpolation" : "LINEAR", + "output" : 6 + } ] + } ], + + "buffers" : [ { + "uri" : "data:application/gltf-buffer;base64,AAABAAMAAAADAAIAAgADAAUAAgAFAAQABAAFAAcABAAHAAYABgAHAAkABgAJAAgAAAAAvwAAAAAAAAAAAAAAPwAAAAAAAAAAAAAAvwAAAD8AAAAAAAAAPwAAAD8AAAAAAAAAvwAAgD8AAAAAAAAAPwAAgD8AAAAAAAAAvwAAwD8AAAAAAAAAPwAAwD8AAAAAAAAAvwAAAEAAAAAAAAAAPwAAAEAAAAAA", + "byteLength" : 168 + }, { + "uri" : "data:application/gltf-buffer;base64,AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAgD8AAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAABAPwAAgD4AAAAAAAAAAAAAQD8AAIA+AAAAAAAAAAAAAAA/AAAAPwAAAAAAAAAAAAAAPwAAAD8AAAAAAAAAAAAAgD4AAEA/AAAAAAAAAAAAAIA+AABAPwAAAAAAAAAAAAAAAAAAgD8AAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAA=", + "byteLength" : 320 + }, { + "uri" : "data:application/gltf-buffer;base64,AACAPwAAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAAAAAAAAgD8AAAAAAAAAAAAAAAAAAAAAAACAPwAAgD8AAAAAAAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAIC/AAAAAAAAgD8=", + "byteLength" : 128 + }, { + "uri" : "data:application/gltf-buffer;base64,AAAAAAAAAD8AAIA/AADAPwAAAEAAACBAAABAQAAAYEAAAIBAAACQQAAAoEAAALBAAAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAkxjEPkSLbD8AAAAAAAAAAPT9ND/0/TQ/AAAAAAAAAAD0/TQ/9P00PwAAAAAAAAAAkxjEPkSLbD8AAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAkxjEvkSLbD8AAAAAAAAAAPT9NL/0/TQ/AAAAAAAAAAD0/TS/9P00PwAAAAAAAAAAkxjEvkSLbD8AAAAAAAAAAAAAAAAAAIA/", + "byteLength" : 240 + } ], + + "bufferViews" : [ { + "buffer" : 0, + "byteLength" : 48, + "target" : 34963 + }, { + "buffer" : 0, + "byteOffset" : 48, + "byteLength" : 120, + "target" : 34962 + }, { + "buffer" : 1, + "byteLength" : 320, + "byteStride" : 16 + }, { + "buffer" : 2, + "byteLength" : 128 + }, { + "buffer" : 3, + "byteLength" : 240 + } ], + + "accessors" : [ { + "bufferView" : 0, + "componentType" : 5123, + "count" : 24, + "type" : "SCALAR" + }, { + "bufferView" : 1, + "componentType" : 5126, + "count" : 10, + "type" : "VEC3", + "max" : [ 0.5, 2.0, 0.0 ], + "min" : [ -0.5, 0.0, 0.0 ] + }, { + "bufferView" : 2, + "componentType" : 5123, + "count" : 10, + "type" : "VEC4" + }, { + "bufferView" : 2, + "byteOffset" : 160, + "componentType" : 5126, + "count" : 10, + "type" : "VEC4" + }, { + "bufferView" : 3, + "componentType" : 5126, + "count" : 2, + "type" : "MAT4" + }, { + "bufferView" : 4, + "componentType" : 5126, + "count" : 12, + "type" : "SCALAR", + "max" : [ 5.5 ], + "min" : [ 0.0 ] + }, { + "bufferView" : 4, + "byteOffset" : 48, + "componentType" : 5126, + "count" : 12, + "type" : "VEC4", + "max" : [ 0.0, 0.0, 0.707, 1.0 ], + "min" : [ 0.0, 0.0, -0.707, 0.707 ] + } ], + + "asset" : { + "version" : "2.0" + } +} \ No newline at end of file diff --git a/assets/models/gltf/SimpleSkin/glTF/SimpleSkin.gltf b/assets/models/gltf/SimpleSkin/glTF/SimpleSkin.gltf new file mode 100644 index 0000000..52f0aa0 --- /dev/null +++ b/assets/models/gltf/SimpleSkin/glTF/SimpleSkin.gltf @@ -0,0 +1,131 @@ +{ + "scene" : 0, + "scenes" : [ { + "nodes" : [ 0, 1 ] + } ], + + "nodes" : [ { + "skin" : 0, + "mesh" : 0 + }, { + "children" : [ 2 ] + }, { + "translation" : [ 0.0, 1.0, 0.0 ], + "rotation" : [ 0.0, 0.0, 0.0, 1.0 ] + } ], + + "meshes" : [ { + "primitives" : [ { + "attributes" : { + "POSITION" : 1, + "JOINTS_0" : 2, + "WEIGHTS_0" : 3 + }, + "indices" : 0 + } ] + } ], + + "skins" : [ { + "inverseBindMatrices" : 4, + "joints" : [ 1, 2 ] + } ], + + "animations" : [ { + "channels" : [ { + "sampler" : 0, + "target" : { + "node" : 2, + "path" : "rotation" + } + } ], + "samplers" : [ { + "input" : 5, + "interpolation" : "LINEAR", + "output" : 6 + } ] + } ], + + "buffers" : [ { + "uri" : "skinGeometry.bin", + "byteLength" : 168 + }, { + "uri" : "skinningData.bin", + "byteLength" : 320 + }, { + "uri" : "inverseBindMatrices.bin", + "byteLength" : 128 + }, { + "uri" : "skinAnimation.bin", + "byteLength" : 240 + } ], + + "bufferViews" : [ { + "buffer" : 0, + "byteLength" : 48, + "target" : 34963 + }, { + "buffer" : 0, + "byteOffset" : 48, + "byteLength" : 120, + "target" : 34962 + }, { + "buffer" : 1, + "byteLength" : 320, + "byteStride" : 16 + }, { + "buffer" : 2, + "byteLength" : 128 + }, { + "buffer" : 3, + "byteLength" : 240 + } ], + + "accessors" : [ { + "bufferView" : 0, + "componentType" : 5123, + "count" : 24, + "type" : "SCALAR" + }, { + "bufferView" : 1, + "componentType" : 5126, + "count" : 10, + "type" : "VEC3", + "max" : [ 0.5, 2.0, 0.0 ], + "min" : [ -0.5, 0.0, 0.0 ] + }, { + "bufferView" : 2, + "componentType" : 5123, + "count" : 10, + "type" : "VEC4" + }, { + "bufferView" : 2, + "byteOffset" : 160, + "componentType" : 5126, + "count" : 10, + "type" : "VEC4" + }, { + "bufferView" : 3, + "componentType" : 5126, + "count" : 2, + "type" : "MAT4" + }, { + "bufferView" : 4, + "componentType" : 5126, + "count" : 12, + "type" : "SCALAR", + "max" : [ 5.5 ], + "min" : [ 0.0 ] + }, { + "bufferView" : 4, + "byteOffset" : 48, + "componentType" : 5126, + "count" : 12, + "type" : "VEC4", + "max" : [ 0.0, 0.0, 0.707, 1.0 ], + "min" : [ 0.0, 0.0, -0.707, 0.707 ] + } ], + + "asset" : { + "version" : "2.0" + } +} \ No newline at end of file diff --git a/assets/models/gltf/SimpleSkin/glTF/inverseBindMatrices.bin b/assets/models/gltf/SimpleSkin/glTF/inverseBindMatrices.bin new file mode 100644 index 0000000..5af0b5e Binary files /dev/null and b/assets/models/gltf/SimpleSkin/glTF/inverseBindMatrices.bin differ diff --git a/assets/models/gltf/SimpleSkin/glTF/skinAnimation.bin b/assets/models/gltf/SimpleSkin/glTF/skinAnimation.bin new file mode 100644 index 0000000..ff550c6 Binary files /dev/null and b/assets/models/gltf/SimpleSkin/glTF/skinAnimation.bin differ diff --git a/assets/models/gltf/SimpleSkin/glTF/skinGeometry.bin b/assets/models/gltf/SimpleSkin/glTF/skinGeometry.bin new file mode 100644 index 0000000..15375c4 Binary files /dev/null and b/assets/models/gltf/SimpleSkin/glTF/skinGeometry.bin differ diff --git a/assets/models/gltf/SimpleSkin/glTF/skinningData.bin b/assets/models/gltf/SimpleSkin/glTF/skinningData.bin new file mode 100644 index 0000000..fd0c165 Binary files /dev/null and b/assets/models/gltf/SimpleSkin/glTF/skinningData.bin differ diff --git a/assets/models/gltf/SimpleSkin/metadata.json b/assets/models/gltf/SimpleSkin/metadata.json new file mode 100644 index 0000000..764a837 --- /dev/null +++ b/assets/models/gltf/SimpleSkin/metadata.json @@ -0,0 +1,26 @@ +{ + "version": 2, + "legal": [ + { + "license": "CC0", + "licenseUrl": "https://creativecommons.org/publicdomain/zero/1.0/legalcode", + "artist": "javagl", + "year": "2017", + "owner": "Public", + "what": "Everything", + "text": "CC0 1.0 Universal", + "spdx": "CC0-1.0", + "icon": "https://licensebuttons.net/p/zero/1.0/88x31.png" + } + ], + "tags": [ + "core", + "testing", + "written" + ], + "screenshot": "screenshot/screenshot.gif", + "name": "Simple Skin", + "path": "./Models/SimpleSkin", + "summary": "A simple example of vertex skinning in glTF", + "createReadme": true +} \ No newline at end of file diff --git a/assets/models/gltf/SimpleSkin/screenshot/inverseBindMatrices.png b/assets/models/gltf/SimpleSkin/screenshot/inverseBindMatrices.png new file mode 100644 index 0000000..a069f59 Binary files /dev/null and b/assets/models/gltf/SimpleSkin/screenshot/inverseBindMatrices.png differ diff --git a/assets/models/gltf/SimpleSkin/screenshot/screenshot.gif b/assets/models/gltf/SimpleSkin/screenshot/screenshot.gif new file mode 100644 index 0000000..c8fb353 Binary files /dev/null and b/assets/models/gltf/SimpleSkin/screenshot/screenshot.gif differ diff --git a/assets/models/gltf/SimpleSkin/screenshot/skinAnimation.png b/assets/models/gltf/SimpleSkin/screenshot/skinAnimation.png new file mode 100644 index 0000000..20257e1 Binary files /dev/null and b/assets/models/gltf/SimpleSkin/screenshot/skinAnimation.png differ diff --git a/assets/models/gltf/SimpleSkin/screenshot/skinGeometry.png b/assets/models/gltf/SimpleSkin/screenshot/skinGeometry.png new file mode 100644 index 0000000..2569d9f Binary files /dev/null and b/assets/models/gltf/SimpleSkin/screenshot/skinGeometry.png differ diff --git a/assets/models/gltf/SimpleSkin/screenshot/skinningData.png b/assets/models/gltf/SimpleSkin/screenshot/skinningData.png new file mode 100644 index 0000000..bd25b70 Binary files /dev/null and b/assets/models/gltf/SimpleSkin/screenshot/skinningData.png differ diff --git a/examples/example_scene.h b/examples/example_scene.h new file mode 100644 index 0000000..eb0be18 --- /dev/null +++ b/examples/example_scene.h @@ -0,0 +1,31 @@ +#pragma once +#include "render_types.h" + +const vec3 pointlight_positions[4] = { + { 0.7, 0.2, 2.0 }, + { 2.3, -3.3, -4.0 }, + { -4.0, 2.0, -12.0 }, + { 0.0, 0.0, -3.0 }, +}; +static point_light point_lights[4]; + +static scene make_default_scene() { + directional_light dir_light = { .direction = (vec3){ -0.2, -1.0, -0.3 }, + .ambient = (vec3){ 0.2, 0.2, 0.2 }, + .diffuse = (vec3){ 0.5, 0.5, 0.5 }, + .specular = (vec3){ 1.0, 1.0, 1.0 } }; + + for (int i = 0; i < 4; i++) { + point_lights[i].position = pointlight_positions[i]; + point_lights[i].ambient = (vec3){ 0.05, 0.05, 0.05 }; + point_lights[i].diffuse = (vec3){ 0.8, 0.8, 0.8 }; + point_lights[i].specular = (vec3){ 1.0, 1.0, 1.0 }; + point_lights[i].constant = 1.0; + point_lights[i].linear = 0.09; + point_lights[i].quadratic = 0.032; + } + + scene our_scene = { .dir_light = dir_light, .n_point_lights = 4 }; + memcpy(&our_scene.point_lights, &point_lights, sizeof(point_light[4])); + return our_scene; +} \ No newline at end of file diff --git a/examples/property_animation/ex_property_animation.c b/examples/property_animation/ex_property_animation.c index 0d4a0d7..5ca0836 100644 --- a/examples/property_animation/ex_property_animation.c +++ b/examples/property_animation/ex_property_animation.c @@ -1,5 +1,6 @@ #include +#include "../example_scene.h" #include "animation.h" #include "camera.h" #include "core.h" @@ -13,14 +14,6 @@ #include "render_backend.h" #include "render_types.h" -const vec3 pointlight_positions[4] = { - { 0.7, 0.2, 2.0 }, - { 2.3, -3.3, -4.0 }, - { -4.0, 2.0, -12.0 }, - { 0.0, 0.0, -3.0 }, -}; -point_light point_lights[4]; - typedef struct game_state { camera camera; vec3 camera_euler; @@ -42,22 +35,7 @@ int main() { model* cube = &core->models->data[animated_cube_handle.raw]; model_upload_meshes(&core->renderer, cube); - directional_light dir_light = { .direction = (vec3){ -0.2, -1.0, -0.3 }, - .ambient = (vec3){ 0.2, 0.2, 0.2 }, - .diffuse = (vec3){ 0.5, 0.5, 0.5 }, - .specular = (vec3){ 1.0, 1.0, 1.0 } }; - - for (int i = 0; i < 4; i++) { - point_lights[i].position = pointlight_positions[i]; - point_lights[i].ambient = (vec3){ 0.05, 0.05, 0.05 }; - point_lights[i].diffuse = (vec3){ 0.8, 0.8, 0.8 }; - point_lights[i].specular = (vec3){ 1.0, 1.0, 1.0 }; - point_lights[i].constant = 1.0; - point_lights[i].linear = 0.09; - point_lights[i].quadratic = 0.032; - } - scene our_scene = { .dir_light = dir_light, .n_point_lights = 4 }; - memcpy(&our_scene.point_lights, &point_lights, sizeof(point_light[4])); + scene our_scene = make_default_scene(); vec3 cam_pos = vec3_create(5, 5, 5); game_state game = { @@ -73,7 +51,7 @@ int main() { const f32 camera_zoom_speed = 0.15; // animation - animation_clip track = cube->animations->data[0]; + animation_clip track = cube->animations->data[0]; f64 total_time = 0.0; while (!should_exit(core)) { @@ -81,8 +59,10 @@ int main() { currentFrame = glfwGetTime(); deltaTime = currentFrame - lastFrame; - total_time += deltaTime * 0.00001; - f64 t = fmod(total_time * 1000.0, 1.0); + 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; diff --git a/src/animation.c b/src/animation.c index f6741e8..de7e9a2 100644 --- a/src/animation.c +++ b/src/animation.c @@ -1,6 +1,6 @@ #include "animation.h" -#include "maths.h" #include "log.h" +#include "maths.h" keyframe animation_sample(animation_sampler *sampler, f32 t) { size_t previous_index = 0; @@ -15,7 +15,6 @@ keyframe animation_sample(animation_sampler *sampler, f32 t) { } previous_time = current_time; previous_index = i; - } size_t next_index = (previous_index + 1) % sampler->animation.n_timestamps; @@ -26,13 +25,14 @@ keyframe animation_sample(animation_sampler *sampler, f32 t) { keyframe next_value = sampler->animation.values.values[next_index]; printf("%d %d\n", previous_index, next_index); - - f32 time_diff = sampler->animation.timestamps[next_index] - sampler->animation.timestamps[previous_index - ]; - f32 percent = (t - sampler->animation.timestamps[next_index]) / time_diff; - quat interpolated_rot = quat_slerp(sampler->animation.values.values[previous_index].rotation, - sampler->animation.values.values[next_index].rotation, percent); + f32 time_diff = + sampler->animation.timestamps[next_index] - sampler->animation.timestamps[previous_index]; + f32 percent = (t - sampler->animation.timestamps[next_index]) / time_diff; + + quat interpolated_rot = + quat_slerp(sampler->animation.values.values[previous_index].rotation, + sampler->animation.values.values[next_index].rotation, percent); - return (keyframe){ .rotation = interpolated_rot }; + return (keyframe){ .rotation = interpolated_rot }; } \ No newline at end of file diff --git a/src/resources/gltf.c b/src/resources/gltf.c index 6081e45..eade8e6 100644 --- a/src/resources/gltf.c +++ b/src/resources/gltf.c @@ -63,6 +63,14 @@ model_handle model_load_gltf(struct core *core, const char *path, bool invert_te return (model_handle){ .raw = index }; } +void assert_path_type_matches_component_type(cgltf_animation_path_type target_path, + cgltf_accessor *output) { + if (target_path == cgltf_animation_path_type_rotation) { + assert(output->component_type == cgltf_component_type_r_32f); + assert(output->type == cgltf_type_vec4); + } +} + // TODO: Brainstorm how I can make this simpler and break it up into more testable pieces bool model_load_gltf_str(const char *file_string, const char *filepath, str8 relative_path, @@ -175,7 +183,8 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel vec2_darray_push(tmp_uvs, tex); } } else if (attribute.type == cgltf_attribute_type_joints) { - // TODO: handle joints + cgltf_accessor *accessor = attribute.data; + assert(accessor->component_type == cgltf_component_type_r_32u); } else { WARN("Unhandled cgltf_attribute_type: %s. skipping..", attribute.name); } @@ -257,19 +266,32 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel animation_sampler *sampler = arena_alloc(arena, sizeof(animation_sampler)); - // animation_sampler **target_property; + animation_sampler **target_property; keyframe_kind data_type; switch (channel.target_path) { case cgltf_animation_path_type_rotation: - // target_property = &clip.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; + *target_property = sampler; sampler->current_index = 0; printf("1 %d index\n", sampler->current_index); @@ -277,48 +299,59 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel // keyframe times size_t n_frames = channel.sampler->input->count; - printf("n_frames: %d\n", n_frames); 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)"); - printf("2 %d index\n", sampler->current_index); f32 *times = arena_alloc(arena, n_frames * sizeof(f32)); - printf("3 %d index\n", sampler->current_index); sampler->animation.n_timestamps = n_frames; - printf("n_timestamps: %d\n", sampler->animation.n_timestamps); sampler->animation.timestamps = times; cgltf_accessor_unpack_floats(channel.sampler->input, times, n_frames); - - printf("4 %d index\n", sampler->current_index); - if (channel.target_path == cgltf_animation_path_type_rotation) { - assert(channel.sampler->output->component_type == cgltf_component_type_r_32f); - assert(channel.sampler->output->type == cgltf_type_vec4); - } + 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); - ERROR("N frames %d", n_frames); 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) { - 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; + 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); + // 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); -- cgit v1.2.3-70-g09d2 From d9f9479694d8a4d74822a876516282329db5ea3d Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sat, 6 Apr 2024 22:24:52 +1100 Subject: separate pipeline for skinned meshes --- assets/shaders/skinned.vert | 47 ++++++++ examples/gltf_loading/ex_gltf_loading.c | 1 - examples/skinned_animation/ex_skinned_animation.c | 4 +- src/animation.h | 6 + src/renderer/render.c | 134 +++++++++++++++++----- src/renderer/render.h | 2 + src/renderer/render_types.h | 18 +-- src/resources/gltf.c | 67 +++++++++-- 8 files changed, 234 insertions(+), 45 deletions(-) create mode 100644 assets/shaders/skinned.vert (limited to 'assets') diff --git a/assets/shaders/skinned.vert b/assets/shaders/skinned.vert new file mode 100644 index 0000000..3d0c2cd --- /dev/null +++ b/assets/shaders/skinned.vert @@ -0,0 +1,47 @@ +#version 410 core +// Inputs +layout (location = 0) in vec3 inPos; +layout (location = 1) in vec3 inNormal; +layout (location = 2) in vec2 inTexCoords; +layout (location = 3) in ivec4 inBoneIndices; +layout (location = 4) in vec4 inWeights; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; +uniform mat4 lightSpaceMatrix; + +const int MAX_BONES = 100; +const int MAX_BONE_INFLUENCES = 4; +uniform mat4 finalBoneMatrices[MAX_BONES]; + +// Output +out VS_OUT { + vec3 FragPos; + vec3 Normal; + vec2 TexCoords; + vec4 FragPosLightSpace; +} vs_out; + +void main() { + vec4 totalPosition = vec4(0.0f); + for(int i = 0 ; i < MAX_BONE_INFLUENCES ; i++) { + if(inBoneIndices[i] == -1) + continue; + if(inBoneIndices[i] >=MAX_BONES) + { + totalPosition = vec4(inPos,1.0f); + break; + } + vec4 localPosition = finalBoneMatrices[inBoneIndices[i]] * vec4(inPos,1.0f); + totalPosition += localPosition * inWeights[i]; + vec3 localNormal = mat3(finalBoneMatrices[inBoneIndices[i]]) * inNormal; + vs_out.Normal = localNormal; + } + + vs_out.FragPos = vec3(model * vec4(inPos, 1.0)); + // vs_out.Normal = inNormal; + vs_out.TexCoords = inTexCoords; + vs_out.FragPosLightSpace = lightSpaceMatrix * vec4(vs_out.FragPos, 1.0); + gl_Position = projection * view * model * totalPosition; +} \ No newline at end of file diff --git a/examples/gltf_loading/ex_gltf_loading.c b/examples/gltf_loading/ex_gltf_loading.c index 7b69127..5d53e78 100644 --- a/examples/gltf_loading/ex_gltf_loading.c +++ b/examples/gltf_loading/ex_gltf_loading.c @@ -56,7 +56,6 @@ int main() { scene our_scene = { .dir_light = dir_light, .n_point_lights = 4 }; memcpy(&our_scene.point_lights, &point_lights, sizeof(point_light[4])); - while (!glfwWindowShouldClose(core->renderer.window)) { currentFrame = glfwGetTime(); deltaTime = currentFrame - lastFrame; diff --git a/examples/skinned_animation/ex_skinned_animation.c b/examples/skinned_animation/ex_skinned_animation.c index 9efffc6..3138256 100644 --- a/examples/skinned_animation/ex_skinned_animation.c +++ b/examples/skinned_animation/ex_skinned_animation.c @@ -37,7 +37,7 @@ int main() { scene our_scene = make_default_scene(); - vec3 cam_pos = vec3_create(5, 5, 5); + vec3 cam_pos = vec3_create(0, 5, 8); 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), @@ -86,7 +86,7 @@ int main() { quat rot = quat_ident(); transform tf = transform_create(VEC3_ZERO, rot, 1.0); - draw_model(&core->renderer, &game.camera, cube, tf, &our_scene); + draw_skinned_model(&core->renderer, &game.camera, cube, tf, &our_scene); // gfx_backend_draw_frame(&core->renderer, &game.camera, model, NULL); diff --git a/src/animation.h b/src/animation.h index 81e150a..18d3ba9 100644 --- a/src/animation.h +++ b/src/animation.h @@ -28,6 +28,12 @@ typedef struct keyframes { size_t count; } keyframes; +typedef struct joint { + char* name; // optional + transform transform_components; + mat4 local_transform; +} joint; + typedef struct animation_spline { f32* timestamps; size_t n_timestamps; diff --git a/src/renderer/render.c b/src/renderer/render.c index 806979d..e420043 100644 --- a/src/renderer/render.c +++ b/src/renderer/render.c @@ -1,3 +1,7 @@ +#include +#include +#include +#include "maths_types.h" #include "mem.h" #define STB_IMAGE_IMPLEMENTATION #include @@ -59,6 +63,9 @@ bool renderer_init(renderer* ren) { ren->blinn_phong = shader_create_separate("assets/shaders/blinn_phong.vert", "assets/shaders/blinn_phong.frag"); + ren->skinned = + shader_create_separate("assets/shaders/skinned.vert", "assets/shaders/blinn_phong.frag"); + default_material_init(); return true; @@ -158,6 +165,59 @@ void draw_mesh(renderer* ren, mesh* mesh, transform tf, material* mat, mat4* vie draw_primitives(CEL_PRIMITIVE_TOPOLOGY_TRIANGLE, 0, num_vertices); } +void draw_skinned_mesh(renderer* ren, mesh* mesh, transform tf, material* mat, mat4* view, + mat4* proj) { + shader lighting_shader = ren->skinned; + + // bind buffer + bind_mesh_vertex_buffer(ren->backend_state, mesh); + + // bind textures + bind_texture(lighting_shader, &mat->diffuse_texture, 0); // bind to slot 0 + bind_texture(lighting_shader, &mat->specular_texture, 1); // bind to slot 1 + uniform_f32(lighting_shader.program_id, "material.shininess", 32.); + + // upload model transform + mat4 trans = mat4_translation(tf.position); + mat4 rot = mat4_rotation(tf.rotation); + mat4 scale = mat4_scale(tf.scale); + mat4 model_tf = mat4_mult(trans, mat4_mult(rot, scale)); + + uniform_mat4f(lighting_shader.program_id, "model", &model_tf); + // upload view & projection matrices + uniform_mat4f(lighting_shader.program_id, "view", view); + uniform_mat4f(lighting_shader.program_id, "projection", proj); + + // draw triangles + u32 num_vertices = vertex_darray_len(mesh->vertices); + draw_primitives(CEL_PRIMITIVE_TOPOLOGY_TRIANGLE, 0, num_vertices); +} + +void draw_skinned_model(renderer* ren, camera* cam, model* model, transform tf, scene* scene) { + mat4 view; + mat4 proj; + camera_view_projection(cam, SCR_HEIGHT, SCR_WIDTH, &view, &proj); + + set_shader(ren->skinned); + + // set camera uniform + uniform_vec3f(ren->blinn_phong.program_id, "viewPos", &cam->position); + // set light uniforms + dir_light_upload_uniforms(ren->blinn_phong, &scene->dir_light); + for (int i = 0; i < scene->n_point_lights; i++) { + point_light_upload_uniforms(ren->blinn_phong, &scene->point_lights[i], '0' + i); + } + + for (size_t i = 0; i < mesh_darray_len(model->meshes); i++) { + mesh* m = &model->meshes->data[i]; + if (vertex_darray_len(m->vertices) == 0) { + continue; + } + material* mat = &model->materials->data[m->material_index]; + draw_skinned_mesh(ren, m, tf, mat, &view, &proj); + } +} + void model_upload_meshes(renderer* ren, model* model) { INFO("Upload mesh vertex data to GPU for model %s", model->name); @@ -173,6 +233,7 @@ void model_upload_meshes(renderer* ren, model* model) { // upload each mesh to the GPU for (int mesh_i = 0; mesh_i < num_meshes; mesh_i++) { + mesh mesh = model->meshes->data[mesh_i]; model->meshes->data[mesh_i].vao = VAOs[mesh_i]; model->meshes->data[mesh_i].vbo = VBOs[mesh_i]; // 3. bind buffers @@ -185,34 +246,51 @@ void model_upload_meshes(renderer* ren, model* model) { // TODO: convert this garbage into a function f32 verts[num_vertices * 8]; // for each face - for (int i = 0; i < (num_vertices / 3); i++) { - // for each vert in face - for (int j = 0; j < 3; j++) { - size_t stride = (i * 24) + j * 8; - // printf("i: %d, stride: %ld, loc %d\n", i, stride, i * 3 + j); - vertex vert = model->meshes->data[mesh_i].vertices->data[i]; - // printf("pos %f %f %f\n", vert.position.x, vert.position.y, vert.position.z); - // printf("norm %f %f %f\n", vert.normal.x, vert.normal.y, vert.normal.z); - // printf("tex %f %f\n", vert.uv.x, vert.uv.y); - verts[stride + 0] = - ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].position.x; - verts[stride + 1] = - ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].position.y; - verts[stride + 2] = - ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].position.z; - verts[stride + 3] = - ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].normal.x; - verts[stride + 4] = - ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].normal.y; - verts[stride + 5] = - ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].normal.z; - verts[stride + 6] = ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].uv.x; - verts[stride + 7] = ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].uv.y; - } + // for (int i = 0; i < (num_vertices / 3); i++) { + // // for each vert in face + // for (int j = 0; j < 3; j++) { + // size_t stride = (i * 24) + j * 8; + // // printf("i: %d, stride: %ld, loc %d\n", i, stride, i * 3 + j); + // vertex vert = model->meshes->data[mesh_i].vertices->data[i]; + // // printf("pos %f %f %f\n", vert.position.x, vert.position.y, vert.position.z); + // // printf("norm %f %f %f\n", vert.normal.x, vert.normal.y, vert.normal.z); + // // printf("tex %f %f\n", vert.uv.x, vert.uv.y); + // verts[stride + 0] = + // ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].position.x; + // verts[stride + 1] = + // ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].position.y; + // verts[stride + 2] = + // ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].position.z; + // verts[stride + 3] = + // ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].normal.x; + // verts[stride + 4] = + // ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].normal.y; + // verts[stride + 5] = + // ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].normal.z; + // verts[stride + 6] = ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + + // j].uv.x; verts[stride + 7] = ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + // + j].uv.y; + // } + // } + size_t vertex_size = mesh.is_skinned + ? sizeof(vec3) * 2 + sizeof(vec2) + sizeof(u32) * 4 + sizeof(vec4) + : sizeof(vec3) * 2 + sizeof(vec2); + if (!mesh.is_skinned) { + printf("sizeof(vertex) -> %ld, vertex_size -> %ld\n", sizeof(vertex), vertex_size); + assert(vertex_size == sizeof(vertex)); + } + size_t buffer_size = vertex_size * num_vertices; + u8* bytes = malloc(buffer_size); + + for (int i = 0; i < num_vertices; i++) { + u8* p = bytes + vertex_size * i; + u8* bone_data_offset = p + sizeof(u32) * 4 + sizeof(vec4); + memcpy(p, &mesh.vertices->data[i], sizeof(vertex)); + memcpy(bone_data_offset, &mesh.vertex_bone_data->data[i], sizeof(vertex_bone_data)); } // 4. upload data - glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, buffer_size, bytes, GL_STATIC_DRAW); // 5. cont. set mesh vertex layout glBindVertexArray(model->meshes->data[mesh_i].vao); @@ -225,6 +303,10 @@ void model_upload_meshes(renderer* ren, model* model) { // tex coords glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); glEnableVertexAttribArray(2); + + // skinning (optional) + if (mesh.is_skinned) { + } } INFO("Uploaded %d submeshes with a total of %d vertices\n", num_meshes, total_verts); @@ -242,7 +324,7 @@ texture texture_data_load(const char* path, bool invert_y) { stbi_set_flip_vertically_on_load(invert_y); #pragma GCC diagnostic ignored "-Wpointer-sign" - char* data = stbi_load(path, &width, &height, &num_channels, 0); // STBI_rgb_alpha); + char* data = stbi_load(path, &width, &height, &num_channels, 0); // STBI_rgb_alpha); if (data) { DEBUG("loaded texture: %s", path); } else { diff --git a/src/renderer/render.h b/src/renderer/render.h index f0118b6..ba2cbdc 100644 --- a/src/renderer/render.h +++ b/src/renderer/render.h @@ -35,6 +35,8 @@ void model_upload_meshes(renderer* ren, model* model); void draw_model(renderer* ren, camera* camera, model* model, transform tf, scene* scene); void draw_mesh(renderer* ren, mesh* mesh, transform tf, material* mat, mat4* view, mat4* proj); +void draw_skinned_model(renderer* ren, camera* cam, model* model, transform tf, scene* scene); + void model_destroy(model* model); // --- diff --git a/src/renderer/render_types.h b/src/renderer/render_types.h index e4d3127..61571e1 100644 --- a/src/renderer/render_types.h +++ b/src/renderer/render_types.h @@ -46,6 +46,7 @@ typedef struct renderer { renderer_config config; // shaders shader blinn_phong; + shader skinned; } renderer; // --- Lighting & Materials @@ -118,24 +119,25 @@ typedef struct vertex { vec2 uv; } vertex; -typedef struct skinned_vertex { - vec3 position; - vec3 normal; - vec2 uv; +typedef struct vertex_bone_data { vec4i joints; /** @brief 4 indices of joints that influence vectors position */ vec4 weights; /** @brief weight (0,1) of each joint */ -} skinned_vertex; +} vertex_bone_data; +#include "animation.h" #ifndef TYPED_VERTEX_ARRAY KITC_DECL_TYPED_ARRAY(vertex) // creates "vertex_darray" -KITC_DECL_TYPED_ARRAY(skinned_vertex) // creates "skinned_vertex_darray" +KITC_DECL_TYPED_ARRAY(vertex_bone_data) // creates "skinned_vertex_darray" +KITC_DECL_TYPED_ARRAY(joint) #define TYPED_VERTEX_ARRAY #endif + typedef struct mesh { vertex_darray* vertices; - // skinned_vertex_darray* skinned_vertices; // only used if model needs it - // bool is_skinned; + vertex_bone_data_darray* vertex_bone_data; // only used if model needs it + joint_darray* bones; + bool is_skinned; u32 vertex_size; /** size in bytes of each vertex including necessary padding */ bool has_indices; u32 *indices; diff --git a/src/resources/gltf.c b/src/resources/gltf.c index 7fbef3b..c7c1f55 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.h" #include "maths_types.h" #include "mem.h" #include "path.h" @@ -30,6 +31,7 @@ KITC_DECL_TYPED_ARRAY(u32) KITC_DECL_TYPED_ARRAY(vec4i) KITC_DECL_TYPED_ARRAY(vec4) KITC_DECL_TYPED_ARRAY(face) +// KITC_DECL_TYPED_ARRAY(joint) bool model_load_gltf_str(const char *file_string, const char *filepath, str8 relative_path, model *out_model, bool invert_textures_y); @@ -84,9 +86,9 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel vec3_darray *tmp_positions = vec3_darray_new(1000); 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 = vec4i_darray_new(1000); + vec4i_darray *tmp_joint_indices = vec4i_darray_new(1000); vec4_darray *tmp_weights = vec4_darray_new(1000); + joint_darray *tmp_joints = joint_darray_new(256); cgltf_options options = { 0 }; cgltf_data *data = NULL; @@ -112,7 +114,33 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel if (is_skinned) { cgltf_skin *gltf_skin = data->skins; - TRACE("loading skin %s", gltf_skin->name); + DEBUG("loading skin %s", gltf_skin->name); + size_t num_joints = gltf_skin->joints_count; + DEBUG("# Joints %d", num_joints); + + // for each one we'll spit out a joint + for (size_t i = 0; i < num_joints; i++) { + cgltf_node *joint_node = gltf_skin->joints[i]; + + joint joint_i = { .name = "testjoint" }; + if (joint_node->children_count > 0 && !joint_node->has_translation && + !joint_node->has_rotation) { + WARN("joint Node with index %d is the root node", i); + joint_i.transform_components = TRANSFORM_DEFAULT; + } else { + TRACE("Storing joint transform"); + joint_i.transform_components = TRANSFORM_DEFAULT; + if (joint_node->has_translation) { + memcpy(&joint_i.transform_components.position, &joint_node->translation, 3 * sizeof(f32)); + } + if (joint_node->has_rotation) { + memcpy(&joint_i.transform_components.rotation, &joint_node->rotation, 4 * sizeof(f32)); + } + // TODO: support scaling as vec instead of float + } + joint_i.local_transform = transform_to_mat(&joint_i.transform_components); + joint_darray_push(tmp_joints, joint_i); + } } // --- Materials @@ -218,9 +246,9 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel joint_indices.y = (u32)joints_as_floats.y; joint_indices.z = (u32)joints_as_floats.z; joint_indices.w = (u32)joints_as_floats.w; - printf("Joints affecting %d %d %d %d\n", joint_indices.x, joint_indices.y, - joint_indices.z, joint_indices.w); - vec4i_darray_push(tmp_joints, joint_indices); + // printf("Joints affecting %d %d %d %d\n", joint_indices.x, joint_indices.y, + // joint_indices.z, joint_indices.w); + vec4i_darray_push(tmp_joint_indices, joint_indices); } } else if (attribute.type == cgltf_attribute_type_weights) { @@ -232,7 +260,7 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel for (cgltf_size v = 0; v < accessor->count; ++v) { vec4 weights; cgltf_accessor_read_float(accessor, v, &weights.x, 4); - printf("Weights affecting %f %f %f %f\n", weights.x, weights.y, weights.z, weights.w); + // printf("Weights affecting %f %f %f %f\n", weights.x, weights.y, weights.z, weights.w); vec4_darray_push(tmp_weights, weights); } } else { @@ -240,7 +268,7 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel } } - mesh mesh = {0}; + mesh mesh = { 0 }; mesh.vertices = vertex_darray_new(10); cgltf_accessor *indices = primitive.indices; @@ -282,7 +310,30 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel } } + if (is_skinned) { + mesh.vertex_bone_data = vertex_bone_data_darray_new(tmp_joint_indices->len); + mesh.bones = joint_darray_new(tmp_joints->len); + for (int i = 0; i < tmp_joint_indices->len; i++) { + vertex_bone_data data; + data.joints = tmp_joint_indices->data[i]; + data.weights =tmp_weights->data[i]; + vertex_bone_data_darray_push(mesh.vertex_bone_data, data); + } + for (int i = 0; i < tmp_joints->len; i++) { + joint data = tmp_joints->data[i]; + joint_darray_push(mesh.bones, data); + } + } + mesh_darray_push(out_model->meshes, mesh); + + // clear data for each mesh + vec3_darray_clear(tmp_positions); + vec3_darray_clear(tmp_normals); + vec2_darray_free(tmp_uvs); + vec4i_darray_clear(tmp_joint_indices); + vec4_darray_clear(tmp_weights); + joint_darray_clear(tmp_joints); } for (int i = 0; i < out_model->meshes->len; i++) { -- cgit v1.2.3-70-g09d2 From 42924fe7b32e93bf55ce034467ceb52e0436c303 Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 7 Apr 2024 13:50:40 +1000 Subject: visualising bone indices --- assets/shaders/blinn_phong.frag | 4 +- assets/shaders/skinned.vert | 46 ++++++++----- examples/skinned_animation/ex_skinned_animation.c | 25 +++++-- src/animation.h | 8 +++ src/maths/maths_types.h | 6 +- src/renderer/render.c | 62 ++++++++++++----- src/renderer/render_types.h | 2 +- src/resources/gltf.c | 83 +++++++++++++---------- 8 files changed, 158 insertions(+), 78 deletions(-) (limited to 'assets') diff --git a/assets/shaders/blinn_phong.frag b/assets/shaders/blinn_phong.frag index 095b19a..adcc53e 100644 --- a/assets/shaders/blinn_phong.frag +++ b/assets/shaders/blinn_phong.frag @@ -33,6 +33,7 @@ in VS_OUT { vec3 Normal; vec2 TexCoords; vec4 FragPosLightSpace; + vec4 Color; } fs_in; // --- Uniforms @@ -55,7 +56,8 @@ void main() { result += CalcPointLight(pointLights[i], norm, fs_in.FragPos, viewDir); } - FragColor = vec4(result, 1.0); + // FragColor = vec4(result, 1.0); + FragColor = fs_in.Color + 0.5; } vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir) diff --git a/assets/shaders/skinned.vert b/assets/shaders/skinned.vert index 3d0c2cd..cb2ca85 100644 --- a/assets/shaders/skinned.vert +++ b/assets/shaders/skinned.vert @@ -13,7 +13,7 @@ uniform mat4 lightSpaceMatrix; const int MAX_BONES = 100; const int MAX_BONE_INFLUENCES = 4; -uniform mat4 finalBoneMatrices[MAX_BONES]; +uniform mat4 boneMatrices[MAX_BONES]; // TODO! // Output out VS_OUT { @@ -21,27 +21,39 @@ out VS_OUT { vec3 Normal; vec2 TexCoords; vec4 FragPosLightSpace; + vec4 Color; } vs_out; void main() { - vec4 totalPosition = vec4(0.0f); - for(int i = 0 ; i < MAX_BONE_INFLUENCES ; i++) { - if(inBoneIndices[i] == -1) - continue; - if(inBoneIndices[i] >=MAX_BONES) - { - totalPosition = vec4(inPos,1.0f); - break; - } - vec4 localPosition = finalBoneMatrices[inBoneIndices[i]] * vec4(inPos,1.0f); - totalPosition += localPosition * inWeights[i]; - vec3 localNormal = mat3(finalBoneMatrices[inBoneIndices[i]]) * inNormal; - vs_out.Normal = localNormal; - } - - vs_out.FragPos = vec3(model * vec4(inPos, 1.0)); + // vec4 totalPosition = vec4(0.0f); + // for(int i = 0 ; i < MAX_BONE_INFLUENCES ; i++) { + // if(inBoneIndices[i] == 0) + // continue; + // if(inBoneIndices[i] >=MAX_BONES) + // { + // totalPosition = vec4(inPos,1.0f); + // break; + // } + // vec4 localPosition = finalBoneMatrices[inBoneIndices[i]] * vec4(inPos,1.0f); + // totalPosition += localPosition * inWeights[i]; + // vec3 localNormal = mat3(finalBoneMatrices[inBoneIndices[i]]) * inNormal; + // vs_out.Normal = localNormal; + // } + + + mat4 skinMatrix = // mat4(1.0) + // boneMatrices[int(inBoneIndices.z)]; // should just be the identtiy + inWeights.x * boneMatrices[int(inBoneIndices.x)] + + inWeights.y * boneMatrices[int(inBoneIndices.y)] + + inWeights.z * boneMatrices[int(inBoneIndices.z)] + + inWeights.w * boneMatrices[int(inBoneIndices.w)]; + + vec4 totalPosition = skinMatrix * vec4(inPos, 1.0); + + vs_out.FragPos = vec3(model * totalPosition); // vs_out.Normal = inNormal; vs_out.TexCoords = inTexCoords; vs_out.FragPosLightSpace = lightSpaceMatrix * vec4(vs_out.FragPos, 1.0); + vs_out.Color = inWeights; gl_Position = projection * view * model * totalPosition; } \ No newline at end of file diff --git a/examples/skinned_animation/ex_skinned_animation.c b/examples/skinned_animation/ex_skinned_animation.c index 3138256..43eb715 100644 --- a/examples/skinned_animation/ex_skinned_animation.c +++ b/examples/skinned_animation/ex_skinned_animation.c @@ -1,3 +1,4 @@ +#include #include #include "../example_scene.h" @@ -30,10 +31,22 @@ int main() { core* core = core_bringup(); - model_handle animated_cube_handle = + model_handle 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); + + model* simple_skin = &core->models->data[handle.raw]; + + // Okay, right here we've loaded the model. let's assert some facts + assert(simple_skin->animations->len == 1); + assert(simple_skin->animations->data[0].rotation != NULL); + assert(simple_skin->animations->data[0].translation == NULL); + assert(simple_skin->animations->data[0].scale == NULL); + + mesh* m = &simple_skin->meshes->data[0]; + assert(m->is_skinned); + assert(m->bones->len == 2); // 1 root and 1 extra joint + + model_upload_meshes(&core->renderer, simple_skin); scene our_scene = make_default_scene(); @@ -48,7 +61,7 @@ int main() { // Main loop const f32 camera_lateral_speed = 0.2; - const f32 camera_zoom_speed = 0.15; + const f32 camera_zoom_speed = 0.10; // animation // animation_clip track = cube->animations->data[0]; @@ -86,7 +99,7 @@ int main() { quat rot = quat_ident(); transform tf = transform_create(VEC3_ZERO, rot, 1.0); - draw_skinned_model(&core->renderer, &game.camera, cube, tf, &our_scene); + draw_skinned_model(&core->renderer, &game.camera, simple_skin, tf, &our_scene); // gfx_backend_draw_frame(&core->renderer, &game.camera, model, NULL); @@ -94,7 +107,7 @@ int main() { } INFO("Shutting down"); - model_destroy(cube); + model_destroy(simple_skin); core_shutdown(core); diff --git a/src/animation.h b/src/animation.h index 18d3ba9..9d5d03b 100644 --- a/src/animation.h +++ b/src/animation.h @@ -31,6 +31,7 @@ typedef struct keyframes { typedef struct joint { char* name; // optional transform transform_components; + mat4 inverse_bind_matrix; mat4 local_transform; } joint; @@ -61,4 +62,11 @@ typedef struct animation_clip { animation_sampler* weights; } animation_clip; +typedef struct skinned_animation { + mat4* joint_matrices; + size_t n_joints; +} skinned_animation; + +// void animation_update_joint_matrices(animation_clip* ) + void animation_play(animation_clip* clip); \ No newline at end of file diff --git a/src/maths/maths_types.h b/src/maths/maths_types.h index 6d38fc7..53cac55 100644 --- a/src/maths/maths_types.h +++ b/src/maths/maths_types.h @@ -64,4 +64,8 @@ typedef struct transform { typedef struct vec4i { i32 x, y, z, w; -} vec4i; \ No newline at end of file +} vec4i; + +typedef struct vec4u { + u32 x, y, z, w; +} vec4u; \ No newline at end of file diff --git a/src/renderer/render.c b/src/renderer/render.c index e420043..42f6ee4 100644 --- a/src/renderer/render.c +++ b/src/renderer/render.c @@ -97,6 +97,7 @@ void default_material_init() { } void model_destroy(model* model) { + TRACE("Freeing all data for model %s", model->name); arena_free_all(&model->animation_data_arena); arena_free_storage(&model->animation_data_arena); mesh_darray_free(model->meshes); @@ -175,19 +176,31 @@ void draw_skinned_mesh(renderer* ren, mesh* mesh, transform tf, material* mat, m // bind textures bind_texture(lighting_shader, &mat->diffuse_texture, 0); // bind to slot 0 bind_texture(lighting_shader, &mat->specular_texture, 1); // bind to slot 1 - uniform_f32(lighting_shader.program_id, "material.shininess", 32.); - // upload model transform + // Uniforms + uniform_f32(lighting_shader.program_id, "material.shininess", 32.); mat4 trans = mat4_translation(tf.position); mat4 rot = mat4_rotation(tf.rotation); mat4 scale = mat4_scale(tf.scale); mat4 model_tf = mat4_mult(trans, mat4_mult(rot, scale)); - uniform_mat4f(lighting_shader.program_id, "model", &model_tf); - // upload view & projection matrices uniform_mat4f(lighting_shader.program_id, "view", view); uniform_mat4f(lighting_shader.program_id, "projection", proj); + // bone transforms + size_t n_bones = mesh->bones->len; + + // for now assume correct ordering + mat4* bone_transforms = malloc(n_bones * sizeof(mat4)); + for (int bone_i = 0; bone_i < n_bones; bone_i++) { + bone_transforms[bone_i] = mat4_ident(); + } + + glUniformMatrix4fv(glGetUniformLocation(lighting_shader.program_id, "boneMatrices"), n_bones, + GL_FALSE, &bone_transforms->data[0]); + + free(bone_transforms); + // draw triangles u32 num_vertices = vertex_darray_len(mesh->vertices); draw_primitives(CEL_PRIMITIVE_TOPOLOGY_TRIANGLE, 0, num_vertices); @@ -201,11 +214,11 @@ void draw_skinned_model(renderer* ren, camera* cam, model* model, transform tf, set_shader(ren->skinned); // set camera uniform - uniform_vec3f(ren->blinn_phong.program_id, "viewPos", &cam->position); + uniform_vec3f(ren->skinned.program_id, "viewPos", &cam->position); // set light uniforms - dir_light_upload_uniforms(ren->blinn_phong, &scene->dir_light); + dir_light_upload_uniforms(ren->skinned, &scene->dir_light); for (int i = 0; i < scene->n_point_lights; i++) { - point_light_upload_uniforms(ren->blinn_phong, &scene->point_lights[i], '0' + i); + point_light_upload_uniforms(ren->skinned, &scene->point_lights[i], '0' + i); } for (size_t i = 0; i < mesh_darray_len(model->meshes); i++) { @@ -213,7 +226,8 @@ void draw_skinned_model(renderer* ren, camera* cam, model* model, transform tf, if (vertex_darray_len(m->vertices) == 0) { continue; } - material* mat = &model->materials->data[m->material_index]; + // material* mat = &model->materials->data[m->material_index]; + material* mat = &DEFAULT_MATERIAL; draw_skinned_mesh(ren, m, tf, mat, &view, &proj); } } @@ -272,21 +286,28 @@ void model_upload_meshes(renderer* ren, model* model) { // + j].uv.y; // } // } - size_t vertex_size = mesh.is_skinned - ? sizeof(vec3) * 2 + sizeof(vec2) + sizeof(u32) * 4 + sizeof(vec4) - : sizeof(vec3) * 2 + sizeof(vec2); - if (!mesh.is_skinned) { - printf("sizeof(vertex) -> %ld, vertex_size -> %ld\n", sizeof(vertex), vertex_size); + size_t static_vertex_size = 2 * sizeof(vec3) + sizeof(vec2); + size_t skinned_vertex_size = 2 * sizeof(vec3) + sizeof(vec2) + 4 * sizeof(u32) + sizeof(vec4); + size_t vertex_size = mesh.is_skinned ? skinned_vertex_size : static_vertex_size; + + TRACE("sizeof(vertex) -> %ld, vertex_size -> %ld\n", sizeof(vertex), vertex_size); + if (mesh.is_skinned) { + assert(vertex_size == (12 + 12 + 8 + 16 + 16)); + } else { assert(vertex_size == sizeof(vertex)); + assert(vertex_size == 8 * sizeof(float)); } size_t buffer_size = vertex_size * num_vertices; u8* bytes = malloc(buffer_size); for (int i = 0; i < num_vertices; i++) { u8* p = bytes + vertex_size * i; - u8* bone_data_offset = p + sizeof(u32) * 4 + sizeof(vec4); + u8* bone_data_offset = p + static_vertex_size; memcpy(p, &mesh.vertices->data[i], sizeof(vertex)); - memcpy(bone_data_offset, &mesh.vertex_bone_data->data[i], sizeof(vertex_bone_data)); + if (mesh.is_skinned) { + // printf("") + memcpy(bone_data_offset, &mesh.vertex_bone_data->data[i], sizeof(vertex_bone_data)); + } } // 4. upload data @@ -295,17 +316,22 @@ void model_upload_meshes(renderer* ren, model* model) { // 5. cont. set mesh vertex layout glBindVertexArray(model->meshes->data[mesh_i].vao); // position attribute - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vertex_size, (void*)0); glEnableVertexAttribArray(0); // normal vector attribute - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, vertex_size, (void*)(3 * sizeof(float))); glEnableVertexAttribArray(1); // tex coords - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, vertex_size, (void*)(6 * sizeof(float))); glEnableVertexAttribArray(2); // skinning (optional) if (mesh.is_skinned) { + glEnableVertexAttribArray(3); + glVertexAttribIPointer(3, 4, GL_INT, vertex_size, (void*)(8 * sizeof(float))); + + glEnableVertexAttribArray(4); + glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, vertex_size, (void*)(12 * sizeof(float))); } } diff --git a/src/renderer/render_types.h b/src/renderer/render_types.h index 61571e1..bfed18d 100644 --- a/src/renderer/render_types.h +++ b/src/renderer/render_types.h @@ -120,7 +120,7 @@ typedef struct vertex { } vertex; typedef struct vertex_bone_data { - vec4i joints; /** @brief 4 indices of joints that influence vectors position */ + vec4u joints; /** @brief 4 indices of joints that influence vectors position */ vec4 weights; /** @brief weight (0,1) of each joint */ } vertex_bone_data; diff --git a/src/resources/gltf.c b/src/resources/gltf.c index c7c1f55..7efd2bb 100644 --- a/src/resources/gltf.c +++ b/src/resources/gltf.c @@ -28,7 +28,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(vec4u) KITC_DECL_TYPED_ARRAY(vec4) KITC_DECL_TYPED_ARRAY(face) // KITC_DECL_TYPED_ARRAY(joint) @@ -86,9 +86,10 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel vec3_darray *tmp_positions = vec3_darray_new(1000); vec3_darray *tmp_normals = vec3_darray_new(1000); vec2_darray *tmp_uvs = vec2_darray_new(1000); - vec4i_darray *tmp_joint_indices = vec4i_darray_new(1000); + vec4u_darray *tmp_joint_indices = vec4u_darray_new(1000); vec4_darray *tmp_weights = vec4_darray_new(1000); 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; @@ -118,6 +119,8 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel size_t num_joints = gltf_skin->joints_count; DEBUG("# Joints %d", num_joints); + cgltf_accessor *gltf_inverse_bind_matrices = gltf_skin->inverse_bind_matrices; + // for each one we'll spit out a joint for (size_t i = 0; i < num_joints; i++) { cgltf_node *joint_node = gltf_skin->joints[i]; @@ -139,6 +142,8 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel // TODO: support scaling as vec instead of float } joint_i.local_transform = transform_to_mat(&joint_i.transform_components); + cgltf_accessor_read_float(gltf_inverse_bind_matrices, i, &joint_i.inverse_bind_matrix.data[0], + 16); joint_darray_push(tmp_joints, joint_i); } } @@ -238,7 +243,7 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel cgltf_accessor *accessor = attribute.data; assert(accessor->component_type == cgltf_component_type_r_16u); assert(accessor->type == cgltf_type_vec4); - vec4i joint_indices; + vec4u joint_indices; vec4 joints_as_floats; for (cgltf_size v = 0; v < accessor->count; ++v) { cgltf_accessor_read_float(accessor, v, &joints_as_floats.x, 4); @@ -246,9 +251,9 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel joint_indices.y = (u32)joints_as_floats.y; joint_indices.z = (u32)joints_as_floats.z; joint_indices.w = (u32)joints_as_floats.w; - // printf("Joints affecting %d %d %d %d\n", joint_indices.x, joint_indices.y, - // joint_indices.z, joint_indices.w); - vec4i_darray_push(tmp_joint_indices, joint_indices); + printf("Joints affecting vertex %d : %d %d %d %d\n", v, joint_indices.x, joint_indices.y, + joint_indices.z, joint_indices.w); + vec4u_darray_push(tmp_joint_indices, joint_indices); } } else if (attribute.type == cgltf_attribute_type_weights) { @@ -260,7 +265,8 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel for (cgltf_size v = 0; v < accessor->count; ++v) { vec4 weights; cgltf_accessor_read_float(accessor, v, &weights.x, 4); - // printf("Weights affecting %f %f %f %f\n", weights.x, weights.y, weights.z, weights.w); + printf("Weights affecting vertex %d : %f %f %f %f\n", v, weights.x, weights.y, weights.z, + weights.w); vec4_darray_push(tmp_weights, weights); } } else { @@ -270,9 +276,38 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel mesh mesh = { 0 }; mesh.vertices = vertex_darray_new(10); + mesh.vertex_bone_data =vertex_bone_data_darray_new(1); + + if (primitive.material != NULL) { + for (int 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) { + TRACE("Found material"); + mesh.material_index = i; + break; + } + } + } + + if (is_skinned) { + mesh.is_skinned = true; + // mesh.vertex_bone_data = vertex_bone_data_darray_new(tmp_joint_indices->len); + mesh.bones = joint_darray_new(tmp_joints->len); + for (int i = 0; i < tmp_joint_indices->len; i++) { + vertex_bone_data data; + data.joints = tmp_joint_indices->data[i]; + data.weights = tmp_weights->data[i]; + vertex_bone_data_darray_push(tmp_vertex_bone_data, data); // Push the temp data that aligns with raw vertices + } + for (int i = 0; i < tmp_joints->len; i++) { + joint data = tmp_joints->data[i]; + joint_darray_push(mesh.bones, data); + } + } cgltf_accessor *indices = primitive.indices; if (primitive.indices > 0) { + WARN("indices!"); mesh.has_indices = true; mesh.indices = malloc(indices->count * sizeof(u32)); @@ -293,45 +328,25 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel 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 { mesh.has_indices = false; return false; // TODO } - if (primitive.material != NULL) { - for (int 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) { - TRACE("Found material"); - mesh.material_index = i; - break; - } - } - } - - if (is_skinned) { - mesh.vertex_bone_data = vertex_bone_data_darray_new(tmp_joint_indices->len); - mesh.bones = joint_darray_new(tmp_joints->len); - for (int i = 0; i < tmp_joint_indices->len; i++) { - vertex_bone_data data; - data.joints = tmp_joint_indices->data[i]; - data.weights =tmp_weights->data[i]; - vertex_bone_data_darray_push(mesh.vertex_bone_data, data); - } - for (int i = 0; i < tmp_joints->len; i++) { - joint data = tmp_joints->data[i]; - joint_darray_push(mesh.bones, data); - } - } - mesh_darray_push(out_model->meshes, mesh); // clear data for each mesh vec3_darray_clear(tmp_positions); vec3_darray_clear(tmp_normals); vec2_darray_free(tmp_uvs); - vec4i_darray_clear(tmp_joint_indices); + vec4u_darray_clear(tmp_joint_indices); vec4_darray_clear(tmp_weights); joint_darray_clear(tmp_joints); } -- cgit v1.2.3-70-g09d2 From 1fc69f183bf8f105048b9d47e096ccd402107bbb Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 21 Apr 2024 12:34:00 +1000 Subject: misc cleanup --- assets/shaders/blinn_phong.frag | 2 +- assets/shaders/blinn_phong.vert | 2 + examples/gltf_loading/ex_gltf_loading.c | 2 +- examples/obj_loading/ex_obj_loading.c | 2 +- src/animation.h | 2 +- src/maths/maths.h | 65 ++++++++++++++++----------------- src/renderer/render.c | 31 +--------------- src/renderer/render_types.h | 9 ++--- 8 files changed, 42 insertions(+), 73 deletions(-) (limited to 'assets') diff --git a/assets/shaders/blinn_phong.frag b/assets/shaders/blinn_phong.frag index adcc53e..a0ba905 100644 --- a/assets/shaders/blinn_phong.frag +++ b/assets/shaders/blinn_phong.frag @@ -56,7 +56,7 @@ void main() { result += CalcPointLight(pointLights[i], norm, fs_in.FragPos, viewDir); } - // FragColor = vec4(result, 1.0); +// FragColor = vec4(result, 1.0); FragColor = fs_in.Color + 0.5; } diff --git a/assets/shaders/blinn_phong.vert b/assets/shaders/blinn_phong.vert index 6028178..06dc5e7 100644 --- a/assets/shaders/blinn_phong.vert +++ b/assets/shaders/blinn_phong.vert @@ -15,6 +15,7 @@ out VS_OUT { vec3 Normal; vec2 TexCoords; vec4 FragPosLightSpace; + vec4 Color; } vs_out; void main() { @@ -22,5 +23,6 @@ void main() { vs_out.Normal = inNormal; vs_out.TexCoords = inTexCoords; vs_out.FragPosLightSpace = lightSpaceMatrix * vec4(vs_out.FragPos, 1.0); + vs_out.Color = vec4(1.0); gl_Position = projection * view * model * vec4(inPos, 1.0); } \ No newline at end of file diff --git a/examples/gltf_loading/ex_gltf_loading.c b/examples/gltf_loading/ex_gltf_loading.c index b181426..900cf1b 100644 --- a/examples/gltf_loading/ex_gltf_loading.c +++ b/examples/gltf_loading/ex_gltf_loading.c @@ -56,7 +56,7 @@ int main() { scene our_scene = { .dir_light = dir_light, .n_point_lights = 4 }; memcpy(&our_scene.point_lights, &point_lights, sizeof(point_light[4])); - while (!glfwWindowShouldClose(core->renderer.window)) { + while (!should_exit(core)) { currentFrame = glfwGetTime(); deltaTime = currentFrame - lastFrame; lastFrame = currentFrame; diff --git a/examples/obj_loading/ex_obj_loading.c b/examples/obj_loading/ex_obj_loading.c index aaed2a0..906816b 100644 --- a/examples/obj_loading/ex_obj_loading.c +++ b/examples/obj_loading/ex_obj_loading.c @@ -55,7 +55,7 @@ int main() { memcpy(&our_scene.point_lights, &point_lights, sizeof(point_light[4])); // --- Enter Main loop - while (!glfwWindowShouldClose(core->renderer.window)) { + while (!should_exit(core)) { input_update(&core->input); threadpool_process_results(&core->threadpool, 1); diff --git a/src/animation.h b/src/animation.h index 9d5d03b..5462e65 100644 --- a/src/animation.h +++ b/src/animation.h @@ -29,7 +29,7 @@ typedef struct keyframes { } keyframes; typedef struct joint { - char* name; // optional + char* name; // optional transform transform_components; mat4 inverse_bind_matrix; mat4 local_transform; diff --git a/src/maths/maths.h b/src/maths/maths.h index 8e48435..e0d39d7 100644 --- a/src/maths/maths.h +++ b/src/maths/maths.h @@ -91,45 +91,42 @@ static inline quat quat_slerp(quat a, quat b, f32 percentage) { quat q1 = quat_normalise(b); // Compute the cosine of the angle between the two vectors. - f32 dot = quat_dot(q0, q1); - - // If the dot product is negative, slerp won't take - // the shorter path. Note that v1 and -v1 are equivalent when - // the negation is applied to all four components. Fix by - // reversing one quaternion. - if (dot < 0.0f) { - q1.x = -q1.x; - q1.y = -q1.y; - q1.z = -q1.z; - q1.w = -q1.w; - dot = -dot; - } + f32 dot = quat_dot(q0, q1); + + // If the dot product is negative, slerp won't take + // the shorter path. Note that v1 and -v1 are equivalent when + // the negation is applied to all four components. Fix by + // reversing one quaternion. + if (dot < 0.0f) { + q1.x = -q1.x; + q1.y = -q1.y; + q1.z = -q1.z; + q1.w = -q1.w; + dot = -dot; + } - const f32 DOT_THRESHOLD = 0.9995f; - if (dot > DOT_THRESHOLD) { - // If the inputs are too close for comfort, linearly interpolate - // and normalize the result. - out_quaternion = (quat){q0.x + ((q1.x - q0.x) * percentage), - q0.y + ((q1.y - q0.y) * percentage), - q0.z + ((q1.z - q0.z) * percentage), - q0.w + ((q1.w - q0.w) * percentage)}; + const f32 DOT_THRESHOLD = 0.9995f; + if (dot > DOT_THRESHOLD) { + // If the inputs are too close for comfort, linearly interpolate + // and normalize the result. + out_quaternion = + (quat){ q0.x + ((q1.x - q0.x) * percentage), q0.y + ((q1.y - q0.y) * percentage), + q0.z + ((q1.z - q0.z) * percentage), q0.w + ((q1.w - q0.w) * percentage) }; - return quat_normalise(out_quaternion); - } + return quat_normalise(out_quaternion); + } - // Since dot is in range [0, DOT_THRESHOLD], acos is safe - f32 theta_0 = cos(dot); // theta_0 = angle between input vectors - f32 theta = theta_0 * percentage; // theta = angle between v0 and result - f32 sin_theta = sin(theta); // compute this value only once - f32 sin_theta_0 = sin(theta_0); // compute this value only once + // Since dot is in range [0, DOT_THRESHOLD], acos is safe + f32 theta_0 = cos(dot); // theta_0 = angle between input vectors + f32 theta = theta_0 * percentage; // theta = angle between v0 and result + f32 sin_theta = sin(theta); // compute this value only once + f32 sin_theta_0 = sin(theta_0); // compute this value only once - f32 s0 = - cos(theta) - - dot * sin_theta / sin_theta_0; // == sin(theta_0 - theta) / sin(theta_0) - f32 s1 = sin_theta / sin_theta_0; + f32 s0 = cos(theta) - dot * sin_theta / sin_theta_0; // == sin(theta_0 - theta) / sin(theta_0) + f32 s1 = sin_theta / sin_theta_0; - return (quat){(q0.x * s0) + (q1.x * s1), (q0.y * s0) + (q1.y * s1), - (q0.z * s0) + (q1.z * s1), (q0.w * s0) + (q1.w * s1)}; + return (quat){ (q0.x * s0) + (q1.x * s1), (q0.y * s0) + (q1.y * s1), (q0.z * s0) + (q1.z * s1), + (q0.w * s0) + (q1.w * s1) }; } // --- Matrix Implementations diff --git a/src/renderer/render.c b/src/renderer/render.c index b688613..b1e2a46 100644 --- a/src/renderer/render.c +++ b/src/renderer/render.c @@ -176,7 +176,6 @@ void draw_mesh(renderer* ren, mesh* mesh, mat4* model_tf, material* mat, mat4* v bind_texture(lighting_shader, &mat->specular_texture, 1); // bind to slot 1 uniform_f32(lighting_shader.program_id, "material.shininess", 32.); - // upload model, view, and projection matrices uniform_mat4f(lighting_shader.program_id, "model", model_tf); uniform_mat4f(lighting_shader.program_id, "view", view); @@ -295,35 +294,6 @@ void model_upload_meshes(renderer* ren, model* model) { // TRACE("Uploading vertex array data: %d verts", num_vertices); total_verts += num_vertices; - // TODO: convert this garbage into a function - f32 verts[num_vertices * 8]; - // for each face - // for (int i = 0; i < (num_vertices / 3); i++) { - // // for each vert in face - // for (int j = 0; j < 3; j++) { - // size_t stride = (i * 24) + j * 8; - // // printf("i: %d, stride: %ld, loc %d\n", i, stride, i * 3 + j); - // vertex vert = model->meshes->data[mesh_i].vertices->data[i]; - // // printf("pos %f %f %f\n", vert.position.x, vert.position.y, vert.position.z); - // // printf("norm %f %f %f\n", vert.normal.x, vert.normal.y, vert.normal.z); - // // printf("tex %f %f\n", vert.uv.x, vert.uv.y); - // verts[stride + 0] = - // ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].position.x; - // verts[stride + 1] = - // ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].position.y; - // verts[stride + 2] = - // ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].position.z; - // verts[stride + 3] = - // ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].normal.x; - // verts[stride + 4] = - // ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].normal.y; - // verts[stride + 5] = - // ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + j].normal.z; - // verts[stride + 6] = ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 + - // j].uv.x; verts[stride + 7] = ((vertex*)model->meshes->data[mesh_i].vertices->data)[i * 3 - // + j].uv.y; - // } - // } size_t static_vertex_size = 2 * sizeof(vec3) + sizeof(vec2); size_t skinned_vertex_size = 2 * sizeof(vec3) + sizeof(vec2) + 4 * sizeof(u32) + sizeof(vec4); size_t vertex_size = mesh.is_skinned ? skinned_vertex_size : static_vertex_size; @@ -335,6 +305,7 @@ void model_upload_meshes(renderer* ren, model* model) { assert(vertex_size == sizeof(vertex)); assert(vertex_size == 8 * sizeof(float)); } + size_t buffer_size = vertex_size * num_vertices; u8* bytes = malloc(buffer_size); diff --git a/src/renderer/render_types.h b/src/renderer/render_types.h index 6e69708..423b58f 100644 --- a/src/renderer/render_types.h +++ b/src/renderer/render_types.h @@ -127,17 +127,16 @@ typedef struct vertex_bone_data { #include "animation.h" #ifndef TYPED_VERTEX_ARRAY -KITC_DECL_TYPED_ARRAY(vertex) // creates "vertex_darray" +KITC_DECL_TYPED_ARRAY(vertex) // creates "vertex_darray" KITC_DECL_TYPED_ARRAY(vertex_bone_data) // creates "skinned_vertex_darray" KITC_DECL_TYPED_ARRAY(joint) #define TYPED_VERTEX_ARRAY #endif - typedef struct mesh { - vertex_darray* vertices; - vertex_bone_data_darray* vertex_bone_data; // only used if model needs it - joint_darray* bones; + vertex_darray *vertices; + vertex_bone_data_darray *vertex_bone_data; // only used if model needs it + joint_darray *bones; bool is_skinned; u32 vertex_size; /** size in bytes of each vertex including necessary padding */ bool has_indices; -- cgit v1.2.3-70-g09d2