models_mesh_picking.c 8.0 KB

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