From f2e3d6eca724ee88794b20934aa9bf9d818280a9 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 2 Jan 2023 14:23:48 -0500 Subject: [models] Add GLTF animation support (#2844) * add GLTF animation support * use correct index when allocating animVertices and animNormals * early exit LoadModelAnimationsGLTF if the gtlf file fails to parse * update models/models_loading_gltf.c to play gltf animation Updated the .blend file to use weights rather than bone parents so it fits into the framework. Exported with weights to the .glb file. * fix order of operations for bone scale in UpdateModelAnimation * minor doc cleanup and improvements * fix formatting * fix float formatting * fix brace alignment and replace asserts with log messages --- examples/models/models_loading_gltf.c | 23 +++++++++++++++++++++- examples/models/resources/models/gltf/robot.blend | Bin 1899312 -> 2107372 bytes examples/models/resources/models/gltf/robot.glb | Bin 1477024 -> 1602604 bytes 3 files changed, 22 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/models/models_loading_gltf.c b/examples/models/models_loading_gltf.c index 68228a3f..87971590 100644 --- a/examples/models/models_loading_gltf.c +++ b/examples/models/models_loading_gltf.c @@ -35,6 +35,10 @@ int main(void) // Loaf gltf model Model model = LoadModel("resources/models/gltf/robot.glb"); + unsigned int animsCount = 0; + ModelAnimation *modelAnimations = LoadModelAnimations("resources/models/gltf/robot.glb", &animsCount); + + unsigned int animIndex = 0; Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position @@ -43,11 +47,26 @@ int main(void) SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- + unsigned int currentFrame = 0; + // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- + ModelAnimation anim = modelAnimations[animIndex]; + if (IsKeyPressed(KEY_UP)) + { + animIndex = (animIndex + 1) % animsCount; + } + + if (IsKeyPressed(KEY_DOWN)) + { + animIndex = (animIndex + animsCount - 1) % animsCount; + } + + currentFrame = (currentFrame + 1) % anim.frameCount; + UpdateModelAnimation(model, anim, currentFrame); UpdateCamera(&camera); //---------------------------------------------------------------------------------- @@ -64,6 +83,8 @@ int main(void) EndMode3D(); + DrawText("Use the up/down arrow keys to switch animation.", 10, 10, 20, WHITE); + EndDrawing(); //---------------------------------------------------------------------------------- } @@ -71,7 +92,7 @@ int main(void) // De-Initialization //-------------------------------------------------------------------------------------- UnloadModel(model); // Unload model and meshes/material - + CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- diff --git a/examples/models/resources/models/gltf/robot.blend b/examples/models/resources/models/gltf/robot.blend index d3bdac23..efe43c5e 100644 Binary files a/examples/models/resources/models/gltf/robot.blend and b/examples/models/resources/models/gltf/robot.blend differ diff --git a/examples/models/resources/models/gltf/robot.glb b/examples/models/resources/models/gltf/robot.glb index 73f5bf44..549011e7 100644 Binary files a/examples/models/resources/models/gltf/robot.glb and b/examples/models/resources/models/gltf/robot.glb differ -- cgit v1.2.3