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.

84 lines
3.5 KiB

  1. --------------------------------------------------------------------------------------------
  2. --
  3. -- raylib [core] example - 3d camera first person
  4. --
  5. -- This example has been created using raylib 1.6 (www.raylib.com)
  6. -- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. --
  8. -- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5)
  9. --
  10. --------------------------------------------------------------------------------------------
  11. MAX_COLUMNS = 20
  12. -- Initialization
  13. -------------------------------------------------------------------------------------------
  14. local screenWidth = 800
  15. local screenHeight = 450
  16. InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera first person")
  17. -- Define the camera to look into our 3d world (position, target, up vector)
  18. local camera = Camera(Vector3(0.0, 10.0, 10.0), Vector3(0.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0), 60.0)
  19. -- Generates some random columns
  20. local heights = {}
  21. local positions = {}
  22. local colors = {}
  23. for i = 1, MAX_COLUMNS do
  24. heights[i] = GetRandomValue(1, 12)
  25. positions[i] = Vector3(GetRandomValue(-15, 15), heights[i]/2, GetRandomValue(-15, 15))
  26. colors[i] = Color(GetRandomValue(20, 255), GetRandomValue(10, 55), 30, 255)
  27. end
  28. local playerPosition = Vector3(4.0, 2.0, 4.0) -- Define player position
  29. SetCameraMode(CameraMode.FIRST_PERSON) -- Set a first person camera mode
  30. SetCameraFovy(camera.fovy) -- Set internal camera field-of-view Y
  31. SetTargetFPS(60) -- Set our game to run at 60 frames-per-second
  32. -------------------------------------------------------------------------------------------
  33. -- Main game loop
  34. while not WindowShouldClose() do -- Detect window close button or ESC key
  35. -- Update
  36. ---------------------------------------------------------------------------------------
  37. camera, playerPosition = UpdateCameraPlayer(camera, playerPosition) -- Update camera and player position
  38. ---------------------------------------------------------------------------------------
  39. -- Draw
  40. ---------------------------------------------------------------------------------------
  41. BeginDrawing()
  42. ClearBackground(RAYWHITE)
  43. Begin3dMode(camera)
  44. DrawPlane(Vector3(0.0, 0.0, 0.0), Vector2(32.0, 32.0), LIGHTGRAY) -- Draw ground
  45. DrawCube(Vector3(-16.0, 2.5, 0.0), 1.0, 5.0, 32.0, BLUE) -- Draw a blue wall
  46. DrawCube(Vector3(16.0, 2.5, 0.0), 1.0, 5.0, 32.0, LIME) -- Draw a green wall
  47. DrawCube(Vector3(0.0, 2.5, 16.0), 32.0, 5.0, 1.0, GOLD) -- Draw a yellow wall
  48. -- Draw some cubes around
  49. for i = 1, MAX_COLUMNS do
  50. DrawCube(positions[i], 2.0, heights[i], 2.0, colors[i])
  51. DrawCubeWires(positions[i], 2.0, heights[i], 2.0, MAROON)
  52. end
  53. End3dMode()
  54. DrawRectangle( 10, 10, 220, 70, Fade(SKYBLUE, 0.5))
  55. DrawRectangleLines( 10, 10, 220, 70, BLUE)
  56. DrawText("First person camera default controls:", 20, 20, 10, BLACK)
  57. DrawText("- Move with keys: W, A, S, D", 40, 40, 10, DARKGRAY)
  58. DrawText("- Mouse move to look around", 40, 60, 10, DARKGRAY)
  59. EndDrawing()
  60. ---------------------------------------------------------------------------------------
  61. end
  62. -- De-Initialization
  63. -------------------------------------------------------------------------------------------
  64. CloseWindow() -- Close window and OpenGL context
  65. -------------------------------------------------------------------------------------------