models_orthographic_projection.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Show the difference between perspective and orthographic projection
  4. *
  5. * This program is heavily based on the geometric objects example
  6. *
  7. * This example has been created using raylib 1.0 (www.raylib.com)
  8. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  9. *
  10. * Copyright (c) 2014 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #define FOVY_PERSPECTIVE 45.0f
  15. #define WIDTH_ORTHOGRAPHIC 10.0f
  16. int main()
  17. {
  18. // Initialization
  19. //--------------------------------------------------------------------------------------
  20. int screenWidth = 800;
  21. int screenHeight = 450;
  22. InitWindow(screenWidth, screenHeight, "raylib [models] example - geometric shapes");
  23. // Define the camera to look into our 3d world
  24. Camera camera = {{ 0.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, FOVY_PERSPECTIVE, CAMERA_PERSPECTIVE };
  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. // TODO: Update your variables here
  33. //----------------------------------------------------------------------------------
  34. //
  35. // Input
  36. //----------------------------------------------------------------------------------
  37. if(IsKeyPressed(KEY_SPACE))
  38. {
  39. if(camera.type == CAMERA_PERSPECTIVE)
  40. {
  41. camera.fovy = WIDTH_ORTHOGRAPHIC;
  42. camera.type = CAMERA_ORTHOGRAPHIC;
  43. }
  44. else
  45. {
  46. camera.fovy = FOVY_PERSPECTIVE;
  47. camera.type = CAMERA_PERSPECTIVE;
  48. }
  49. }
  50. // Draw
  51. //----------------------------------------------------------------------------------
  52. BeginDrawing();
  53. ClearBackground(RAYWHITE);
  54. Begin3dMode(camera);
  55. DrawCube((Vector3){-4.0f, 0.0f, 2.0f}, 2.0f, 5.0f, 2.0f, RED);
  56. DrawCubeWires((Vector3){-4.0f, 0.0f, 2.0f}, 2.0f, 5.0f, 2.0f, GOLD);
  57. DrawCubeWires((Vector3){-4.0f, 0.0f, -2.0f}, 3.0f, 6.0f, 2.0f, MAROON);
  58. DrawSphere((Vector3){-1.0f, 0.0f, -2.0f}, 1.0f, GREEN);
  59. DrawSphereWires((Vector3){1.0f, 0.0f, 2.0f}, 2.0f, 16, 16, LIME);
  60. DrawCylinder((Vector3){4.0f, 0.0f, -2.0f}, 1.0f, 2.0f, 3.0f, 4, SKYBLUE);
  61. DrawCylinderWires((Vector3){4.0f, 0.0f, -2.0f}, 1.0f, 2.0f, 3.0f, 4, DARKBLUE);
  62. DrawCylinderWires((Vector3){4.5f, -1.0f, 2.0f}, 1.0f, 1.0f, 2.0f, 6, BROWN);
  63. DrawCylinder((Vector3){1.0f, 0.0f, -4.0f}, 0.0f, 1.5f, 3.0f, 8, GOLD);
  64. DrawCylinderWires((Vector3){1.0f, 0.0f, -4.0f}, 0.0f, 1.5f, 3.0f, 8, PINK);
  65. DrawGrid(10, 1.0f); // Draw a grid
  66. End3dMode();
  67. DrawFPS(10, 10);
  68. DrawText("Press Spacebar to switch camera type", 10, 40, 24, BLACK);
  69. if(camera.type == CAMERA_ORTHOGRAPHIC)
  70. {
  71. DrawText("Orthographic", 10, 65, 24, BLACK);
  72. }
  73. else if(camera.type == CAMERA_PERSPECTIVE)
  74. {
  75. DrawText("Perspective", 10, 65, 24, BLACK);
  76. }
  77. EndDrawing();
  78. //----------------------------------------------------------------------------------
  79. }
  80. // De-Initialization
  81. //--------------------------------------------------------------------------------------
  82. CloseWindow(); // Close window and OpenGL context
  83. //--------------------------------------------------------------------------------------
  84. return 0;
  85. }