models_mesh_picking.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. int main(void)
  17. {
  18. // Initialization
  19. //--------------------------------------------------------------------------------------
  20. const int screenWidth = 800;
  21. const int screenHeight = 450;
  22. InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh picking");
  23. // Define the camera to look into our 3d world
  24. Camera camera = { 0 };
  25. camera.position = (Vector3){ 20.0f, 20.0f, 20.0f }; // Camera position
  26. camera.target = (Vector3){ 0.0f, 8.0f, 0.0f }; // Camera looking at point
  27. camera.up = (Vector3){ 0.0f, 1.6f, 0.0f }; // Camera up vector (rotation towards target)
  28. camera.fovy = 45.0f; // Camera field-of-view Y
  29. camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
  30. Ray ray = { 0 }; // Picking ray
  31. Model tower = LoadModel("resources/models/turret.obj"); // Load OBJ model
  32. Texture2D texture = LoadTexture("resources/models/turret_diffuse.png"); // Load model texture
  33. tower.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set model diffuse texture
  34. Vector3 towerPos = { 0.0f, 0.0f, 0.0f }; // Set model position
  35. BoundingBox towerBBox = GetMeshBoundingBox(tower.meshes[0]); // Get mesh bounding box
  36. // Ground quad
  37. Vector3 g0 = (Vector3){ -50.0f, 0.0f, -50.0f };
  38. Vector3 g1 = (Vector3){ -50.0f, 0.0f, 50.0f };
  39. Vector3 g2 = (Vector3){ 50.0f, 0.0f, 50.0f };
  40. Vector3 g3 = (Vector3){ 50.0f, 0.0f, -50.0f };
  41. // Test triangle
  42. Vector3 ta = (Vector3){ -25.0f, 0.5f, 0.0f };
  43. Vector3 tb = (Vector3){ -4.0f, 2.5f, 1.0f };
  44. Vector3 tc = (Vector3){ -8.0f, 6.5f, 0.0f };
  45. Vector3 bary = { 0.0f, 0.0f, 0.0f };
  46. // Test sphere
  47. Vector3 sp = (Vector3){ -30.0f, 5.0f, 5.0f };
  48. float sr = 4.0f;
  49. SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
  50. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  51. //--------------------------------------------------------------------------------------
  52. // Main game loop
  53. while (!WindowShouldClose()) // Detect window close button or ESC key
  54. {
  55. // Update
  56. //----------------------------------------------------------------------------------
  57. UpdateCamera(&camera); // Update camera
  58. // Display information about closest hit
  59. RayCollision collision = { 0 };
  60. char *hitObjectName = "None";
  61. collision.distance = FLT_MAX;
  62. collision.hit = false;
  63. Color cursorColor = WHITE;
  64. // Get ray and test against objects
  65. ray = GetMouseRay(GetMousePosition(), camera);
  66. // Check ray collision against ground quad
  67. RayCollision groundHitInfo = GetRayCollisionQuad(ray, g0, g1, g2, g3);
  68. if ((groundHitInfo.hit) && (groundHitInfo.distance < collision.distance))
  69. {
  70. collision = groundHitInfo;
  71. cursorColor = GREEN;
  72. hitObjectName = "Ground";
  73. }
  74. // Check ray collision against test triangle
  75. RayCollision triHitInfo = GetRayCollisionTriangle(ray, ta, tb, tc);
  76. if ((triHitInfo.hit) && (triHitInfo.distance < collision.distance))
  77. {
  78. collision = triHitInfo;
  79. cursorColor = PURPLE;
  80. hitObjectName = "Triangle";
  81. bary = Vector3Barycenter(collision.point, ta, tb, tc);
  82. }
  83. // Check ray collision against test sphere
  84. RayCollision sphereHitInfo = GetRayCollisionSphere(ray, sp, sr);
  85. if ((sphereHitInfo.hit) && (sphereHitInfo.distance < collision.distance)) {
  86. collision = sphereHitInfo;
  87. cursorColor = ORANGE;
  88. hitObjectName = "Sphere";
  89. }
  90. // Check ray collision against bounding box first, before trying the full ray-mesh test
  91. RayCollision boxHitInfo = GetRayCollisionBox(ray, towerBBox);
  92. if ((boxHitInfo.hit) && (boxHitInfo.distance < collision.distance))
  93. {
  94. collision = boxHitInfo;
  95. cursorColor = ORANGE;
  96. hitObjectName = "Box";
  97. // Check ray collision against model
  98. // NOTE: It considers model.transform matrix!
  99. RayCollision meshHitInfo = GetRayCollisionModel(ray, tower);
  100. if (meshHitInfo.hit)
  101. {
  102. collision = meshHitInfo;
  103. cursorColor = ORANGE;
  104. hitObjectName = "Mesh";
  105. }
  106. }
  107. //----------------------------------------------------------------------------------
  108. // Draw
  109. //----------------------------------------------------------------------------------
  110. BeginDrawing();
  111. ClearBackground(RAYWHITE);
  112. BeginMode3D(camera);
  113. // Draw the tower
  114. // WARNING: If scale is different than 1.0f,
  115. // not considered by GetRayCollisionModel()
  116. DrawModel(tower, towerPos, 1.0f, WHITE);
  117. // Draw the test triangle
  118. DrawLine3D(ta, tb, PURPLE);
  119. DrawLine3D(tb, tc, PURPLE);
  120. DrawLine3D(tc, ta, PURPLE);
  121. // Draw the test sphere
  122. DrawSphereWires(sp, sr, 8, 8, PURPLE);
  123. // Draw the mesh bbox if we hit it
  124. if (boxHitInfo.hit) DrawBoundingBox(towerBBox, LIME);
  125. // If we hit something, draw the cursor at the hit point
  126. if (collision.hit)
  127. {
  128. DrawCube(collision.point, 0.3f, 0.3f, 0.3f, cursorColor);
  129. DrawCubeWires(collision.point, 0.3f, 0.3f, 0.3f, RED);
  130. Vector3 normalEnd;
  131. normalEnd.x = collision.point.x + collision.normal.x;
  132. normalEnd.y = collision.point.y + collision.normal.y;
  133. normalEnd.z = collision.point.z + collision.normal.z;
  134. DrawLine3D(collision.point, normalEnd, RED);
  135. }
  136. DrawRay(ray, MAROON);
  137. DrawGrid(10, 10.0f);
  138. EndMode3D();
  139. // Draw some debug GUI text
  140. DrawText(TextFormat("Hit Object: %s", hitObjectName), 10, 50, 10, BLACK);
  141. if (collision.hit)
  142. {
  143. int ypos = 70;
  144. DrawText(TextFormat("Distance: %3.2f", collision.distance), 10, ypos, 10, BLACK);
  145. DrawText(TextFormat("Hit Pos: %3.2f %3.2f %3.2f",
  146. collision.point.x,
  147. collision.point.y,
  148. collision.point.z), 10, ypos + 15, 10, BLACK);
  149. DrawText(TextFormat("Hit Norm: %3.2f %3.2f %3.2f",
  150. collision.normal.x,
  151. collision.normal.y,
  152. collision.normal.z), 10, ypos + 30, 10, BLACK);
  153. if (triHitInfo.hit && strcmp(hitObjectName, "Triangle") == 0)
  154. DrawText(TextFormat("Barycenter: %3.2f %3.2f %3.2f", bary.x, bary.y, bary.z), 10, ypos + 45, 10, BLACK);
  155. }
  156. DrawText("Use Mouse to Move Camera", 10, 430, 10, GRAY);
  157. DrawText("(c) Turret 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY);
  158. DrawFPS(10, 10);
  159. EndDrawing();
  160. //----------------------------------------------------------------------------------
  161. }
  162. // De-Initialization
  163. //--------------------------------------------------------------------------------------
  164. UnloadModel(tower); // Unload model
  165. UnloadTexture(texture); // Unload texture
  166. CloseWindow(); // Close window and OpenGL context
  167. //--------------------------------------------------------------------------------------
  168. return 0;
  169. }