Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

120 Zeilen
5.1 KiB

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