core_3d_camera_free.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Initialize 3d camera free
  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 camera free");
  19. // Define the camera to look into our 3d world
  20. Camera camera;
  21. camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
  22. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  23. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  24. camera.fovy = 45.0f; // Camera field-of-view Y
  25. Vector3 cubePosition = { 0.0f, 0.0f, 0.0f };
  26. SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
  27. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  28. //--------------------------------------------------------------------------------------
  29. // Main game loop
  30. while (!WindowShouldClose()) // Detect window close button or ESC key
  31. {
  32. // Update
  33. //----------------------------------------------------------------------------------
  34. UpdateCamera(&camera); // Update camera
  35. if (IsKeyDown('Z')) camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
  36. //----------------------------------------------------------------------------------
  37. // Draw
  38. //----------------------------------------------------------------------------------
  39. BeginDrawing();
  40. ClearBackground(RAYWHITE);
  41. Begin3dMode(camera);
  42. DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED);
  43. DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, MAROON);
  44. DrawGrid(10, 1.0f);
  45. End3dMode();
  46. DrawRectangle( 10, 10, 320, 133, Fade(SKYBLUE, 0.5f));
  47. DrawRectangleLines( 10, 10, 320, 133, BLUE);
  48. DrawText("Free camera default controls:", 20, 20, 10, BLACK);
  49. DrawText("- Mouse Wheel to Zoom in-out", 40, 40, 10, DARKGRAY);
  50. DrawText("- Mouse Wheel Pressed to Pan", 40, 60, 10, DARKGRAY);
  51. DrawText("- Alt + Mouse Wheel Pressed to Rotate", 40, 80, 10, DARKGRAY);
  52. DrawText("- Alt + Ctrl + Mouse Wheel Pressed for Smooth Zoom", 40, 100, 10, DARKGRAY);
  53. DrawText("- Z to zoom to (0, 0, 0)", 40, 120, 10, DARKGRAY);
  54. EndDrawing();
  55. //----------------------------------------------------------------------------------
  56. }
  57. // De-Initialization
  58. //--------------------------------------------------------------------------------------
  59. CloseWindow(); // Close window and OpenGL context
  60. //--------------------------------------------------------------------------------------
  61. return 0;
  62. }