core_3d_camera_free.c 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. Camera3D 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. camera.type = CAMERA_PERSPECTIVE; // Camera mode type
  26. Vector3 cubePosition = { 0.0f, 0.0f, 0.0f };
  27. SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
  28. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  29. //--------------------------------------------------------------------------------------
  30. // Main game loop
  31. while (!WindowShouldClose()) // Detect window close button or ESC key
  32. {
  33. // Update
  34. //----------------------------------------------------------------------------------
  35. UpdateCamera(&camera); // Update camera
  36. if (IsKeyDown('Z')) camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
  37. //----------------------------------------------------------------------------------
  38. // Draw
  39. //----------------------------------------------------------------------------------
  40. BeginDrawing();
  41. ClearBackground(RAYWHITE);
  42. BeginMode3D(camera);
  43. DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED);
  44. DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, MAROON);
  45. DrawGrid(10, 1.0f);
  46. EndMode3D();
  47. DrawRectangle( 10, 10, 320, 133, Fade(SKYBLUE, 0.5f));
  48. DrawRectangleLines( 10, 10, 320, 133, BLUE);
  49. DrawText("Free camera default controls:", 20, 20, 10, BLACK);
  50. DrawText("- Mouse Wheel to Zoom in-out", 40, 40, 10, DARKGRAY);
  51. DrawText("- Mouse Wheel Pressed to Pan", 40, 60, 10, DARKGRAY);
  52. DrawText("- Alt + Mouse Wheel Pressed to Rotate", 40, 80, 10, DARKGRAY);
  53. DrawText("- Alt + Ctrl + Mouse Wheel Pressed for Smooth Zoom", 40, 100, 10, DARKGRAY);
  54. DrawText("- Z to zoom to (0, 0, 0)", 40, 120, 10, DARKGRAY);
  55. EndDrawing();
  56. //----------------------------------------------------------------------------------
  57. }
  58. // De-Initialization
  59. //--------------------------------------------------------------------------------------
  60. CloseWindow(); // Close window and OpenGL context
  61. //--------------------------------------------------------------------------------------
  62. return 0;
  63. }