-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
249 lines (215 loc) · 7.3 KB
/
main.cpp
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <string>
#include <vector>
#include "graphics.h"
#include "input.h"
#include "display.h"
const unsigned short WIDTH = 1280;
const unsigned short HEIGHT = (WIDTH / 4) * 3;
Graphics graphics;
Input input;
double playerX = 22;
double playerY = 12;
double dirX = -1;
double dirY = 0;
double planeX = 0;
double planeY = 0.66;
const double moveSpeed = 0.1; // squares per second
const double rotSpeed = 0.1; // radians per second
#define mapWidth 26
#define mapHeight 26
char worldMap[mapWidth][mapHeight]=
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
void pollInput(void);
void renderWalls(void);
void renderBackground(void);
int main(int argc, char * argv)
{
Display display(WIDTH, HEIGHT, false, "Raycasting Game");
graphics.loadTexture("red.tga"); // these have to be loaded in the correct order
graphics.loadTexture("green.tga"); // the order corresponds to the numbers on the map
graphics.loadTexture("blue.tga");
graphics.loadTexture("yellow.tga");
bool running = true;
while (running)
{
pollInput();
renderBackground();
renderWalls();
graphics.flush();
running = !input.isKeyDown(GLFW_KEY_ESC) && !display.isCloseRequested();
}
display.destroy();
exit(0);
}
void pollInput(void)
{
//move forward if there isn't a wall
if (input.isKeyDown(GLFW_KEY_UP) || input.isKeyDown('W'))
{
if(worldMap[int(playerX + dirX * moveSpeed)][int(playerY)] == false) playerX += dirX * moveSpeed;
if(worldMap[int(playerX)][int(playerY + dirY * moveSpeed)] == false) playerY += dirY * moveSpeed;
}
//move backwards if there isn't a wall
if (input.isKeyDown(GLFW_KEY_DOWN) || input.isKeyDown('S'))
{
if(worldMap[int(playerX - dirX * moveSpeed)][int(playerY)] == false) playerX -= dirX * moveSpeed;
if(worldMap[int(playerX)][int(playerY - dirY * moveSpeed)] == false) playerY -= dirY * moveSpeed;
}
//rotate clockwise
if (input.isKeyDown(GLFW_KEY_RIGHT) || input.isKeyDown('D'))
{
//both direction and camera plane must be rotated
double oldDirX = dirX;
dirX = dirX * cos(-rotSpeed) - dirY * sin(-rotSpeed);
dirY = oldDirX * sin(-rotSpeed) + dirY * cos(-rotSpeed);
double oldPlaneX = planeX;
planeX = planeX * cos(-rotSpeed) - planeY * sin(-rotSpeed);
planeY = oldPlaneX * sin(-rotSpeed) + planeY * cos(-rotSpeed);
}
//rotate anticlockwise
if (input.isKeyDown(GLFW_KEY_LEFT) || input.isKeyDown('A'))
{
//both direction and camera plane must be rotated
double oldDirX = dirX;
dirX = dirX * cos(rotSpeed) - dirY * sin(rotSpeed);
dirY = oldDirX * sin(rotSpeed) + dirY * cos(rotSpeed);
double oldPlaneX = planeX;
planeX = planeX * cos(rotSpeed) - planeY * sin(rotSpeed);
planeY = oldPlaneX * sin(rotSpeed) + planeY * cos(rotSpeed);
}
}
void renderWalls(void)
{
for(int x = 0; x < WIDTH; x++)
{
double cameraX = 2 * x / double(WIDTH) - 1; // x-coordinate in camera space
double rayPosX = playerX;
double rayPosY = playerY;
double rayDirX = dirX + planeX * cameraX;
double rayDirY = dirY + planeY * cameraX;
// which box of the map we're in
int mapX = int(rayPosX);
int mapY = int(rayPosY);
// length of ray from current position to next x or y-side
double sideDistX;
double sideDistY;
// length of ray from one x or y-side to next x or y-side
double deltaDistX = sqrt(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX));
double deltaDistY = sqrt(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY));
double perpWallDist;
// what direction to step in x or y-direction (either +1 or -1)
int stepX;
int stepY;
bool hit = false; // was there a wall hit?
char side; // was a NS or a EW wall hit?
// calculate step and initial side distance
if (rayDirX < 0)
{
stepX = -1;
sideDistX = (rayPosX - mapX) * deltaDistX;
}
else
{
stepX = 1;
sideDistX = (mapX + 1.0 - rayPosX) * deltaDistX;
}
if (rayDirY < 0)
{
stepY = -1;
sideDistY = (rayPosY - mapY) * deltaDistY;
}
else
{
stepY = 1;
sideDistY = (mapY + 1.0 - rayPosY) * deltaDistY;
}
// perform differential analysis
while (!hit)
{
// jump to next map square, OR in x-direction, OR in y-direction
if (sideDistX < sideDistY)
{
sideDistX += deltaDistX;
mapX += stepX;
side = 0;
}
else
{
sideDistY += deltaDistY;
mapY += stepY;
side = 1;
}
if (worldMap[mapX][mapY] > 0) hit = true;
}
// calculate distance projected on camera direction to stop the fisheye effect
if (side == 0)
perpWallDist = fabs((mapX - rayPosX + (1 - stepX) / 2) / rayDirX);
else
perpWallDist = fabs((mapY - rayPosY + (1 - stepY) / 2) / rayDirY);
int lineHeight = abs(int(HEIGHT / perpWallDist));
// calculate lowest and highest pixel to fill in current wall slice
int startY = -lineHeight / 2 + HEIGHT / 2;
if(startY < 0)startY = 0;
int endY = lineHeight / 2 + HEIGHT / 2;
if(endY >= HEIGHT)endY = HEIGHT - 1;
// calculate the correct texture offset and colours for the current wall slice
double textureX;
if (side == 1)
{
graphics.setColour(0.75f, 0.75f, 0.75f);
textureX = rayPosX + ((mapY - rayPosY + (1 - stepY) / 2) / rayDirY) * rayDirX;
}
else
{
graphics.setColour(0.5f, 0.5f, 0.5f);
textureX = rayPosY + ((mapX - rayPosX + (1 - stepX) / 2) / rayDirX) * rayDirY;
}
textureX -= floor((textureX));
// draw it!
graphics.setTexture(worldMap[mapX][mapY]);
graphics.texturedRect(x, startY, 1, endY - startY, textureX, 0.0, 1 / 64, 1.0);
}
}
void renderBackground(void)
{
// ceiling
int r;
for (r = 0; r < HEIGHT / 2; r += 10)
{
graphics.setColour(0.2f, 0.2f, 0.2f);
graphics.rect(0, r, WIDTH, 10);
}
// ground
for (; r < HEIGHT; r += 15)
{
graphics.setColour(0.1f, 0.1f, 0.1f);
graphics.rect(0, r, WIDTH, 15);
}
}