core_3d_camera_free.c 3.7 KB

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