models_mesh_picking.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - mesh picking
  4. *
  5. * Example complexity rating: [★★★☆] 3/4
  6. *
  7. * Example originally created with raylib 1.7, last time updated with raylib 4.0
  8. *
  9. * Example contributed by Joel Davis (@joeld42) 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) 2017-2025 Joel Davis (@joeld42) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include "raymath.h"
  19. #undef FLT_MAX
  20. #define FLT_MAX 340282346638528859811704183484516925440.0f // Maximum value of a float, from bit pattern 01111111011111111111111111111111
  21. //------------------------------------------------------------------------------------
  22. // Program main entry point
  23. //------------------------------------------------------------------------------------
  24. int main(void)
  25. {
  26. // Initialization
  27. //--------------------------------------------------------------------------------------
  28. const int screenWidth = 800;
  29. const int screenHeight = 450;
  30. InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh picking");
  31. // Define the camera to look into our 3d world
  32. Camera camera = { 0 };
  33. camera.position = (Vector3){ 20.0f, 20.0f, 20.0f }; // Camera position
  34. camera.target = (Vector3){ 0.0f, 8.0f, 0.0f }; // Camera looking at point
  35. camera.up = (Vector3){ 0.0f, 1.6f, 0.0f }; // Camera up vector (rotation towards target)
  36. camera.fovy = 45.0f; // Camera field-of-view Y
  37. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  38. Ray ray = { 0 }; // Picking ray
  39. Model tower = LoadModel("resources/models/obj/turret.obj"); // Load OBJ model
  40. Texture2D texture = LoadTexture("resources/models/obj/turret_diffuse.png"); // Load model texture
  41. tower.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set model diffuse texture
  42. Vector3 towerPos = { 0.0f, 0.0f, 0.0f }; // Set model position
  43. BoundingBox towerBBox = GetMeshBoundingBox(tower.meshes[0]); // Get mesh bounding box
  44. // Ground quad
  45. Vector3 g0 = (Vector3){ -50.0f, 0.0f, -50.0f };
  46. Vector3 g1 = (Vector3){ -50.0f, 0.0f, 50.0f };
  47. Vector3 g2 = (Vector3){ 50.0f, 0.0f, 50.0f };
  48. Vector3 g3 = (Vector3){ 50.0f, 0.0f, -50.0f };
  49. // Test triangle
  50. Vector3 ta = (Vector3){ -25.0f, 0.5f, 0.0f };
  51. Vector3 tb = (Vector3){ -4.0f, 2.5f, 1.0f };
  52. Vector3 tc = (Vector3){ -8.0f, 6.5f, 0.0f };
  53. Vector3 bary = { 0.0f, 0.0f, 0.0f };
  54. // Test sphere
  55. Vector3 sp = (Vector3){ -30.0f, 5.0f, 5.0f };
  56. float sr = 4.0f;
  57. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  58. //--------------------------------------------------------------------------------------
  59. // Main game loop
  60. while (!WindowShouldClose()) // Detect window close button or ESC key
  61. {
  62. // Update
  63. //----------------------------------------------------------------------------------
  64. if (IsCursorHidden()) UpdateCamera(&camera, CAMERA_FIRST_PERSON); // Update camera
  65. // Toggle camera controls
  66. if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT))
  67. {
  68. if (IsCursorHidden()) EnableCursor();
  69. else DisableCursor();
  70. }
  71. // Display information about closest hit
  72. RayCollision collision = { 0 };
  73. const char *hitObjectName = "None";
  74. collision.distance = FLT_MAX;
  75. collision.hit = false;
  76. Color cursorColor = WHITE;
  77. // Get ray and test against objects
  78. ray = GetScreenToWorldRay(GetMousePosition(), camera);
  79. // Check ray collision against ground quad
  80. RayCollision groundHitInfo = GetRayCollisionQuad(ray, g0, g1, g2, g3);
  81. if ((groundHitInfo.hit) && (groundHitInfo.distance < collision.distance))
  82. {
  83. collision = groundHitInfo;
  84. cursorColor = GREEN;
  85. hitObjectName = "Ground";
  86. }
  87. // Check ray collision against test triangle
  88. RayCollision triHitInfo = GetRayCollisionTriangle(ray, ta, tb, tc);
  89. if ((triHitInfo.hit) && (triHitInfo.distance < collision.distance))
  90. {
  91. collision = triHitInfo;
  92. cursorColor = PURPLE;
  93. hitObjectName = "Triangle";
  94. bary = Vector3Barycenter(collision.point, ta, tb, tc);
  95. }
  96. // Check ray collision against test sphere
  97. RayCollision sphereHitInfo = GetRayCollisionSphere(ray, sp, sr);
  98. if ((sphereHitInfo.hit) && (sphereHitInfo.distance < collision.distance))
  99. {
  100. collision = sphereHitInfo;
  101. cursorColor = ORANGE;
  102. hitObjectName = "Sphere";
  103. }
  104. // Check ray collision against bounding box first, before trying the full ray-mesh test
  105. RayCollision boxHitInfo = GetRayCollisionBox(ray, towerBBox);
  106. if ((boxHitInfo.hit) && (boxHitInfo.distance < collision.distance))
  107. {
  108. collision = boxHitInfo;
  109. cursorColor = ORANGE;
  110. hitObjectName = "Box";
  111. // Check ray collision against model meshes
  112. RayCollision meshHitInfo = { 0 };
  113. for (int m = 0; m < tower.meshCount; m++)
  114. {
  115. // NOTE: We consider the model.transform for the collision check but
  116. // it can be checked against any transform Matrix, used when checking against same
  117. // model drawn multiple times with multiple transforms
  118. meshHitInfo = GetRayCollisionMesh(ray, tower.meshes[m], tower.transform);
  119. if (meshHitInfo.hit)
  120. {
  121. // Save the closest hit mesh
  122. if ((!collision.hit) || (collision.distance > meshHitInfo.distance)) collision = meshHitInfo;
  123. break; // Stop once one mesh collision is detected, the colliding mesh is m
  124. }
  125. }
  126. if (meshHitInfo.hit)
  127. {
  128. collision = meshHitInfo;
  129. cursorColor = ORANGE;
  130. hitObjectName = "Mesh";
  131. }
  132. }
  133. //----------------------------------------------------------------------------------
  134. // Draw
  135. //----------------------------------------------------------------------------------
  136. BeginDrawing();
  137. ClearBackground(RAYWHITE);
  138. BeginMode3D(camera);
  139. // Draw the tower
  140. // WARNING: If scale is different than 1.0f,
  141. // not considered by GetRayCollisionModel()
  142. DrawModel(tower, towerPos, 1.0f, WHITE);
  143. // Draw the test triangle
  144. DrawLine3D(ta, tb, PURPLE);
  145. DrawLine3D(tb, tc, PURPLE);
  146. DrawLine3D(tc, ta, PURPLE);
  147. // Draw the test sphere
  148. DrawSphereWires(sp, sr, 8, 8, PURPLE);
  149. // Draw the mesh bbox if we hit it
  150. if (boxHitInfo.hit) DrawBoundingBox(towerBBox, LIME);
  151. // If we hit something, draw the cursor at the hit point
  152. if (collision.hit)
  153. {
  154. DrawCube(collision.point, 0.3f, 0.3f, 0.3f, cursorColor);
  155. DrawCubeWires(collision.point, 0.3f, 0.3f, 0.3f, RED);
  156. Vector3 normalEnd;
  157. normalEnd.x = collision.point.x + collision.normal.x;
  158. normalEnd.y = collision.point.y + collision.normal.y;
  159. normalEnd.z = collision.point.z + collision.normal.z;
  160. DrawLine3D(collision.point, normalEnd, RED);
  161. }
  162. DrawRay(ray, MAROON);
  163. DrawGrid(10, 10.0f);
  164. EndMode3D();
  165. // Draw some debug GUI text
  166. DrawText(TextFormat("Hit Object: %s", hitObjectName), 10, 50, 10, BLACK);
  167. if (collision.hit)
  168. {
  169. int ypos = 70;
  170. DrawText(TextFormat("Distance: %3.2f", collision.distance), 10, ypos, 10, BLACK);
  171. DrawText(TextFormat("Hit Pos: %3.2f %3.2f %3.2f",
  172. collision.point.x,
  173. collision.point.y,
  174. collision.point.z), 10, ypos + 15, 10, BLACK);
  175. DrawText(TextFormat("Hit Norm: %3.2f %3.2f %3.2f",
  176. collision.normal.x,
  177. collision.normal.y,
  178. collision.normal.z), 10, ypos + 30, 10, BLACK);
  179. if (triHitInfo.hit && TextIsEqual(hitObjectName, "Triangle"))
  180. DrawText(TextFormat("Barycenter: %3.2f %3.2f %3.2f", bary.x, bary.y, bary.z), 10, ypos + 45, 10, BLACK);
  181. }
  182. DrawText("Right click mouse to toggle camera controls", 10, 430, 10, GRAY);
  183. DrawText("(c) Turret 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY);
  184. DrawFPS(10, 10);
  185. EndDrawing();
  186. //----------------------------------------------------------------------------------
  187. }
  188. // De-Initialization
  189. //--------------------------------------------------------------------------------------
  190. UnloadModel(tower); // Unload model
  191. UnloadTexture(texture); // Unload texture
  192. CloseWindow(); // Close window and OpenGL context
  193. //--------------------------------------------------------------------------------------
  194. return 0;
  195. }