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.

202 lines
7.8 KiB

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