No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

222 líneas
8.6 KiB

hace 5 años
  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.projection = 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[MATERIAL_MAP_DIFFUSE].texture = texture; // Set model diffuse texture
  34. Vector3 towerPos = { 0.0f, 0.0f, 0.0f }; // Set model position
  35. BoundingBox towerBBox = GetMeshBoundingBox(tower.meshes[0]); // Get mesh bounding box
  36. // Ground quad
  37. Vector3 g0 = (Vector3){ -50.0f, 0.0f, -50.0f };
  38. Vector3 g1 = (Vector3){ -50.0f, 0.0f, 50.0f };
  39. Vector3 g2 = (Vector3){ 50.0f, 0.0f, 50.0f };
  40. Vector3 g3 = (Vector3){ 50.0f, 0.0f, -50.0f };
  41. // Test triangle
  42. Vector3 ta = (Vector3){ -25.0f, 0.5f, 0.0f };
  43. Vector3 tb = (Vector3){ -4.0f, 2.5f, 1.0f };
  44. Vector3 tc = (Vector3){ -8.0f, 6.5f, 0.0f };
  45. Vector3 bary = { 0.0f, 0.0f, 0.0f };
  46. // Test sphere
  47. Vector3 sp = (Vector3){ -30.0f, 5.0f, 5.0f };
  48. float sr = 4.0f;
  49. SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
  50. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  51. //--------------------------------------------------------------------------------------
  52. // Main game loop
  53. while (!WindowShouldClose()) // Detect window close button or ESC key
  54. {
  55. // Update
  56. //----------------------------------------------------------------------------------
  57. UpdateCamera(&camera); // Update camera
  58. // Display information about closest hit
  59. RayCollision collision = { 0 };
  60. char *hitObjectName = "None";
  61. collision.distance = FLT_MAX;
  62. collision.hit = false;
  63. Color cursorColor = WHITE;
  64. // Get ray and test against objects
  65. ray = GetMouseRay(GetMousePosition(), camera);
  66. // Check ray collision against ground quad
  67. RayCollision groundHitInfo = GetRayCollisionQuad(ray, g0, g1, g2, g3);
  68. if ((groundHitInfo.hit) && (groundHitInfo.distance < collision.distance))
  69. {
  70. collision = groundHitInfo;
  71. cursorColor = GREEN;
  72. hitObjectName = "Ground";
  73. }
  74. // Check ray collision against test triangle
  75. RayCollision triHitInfo = GetRayCollisionTriangle(ray, ta, tb, tc);
  76. if ((triHitInfo.hit) && (triHitInfo.distance < collision.distance))
  77. {
  78. collision = triHitInfo;
  79. cursorColor = PURPLE;
  80. hitObjectName = "Triangle";
  81. bary = Vector3Barycenter(collision.point, ta, tb, tc);
  82. }
  83. // Check ray collision against test sphere
  84. RayCollision sphereHitInfo = GetRayCollisionSphere(ray, sp, sr);
  85. if ((sphereHitInfo.hit) && (sphereHitInfo.distance < collision.distance))
  86. {
  87. collision = sphereHitInfo;
  88. cursorColor = ORANGE;
  89. hitObjectName = "Sphere";
  90. }
  91. // Check ray collision against bounding box first, before trying the full ray-mesh test
  92. RayCollision boxHitInfo = GetRayCollisionBox(ray, towerBBox);
  93. if ((boxHitInfo.hit) && (boxHitInfo.distance < collision.distance))
  94. {
  95. collision = boxHitInfo;
  96. cursorColor = ORANGE;
  97. hitObjectName = "Box";
  98. // Check ray collision against model
  99. // NOTE: It considers model.transform matrix!
  100. RayCollision meshHitInfo = GetRayCollisionModel(ray, tower);
  101. if (meshHitInfo.hit)
  102. {
  103. collision = meshHitInfo;
  104. cursorColor = ORANGE;
  105. hitObjectName = "Mesh";
  106. }
  107. }
  108. //----------------------------------------------------------------------------------
  109. // Draw
  110. //----------------------------------------------------------------------------------
  111. BeginDrawing();
  112. ClearBackground(RAYWHITE);
  113. BeginMode3D(camera);
  114. // Draw the tower
  115. // WARNING: If scale is different than 1.0f,
  116. // not considered by GetRayCollisionModel()
  117. DrawModel(tower, towerPos, 1.0f, WHITE);
  118. // Draw the test triangle
  119. DrawLine3D(ta, tb, PURPLE);
  120. DrawLine3D(tb, tc, PURPLE);
  121. DrawLine3D(tc, ta, PURPLE);
  122. // Draw the test sphere
  123. DrawSphereWires(sp, sr, 8, 8, PURPLE);
  124. // Draw the mesh bbox if we hit it
  125. if (boxHitInfo.hit) DrawBoundingBox(towerBBox, LIME);
  126. // If we hit something, draw the cursor at the hit point
  127. if (collision.hit)
  128. {
  129. DrawCube(collision.point, 0.3f, 0.3f, 0.3f, cursorColor);
  130. DrawCubeWires(collision.point, 0.3f, 0.3f, 0.3f, RED);
  131. Vector3 normalEnd;
  132. normalEnd.x = collision.point.x + collision.normal.x;
  133. normalEnd.y = collision.point.y + collision.normal.y;
  134. normalEnd.z = collision.point.z + collision.normal.z;
  135. DrawLine3D(collision.point, normalEnd, RED);
  136. }
  137. DrawRay(ray, MAROON);
  138. DrawGrid(10, 10.0f);
  139. EndMode3D();
  140. // Draw some debug GUI text
  141. DrawText(TextFormat("Hit Object: %s", hitObjectName), 10, 50, 10, BLACK);
  142. if (collision.hit)
  143. {
  144. int ypos = 70;
  145. DrawText(TextFormat("Distance: %3.2f", collision.distance), 10, ypos, 10, BLACK);
  146. DrawText(TextFormat("Hit Pos: %3.2f %3.2f %3.2f",
  147. collision.point.x,
  148. collision.point.y,
  149. collision.point.z), 10, ypos + 15, 10, BLACK);
  150. DrawText(TextFormat("Hit Norm: %3.2f %3.2f %3.2f",
  151. collision.normal.x,
  152. collision.normal.y,
  153. collision.normal.z), 10, ypos + 30, 10, BLACK);
  154. if (triHitInfo.hit && TextIsEqual(hitObjectName, "Triangle"))
  155. DrawText(TextFormat("Barycenter: %3.2f %3.2f %3.2f", bary.x, bary.y, bary.z), 10, ypos + 45, 10, BLACK);
  156. }
  157. DrawText("Use Mouse to Move Camera", 10, 430, 10, GRAY);
  158. DrawText("(c) Turret 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY);
  159. DrawFPS(10, 10);
  160. EndDrawing();
  161. //----------------------------------------------------------------------------------
  162. }
  163. // De-Initialization
  164. //--------------------------------------------------------------------------------------
  165. UnloadModel(tower); // Unload model
  166. UnloadTexture(texture); // Unload texture
  167. CloseWindow(); // Close window and OpenGL context
  168. //--------------------------------------------------------------------------------------
  169. return 0;
  170. }