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.

194 lines
7.5 KiB

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