models_mesh_picking.c 9.4 KB

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