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.

193 lines
7.4 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. Ray ray; // Picking line ray
  30. Model tower = LoadModel("resources/model/lowpoly-tower.obj"); // Load OBJ model
  31. Texture2D texture = LoadTexture("resources/model/lowpoly-tower.png"); // Load model texture
  32. tower.material.texDiffuse = texture; // Set model diffuse texture
  33. Vector3 towerPos = { 0.0f, 0.0f, 0.0f }; // Set model position
  34. BoundingBox towerBBox = CalculateBoundingBox( tower.mesh );
  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 = Barycenter(nearestHit.hitPosition, 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 mesh
  84. meshHitInfo = GetCollisionRayMesh(ray, &tower.mesh);
  85. if ((meshHitInfo.hit) && (meshHitInfo.distance < nearestHit.distance))
  86. {
  87. nearestHit = meshHitInfo;
  88. cursorColor = ORANGE;
  89. hitObjectName = "Mesh";
  90. }
  91. } hitMeshBBox = false;
  92. //----------------------------------------------------------------------------------
  93. // Draw
  94. //----------------------------------------------------------------------------------
  95. BeginDrawing();
  96. ClearBackground(RAYWHITE);
  97. Begin3dMode(camera);
  98. // Draw the tower
  99. DrawModel(tower, towerPos, 1.0, WHITE);
  100. // Draw the test triangle
  101. DrawLine3D(ta, tb, PURPLE);
  102. DrawLine3D(tb, tc, PURPLE);
  103. DrawLine3D(tc, ta, PURPLE);
  104. // Draw the mesh bbox if we hit it
  105. if (hitMeshBBox) DrawBoundingBox(towerBBox, LIME);
  106. // If we hit something, draw the cursor at the hit point
  107. if (nearestHit.hit)
  108. {
  109. DrawCube(nearestHit.hitPosition, 0.5, 0.5, 0.5, cursorColor);
  110. DrawCubeWires(nearestHit.hitPosition, 0.5, 0.5, 0.5, YELLOW);
  111. Vector3 normalEnd;
  112. normalEnd.x = nearestHit.hitPosition.x + nearestHit.hitNormal.x;
  113. normalEnd.y = nearestHit.hitPosition.y + nearestHit.hitNormal.y;
  114. normalEnd.z = nearestHit.hitPosition.z + nearestHit.hitNormal.z;
  115. DrawLine3D(nearestHit.hitPosition, normalEnd, YELLOW);
  116. }
  117. DrawRay(ray, MAROON);
  118. DrawGrid(100, 1.0f);
  119. End3dMode();
  120. // Draw some debug GUI text
  121. DrawText(FormatText("Hit Object: %s", hitObjectName), 10, 50, 10, BLACK);
  122. if (nearestHit.hit)
  123. {
  124. int ypos = 70;
  125. DrawText(FormatText("Distance: %3.2f", nearestHit.distance), 10, ypos, 10, BLACK);
  126. DrawText(FormatText("Hit Pos: %3.2f %3.2f %3.2f",
  127. nearestHit.hitPosition.x,
  128. nearestHit.hitPosition.y,
  129. nearestHit.hitPosition.z), 10, ypos + 15, 10, BLACK);
  130. DrawText(FormatText("Hit Norm: %3.2f %3.2f %3.2f",
  131. nearestHit.hitNormal.x,
  132. nearestHit.hitNormal.y,
  133. nearestHit.hitNormal.z), 10, ypos + 30, 10, BLACK);
  134. if (hitTriangle) DrawText(FormatText("Barycenter: %3.2f %3.2f %3.2f", bary.x, bary.y, bary.z), 10, ypos + 45, 10, BLACK);
  135. }
  136. DrawText("Use Mouse to Move Camera", 10, 430, 10, GRAY);
  137. DrawFPS(10, 10);
  138. EndDrawing();
  139. //----------------------------------------------------------------------------------
  140. }
  141. // De-Initialization
  142. //--------------------------------------------------------------------------------------
  143. CloseWindow(); // Close window and OpenGL context
  144. //--------------------------------------------------------------------------------------
  145. return 0;
  146. }