You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
9.7 KiB

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