models_bone_socket.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - bone socket
  4. *
  5. * Example complexity rating: [★★★★] 4/4
  6. *
  7. * Example originally created with raylib 4.5, last time updated with raylib 4.5
  8. *
  9. * Example contributed by iP (@ipzaur) 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 iP (@ipzaur)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include "raymath.h"
  19. #define BONE_SOCKETS 3
  20. #define BONE_SOCKET_HAT 0
  21. #define BONE_SOCKET_HAND_R 1
  22. #define BONE_SOCKET_HAND_L 2
  23. //------------------------------------------------------------------------------------
  24. // Program main entry point
  25. //------------------------------------------------------------------------------------
  26. int main(void)
  27. {
  28. // Initialization
  29. //--------------------------------------------------------------------------------------
  30. const int screenWidth = 800;
  31. const int screenHeight = 450;
  32. InitWindow(screenWidth, screenHeight, "raylib [models] example - bone socket");
  33. // Define the camera to look into our 3d world
  34. Camera camera = { 0 };
  35. camera.position = (Vector3){ 5.0f, 5.0f, 5.0f }; // Camera position
  36. camera.target = (Vector3){ 0.0f, 2.0f, 0.0f }; // Camera looking at point
  37. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  38. camera.fovy = 45.0f; // Camera field-of-view Y
  39. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  40. // Load gltf model
  41. Model characterModel = LoadModel("resources/models/gltf/greenman.glb"); // Load character model
  42. Model equipModel[BONE_SOCKETS] = {
  43. LoadModel("resources/models/gltf/greenman_hat.glb"), // Index for the hat model is the same as BONE_SOCKET_HAT
  44. LoadModel("resources/models/gltf/greenman_sword.glb"), // Index for the sword model is the same as BONE_SOCKET_HAND_R
  45. LoadModel("resources/models/gltf/greenman_shield.glb") // Index for the shield model is the same as BONE_SOCKET_HAND_L
  46. };
  47. bool showEquip[3] = { true, true, true }; // Toggle on/off equip
  48. // Load gltf model animations
  49. int animsCount = 0;
  50. unsigned int animIndex = 0;
  51. unsigned int animCurrentFrame = 0;
  52. ModelAnimation *modelAnimations = LoadModelAnimations("resources/models/gltf/greenman.glb", &animsCount);
  53. // indices of bones for sockets
  54. int boneSocketIndex[BONE_SOCKETS] = { -1, -1, -1 };
  55. // search bones for sockets
  56. for (int i = 0; i < characterModel.boneCount; i++)
  57. {
  58. if (TextIsEqual(characterModel.bones[i].name, "socket_hat"))
  59. {
  60. boneSocketIndex[BONE_SOCKET_HAT] = i;
  61. continue;
  62. }
  63. if (TextIsEqual(characterModel.bones[i].name, "socket_hand_R"))
  64. {
  65. boneSocketIndex[BONE_SOCKET_HAND_R] = i;
  66. continue;
  67. }
  68. if (TextIsEqual(characterModel.bones[i].name, "socket_hand_L"))
  69. {
  70. boneSocketIndex[BONE_SOCKET_HAND_L] = i;
  71. continue;
  72. }
  73. }
  74. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  75. unsigned short angle = 0; // Set angle for rotate character
  76. DisableCursor(); // Limit cursor to relative movement inside the window
  77. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  78. //--------------------------------------------------------------------------------------
  79. // Main game loop
  80. while (!WindowShouldClose()) // Detect window close button or ESC key
  81. {
  82. // Update
  83. //----------------------------------------------------------------------------------
  84. UpdateCamera(&camera, CAMERA_THIRD_PERSON);
  85. // Rotate character
  86. if (IsKeyDown(KEY_F)) angle = (angle + 1)%360;
  87. else if (IsKeyDown(KEY_H)) angle = (360 + angle - 1)%360;
  88. // Select current animation
  89. if (IsKeyPressed(KEY_T)) animIndex = (animIndex + 1)%animsCount;
  90. else if (IsKeyPressed(KEY_G)) animIndex = (animIndex + animsCount - 1)%animsCount;
  91. // Toggle shown of equip
  92. if (IsKeyPressed(KEY_ONE)) showEquip[BONE_SOCKET_HAT] = !showEquip[BONE_SOCKET_HAT];
  93. if (IsKeyPressed(KEY_TWO)) showEquip[BONE_SOCKET_HAND_R] = !showEquip[BONE_SOCKET_HAND_R];
  94. if (IsKeyPressed(KEY_THREE)) showEquip[BONE_SOCKET_HAND_L] = !showEquip[BONE_SOCKET_HAND_L];
  95. // Update model animation
  96. ModelAnimation anim = modelAnimations[animIndex];
  97. animCurrentFrame = (animCurrentFrame + 1)%anim.frameCount;
  98. UpdateModelAnimation(characterModel, anim, animCurrentFrame);
  99. //----------------------------------------------------------------------------------
  100. // Draw
  101. //----------------------------------------------------------------------------------
  102. BeginDrawing();
  103. ClearBackground(RAYWHITE);
  104. BeginMode3D(camera);
  105. // Draw character
  106. Quaternion characterRotate = QuaternionFromAxisAngle((Vector3){ 0.0f, 1.0f, 0.0f }, angle*DEG2RAD);
  107. characterModel.transform = MatrixMultiply(QuaternionToMatrix(characterRotate), MatrixTranslate(position.x, position.y, position.z));
  108. UpdateModelAnimation(characterModel, anim, animCurrentFrame);
  109. DrawMesh(characterModel.meshes[0], characterModel.materials[1], characterModel.transform);
  110. // Draw equipments (hat, sword, shield)
  111. for (int i = 0; i < BONE_SOCKETS; i++)
  112. {
  113. if (!showEquip[i]) continue;
  114. Transform *transform = &anim.framePoses[animCurrentFrame][boneSocketIndex[i]];
  115. Quaternion inRotation = characterModel.bindPose[boneSocketIndex[i]].rotation;
  116. Quaternion outRotation = transform->rotation;
  117. // Calculate socket rotation (angle between bone in initial pose and same bone in current animation frame)
  118. Quaternion rotate = QuaternionMultiply(outRotation, QuaternionInvert(inRotation));
  119. Matrix matrixTransform = QuaternionToMatrix(rotate);
  120. // Translate socket to its position in the current animation
  121. matrixTransform = MatrixMultiply(matrixTransform, MatrixTranslate(transform->translation.x, transform->translation.y, transform->translation.z));
  122. // Transform the socket using the transform of the character (angle and translate)
  123. matrixTransform = MatrixMultiply(matrixTransform, characterModel.transform);
  124. // Draw mesh at socket position with socket angle rotation
  125. DrawMesh(equipModel[i].meshes[0], equipModel[i].materials[1], matrixTransform);
  126. }
  127. DrawGrid(10, 1.0f);
  128. EndMode3D();
  129. DrawText("Use the T/G to switch animation", 10, 10, 20, GRAY);
  130. DrawText("Use the F/H to rotate character left/right", 10, 35, 20, GRAY);
  131. DrawText("Use the 1,2,3 to toggle shown of hat, sword and shield", 10, 60, 20, GRAY);
  132. EndDrawing();
  133. //----------------------------------------------------------------------------------
  134. }
  135. // De-Initialization
  136. //--------------------------------------------------------------------------------------
  137. UnloadModelAnimations(modelAnimations, animsCount);
  138. UnloadModel(characterModel); // Unload character model and meshes/material
  139. // Unload equipment model and meshes/material
  140. for (int i = 0; i < BONE_SOCKETS; i++) UnloadModel(equipModel[i]);
  141. CloseWindow(); // Close window and OpenGL context
  142. //--------------------------------------------------------------------------------------
  143. return 0;
  144. }