core_3d_picking.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Picking in 3d mode
  4. *
  5. * This example has been created using raylib 1.3 (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. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. int main()
  13. {
  14. // Initialization
  15. //--------------------------------------------------------------------------------------
  16. int screenWidth = 800;
  17. int screenHeight = 450;
  18. InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d picking");
  19. // Define the camera to look into our 3d world
  20. Camera camera = {{ 0.0, 10.0, 10.0 }, { 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }};
  21. Vector3 cubePosition = { 0.0, 1.0, 0.0 };
  22. Ray ray; // Picking line ray
  23. SetCameraMode(CAMERA_FREE); // Set a free camera mode
  24. SetCameraPosition(camera.position); // Set internal camera position to match our camera position
  25. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  26. //--------------------------------------------------------------------------------------
  27. // Main game loop
  28. while (!WindowShouldClose()) // Detect window close button or ESC key
  29. {
  30. // Update
  31. //----------------------------------------------------------------------------------
  32. UpdateCamera(&camera); // Update internal camera and our camera
  33. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
  34. {
  35. // NOTE: This function is NOT WORKING properly!
  36. ray = GetMouseRay(GetMousePosition(), camera);
  37. // TODO: Check collision between ray and box
  38. }
  39. //----------------------------------------------------------------------------------
  40. // Draw
  41. //----------------------------------------------------------------------------------
  42. BeginDrawing();
  43. ClearBackground(RAYWHITE);
  44. Begin3dMode(camera);
  45. DrawCube(cubePosition, 2, 2, 2, GRAY);
  46. DrawCubeWires(cubePosition, 2, 2, 2, DARKGRAY);
  47. DrawGrid(10.0, 1.0);
  48. DrawRay(ray, MAROON);
  49. End3dMode();
  50. DrawText("Try selecting the box with mouse!", 240, 10, 20, GRAY);
  51. DrawFPS(10, 10);
  52. EndDrawing();
  53. //----------------------------------------------------------------------------------
  54. }
  55. // De-Initialization
  56. //--------------------------------------------------------------------------------------
  57. CloseWindow(); // Close window and OpenGL context
  58. //--------------------------------------------------------------------------------------
  59. return 0;
  60. }