-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
331 lines (288 loc) · 8.85 KB
/
main.cpp
File metadata and controls
331 lines (288 loc) · 8.85 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//main.cpp
#include <iostream>
#include <GL/glut.h>
#include <cmath>
#include "PlayerClass.h"
#include "Constants.h"
#include "Util.h"
using namespace std;
//Global player variable
Player player;
int mapX = 8, mapY = 8, mapSize = 64;
int map[8][8] =
{
{1,1,1,1,1,1,1,1},
{1,0,1,0,0,0,0,1},
{1,0,1,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,1,0,1},
{1,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1}
};
void drawMap2D(void)
{
int coordX, coordY, baseX, baseY = 0;
//Loop through each spot in the array
for(coordY = 0; coordY < mapY; coordY++)
{
for(coordX = 0; coordX < mapX; coordX++)
{
if(map[coordY][coordX] == 1)
//Set color to white
glColor3f(1, 1, 1);
else
//Set color to black
glColor3f(0, 0, 0);
//Gets to base point of square on display window
baseX = coordX * mapSize;
baseY = coordY * mapSize;
//Create the vertexes for the shape, start at top left and draw CC
glBegin(GL_QUADS);
glVertex2i(baseX + 1, baseY + 1);
glVertex2i(baseX + 1, baseY + mapSize - 1);
glVertex2i(baseX + mapSize - 1, baseY + mapSize - 1);
glVertex2i(baseX + mapSize - 1, baseY + 1);
glEnd();
}
}
}
//Uses DDA algorithm
void drawRays2D(void)
{
//Initial starting info
int x1 = player.getPosX();
int y1 = player.getPosY();
float rayAngle = player.getAngle();
float endDist = 0.0;
//For casting multiple rays
rayAngle -= RADDEG * (NUMRAYS / 2.0);
for(int i = 0; i < NUMRAYS; i++)
{
if(rayAngle < 0)
{
rayAngle += 2 * PI;
}
else if(rayAngle > 2 * PI)
{
rayAngle -= 2 * PI;
}
int depthOfField = 0;
//***************************
//Checking for vertical lines
//***************************
//Checking for first vertical line intersections
float rayXVert = 0.0;
float rayYVert = 0.0;
float deltaY = 0.0;
int directionX = 0;
//Looking to the right
if((rayAngle < PI / 2) || (rayAngle > 1.5 * PI))
{
rayXVert = x1 + (mapSize - (x1 % mapSize));
directionX = 1;
}
//Looking to the left
else if((rayAngle > PI / 2) && (rayAngle <= 1.5 * PI))
{
rayXVert = x1 - (x1 % mapSize);
directionX = -1;
}
else
{
rayXVert = x1;
depthOfField = 8;
directionX = 0;
}
//Finish calculating first point of intersection
rayYVert = y1 + tan(rayAngle) * (rayXVert - x1);
//Calculate the slope
deltaY = tan(rayAngle) * (64 * directionX);
//Loop to extend ray until it hits a wall
while(depthOfField < 8)
{
//The .001 is so that it will round correctly while looking either way
int tileX = pixelToTile(rayXVert + (.001 * directionX));
int tileY = pixelToTile(rayYVert);
if(tileX < 0 || tileX > 8 || tileY < 0 || tileY > 8 || map[tileY][tileX] == 1)
{
depthOfField = 8;
}
else
{
rayYVert += deltaY;
rayXVert += 64 * directionX;
depthOfField++;
}
}
//*****************************
//Checking for horizontal lines
//*****************************
depthOfField = 0;
//Checking for first horizontal line intersections
float rayXHori = 0.0;
float rayYHori = 0.0;
float deltaX = 0.0;
int directionY = 0;
//Looking up
if(rayAngle > 0 && rayAngle < PI)
{
rayYHori = y1 + (mapSize - (y1 % mapSize));
directionY = 1;
}
//Looking down
else if(rayAngle > PI)
{
rayYHori = y1 - (y1 % mapSize);
directionY = -1;
}
else
{
rayYHori = y1;
depthOfField = 8;
directionY = 0;
}
//Finish calculating the first point of intersection
rayXHori = x1 + ((rayYHori - y1) / tan(rayAngle));
//Calculate the slope
deltaX = ((64 * directionY) / tan(rayAngle));
while(depthOfField < 8)
{
//The .001 is so that it will round correctly while looking either way
int tileX = pixelToTile(rayXHori);
int tileY = pixelToTile(rayYHori + (.001 * directionY));
if(tileX < 0 || tileX >= 8 || tileY < 0 || tileY >= 8 || map[tileY][tileX] == 1)
{
depthOfField = 8;
}
else
{
rayYHori += 64 * directionY;
rayXHori += deltaX;
depthOfField++;
}
}
//Draw the shorter ray
glLineWidth(3);
glBegin(GL_LINES);
if(dist(x1, y1, rayXHori, rayYHori) <= dist(x1, y1, rayXVert, rayYVert))
{
//If horizontal intersection first
glColor3f(0.6, 0, 0);
glVertex2i(x1, y1);
glVertex2i(rayXHori, rayYHori);
endDist = dist(x1, y1, rayXHori, rayYHori);
}
else
{
//If vertical intersection first
glColor3f(0.9, 0, 0);
glVertex2i(x1, y1);
glVertex2i(rayXVert, rayYVert);
endDist = dist(x1, y1, rayXVert, rayYVert);
}
glEnd();
//*****************
//Draw the 3D Walls
//*****************
//Fixes the fish eye effect
float deltaAngle = player.getAngle() - rayAngle;
if(deltaAngle < 0)
{
deltaAngle += 2 * PI;
}
else if(deltaAngle > 2 * PI)
{
deltaAngle -= 2 * PI;
}
endDist = endDist * cos(deltaAngle);
//Determine the height of the line(wall)
float lineHeight = (mapSize * 320.0) / endDist;
if(lineHeight > 320)
{
lineHeight = 320;
}
//Play around with the first number to adjust where it is on the screen
//adjusting the rest of the formula will shift the perspective
float lineOffset = -50 + (WINDOWHEIGHT - lineHeight / 2); //Shift lines downward so they are more centered on screen
//Drawing the lines for the walls
glLineWidth(COLUMNWIDTH);
glBegin(GL_LINES);
glVertex2i((i * COLUMNWIDTH) + 530, lineOffset);
glVertex2i((i * COLUMNWIDTH) + 530, lineHeight + lineOffset);
glEnd();
//Increment the ray angle so that the next ray can be cast
rayAngle += RADDEG;
}
}
static void display(void)
{
//Clears buffers to preset values
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Draw the map
drawMap2D();
//Draw the rays
drawRays2D();
//Draw the player
player.draw();
//Swaps frame buffers
glutSwapBuffers();
}
//Player movement function
void buttons(unsigned char key, int x, int y)
{
if(key == 'a')
{
player.setAngle(player.getAngle() - TURNAMOUNT);
player.setDeltaX(cos(player.getAngle()) * MOVEAMOUNT);
player.setDeltaY(sin(player.getAngle()) * MOVEAMOUNT);
}
if(key == 'd')
{
player.setAngle(player.getAngle() + TURNAMOUNT);
player.setDeltaX(cos(player.getAngle()) * MOVEAMOUNT);
player.setDeltaY(sin(player.getAngle()) * MOVEAMOUNT);
}
if(key == 'w')
{
player.setPosX(player.getPosX() + player.getDeltaX());
player.setPosY(player.getPosY() + player.getDeltaY());
}
if(key == 's')
{
player.setPosX(player.getPosX() - player.getDeltaX());
player.setPosY(player.getPosY() - player.getDeltaY());
}
//Updates the display with the movement
glutPostRedisplay();
}
//Initial setup of things on display
void init()
{
//Set background to dark grey
glClearColor(0.3, 0.3, 0.3, 0);
//Creates an 2D orthographic matrix
gluOrtho2D(0, WIDTH, HEIGHT, 0);
player.setPosX(300);
player.setPosY(300);
player.setDeltaX(cos(player.getAngle()) * MOVEAMOUNT);
player.setDeltaY(sin(player.getAngle()) * MOVEAMOUNT);
}
int main(int argc, char* argv[])
{
//Initialize GLUT library
glutInit(&argc, argv);
//Initializes display to be double buffered and use RGBA color model
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
//Sets size of the window
glutInitWindowSize(WIDTH, HEIGHT);
//Creates a top level window
glutCreateWindow("Raycasting-Project");
init();
//Redraws window when it is resized/moved/hidden
glutDisplayFunc(display);
//Sets the keyboard callback for the current window
glutKeyboardFunc(buttons);
//Calls display callback repeatedly (glutDisplayFunc)
glutMainLoop();
}