From a629e16fe22369233f276a55e819ef39539cea66 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Thu, 8 Oct 2026 14:34:08 -0400 Subject: [PATCH] Fix how loading works before clenaup. --- 45-animation/config.json | 4 ---- .../shaders/45-framebuffers.vert.glsl | 19 ++++++++++++------- 45-animation/src/animation.cpp | 3 ++- 45-animation/src/animation.hpp | 2 +- 45-animation/src/mesh.cpp | 3 +++ 45-animation/src/model.cpp | 1 - 45-animation/src/scene.hpp | 2 +- 7 files changed, 19 insertions(+), 15 deletions(-) diff --git a/45-animation/config.json b/45-animation/config.json index b341739..0f5a5f6 100644 --- a/45-animation/config.json +++ b/45-animation/config.json @@ -67,10 +67,6 @@ "directory": "../assets", "model_path": "janko_pastorius_anim.glb" }, - "human_run": { - "directory": "../assets", - "model_path": "janko_pastorius_running.glb" - }, "brick_cube": { "directory": "../assets", "model_path": "brick_cube_normal.obj" diff --git a/45-animation/shaders/45-framebuffers.vert.glsl b/45-animation/shaders/45-framebuffers.vert.glsl index 73856c5..2583797 100644 --- a/45-animation/shaders/45-framebuffers.vert.glsl +++ b/45-animation/shaders/45-framebuffers.vert.glsl @@ -6,6 +6,7 @@ layout (location = 3) in vec3 aTangent; layout (location = 4) in vec3 aBitangent; layout (location = 5) in ivec4 boneIds; layout (location = 6) in vec4 weights; +layout (location = 7) in int bone_count; out vec3 FragPos; out vec3 Normal; @@ -20,26 +21,30 @@ uniform mat4 lightSpaceMatrix; uniform bool hasBones; const int MAX_BONES = 100; -const int MAX_BONE_INFLUENCE = 4; uniform mat4 finalBonesMatrices[MAX_BONES]; vec4 AnimateBones() { vec4 totalPosition = vec4(0.0f); - for(int i = 0; i < MAX_BONE_INFLUENCE; i++) { - if(boneIds[i] == -1) continue; - if(boneIds[i] >= MAX_BONES) { + for(int i = 0; i < bone_count; i++) { + int bone_id = boneIds[i]; + + if(bone_id >= MAX_BONES) { totalPosition = vec4(aPos, 1.0f); break; } - int bone_id = boneIds[i]; + // bone found, mark it vec4 localPosition = finalBonesMatrices[bone_id] * vec4(aPos, 1.0f); totalPosition += localPosition * weights[i]; - vec3 localNormal = mat3(finalBonesMatrices[boneIds[i]]) * aNormal; + vec3 localNormal = mat3(finalBonesMatrices[bone_id]) * aNormal; } - return totalPosition; + if(totalPosition == vec4(0.0f)) { + return vec4(aPos, 1.0f); + } else { + return totalPosition; + } } void main() diff --git a/45-animation/src/animation.cpp b/45-animation/src/animation.cpp index 1c31ec9..d247249 100644 --- a/45-animation/src/animation.cpp +++ b/45-animation/src/animation.cpp @@ -2,7 +2,8 @@ #include "model.hpp" #include "assimp_glm_helpers.hpp" -Animation::Animation(const std::string& animationPath, Model *model) { +Animation::Animation(Model *model) { + std::string animationPath = model->directory + "/" + model->model_path; Assimp::Importer importer; const aiScene* scene = importer.ReadFile(animationPath, aiProcess_Triangulate); diff --git a/45-animation/src/animation.hpp b/45-animation/src/animation.hpp index a07b1a0..345b1ea 100644 --- a/45-animation/src/animation.hpp +++ b/45-animation/src/animation.hpp @@ -26,7 +26,7 @@ struct Animation { Animation() = default; - Animation(const std::string& animationPath, Model *model); + Animation(Model *model); ~Animation() {} diff --git a/45-animation/src/mesh.cpp b/45-animation/src/mesh.cpp index 1d574ff..d42a0b0 100644 --- a/45-animation/src/mesh.cpp +++ b/45-animation/src/mesh.cpp @@ -70,4 +70,7 @@ void Mesh::setup_mesh() { // bone weights glEnableVertexAttribArray(6); glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, weights)); + + glEnableVertexAttribArray(7); + glVertexAttribIPointer(7, 1, GL_INT, sizeof(Vertex), (void*)offsetof(Vertex, bone_count)); } diff --git a/45-animation/src/model.cpp b/45-animation/src/model.cpp index 8b3a3c9..b19817b 100644 --- a/45-animation/src/model.cpp +++ b/45-animation/src/model.cpp @@ -132,7 +132,6 @@ void Model::SetVertexBoneData(Vertex& vertex, int boneID, float weight) } void Model::ExtractBoneWeightForVertices(std::vector& vertices, aiMesh *mesh, const aiScene* scene) { - for(size_t boneIndex = 0; boneIndex < mesh->mNumBones; ++boneIndex) { int boneID = -1; std::string bone_name = mesh->mBones[boneIndex]->mName.C_Str(); diff --git a/45-animation/src/scene.hpp b/45-animation/src/scene.hpp index e4fb09c..e37c8f5 100644 --- a/45-animation/src/scene.hpp +++ b/45-animation/src/scene.hpp @@ -88,7 +88,7 @@ struct Scene { if(thing.animate) { // LEAK! Just temporary until I can clean up the original code - anim = new Animation(model.model_path, &model); + anim = new Animation(&model); // WARNING! Only works with one right now animator.PlayAnimation(anim); }