core_3d_camera_free.c 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Initialize 3d camera free
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * Example originally created with raylib 1.3, last time updated with raylib 1.3
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2015-2024 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. //------------------------------------------------------------------------------------
  17. // Program main entry point
  18. //------------------------------------------------------------------------------------
  19. int main(void)
  20. {
  21. // Initialization
  22. //--------------------------------------------------------------------------------------
  23. const int screenWidth = 800;
  24. const int screenHeight = 450;
  25. InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera free");
  26. // Define the camera to look into our 3d world
  27. Camera3D camera = { 0 };
  28. camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
  29. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  30. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  31. camera.fovy = 45.0f; // Camera field-of-view Y
  32. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  33. Vector3 cubePosition = { 0.0f, 0.0f, 0.0f };
  34. DisableCursor(); // Limit cursor to relative movement inside the window
  35. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  36. //--------------------------------------------------------------------------------------
  37. // Main game loop
  38. while (!WindowShouldClose()) // Detect window close button or ESC key
  39. {
  40. // Update
  41. //----------------------------------------------------------------------------------
  42. UpdateCamera(&camera, CAMERA_FREE);
  43. if (IsKeyPressed('Z')) camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
  44. //----------------------------------------------------------------------------------
  45. // Draw
  46. //----------------------------------------------------------------------------------
  47. BeginDrawing();
  48. ClearBackground(RAYWHITE);
  49. BeginMode3D(camera);
  50. DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED);
  51. DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, MAROON);
  52. DrawGrid(10, 1.0f);
  53. EndMode3D();
  54. DrawRectangle( 10, 10, 320, 93, Fade(SKYBLUE, 0.5f));
  55. DrawRectangleLines( 10, 10, 320, 93, BLUE);
  56. DrawText("Free camera default controls:", 20, 20, 10, BLACK);
  57. DrawText("- Mouse Wheel to Zoom in-out", 40, 40, 10, DARKGRAY);
  58. DrawText("- Mouse Wheel Pressed to Pan", 40, 60, 10, DARKGRAY);
  59. DrawText("- Z to zoom to (0, 0, 0)", 40, 80, 10, DARKGRAY);
  60. EndDrawing();
  61. //----------------------------------------------------------------------------------
  62. }
  63. // De-Initialization
  64. //--------------------------------------------------------------------------------------
  65. CloseWindow(); // Close window and OpenGL context
  66. //--------------------------------------------------------------------------------------
  67. return 0;
  68. }