You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
3.7 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - 3d camera first person
  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. #define MAX_COLUMNS 20
  13. int main()
  14. {
  15. // Initialization
  16. //--------------------------------------------------------------------------------------
  17. int screenWidth = 800;
  18. int screenHeight = 450;
  19. InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera first person");
  20. // Define the camera to look into our 3d world (position, target, up vector)
  21. Camera camera = {{ 4.0f, 2.0f, 4.0f }, { 0.0f, 1.8f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 60.0f };
  22. // Generates some random columns
  23. float heights[MAX_COLUMNS];
  24. Vector3 positions[MAX_COLUMNS];
  25. Color colors[MAX_COLUMNS];
  26. for (int i = 0; i < MAX_COLUMNS; i++)
  27. {
  28. heights[i] = (float)GetRandomValue(1, 12);
  29. positions[i] = (Vector3){ GetRandomValue(-15, 15), heights[i]/2, GetRandomValue(-15, 15) };
  30. colors[i] = (Color){ GetRandomValue(20, 255), GetRandomValue(10, 55), 30, 255 };
  31. }
  32. SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set a first person camera mode
  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); // Update camera
  41. //----------------------------------------------------------------------------------
  42. // Draw
  43. //----------------------------------------------------------------------------------
  44. BeginDrawing();
  45. ClearBackground(RAYWHITE);
  46. Begin3dMode(camera);
  47. DrawPlane((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector2){ 32.0f, 32.0f }, LIGHTGRAY); // Draw ground
  48. DrawCube((Vector3){ -16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, BLUE); // Draw a blue wall
  49. DrawCube((Vector3){ 16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, LIME); // Draw a green wall
  50. DrawCube((Vector3){ 0.0f, 2.5f, 16.0f }, 32.0f, 5.0f, 1.0f, GOLD); // Draw a yellow wall
  51. // Draw some cubes around
  52. for (int i = 0; i < MAX_COLUMNS; i++)
  53. {
  54. DrawCube(positions[i], 2.0f, heights[i], 2.0f, colors[i]);
  55. DrawCubeWires(positions[i], 2.0f, heights[i], 2.0f, MAROON);
  56. }
  57. End3dMode();
  58. DrawRectangle( 10, 10, 220, 70, Fade(SKYBLUE, 0.5f));
  59. DrawRectangleLines( 10, 10, 220, 70, BLUE);
  60. DrawText("First person camera default controls:", 20, 20, 10, BLACK);
  61. DrawText("- Move with keys: W, A, S, D", 40, 40, 10, DARKGRAY);
  62. DrawText("- Mouse move to look around", 40, 60, 10, DARKGRAY);
  63. EndDrawing();
  64. //----------------------------------------------------------------------------------
  65. }
  66. // De-Initialization
  67. //--------------------------------------------------------------------------------------
  68. CloseWindow(); // Close window and OpenGL context
  69. //--------------------------------------------------------------------------------------
  70. return 0;
  71. }