1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#include <stdlib.h>
#include "raylib.h"
#include "raymath.h"
#include "rfps_defines.h"
#include "rfps_utils.h"
static Vector2 lastMousePos;
static Vector3 movement = {0, -0.2f, 0};
static Vector3 rotation;
int main(void) {
// create window
InitWindow(G_SCREENWIDTH, G_SCREENHEIGHT, G_TITLE);
// create image
Image checked = GenImageChecked(2, 2, 1, 1, BLACK, RAYWHITE);
Texture2D texture = LoadTextureFromImage(checked);
UnloadImage(checked);
// create model with image as texture
Model model = {0};
model = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
// setup camera
Camera camera = {
{5.0f, 5.0f, 5.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, 45.0f, 0};
SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set camera mode
camera.position = (Vector3){50.0f, 1.0f, 50.0f};
camera.target = (Vector3){50.0f, 1.0f, 50.0f};
camera.up = (Vector3){0.0f, 1.0f, 0.0f};
camera.fovy = 60.0f;
camera.projection = CAMERA_PERSPECTIVE;
rotation = (Vector3){0.0f, 0.0f, 0.0f};
lastMousePos = GetMousePosition();
SetTargetFPS(60);
// game loop
while (!WindowShouldClose()) {
// mouse look
Vector2 mouseMovement =
Vector2Subtract(GetMousePosition(), lastMousePos);
lastMousePos = GetMousePosition();
rotation.x += (mouseMovement.x * -G_CAMERA_MOUSE_MOVE_SENSITIVITY);
rotation.y += (mouseMovement.y * -G_CAMERA_MOUSE_MOVE_SENSITIVITY);
// direction array
bool direction[4] = {IsKeyDown(KEY_W), IsKeyDown(KEY_S),
IsKeyDown(KEY_D), IsKeyDown(KEY_A)};
// compute movement
movement = (Vector3){0, 0, 0};
movement.x =
(sinf(rotation.x) * direction[1] - sinf(rotation.x) * direction[0] -
cosf(rotation.x) * direction[3] +
cosf(rotation.x) * direction[2]) /
G_PLAYER_MOVEMENT_SENSITIVITY;
movement.z =
(cosf(rotation.x) * direction[1] - cosf(rotation.x) * direction[0] +
sinf(rotation.x) * direction[3] -
sinf(rotation.x) * direction[2]) /
G_PLAYER_MOVEMENT_SENSITIVITY;
// elevation
if (IsKeyDown(KEY_SPACE))
movement.y -= 0.12f;
if (IsKeyDown(KEY_LEFT_CONTROL))
movement.y += 0.12f;
// apply movement
camera.position.x += movement.x / G_PLAYER_MOVEMENT_SENSITIVITY;
camera.position.y += movement.y;
camera.position.z += movement.z / G_PLAYER_MOVEMENT_SENSITIVITY;
Matrix translation = MatrixTranslate(0, 0, (10));
Matrix rotation2 = MatrixRotateXYZ(
(Vector3){PI * 2 - rotation.y, PI * 2 - rotation.x, 0});
Matrix transform = MatrixMultiply(translation, rotation2);
// apply camera rotation
camera.target.x = camera.position.x - transform.m12;
camera.target.y = camera.position.y - transform.m13;
camera.target.z = camera.position.z - transform.m14;
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
// draw checkerboard model
for (int x = 0; x < 50; x++) {
for (int z = 0; z < 50; z++) {
Vector3 position;
position.x = x;
position.y = 0;
position.z = z;
DrawModel(model, position, 1.0f, WHITE);
}
}
EndMode3D();
DrawFPS(0, 0);
DrawText("Controls - Mouse / WASD / R/F.", 400, 0, 20, DARKGRAY);
EndDrawing();
}
UnloadTexture(texture);
UnloadModel(model);
CloseWindow();
return 0;
}
|