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.

94 lines
3.9 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 = {{ 0.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 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. Vector3 playerPosition = { 4.0f, 2.0f, 4.0f }; // Define player position
  33. SetCameraMode(CAMERA_FIRST_PERSON); // Set a first person camera mode
  34. SetCameraFovy(camera.fovy); // Set internal camera field-of-view Y
  35. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  36. //--------------------------------------------------------------------------------------
  37. // Main game loop
  38. while (!WindowShouldClose()) // Detect window close button or ESC key
  39. {
  40. // Update
  41. //----------------------------------------------------------------------------------
  42. UpdateCameraPlayer(&camera, &playerPosition); // Update camera and player position
  43. //----------------------------------------------------------------------------------
  44. // Draw
  45. //----------------------------------------------------------------------------------
  46. BeginDrawing();
  47. ClearBackground(RAYWHITE);
  48. Begin3dMode(camera);
  49. DrawPlane((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector2){ 32.0f, 32.0f }, LIGHTGRAY); // Draw ground
  50. DrawCube((Vector3){ -16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, BLUE); // Draw a blue wall
  51. DrawCube((Vector3){ 16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, LIME); // Draw a green wall
  52. DrawCube((Vector3){ 0.0f, 2.5f, 16.0f }, 32.0f, 5.0f, 1.0f, GOLD); // Draw a yellow wall
  53. // Draw some cubes around
  54. for (int i = 0; i < MAX_COLUMNS; i++)
  55. {
  56. DrawCube(positions[i], 2.0f, heights[i], 2.0f, colors[i]);
  57. DrawCubeWires(positions[i], 2.0f, heights[i], 2.0f, MAROON);
  58. }
  59. End3dMode();
  60. DrawRectangle( 10, 10, 220, 70, Fade(SKYBLUE, 0.5f));
  61. DrawRectangleLines( 10, 10, 220, 70, BLUE);
  62. DrawText("First person camera default controls:", 20, 20, 10, BLACK);
  63. DrawText("- Move with keys: W, A, S, D", 40, 40, 10, DARKGRAY);
  64. DrawText("- Mouse move to look around", 40, 60, 10, DARKGRAY);
  65. EndDrawing();
  66. //----------------------------------------------------------------------------------
  67. }
  68. // De-Initialization
  69. //--------------------------------------------------------------------------------------
  70. CloseWindow(); // Close window and OpenGL context
  71. //--------------------------------------------------------------------------------------
  72. return 0;
  73. }