You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
5.1 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Doing skinning on the gpu using a vertex shader
  4. *
  5. * Example originally created with raylib 4.5, last time updated with raylib 4.5
  6. *
  7. * Example contributed by Daniel Holden (@orangeduck) and reviewed by Ramon Santamaria (@raysan5)
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2024 Daniel Holden (@orangeduck)
  13. *
  14. * Note: Due to limitations in the Apple OpenGL driver, this feature does not work on MacOS
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include "raymath.h"
  19. #if defined(PLATFORM_DESKTOP)
  20. #define GLSL_VERSION 330
  21. #else // PLATFORM_ANDROID, PLATFORM_WEB
  22. #define GLSL_VERSION 100
  23. #endif
  24. //------------------------------------------------------------------------------------
  25. // Program main entry point
  26. //------------------------------------------------------------------------------------
  27. int main(void)
  28. {
  29. // Initialization
  30. //--------------------------------------------------------------------------------------
  31. const int screenWidth = 800;
  32. const int screenHeight = 450;
  33. InitWindow(screenWidth, screenHeight, "raylib [models] example - GPU skinning");
  34. // Define the camera to look into our 3d world
  35. Camera camera = { 0 };
  36. camera.position = (Vector3){ 5.0f, 5.0f, 5.0f }; // Camera position
  37. camera.target = (Vector3){ 0.0f, 2.0f, 0.0f }; // Camera looking at point
  38. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  39. camera.fovy = 45.0f; // Camera field-of-view Y
  40. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  41. // Load gltf model
  42. Model characterModel = LoadModel("resources/models/gltf/greenman.glb"); // Load character model
  43. // Load skinning shader
  44. Shader skinningShader = LoadShader(TextFormat("resources/shaders/glsl%i/skinning.vs", GLSL_VERSION),
  45. TextFormat("resources/shaders/glsl%i/skinning.fs", GLSL_VERSION));
  46. characterModel.materials[1].shader = skinningShader;
  47. // Load gltf model animations
  48. int animsCount = 0;
  49. unsigned int animIndex = 0;
  50. unsigned int animCurrentFrame = 0;
  51. ModelAnimation *modelAnimations = LoadModelAnimations("resources/models/gltf/greenman.glb", &animsCount);
  52. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  53. DisableCursor(); // Limit cursor to relative movement inside the window
  54. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  55. //--------------------------------------------------------------------------------------
  56. // Main game loop
  57. while (!WindowShouldClose()) // Detect window close button or ESC key
  58. {
  59. // Update
  60. //----------------------------------------------------------------------------------
  61. UpdateCamera(&camera, CAMERA_THIRD_PERSON);
  62. // Select current animation
  63. if (IsKeyPressed(KEY_T)) animIndex = (animIndex + 1)%animsCount;
  64. else if (IsKeyPressed(KEY_G)) animIndex = (animIndex + animsCount - 1)%animsCount;
  65. // Update model animation
  66. ModelAnimation anim = modelAnimations[animIndex];
  67. animCurrentFrame = (animCurrentFrame + 1)%anim.frameCount;
  68. characterModel.transform = MatrixTranslate(position.x, position.y, position.z);
  69. UpdateModelAnimationBones(characterModel, anim, animCurrentFrame);
  70. //----------------------------------------------------------------------------------
  71. // Draw
  72. //----------------------------------------------------------------------------------
  73. BeginDrawing();
  74. ClearBackground(RAYWHITE);
  75. BeginMode3D(camera);
  76. // Draw character mesh, pose calculation is done in shader (GPU skinning)
  77. DrawMesh(characterModel.meshes[0], characterModel.materials[1], characterModel.transform);
  78. DrawGrid(10, 1.0f);
  79. EndMode3D();
  80. DrawText("Use the T/G to switch animation", 10, 10, 20, GRAY);
  81. EndDrawing();
  82. //----------------------------------------------------------------------------------
  83. }
  84. // De-Initialization
  85. //--------------------------------------------------------------------------------------
  86. UnloadModelAnimations(modelAnimations, animsCount); // Unload model animation
  87. UnloadModel(characterModel); // Unload model and meshes/material
  88. UnloadShader(skinningShader); // Unload GPU skinning shader
  89. CloseWindow(); // Close window and OpenGL context
  90. //--------------------------------------------------------------------------------------
  91. return 0;
  92. }