Fix how loading works before clenaup.

This commit is contained in:
Zed A. Shaw 2026-10-08 14:34:08 -04:00
parent 6d21b7f163
commit a629e16fe2
7 changed files with 19 additions and 15 deletions

View file

@ -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"

View file

@ -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;
}
if(totalPosition == vec4(0.0f)) {
return vec4(aPos, 1.0f);
} else {
return totalPosition;
}
}
void main()

View file

@ -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);

View file

@ -26,7 +26,7 @@ struct Animation {
Animation() = default;
Animation(const std::string& animationPath, Model *model);
Animation(Model *model);
~Animation() {}

View file

@ -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));
}

View file

@ -132,7 +132,6 @@ void Model::SetVertexBoneData(Vertex& vertex, int boneID, float weight)
}
void Model::ExtractBoneWeightForVertices(std::vector<Vertex>& 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();

View file

@ -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);
}