您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

240 行
9.5 KiB

  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-2022 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 mode 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. SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
  55. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  56. //--------------------------------------------------------------------------------------
  57. // Main game loop
  58. while (!WindowShouldClose()) // Detect window close button or ESC key
  59. {
  60. // Update
  61. //----------------------------------------------------------------------------------
  62. UpdateCamera(&camera);
  63. // Display information about closest hit
  64. RayCollision collision = { 0 };
  65. char *hitObjectName = "None";
  66. collision.distance = FLT_MAX;
  67. collision.hit = false;
  68. Color cursorColor = WHITE;
  69. // Get ray and test against objects
  70. ray = GetMouseRay(GetMousePosition(), camera);
  71. // Check ray collision against ground quad
  72. RayCollision groundHitInfo = GetRayCollisionQuad(ray, g0, g1, g2, g3);
  73. if ((groundHitInfo.hit) && (groundHitInfo.distance < collision.distance))
  74. {
  75. collision = groundHitInfo;
  76. cursorColor = GREEN;
  77. hitObjectName = "Ground";
  78. }
  79. // Check ray collision against test triangle
  80. RayCollision triHitInfo = GetRayCollisionTriangle(ray, ta, tb, tc);
  81. if ((triHitInfo.hit) && (triHitInfo.distance < collision.distance))
  82. {
  83. collision = triHitInfo;
  84. cursorColor = PURPLE;
  85. hitObjectName = "Triangle";
  86. bary = Vector3Barycenter(collision.point, ta, tb, tc);
  87. }
  88. // Check ray collision against test sphere
  89. RayCollision sphereHitInfo = GetRayCollisionSphere(ray, sp, sr);
  90. if ((sphereHitInfo.hit) && (sphereHitInfo.distance < collision.distance))
  91. {
  92. collision = sphereHitInfo;
  93. cursorColor = ORANGE;
  94. hitObjectName = "Sphere";
  95. }
  96. // Check ray collision against bounding box first, before trying the full ray-mesh test
  97. RayCollision boxHitInfo = GetRayCollisionBox(ray, towerBBox);
  98. if ((boxHitInfo.hit) && (boxHitInfo.distance < collision.distance))
  99. {
  100. collision = boxHitInfo;
  101. cursorColor = ORANGE;
  102. hitObjectName = "Box";
  103. // Check ray collision against model meshes
  104. RayCollision meshHitInfo = { 0 };
  105. for (int m = 0; m < tower.meshCount; m++)
  106. {
  107. // NOTE: We consider the model.transform for the collision check but
  108. // it can be checked against any transform Matrix, used when checking against same
  109. // model drawn multiple times with multiple transforms
  110. meshHitInfo = GetRayCollisionMesh(ray, tower.meshes[m], tower.transform);
  111. if (meshHitInfo.hit)
  112. {
  113. // Save the closest hit mesh
  114. if ((!collision.hit) || (collision.distance > meshHitInfo.distance)) collision = meshHitInfo;
  115. break; // Stop once one mesh collision is detected, the colliding mesh is m
  116. }
  117. }
  118. if (meshHitInfo.hit)
  119. {
  120. collision = meshHitInfo;
  121. cursorColor = ORANGE;
  122. hitObjectName = "Mesh";
  123. }
  124. }
  125. //----------------------------------------------------------------------------------
  126. // Draw
  127. //----------------------------------------------------------------------------------
  128. BeginDrawing();
  129. ClearBackground(RAYWHITE);
  130. BeginMode3D(camera);
  131. // Draw the tower
  132. // WARNING: If scale is different than 1.0f,
  133. // not considered by GetRayCollisionModel()
  134. DrawModel(tower, towerPos, 1.0f, WHITE);
  135. // Draw the test triangle
  136. DrawLine3D(ta, tb, PURPLE);
  137. DrawLine3D(tb, tc, PURPLE);
  138. DrawLine3D(tc, ta, PURPLE);
  139. // Draw the test sphere
  140. DrawSphereWires(sp, sr, 8, 8, PURPLE);
  141. // Draw the mesh bbox if we hit it
  142. if (boxHitInfo.hit) DrawBoundingBox(towerBBox, LIME);
  143. // If we hit something, draw the cursor at the hit point
  144. if (collision.hit)
  145. {
  146. DrawCube(collision.point, 0.3f, 0.3f, 0.3f, cursorColor);
  147. DrawCubeWires(collision.point, 0.3f, 0.3f, 0.3f, RED);
  148. Vector3 normalEnd;
  149. normalEnd.x = collision.point.x + collision.normal.x;
  150. normalEnd.y = collision.point.y + collision.normal.y;
  151. normalEnd.z = collision.point.z + collision.normal.z;
  152. DrawLine3D(collision.point, normalEnd, RED);
  153. }
  154. DrawRay(ray, MAROON);
  155. DrawGrid(10, 10.0f);
  156. EndMode3D();
  157. // Draw some debug GUI text
  158. DrawText(TextFormat("Hit Object: %s", hitObjectName), 10, 50, 10, BLACK);
  159. if (collision.hit)
  160. {
  161. int ypos = 70;
  162. DrawText(TextFormat("Distance: %3.2f", collision.distance), 10, ypos, 10, BLACK);
  163. DrawText(TextFormat("Hit Pos: %3.2f %3.2f %3.2f",
  164. collision.point.x,
  165. collision.point.y,
  166. collision.point.z), 10, ypos + 15, 10, BLACK);
  167. DrawText(TextFormat("Hit Norm: %3.2f %3.2f %3.2f",
  168. collision.normal.x,
  169. collision.normal.y,
  170. collision.normal.z), 10, ypos + 30, 10, BLACK);
  171. if (triHitInfo.hit && TextIsEqual(hitObjectName, "Triangle"))
  172. DrawText(TextFormat("Barycenter: %3.2f %3.2f %3.2f", bary.x, bary.y, bary.z), 10, ypos + 45, 10, BLACK);
  173. }
  174. DrawText("Use Mouse to Move Camera", 10, 430, 10, GRAY);
  175. DrawText("(c) Turret 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY);
  176. DrawFPS(10, 10);
  177. EndDrawing();
  178. //----------------------------------------------------------------------------------
  179. }
  180. // De-Initialization
  181. //--------------------------------------------------------------------------------------
  182. UnloadModel(tower); // Unload model
  183. UnloadTexture(texture); // Unload texture
  184. CloseWindow(); // Close window and OpenGL context
  185. //--------------------------------------------------------------------------------------
  186. return 0;
  187. }