core_3d_camera_first_person.c 3.6 KB

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