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 | |
12 | #include "raylib.h" |
13 | |
14 | #define MAX_COLUMNS 20 |
15 | |
16 | int main(void) |
17 | { |
18 | // Initialization |
19 | //-------------------------------------------------------------------------------------- |
20 | const int screenWidth = 800; |
21 | const int screenHeight = 450; |
22 | |
23 | InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera first person" ); |
24 | |
25 | // Define the camera to look into our 3d world (position, target, up vector) |
26 | Camera camera = { 0 }; |
27 | camera.position = (Vector3){ 4.0f, 2.0f, 4.0f }; |
28 | camera.target = (Vector3){ 0.0f, 1.8f, 0.0f }; |
29 | camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; |
30 | camera.fovy = 60.0f; |
31 | camera.type = CAMERA_PERSPECTIVE; |
32 | |
33 | // Generates some random columns |
34 | float heights[MAX_COLUMNS] = { 0.0f }; |
35 | Vector3 positions[MAX_COLUMNS] = { 0 }; |
36 | Color colors[MAX_COLUMNS] = { 0 }; |
37 | |
38 | for (int i = 0; i < MAX_COLUMNS; i++) |
39 | { |
40 | heights[i] = (float)GetRandomValue(1, 12); |
41 | positions[i] = (Vector3){ GetRandomValue(-15, 15), heights[i]/2, GetRandomValue(-15, 15) }; |
42 | colors[i] = (Color){ GetRandomValue(20, 255), GetRandomValue(10, 55), 30, 255 }; |
43 | } |
44 | |
45 | SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set a first person camera mode |
46 | |
47 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
48 | //-------------------------------------------------------------------------------------- |
49 | |
50 | // Main game loop |
51 | while (!WindowShouldClose()) // Detect window close button or ESC key |
52 | { |
53 | // Update |
54 | //---------------------------------------------------------------------------------- |
55 | UpdateCamera(&camera); // Update camera |
56 | //---------------------------------------------------------------------------------- |
57 | |
58 | // Draw |
59 | //---------------------------------------------------------------------------------- |
60 | BeginDrawing(); |
61 | |
62 | ClearBackground(RAYWHITE); |
63 | |
64 | BeginMode3D(camera); |
65 | |
66 | DrawPlane((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector2){ 32.0f, 32.0f }, LIGHTGRAY); // Draw ground |
67 | DrawCube((Vector3){ -16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, BLUE); // Draw a blue wall |
68 | DrawCube((Vector3){ 16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, LIME); // Draw a green wall |
69 | DrawCube((Vector3){ 0.0f, 2.5f, 16.0f }, 32.0f, 5.0f, 1.0f, GOLD); // Draw a yellow wall |
70 | |
71 | // Draw some cubes around |
72 | for (int i = 0; i < MAX_COLUMNS; i++) |
73 | { |
74 | DrawCube(positions[i], 2.0f, heights[i], 2.0f, colors[i]); |
75 | DrawCubeWires(positions[i], 2.0f, heights[i], 2.0f, MAROON); |
76 | } |
77 | |
78 | EndMode3D(); |
79 | |
80 | DrawRectangle( 10, 10, 220, 70, Fade(SKYBLUE, 0.5f)); |
81 | DrawRectangleLines( 10, 10, 220, 70, BLUE); |
82 | |
83 | DrawText("First person camera default controls:" , 20, 20, 10, BLACK); |
84 | DrawText("- Move with keys: W, A, S, D" , 40, 40, 10, DARKGRAY); |
85 | DrawText("- Mouse move to look around" , 40, 60, 10, DARKGRAY); |
86 | |
87 | EndDrawing(); |
88 | //---------------------------------------------------------------------------------- |
89 | } |
90 | |
91 | // De-Initialization |
92 | //-------------------------------------------------------------------------------------- |
93 | CloseWindow(); // Close window and OpenGL context |
94 | //-------------------------------------------------------------------------------------- |
95 | |
96 | return 0; |
97 | } |