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.

196 lines
7.5 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Ray 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 "../src/raymath.h"
  14. #include <stdio.h>
  15. #include <float.h>
  16. int main()
  17. {
  18. // Initialization
  19. //--------------------------------------------------------------------------------------
  20. int screenWidth = 800;
  21. int screenHeight = 450;
  22. InitWindow(screenWidth, screenHeight, "raylib [models] example - 3d ray picking");
  23. // Define the camera to look into our 3d world
  24. Camera camera;
  25. camera.position = (Vector3){ 10.0f, 8.0f, 10.0f }; // Camera position
  26. camera.target = (Vector3){ 0.0f, 2.3f, 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. Vector3 cubePosition = { 0.0f, 1.0f, 0.0f };
  30. Vector3 cubeSize = { 2.0f, 2.0f, 2.0f };
  31. Ray ray; // Picking line ray
  32. Model tower = LoadModel("resources/model/lowpoly-tower.obj"); // Load OBJ model
  33. Texture2D texture = LoadTexture("resources/model/lowpoly-tower.png"); // Load model texture
  34. tower.material.texDiffuse = texture; // Set model diffuse texture
  35. Vector3 towerPos = { 0.0f, 0.0f, 0.0f }; // Set model position
  36. BoundingBox towerBBox = CalculateBoundingBox( tower.mesh );
  37. bool hitMeshBBox = false;
  38. bool hitTriangle = false;
  39. // Test triangle
  40. Vector3 ta = (Vector3){ -25.0, 0.5, 0.0 };
  41. Vector3 tb = (Vector3){ -4.0, 2.5, 1.0 };
  42. Vector3 tc = (Vector3){ -8.0, 6.5, 0.0 };
  43. Vector3 bary = { 0.0f, 0.0f, 0.0f };
  44. SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
  45. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  46. //--------------------------------------------------------------------------------------
  47. // Main game loop
  48. while (!WindowShouldClose()) // Detect window close button or ESC key
  49. {
  50. // Update
  51. //----------------------------------------------------------------------------------
  52. UpdateCamera(&camera); // Update camera
  53. // Display information about closest hit
  54. RayHitInfo nearestHit;
  55. char *hitObjectName = "None";
  56. nearestHit.distance = FLT_MAX;
  57. nearestHit.hit = false;
  58. Color cursorColor = WHITE;
  59. // Get ray and test against ground, triangle, and mesh
  60. ray = GetMouseRay(GetMousePosition(), camera);
  61. // Check ray collision aginst ground plane
  62. RayHitInfo groundHitInfo = GetCollisionRayGround(ray, 0.0f);
  63. if ((groundHitInfo.hit) && (groundHitInfo.distance < nearestHit.distance))
  64. {
  65. nearestHit = groundHitInfo;
  66. cursorColor = GREEN;
  67. hitObjectName = "Ground";
  68. }
  69. // Check ray collision against test triangle
  70. RayHitInfo triHitInfo = GetCollisionRayTriangle(ray, ta, tb, tc);
  71. if ((triHitInfo.hit) && (triHitInfo.distance < nearestHit.distance))
  72. {
  73. nearestHit = triHitInfo;
  74. cursorColor = PURPLE;
  75. hitObjectName = "Triangle";
  76. bary = Barycenter(nearestHit.hitPosition, ta, tb, tc);
  77. hitTriangle = true;
  78. }
  79. else hitTriangle = false;
  80. RayHitInfo meshHitInfo;
  81. // Check ray collision against bounding box first, before trying the full ray-mesh test
  82. if (CheckCollisionRayBox(ray, towerBBox))
  83. {
  84. hitMeshBBox = true;
  85. // Check ray collision against mesh
  86. meshHitInfo = GetCollisionRayMesh(ray, &tower.mesh);
  87. if ((meshHitInfo.hit) && (meshHitInfo.distance < nearestHit.distance))
  88. {
  89. nearestHit = meshHitInfo;
  90. cursorColor = ORANGE;
  91. hitObjectName = "Mesh";
  92. }
  93. } hitMeshBBox = false;
  94. //----------------------------------------------------------------------------------
  95. // Draw
  96. //----------------------------------------------------------------------------------
  97. BeginDrawing();
  98. ClearBackground(RAYWHITE);
  99. Begin3dMode(camera);
  100. // Draw the tower
  101. DrawModel(tower, towerPos, 1.0, WHITE);
  102. // Draw the test triangle
  103. DrawLine3D(ta, tb, PURPLE);
  104. DrawLine3D(tb, tc, PURPLE);
  105. DrawLine3D(tc, ta, PURPLE);
  106. // Draw the mesh bbox if we hit it
  107. if (hitMeshBBox) DrawBoundingBox(towerBBox, LIME);
  108. // If we hit something, draw the cursor at the hit point
  109. if (nearestHit.hit)
  110. {
  111. DrawCube(nearestHit.hitPosition, 0.5, 0.5, 0.5, cursorColor);
  112. DrawCubeWires(nearestHit.hitPosition, 0.5, 0.5, 0.5, YELLOW);
  113. Vector3 normalEnd;
  114. normalEnd.x = nearestHit.hitPosition.x + nearestHit.hitNormal.x;
  115. normalEnd.y = nearestHit.hitPosition.y + nearestHit.hitNormal.y;
  116. normalEnd.z = nearestHit.hitPosition.z + nearestHit.hitNormal.z;
  117. DrawLine3D(nearestHit.hitPosition, normalEnd, YELLOW);
  118. }
  119. DrawRay(ray, MAROON);
  120. DrawGrid(100, 1.0f);
  121. End3dMode();
  122. // Draw some debug GUI text
  123. DrawText(FormatText("Hit Object: %s", hitObjectName), 10, 50, 10, BLACK);
  124. if (nearestHit.hit)
  125. {
  126. int ypos = 70;
  127. DrawText(FormatText("Distance: %3.2f", nearestHit.distance), 10, ypos, 10, BLACK);
  128. DrawText(FormatText("Hit Pos: %3.2f %3.2f %3.2f",
  129. nearestHit.hitPosition.x,
  130. nearestHit.hitPosition.y,
  131. nearestHit.hitPosition.z), 10, ypos + 15, 10, BLACK);
  132. DrawText(FormatText("Hit Norm: %3.2f %3.2f %3.2f",
  133. nearestHit.hitNormal.x,
  134. nearestHit.hitNormal.y,
  135. nearestHit.hitNormal.z), 10, ypos + 30, 10, BLACK);
  136. if (hitTriangle) DrawText(FormatText("Barycenter: %3.2f %3.2f %3.2f", bary.x, bary.y, bary.z), 10, ypos + 45, 10, BLACK);
  137. }
  138. DrawText("Use Mouse to Move Camera", 10, 430, 10, GRAY);
  139. DrawFPS(10, 10);
  140. EndDrawing();
  141. //----------------------------------------------------------------------------------
  142. }
  143. // De-Initialization
  144. //--------------------------------------------------------------------------------------
  145. CloseWindow(); // Close window and OpenGL context
  146. //--------------------------------------------------------------------------------------
  147. return 0;
  148. }