models_mesh_picking.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. * Copyright (c) 2015 Ramon Santamaria (@raysan5)
  9. * Example contributed by Joel Davis (@joeld42)
  10. *
  11. ********************************************************************************************/
  12. #include "raylib.h"
  13. #include "raymath.h"
  14. #define FLT_MAX 3.40282347E+38F // Maximum value of a float, defined in <float.h>
  15. #if defined(PLATFORM_WEB)
  16. #include <emscripten/emscripten.h>
  17. #endif
  18. //----------------------------------------------------------------------------------
  19. // Global Variables Definition
  20. //----------------------------------------------------------------------------------
  21. const int screenWidth = 800;
  22. const int screenHeight = 450;
  23. Camera camera;
  24. Vector3 cubePosition = { 0.0f, 1.0f, 0.0f };
  25. Vector3 cubeSize = { 2.0f, 2.0f, 2.0f };
  26. Ray ray; // Picking line ray
  27. Model tower;
  28. Texture2D texture;
  29. Vector3 towerPos = { 0.0f, 0.0f, 0.0f };
  30. BoundingBox towerBBox;
  31. bool hitMeshBBox = false;
  32. bool hitTriangle = false;
  33. // Test triangle
  34. Vector3 ta = (Vector3){ -25.0, 0.5, 0.0 };
  35. Vector3 tb = (Vector3){ -4.0, 2.5, 1.0 };
  36. Vector3 tc = (Vector3){ -8.0, 6.5, 0.0 };
  37. Vector3 bary = { 0.0f, 0.0f, 0.0f };
  38. //----------------------------------------------------------------------------------
  39. // Module Functions Declaration
  40. //----------------------------------------------------------------------------------
  41. void UpdateDrawFrame(void); // Update and Draw one frame
  42. //----------------------------------------------------------------------------------
  43. // Main Enry Point
  44. //----------------------------------------------------------------------------------
  45. int main()
  46. {
  47. // Initialization
  48. //--------------------------------------------------------------------------------------
  49. int screenWidth = 800;
  50. int screenHeight = 450;
  51. InitWindow(screenWidth, screenHeight, "raylib [models] example - 3d mesh picking");
  52. // Define the camera to look into our 3d world
  53. camera.position = (Vector3){ 10.0f, 8.0f, 10.0f }; // Camera position
  54. camera.target = (Vector3){ 0.0f, 2.3f, 0.0f }; // Camera looking at point
  55. camera.up = (Vector3){ 0.0f, 1.6f, 0.0f }; // Camera up vector (rotation towards target)
  56. camera.fovy = 45.0f; // Camera field-of-view Y
  57. tower = LoadModel("resources/tower.obj"); // Load OBJ model
  58. texture = LoadTexture("resources/tower.png"); // Load model texture
  59. tower.material.texDiffuse = texture; // Set model diffuse texture
  60. towerBBox = CalculateBoundingBox(tower.mesh);
  61. SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
  62. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  63. //--------------------------------------------------------------------------------------
  64. #if defined(PLATFORM_WEB)
  65. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  66. #else
  67. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  68. //--------------------------------------------------------------------------------------
  69. // Main game loop
  70. while (!WindowShouldClose()) // Detect window close button or ESC key
  71. {
  72. UpdateDrawFrame();
  73. }
  74. #endif
  75. // De-Initialization
  76. //--------------------------------------------------------------------------------------
  77. UnloadModel(tower); // Unload model from GPU
  78. UnloadTexture(texture); // Unload texture from GPU
  79. CloseWindow(); // Close window and OpenGL context
  80. //--------------------------------------------------------------------------------------
  81. return 0;
  82. }
  83. //----------------------------------------------------------------------------------
  84. // Module Functions Definition
  85. //----------------------------------------------------------------------------------
  86. void UpdateDrawFrame(void)
  87. {
  88. // Update
  89. //----------------------------------------------------------------------------------
  90. UpdateCamera(&camera); // Update camera
  91. // Display information about closest hit
  92. RayHitInfo nearestHit;
  93. char *hitObjectName = "None";
  94. nearestHit.distance = FLT_MAX;
  95. nearestHit.hit = false;
  96. Color cursorColor = WHITE;
  97. // Get ray and test against ground, triangle, and mesh
  98. ray = GetMouseRay(GetMousePosition(), camera);
  99. // Check ray collision aginst ground plane
  100. RayHitInfo groundHitInfo = GetCollisionRayGround(ray, 0.0f);
  101. if ((groundHitInfo.hit) && (groundHitInfo.distance < nearestHit.distance))
  102. {
  103. nearestHit = groundHitInfo;
  104. cursorColor = GREEN;
  105. hitObjectName = "Ground";
  106. }
  107. // Check ray collision against test triangle
  108. RayHitInfo triHitInfo = GetCollisionRayTriangle(ray, ta, tb, tc);
  109. if ((triHitInfo.hit) && (triHitInfo.distance < nearestHit.distance))
  110. {
  111. nearestHit = triHitInfo;
  112. cursorColor = PURPLE;
  113. hitObjectName = "Triangle";
  114. bary = VectorBarycenter(nearestHit.hitPosition, ta, tb, tc);
  115. hitTriangle = true;
  116. }
  117. else hitTriangle = false;
  118. RayHitInfo meshHitInfo;
  119. // Check ray collision against bounding box first, before trying the full ray-mesh test
  120. if (CheckCollisionRayBox(ray, towerBBox))
  121. {
  122. hitMeshBBox = true;
  123. // Check ray collision against mesh
  124. meshHitInfo = GetCollisionRayMesh(ray, &tower.mesh);
  125. if ((meshHitInfo.hit) && (meshHitInfo.distance < nearestHit.distance))
  126. {
  127. nearestHit = meshHitInfo;
  128. cursorColor = ORANGE;
  129. hitObjectName = "Mesh";
  130. }
  131. } hitMeshBBox = false;
  132. //----------------------------------------------------------------------------------
  133. // Draw
  134. //----------------------------------------------------------------------------------
  135. BeginDrawing();
  136. ClearBackground(RAYWHITE);
  137. Begin3dMode(camera);
  138. // Draw the tower
  139. DrawModel(tower, towerPos, 1.0, WHITE);
  140. // Draw the test triangle
  141. DrawLine3D(ta, tb, PURPLE);
  142. DrawLine3D(tb, tc, PURPLE);
  143. DrawLine3D(tc, ta, PURPLE);
  144. // Draw the mesh bbox if we hit it
  145. if (hitMeshBBox) DrawBoundingBox(towerBBox, LIME);
  146. // If we hit something, draw the cursor at the hit point
  147. if (nearestHit.hit)
  148. {
  149. DrawCube(nearestHit.hitPosition, 0.5, 0.5, 0.5, cursorColor);
  150. DrawCubeWires(nearestHit.hitPosition, 0.5, 0.5, 0.5, YELLOW);
  151. Vector3 normalEnd;
  152. normalEnd.x = nearestHit.hitPosition.x + nearestHit.hitNormal.x;
  153. normalEnd.y = nearestHit.hitPosition.y + nearestHit.hitNormal.y;
  154. normalEnd.z = nearestHit.hitPosition.z + nearestHit.hitNormal.z;
  155. DrawLine3D(nearestHit.hitPosition, normalEnd, YELLOW);
  156. }
  157. DrawRay(ray, MAROON);
  158. DrawGrid(100, 1.0f);
  159. End3dMode();
  160. // Draw some debug GUI text
  161. DrawText(FormatText("Hit Object: %s", hitObjectName), 10, 50, 10, BLACK);
  162. if (nearestHit.hit)
  163. {
  164. int ypos = 70;
  165. DrawText(FormatText("Distance: %3.2f", nearestHit.distance), 10, ypos, 10, BLACK);
  166. DrawText(FormatText("Hit Pos: %3.2f %3.2f %3.2f",
  167. nearestHit.hitPosition.x,
  168. nearestHit.hitPosition.y,
  169. nearestHit.hitPosition.z), 10, ypos + 15, 10, BLACK);
  170. DrawText(FormatText("Hit Norm: %3.2f %3.2f %3.2f",
  171. nearestHit.hitNormal.x,
  172. nearestHit.hitNormal.y,
  173. nearestHit.hitNormal.z), 10, ypos + 30, 10, BLACK);
  174. if (hitTriangle) DrawText(FormatText("Barycenter: %3.2f %3.2f %3.2f", bary.x, bary.y, bary.z), 10, ypos + 45, 10, BLACK);
  175. }
  176. DrawText("Use Mouse to Move Camera", 10, 430, 10, GRAY);
  177. DrawFPS(10, 10);
  178. EndDrawing();
  179. //----------------------------------------------------------------------------------
  180. }