| @ -1,121 +1,121 @@ | |||
| /******************************************************************************************* | |||
| * | |||
| * raylib [models] example - gpu skinning | |||
| * | |||
| * Example complexity rating: [★★★☆] 3/4 | |||
| * | |||
| * Example originally created with raylib 4.5, last time updated with raylib 4.5 | |||
| * | |||
| * Example contributed by Daniel Holden (@orangeduck) and reviewed by Ramon Santamaria (@raysan5) | |||
| * | |||
| * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, | |||
| * BSD-like license that allows static linking with closed source software | |||
| * | |||
| * Copyright (c) 2024-2025 Daniel Holden (@orangeduck) | |||
| * | |||
| * Note: Due to limitations in the Apple OpenGL driver, this feature does not work on MacOS | |||
| * | |||
| ********************************************************************************************/ | |||
| #include "raylib.h" | |||
| #include "raymath.h" | |||
| #if defined(PLATFORM_DESKTOP) | |||
| #define GLSL_VERSION 330 | |||
| #else 1">// PLATFORM_ANDROID, PLATFORM_WEB | |||
| #define GLSL_VERSION 100 | |||
| #endif | |||
| c1">//------------------------------------------------------------------------------------ | |||
| c1">// Program main entry point | |||
| c1">//------------------------------------------------------------------------------------ | |||
| int main(void) | |||
| { | |||
| c1">// Initialization | |||
| c1">//-------------------------------------------------------------------------------------- | |||
| const int screenWidth = 800; | |||
| const int screenHeight = 450; | |||
| InitWindow(screenWidth, screenHeight, "raylib [models] example - gpu skinning"); | |||
| c1">// Define the camera to look into our 3d world | |||
| Camera camera = { 0 }; | |||
| camera.position = (Vector3){ 5.0f, 5.0f, 5.0f }; c1">// Camera position | |||
| camera.target = (Vector3){ 0.0f, 2.0f, 0.0f }; c1">// Camera looking at point | |||
| camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; c1">// Camera up vector (rotation towards target) | |||
| camera.fovy = 45.0f; c1">// Camera field-of-view Y | |||
| camera.projection = CAMERA_PERSPECTIVE; c1">// Camera projection type | |||
| c1">// Load gltf model | |||
| Model characterModel = LoadModel("resources/models/gltf/greenman.glb"); c1">// Load character model | |||
| c1">// Load skinning shader | |||
| Shader skinningShader = LoadShader(TextFormat("resources/shaders/glsl%i/skinning.vs", GLSL_VERSION), | |||
| TextFormat("resources/shaders/glsl%i/skinning.fs", GLSL_VERSION)); | |||
| characterModel.materials[1].shader = skinningShader; | |||
| c1">// Load gltf model animations | |||
| int animsCount = 0; | |||
| unsigned int animIndex = 0; | |||
| unsigned int animCurrentFrame = 0; | |||
| ModelAnimation *modelAnimations = LoadModelAnimations("resources/models/gltf/greenman.glb", &animsCount); | |||
| Vector3 position = { 0.0f, 0.0f, 0.0f }; c1">// Set model position | |||
| DisableCursor(); c1">// Limit cursor to relative movement inside the window | |||
| SetTargetFPS(60); c1">// Set our game to run at 60 frames-per-second | |||
| c1">//-------------------------------------------------------------------------------------- | |||
| c1">// Main game loop | |||
| while (!WindowShouldClose()) c1">// Detect window close button or ESC key | |||
| { | |||
| c1">// Update | |||
| c1">//---------------------------------------------------------------------------------- | |||
| UpdateCamera(&camera, CAMERA_THIRD_PERSON); | |||
| c1">// Select current animation | |||
| if (IsKeyPressed(KEY_T)) animIndex = (animIndex + 1)%animsCount; | |||
| else if (IsKeyPressed(KEY_G)) animIndex = (animIndex + animsCount - 1)%animsCount; | |||
| c1">// Update model animation | |||
| ModelAnimation anim = modelAnimations[animIndex]; | |||
| animCurrentFrame = (animCurrentFrame + 1)%anim.frameCount; | |||
| characterModel.transform = MatrixTranslate(position.x, position.y, position.z); | |||
| UpdateModelAnimationBones(characterModel, anim, animCurrentFrame); | |||
| c1">//---------------------------------------------------------------------------------- | |||
| c1">// Draw | |||
| c1">//---------------------------------------------------------------------------------- | |||
| BeginDrawing(); | |||
| ClearBackground(RAYWHITE); | |||
| BeginMode3D(camera); | |||
| c1">// Draw character mesh, pose calculation is done in shader (GPU skinning) | |||
| DrawMesh(characterModel.meshes[0], characterModel.materials[1], characterModel.transform); | |||
| DrawGrid(10, 1.0f); | |||
| EndMode3D(); | |||
| DrawText("Use the T/G to switch animation", 10, 10, 20, GRAY); | |||
| EndDrawing(); | |||
| c1">//---------------------------------------------------------------------------------- | |||
| } | |||
| c1">// De-Initialization | |||
| c1">//-------------------------------------------------------------------------------------- | |||
| UnloadModelAnimations(modelAnimations, animsCount); c1">// Unload model animation | |||
| UnloadModel(characterModel); c1">// Unload model and meshes/material | |||
| UnloadShader(skinningShader); c1">// Unload GPU skinning shader | |||
| CloseWindow(); c1">// Close window and OpenGL context | |||
| c1">//-------------------------------------------------------------------------------------- | |||
| return 0; | |||
| /******************************************************************************************* | |||
| * | |||
| * raylib [models] example - animation gpu skinning | |||
| * | |||
| * Example complexity rating: [★★★☆] 3/4 | |||
| * | |||
| * Example originally created with raylib 4.5, last time updated with raylib 4.5 | |||
| * | |||
| * Example contributed by Daniel Holden (@orangeduck) and reviewed by Ramon Santamaria (@raysan5) | |||
| * | |||
| * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, | |||
| * BSD-like license that allows static linking with closed source software | |||
| * | |||
| * Copyright (c) 2024-2025 Daniel Holden (@orangeduck) | |||
| * | |||
| * Note: Due to limitations in the Apple OpenGL driver, this feature does not work on MacOS | |||
| * | |||
| ********************************************************************************************/ | |||
| #include "raylib.h" | |||
| #include "raymath.h" | |||
| #if defined(PLATFORM_DESKTOP) | |||
| #define GLSL_VERSION 330 | |||
| #else p">// PLATFORM_ANDROID, PLATFORM_WEB | |||
| #define GLSL_VERSION 100 | |||
| #endif | |||
| o">//------------------------------------------------------------------------------------ | |||
| o">// Program main entry point | |||
| o">//------------------------------------------------------------------------------------ | |||
| int main(void) | |||
| { | |||
| o">// Initialization | |||
| o">//-------------------------------------------------------------------------------------- | |||
| const int screenWidth = 800; | |||
| const int screenHeight = 450; | |||
| InitWindow(screenWidth, screenHeight, "raylib [models] example - animation gpu skinning"); | |||
| o">// Define the camera to look into our 3d world | |||
| Camera camera = { 0 }; | |||
| camera.position = (Vector3){ 5.0f, 5.0f, 5.0f }; o">// Camera position | |||
| camera.target = (Vector3){ 0.0f, 2.0f, 0.0f }; o">// Camera looking at point | |||
| camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; o">// Camera up vector (rotation towards target) | |||
| camera.fovy = 45.0f; o">// Camera field-of-view Y | |||
| camera.projection = CAMERA_PERSPECTIVE; o">// Camera projection type | |||
| o">// Load gltf model | |||
| Model characterModel = LoadModel("resources/models/gltf/greenman.glb"); o">// Load character model | |||
| o">// Load skinning shader | |||
| Shader skinningShader = LoadShader(TextFormat("resources/shaders/glsl%i/skinning.vs", GLSL_VERSION), | |||
| TextFormat("resources/shaders/glsl%i/skinning.fs", GLSL_VERSION)); | |||
| characterModel.materials[1].shader = skinningShader; | |||
| o">// Load gltf model animations | |||
| int animsCount = 0; | |||
| unsigned int animIndex = 0; | |||
| unsigned int animCurrentFrame = 0; | |||
| ModelAnimation *modelAnimations = LoadModelAnimations("resources/models/gltf/greenman.glb", &animsCount); | |||
| Vector3 position = { 0.0f, 0.0f, 0.0f }; o">// Set model position | |||
| DisableCursor(); o">// Limit cursor to relative movement inside the window | |||
| SetTargetFPS(60); o">// Set our game to run at 60 frames-per-second | |||
| o">//-------------------------------------------------------------------------------------- | |||
| o">// Main game loop | |||
| while (!WindowShouldClose()) o">// Detect window close button or ESC key | |||
| { | |||
| o">// Update | |||
| o">//---------------------------------------------------------------------------------- | |||
| UpdateCamera(&camera, CAMERA_THIRD_PERSON); | |||
| o">// Select current animation | |||
| if (IsKeyPressed(KEY_T)) animIndex = (animIndex + 1)%animsCount; | |||
| else if (IsKeyPressed(KEY_G)) animIndex = (animIndex + animsCount - 1)%animsCount; | |||
| o">// Update model animation | |||
| ModelAnimation anim = modelAnimations[animIndex]; | |||
| animCurrentFrame = (animCurrentFrame + 1)%anim.frameCount; | |||
| characterModel.transform = MatrixTranslate(position.x, position.y, position.z); | |||
| UpdateModelAnimationBones(characterModel, anim, animCurrentFrame); | |||
| o">//---------------------------------------------------------------------------------- | |||
| o">// Draw | |||
| o">//---------------------------------------------------------------------------------- | |||
| BeginDrawing(); | |||
| ClearBackground(RAYWHITE); | |||
| BeginMode3D(camera); | |||
| o">// Draw character mesh, pose calculation is done in shader (GPU skinning) | |||
| DrawMesh(characterModel.meshes[0], characterModel.materials[1], characterModel.transform); | |||
| DrawGrid(10, 1.0f); | |||
| EndMode3D(); | |||
| DrawText("Use the T/G to switch animation", 10, 10, 20, GRAY); | |||
| EndDrawing(); | |||
| o">//---------------------------------------------------------------------------------- | |||
| } | |||
| o">// De-Initialization | |||
| o">//-------------------------------------------------------------------------------------- | |||
| UnloadModelAnimations(modelAnimations, animsCount); o">// Unload model animation | |||
| UnloadModel(characterModel); o">// Unload model and meshes/material | |||
| UnloadShader(skinningShader); o">// Unload GPU skinning shader | |||
| CloseWindow(); o">// Close window and OpenGL context | |||
| o">//-------------------------------------------------------------------------------------- | |||
| return 0; | |||
| } | |||