-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
350 lines (309 loc) · 7.66 KB
/
main.c
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <GL/freeglut_std.h>
#include <GL/glext.h>
#include <GL/glut.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <string.h>
#define PI 3.141592653589793238462
#define PI_OVER_TWO PI / 2
#define PI3_OVER_TWO (3 * PI) / 2
#define ONE_DEGREE_IN_RADIANS 0.01745329252
#define WINDOW_WIDTH 1000
#define WINDOW_HEIGHT 530
struct level {
int x;
int y;
int size;
unsigned char map[64];
};
struct level map = {
8,8,64,
{
1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,1,
1,0,1,0,0,1,0,1,
1,0,1,1,0,1,1,1,
1,0,1,0,0,0,0,1,
1,0,1,1,0,0,0,1,
1,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,
}
};
void drawMap2D() {
int x,y,xo,yo;
for (int y=0; y<map.y;y++) {
for (int x=0; x<map.x;x++) {
if (map.map[y*map.x+x] == 1) { glColor3f(1,1,1); } else { glColor3f(0,0,0); }
xo=x*map.size;
yo=y*map.size;
glBegin(GL_QUADS);
glVertex2i(xo+1, yo+1);
glVertex2i(xo+1, yo+map.size-1);
glVertex2i(xo+map.size-1, yo+map.size-1);
glVertex2i(xo+map.size-1, yo+1);
glEnd();
}
}
}
struct player {
float x;
float y;
float dX;
float dY;
float ang;
};
struct player p;
void draw_player() {
glColor3f(1,1,0);
glPointSize(8);
glBegin(GL_POINTS);
glVertex2i(p.x, p.y);
glEnd();
glLineWidth(3);
glBegin(GL_LINES);
glVertex2i(p.x, p.y);
glVertex2i(p.x + p.dX * 5, p.y + p.dY * 5);
glEnd();
}
struct raycaster {
float x;
float y;
};
/* struct ray { */
/* float x; */
/* float y; */
/* float ang; */
/* }; */
float dist(float ax, float ay, float bx, float by, float angle) // teorema de pitagoras
{
return ( sqrt((bx-ax) * (bx-ax) + (by-ay) * (by-ay)) );
}
void raycast() {
int r,mapx,mapy,mappos,dof;
float rx,ry,ra,xoff,yoff;
ra = p.ang - ONE_DEGREE_IN_RADIANS * 40;
if (ra<0) { ra +=2*PI; }
if (ra>2*PI) { ra-=2*PI; }
for (r=0; r<80; r++) {
// -------------------------- horizontal --------------------------
float a_tan = (-1/tan(ra));
float hx = p.x;
float hy = p.y;
float distH = 100000;
dof = 0;
// olhando pra baixo // looking down
if (ra>PI) {
ry = (((int)p.y>>6) <<6) - 0.0001;
rx = (p.y - ry) * a_tan+p.x;
yoff = -64;
xoff = -yoff * a_tan;
}
//olhando pra cima // looking up
if (ra<PI) {
ry = (((int)p.y>>6) <<6) + 64;
rx = (p.y - ry) * a_tan+p.x;
yoff = 64;
xoff = -yoff * a_tan;
}
//olhando diretamente pra esquerda ou direita // looking directly either right or left
if (ra == 0 || ra == PI) {
rx = p.x;
ry = p.y;
dof = 8;
}
while(dof<8) {
mapx = (int)rx >> 6;
mapy = (int)ry >> 6;
mappos = mapy * map.x + mapx;
if (mappos > 0 && mappos < map.x * map.y && map.map[mappos] == 1) { // ray hit a wall
hx = rx;
hy = ry;
distH = dist(p.x, p.y, hx, hy, ra);
dof = 8;
} else {
rx += xoff;
ry += yoff;
dof++;
}
}
// -------------------------- vertical --------------------------
float n_tan = -tan(ra);
float vx = p.x;
float vy = p.y;
float distV = 100000;
dof = 0;
// olhando pra esquerda // looking left
if (ra > PI_OVER_TWO && ra < PI3_OVER_TWO) {
rx = (((int)p.x>>6) <<6) - 0.0001;
ry = (p.x - rx) * n_tan+p.y;
xoff = -64;
yoff = -xoff * n_tan;
}
//olhando pra direita // looking right
if (ra < PI_OVER_TWO || ra > PI3_OVER_TWO) {
rx = (((int)p.x>>6) <<6) + 64;
ry = (p.x - rx) * n_tan+p.y;
xoff = 64;
yoff = -xoff * n_tan;
}
//olhando diretamente pra cima ou pra baixo // looking directly up or down
if (ra == 0 || ra == PI) {
rx = p.x;
ry = p.y;
dof = 8;
}
while(dof<8) {
mapx = (int)rx >> 6;
mapy = (int)ry >> 6;
mappos = mapy * map.x + mapx;
if (mappos > 0 && mappos < map.x * map.y && map.map[mappos] == 1) { // ray hit a wall
vx = rx;
vy = ry;
distV = dist(p.x, p.y, vx, vy, ra);
dof = 8;
} else {
rx += xoff;
ry += yoff;
dof++;
}
}
float distance;
if (distV < distH) {
distance = distV;
rx = vx;
ry = vy;
glColor3f(0.9, 0, 0);
} else if (distH < distV) {
distance = distH;
rx = hx;
ry = hy;
glColor3f(0.7, 0, 0);
}
ra+=ONE_DEGREE_IN_RADIANS;
if (ra<0) { ra +=2*PI; }
if (ra>2*PI) { ra-=2*PI; }
// render raycasts in map
/* glColor3f(1, 0, 0); */
glLineWidth(1);
glBegin(GL_LINES);
glVertex2i(p.x, p.y);
glVertex2i(rx, ry);
glEnd();
//render 3D
float ca=p.ang-ra;
if (ca<0) { ca+=2*PI; }
if (ca>2*PI) { ca-=2*PI; }
distance = distance * cos(ca); // fix fisheye effect
float line_height = (map.size * WINDOW_HEIGHT - 800)/distance;
float line_offset = 200-line_height/2;
glLineWidth(12);
glBegin(GL_LINES);
glVertex2i(r*6+530,line_offset);
glVertex2i(r*6+530, line_height+line_offset);
glEnd();
}
}
int fix_ang(int a){
if(a>359){
a-=360;
} if(a<0){
a+=360;
}
return a;
}
// checks state of key in map array given
// x and y coordinates mapped to
// single dimensional 8x8 array.
int check_collision(float x, float y) {
int r_x, r_y;
r_x = ceil(x / map.size) - 1; // -1 because the very first cell starts at 0
r_y = ceil(y / map.size);
int single_d_coord = (8 * r_y) - (8 - r_x);
return map.map[single_d_coord];
}
unsigned char keymaps[256];
void handle_keyboard() {
if (keymaps['w'] == 1) {
if (check_collision(p.x + p.dX, p.y) == 0)
p.x+=p.dX;
if (check_collision(p.x, p.y + p.dY) == 0)
p.y+=p.dY;
}
if (keymaps['s'] == 1) {
if (check_collision(p.x - p.dX, p.y) == 0)
p.x-=p.dX;
if (check_collision(p.x, p.y - p.dY) == 0)
p.y-=p.dY;
}
if (keymaps['a'] == 1) {
p.ang-=0.1;
fix_ang(p.ang);
if (p.ang < 0) { p.ang += 2*PI; }
p.dX = cos(p.ang) * 5;
p.dY = sin(p.ang) * 5;
}
if (keymaps['d'] == 1) {
p.ang+=0.1;
fix_ang(p.ang);
if (p.ang > 2*PI) { p.ang -= 2*PI; }
p.dX = cos(p.ang) * 5;
p.dY = sin(p.ang) * 5;
}
}
void handle_keydown(unsigned char key, int x, int y) { keymaps[key] = 1; }
void handle_keyup(unsigned char key, int x, int y) { keymaps[key] = 0; }
size_t size = 120;
char buff[120];
void show_player_coords() {
glColor3f(1,1,1);
glRasterPos2f(WINDOW_WIDTH - 160, WINDOW_HEIGHT - 30);
snprintf(buff, size, "XY: %.1f %.1f (pitch: %.2f)", p.x, p.y, p.ang);
for (int i = 0; i < size; i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, buff[i]);
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawMap2D();
draw_player();
raycast();
handle_keyboard();
show_player_coords();
glutSwapBuffers();
glutPostRedisplay();
}
void init() {
glClearColor(0.3, 0.3, 0.3, 0.3);
gluOrtho2D(0, 1024, 512, 0);
p.x=300; p.y=300;
p.dX = cos(p.ang) * 2;
p.dY = sin(p.ang) * 2;
for (int i = 0; i < size; i++) {
buff[i] = ' ';
}
}
int main(int argc, char* argv[]) {
if (argc == 2) {
FILE *file_ptr;
file_ptr = fopen(argv[1], "rb");
if (file_ptr != NULL) {
while (fread(&map.map, sizeof(unsigned char)*64, 1, file_ptr)) {
printf("[start] reading %lu bytes from %s...\n", sizeof(unsigned char)*64, argv[1]);
}
}
fclose(file_ptr);
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("KarboXXX's Raycaster");
init();
glutDisplayFunc(display);
glutIgnoreKeyRepeat(GLUT_KEY_REPEAT_OFF);
glutKeyboardFunc(handle_keydown);
glutKeyboardUpFunc(handle_keyup);
glutMainLoop();
}