-
Notifications
You must be signed in to change notification settings - Fork 0
/
floor_render.hpp
216 lines (159 loc) · 6.47 KB
/
floor_render.hpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#ifndef FLOOR_RENDER_HPP // include guard
#define FLOOR_RENDER_HPP
#include <cmath>
#include <vector>
#include <SFML/Graphics.hpp>
#include "settings.hpp"
#include "player.hpp"
class RayFloor
{
public:
sf::Vector2f uv11;
sf::Vector2f uv12;
sf::Vector2f uv21;
sf::Vector2f uv22;
RayFloor()
{
uv11 = sf::Vector2f(0.f, 0.f);
uv12 = sf::Vector2f(0.f, 0.f);
uv21 = sf::Vector2f(0.f, 0.f);
uv22 = sf::Vector2f(0.f, 0.f);
};
};
class Raycast_floor
{
public:
std::vector<RayFloor> rays;
Raycast_floor(Player &player)
{
float screen_dist = 1.f;
float half_width = screen_dist * tan(HALF_FOV);
float half_height = half_width * WINDOW_HEIGHT / WINDOW_WIDTH;
float scale = half_height / HALF_HEIGHT;
float camera_shake = head_bob(player.x, player.y);
sf::Vector2f player_pos = sf::Vector2f(player.x, player.y);
float a = player.angle;
float cos_a = cos(a);
float sin_a = sin(a);
float min_ray_depth = screen_dist / half_height * (PLAYER_Z + camera_shake);
float ray_depth = min_ray_depth;
float ray_screen_height = 0.f;
while (ray_depth < MAX_DISTANCE) {
RayFloor ray;
ray_screen_height = ray_screen_height + scale;
float texture_height = screen_dist
/ (half_height - ray_screen_height) * (PLAYER_Z + camera_shake) - ray_depth;
float texture_half_width = half_width / screen_dist
* (ray_depth + texture_height / 2.0);
sf::Vector2f ray_dir1 = sf::Vector2f(
ray_depth * cos_a,
ray_depth * sin_a
);
sf::Vector2f ray_dir2 = sf::Vector2f(
(ray_depth + texture_height) * cos_a,
(ray_depth + texture_height) * sin_a
);
sf::Vector2f perp_dir = sf::Vector2f(
texture_half_width * sin_a,
-texture_half_width * cos_a
);
ray.uv11 = (player_pos + ray_dir2 + perp_dir) / FLOOR_SIZE;
ray.uv12 = (player_pos + ray_dir2 - perp_dir) / FLOOR_SIZE;
ray.uv21 = (player_pos + ray_dir1 + perp_dir) / FLOOR_SIZE;
ray.uv22 = (player_pos + ray_dir1 - perp_dir) / FLOOR_SIZE;
ray.uv11 *= FLOOR_TEXTURE_SIZE;
ray.uv12 *= FLOOR_TEXTURE_SIZE;
ray.uv21 *= FLOOR_TEXTURE_SIZE;
ray.uv22 *= FLOOR_TEXTURE_SIZE;
rays.push_back(ray);
ray_depth = ray_depth + texture_height;
};
};
sf::VertexArray draw_floor () {
int rays_number = rays.size();
sf::VertexArray to_draw(sf::Quads, rays_number * 4);
float height_offset;
for (int k = 0; k < rays_number; k++)
{
height_offset = k * 1.f;
to_draw[4 * k].position = sf::Vector2f(0.0, WINDOW_HEIGHT - (height_offset + STEP_SIZE));
to_draw[4 * k + 1].position = sf::Vector2f(0.0, WINDOW_HEIGHT - height_offset);
to_draw[4 * k + 2].position = sf::Vector2f(WINDOW_WIDTH, WINDOW_HEIGHT - height_offset);
to_draw[4 * k + 3].position = sf::Vector2f(WINDOW_WIDTH, WINDOW_HEIGHT - (height_offset + STEP_SIZE));
to_draw[4 * k].texCoords = rays[k].uv11;
to_draw[4 * k + 1].texCoords = rays[k].uv21;
to_draw[4 * k + 2].texCoords = rays[k].uv22;
to_draw[4 * k + 3].texCoords = rays[k].uv12;
};
return(to_draw);
};
};
class Raycast_ceiling
{
public:
std::vector<RayFloor> rays;
Raycast_ceiling(Player &player)
{
float screen_dist = 1.f;
float half_width = screen_dist * tan(HALF_FOV);
float half_height = half_width * WINDOW_HEIGHT / WINDOW_WIDTH;
float scale = half_height / HALF_HEIGHT;
float camera_shake = head_bob(player.x, player.y);
sf::Vector2f player_pos = sf::Vector2f(player.x, player.y);
float a = player.angle;
float cos_a = cos(a);
float sin_a = sin(a);
float min_ray_depth = screen_dist / half_height * (PLAYER_Z - camera_shake);
float ray_depth = min_ray_depth;
float ray_screen_height = 0.f;
while (ray_depth < MAX_DISTANCE) {
RayFloor ray;
ray_screen_height = ray_screen_height + scale;
float texture_height = screen_dist
/ (half_height - ray_screen_height) * (PLAYER_Z - camera_shake) - ray_depth;
float texture_half_width = half_width / screen_dist
* (ray_depth + texture_height / 2.0);
sf::Vector2f ray_dir1 = sf::Vector2f(
ray_depth * cos_a,
ray_depth * sin_a
);
sf::Vector2f ray_dir2 = sf::Vector2f(
(ray_depth + texture_height) * cos_a,
(ray_depth + texture_height) * sin_a
);
sf::Vector2f perp_dir = sf::Vector2f(
texture_half_width * sin_a,
-texture_half_width * cos_a
);
ray.uv11 = (player_pos + ray_dir2 + perp_dir) / FLOOR_SIZE;
ray.uv12 = (player_pos + ray_dir2 - perp_dir) / FLOOR_SIZE;
ray.uv21 = (player_pos + ray_dir1 + perp_dir) / FLOOR_SIZE;
ray.uv22 = (player_pos + ray_dir1 - perp_dir) / FLOOR_SIZE;
ray.uv11 *= FLOOR_TEXTURE_SIZE;
ray.uv12 *= FLOOR_TEXTURE_SIZE;
ray.uv21 *= FLOOR_TEXTURE_SIZE;
ray.uv22 *= FLOOR_TEXTURE_SIZE;
rays.push_back(ray);
ray_depth = ray_depth + texture_height;
};
};
sf::VertexArray draw_ceiling () {
int rays_number = rays.size();
sf::VertexArray to_draw(sf::Quads, rays_number * 4);
float height_offset;
for (int k = 0; k < rays_number; k++)
{
height_offset = k * 1.f;
to_draw[4 * k].position = sf::Vector2f(0.0, height_offset);
to_draw[4 * k + 1].position = sf::Vector2f(0.0, height_offset + STEP_SIZE);
to_draw[4 * k + 2].position = sf::Vector2f(WINDOW_WIDTH, height_offset + STEP_SIZE);
to_draw[4 * k + 3].position = sf::Vector2f(WINDOW_WIDTH, height_offset);
to_draw[4 * k].texCoords = rays[k].uv11;
to_draw[4 * k + 1].texCoords = rays[k].uv21;
to_draw[4 * k + 2].texCoords = rays[k].uv22;
to_draw[4 * k + 3].texCoords = rays[k].uv12;
};
return(to_draw);
};
};
#endif /* FLOOR_RENDER_HPP */