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.

247 lines
9.8 KiB

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