models_mesh_picking.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. 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;
  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; // 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.material.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.mesh); // 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;
  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;
  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. } hitMeshBBox = false;
  93. //----------------------------------------------------------------------------------
  94. // Draw
  95. //----------------------------------------------------------------------------------
  96. BeginDrawing();
  97. ClearBackground(RAYWHITE);
  98. BeginMode3D(camera);
  99. // Draw the tower
  100. // WARNING: If scale is different than 1.0f,
  101. // not considered by GetCollisionRayModel()
  102. DrawModel(tower, towerPos, 1.0f, WHITE);
  103. // Draw the test triangle
  104. DrawLine3D(ta, tb, PURPLE);
  105. DrawLine3D(tb, tc, PURPLE);
  106. DrawLine3D(tc, ta, PURPLE);
  107. // Draw the mesh bbox if we hit it
  108. if (hitMeshBBox) DrawBoundingBox(towerBBox, LIME);
  109. // If we hit something, draw the cursor at the hit point
  110. if (nearestHit.hit)
  111. {
  112. DrawCube(nearestHit.position, 0.3, 0.3, 0.3, cursorColor);
  113. DrawCubeWires(nearestHit.position, 0.3, 0.3, 0.3, RED);
  114. Vector3 normalEnd;
  115. normalEnd.x = nearestHit.position.x + nearestHit.normal.x;
  116. normalEnd.y = nearestHit.position.y + nearestHit.normal.y;
  117. normalEnd.z = nearestHit.position.z + nearestHit.normal.z;
  118. DrawLine3D(nearestHit.position, normalEnd, RED);
  119. }
  120. DrawRay(ray, MAROON);
  121. DrawGrid(10, 10.0f);
  122. EndMode3D();
  123. // Draw some debug GUI text
  124. DrawText(FormatText("Hit Object: %s", hitObjectName), 10, 50, 10, BLACK);
  125. if (nearestHit.hit)
  126. {
  127. int ypos = 70;
  128. DrawText(FormatText("Distance: %3.2f", nearestHit.distance), 10, ypos, 10, BLACK);
  129. DrawText(FormatText("Hit Pos: %3.2f %3.2f %3.2f",
  130. nearestHit.position.x,
  131. nearestHit.position.y,
  132. nearestHit.position.z), 10, ypos + 15, 10, BLACK);
  133. DrawText(FormatText("Hit Norm: %3.2f %3.2f %3.2f",
  134. nearestHit.normal.x,
  135. nearestHit.normal.y,
  136. nearestHit.normal.z), 10, ypos + 30, 10, BLACK);
  137. if (hitTriangle) DrawText(FormatText("Barycenter: %3.2f %3.2f %3.2f", bary.x, bary.y, bary.z), 10, ypos + 45, 10, BLACK);
  138. }
  139. DrawText("Use Mouse to Move Camera", 10, 430, 10, GRAY);
  140. DrawText("(c) Turret 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY);
  141. DrawFPS(10, 10);
  142. EndDrawing();
  143. //----------------------------------------------------------------------------------
  144. }
  145. // De-Initialization
  146. //--------------------------------------------------------------------------------------
  147. UnloadModel(tower); // Unload model
  148. UnloadTexture(texture); // Unload texture
  149. CloseWindow(); // Close window and OpenGL context
  150. //--------------------------------------------------------------------------------------
  151. return 0;
  152. }