forked from l-yohai/cub3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_floor_ceiling.c
467 lines (401 loc) · 13.8 KB
/
04_floor_ceiling.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 04_floor_ceiling_macos.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yohlee <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/29 19:53:20 by yohlee #+# #+# */
/* Updated: 2020/07/21 08:11:30 by yohlee ### ########.fr */
/* */
/* ************************************************************************** */
#include "mlx/mlx.h"
#include "key_macos.h"
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define X_EVENT_KEY_PRESS 2
#define X_EVENT_KEY_EXIT 17
#define texWidth 64
#define texHeight 64
#define mapWidth 24
#define mapHeight 24
#define width 640
#define height 480
typedef struct s_img
{
void *img;
int *data;
int size_l;
int bpp;
int endian;
int img_width;
int img_height;
} t_img;
typedef struct s_info
{
double posX;
double posY;
double dirX;
double dirY;
double planeX;
double planeY;
void *mlx;
void *win;
t_img img;
int buf[height][width];
int **texture;
double moveSpeed;
double rotSpeed;
} t_info;
int worldMap[mapWidth][mapHeight] =
{
{8,8,8,8,8,8,8,8,8,8,8,4,4,6,4,4,6,4,6,4,4,4,6,4},
{8,0,0,0,0,0,0,0,0,0,8,4,0,0,0,0,0,0,0,0,0,0,0,4},
{8,0,3,3,0,0,0,0,0,8,8,4,0,0,0,0,0,0,0,0,0,0,0,6},
{8,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6},
{8,0,3,3,0,0,0,0,0,8,8,4,0,0,0,0,0,0,0,0,0,0,0,4},
{8,0,0,0,0,0,0,0,0,0,8,4,0,0,0,0,0,6,6,6,0,6,4,6},
{8,8,8,8,0,8,8,8,8,8,8,4,4,4,4,4,4,6,0,0,0,0,0,6},
{7,7,7,7,0,7,7,7,7,0,8,0,8,0,8,0,8,4,0,4,0,6,0,6},
{7,7,0,0,0,0,0,0,7,8,0,8,0,8,0,8,8,6,0,0,0,0,0,6},
{7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,6,0,0,0,0,0,4},
{7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,6,0,6,0,6,0,6},
{7,7,0,0,0,0,0,0,7,8,0,8,0,8,0,8,8,6,4,6,0,6,6,6},
{7,7,7,7,0,7,7,7,7,8,8,4,0,6,8,4,8,3,3,3,0,3,3,3},
{2,2,2,2,0,2,2,2,2,4,6,4,0,0,6,0,6,3,0,0,0,0,0,3},
{2,2,0,0,0,0,0,2,2,4,0,0,0,0,0,0,4,3,0,0,0,0,0,3},
{2,0,0,0,0,0,0,0,2,4,0,0,0,0,0,0,4,3,0,0,0,0,0,3},
{1,0,0,0,0,0,0,0,1,4,4,4,4,4,6,0,6,3,3,0,0,0,3,3},
{2,0,0,0,0,0,0,0,2,2,2,1,2,2,2,6,6,0,0,5,0,5,0,5},
{2,2,0,0,0,0,0,2,2,2,0,0,0,2,2,0,5,0,5,0,0,0,5,5},
{2,0,0,0,0,0,0,0,2,0,0,0,0,0,2,5,0,5,0,5,0,5,0,5},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5},
{2,0,0,0,0,0,0,0,2,0,0,0,0,0,2,5,0,5,0,5,0,5,0,5},
{2,2,0,0,0,0,0,2,2,2,0,0,0,2,2,0,5,0,5,0,0,0,5,5},
{2,2,2,2,1,2,2,2,2,2,2,1,2,2,2,5,5,5,5,5,5,5,5,5}
};
void draw(t_info *info)
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
info->img.data[y * width + x] = info->buf[y][x];
}
}
mlx_put_image_to_window(info->mlx, info->win, info->img.img, 0, 0);
}
void calc(t_info *info)
{
//FLOOR CASTING
for(int y = 0; y < height; y++)
{
// rayDir for leftmost ray (x = 0) and rightmost ray (x = w)
float rayDirX0 = info->dirX - info->planeX;
float rayDirY0 = info->dirY - info->planeY;
float rayDirX1 = info->dirX + info->planeX;
float rayDirY1 = info->dirY + info->planeY;
// Current y position compared to the center of the screen (the horizon)
int p = y - height / 2;
// Vertical position of the camera.
float posZ = 0.5 * height;
// Horizontal distance from the camera to the floor for the current row.
// 0.5 is the z position exactly in the middle between floor and ceiling.
float rowDistance = posZ / p;
// calculate the real world step vector we have to add for each x (parallel to camera plane)
// adding step by step avoids multiplications with a weight in the inner loop
float floorStepX = rowDistance * (rayDirX1 - rayDirX0) / width;
float floorStepY = rowDistance * (rayDirY1 - rayDirY0) / width;
// real world coordinates of the leftmost column. This will be updated as we step to the right.
float floorX = info->posX + rowDistance * rayDirX0;
float floorY = info->posY + rowDistance * rayDirY0;
for(int x = 0; x < width; ++x)
{
// the cell coord is simply got from the integer parts of floorX and floorY
int cellX = (int)(floorX);
int cellY = (int)(floorY);
// get the texture coordinate from the fractional part
int tx = (int)(texWidth * (floorX - cellX)) & (texWidth - 1);
int ty = (int)(texHeight * (floorY - cellY)) & (texHeight - 1);
floorX += floorStepX;
floorY += floorStepY;
// choose texture and draw the pixel
int floorTexture = 3;
int ceilingTexture = 6;
int color;
// floor
color = info->texture[floorTexture][texWidth * ty + tx];
color = (color >> 1) & 8355711; // make a bit darker
info->buf[y][x] = color;
//ceiling (symmetrical, at screenHeight - y - 1 instead of y)
color = info->texture[ceilingTexture][texWidth * ty + tx];
color = (color >> 1) & 8355711; // make a bit darker
info->buf[height - y - 1][x] = color;
}
}
//WALL CASTING
for(int x = 0; x < width; x++)
{
double cameraX = 2 * x / (double)width - 1;
double rayDirX = info->dirX + info->planeX * cameraX;
double rayDirY = info->dirY + info->planeY * cameraX;
int mapX = (int)info->posX;
int mapY = (int)info->posY;
//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 = fabs(1 / rayDirX);
double deltaDistY = fabs(1 / rayDirY);
double perpWallDist;
//what direction to step in x or y-direction (either +1 or -1)
int stepX;
int stepY;
int hit = 0; //was there a wall hit?
int side; //was a NS or a EW wall hit?
if (rayDirX < 0)
{
stepX = -1;
sideDistX = (info->posX - mapX) * deltaDistX;
}
else
{
stepX = 1;
sideDistX = (mapX + 1.0 - info->posX) * deltaDistX;
}
if (rayDirY < 0)
{
stepY = -1;
sideDistY = (info->posY - mapY) * deltaDistY;
}
else
{
stepY = 1;
sideDistY = (mapY + 1.0 - info->posY) * deltaDistY;
}
while (hit == 0)
{
//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;
}
//Check if ray has hit a wall
if (worldMap[mapX][mapY] > 0) hit = 1;
}
if (side == 0)
perpWallDist = (mapX - info->posX + (1 - stepX) / 2) / rayDirX;
else
perpWallDist = (mapY - info->posY + (1 - stepY) / 2) / rayDirY;
//Calculate height of line to draw on screen
int lineHeight = (int)(height / perpWallDist);
//calculate lowest and highest pixel to fill in current stripe
int drawStart = -lineHeight / 2 + height / 2;
if(drawStart < 0)
drawStart = 0;
int drawEnd = lineHeight / 2 + height / 2;
if(drawEnd >= height)
drawEnd = height - 1;
// texturing calculations
int texNum = worldMap[mapX][mapY] - 1;
// calculate value of wallX
double wallX;
if (side == 0)
wallX = info->posY + perpWallDist * rayDirY;
else
wallX = info->posX + perpWallDist * rayDirX;
wallX -= floor(wallX);
// x coordinate on the texture
int texX = (int)(wallX * (double)texWidth);
if (side == 0 && rayDirX > 0)
texX = texWidth - texX - 1;
if (side == 1 && rayDirY < 0)
texX = texWidth - texX - 1;
// How much to increase the texture coordinate perscreen pixel
double step = 1.0 * texHeight / lineHeight;
// Starting texture coordinate
double texPos = (drawStart - height / 2 + lineHeight / 2) * step;
for (int y = drawStart; y < drawEnd; y++)
{
// Cast the texture coordinate to integer, and mask with (texHeight - 1) in case of overflow
int texY = (int)texPos & (texHeight - 1);
texPos += step;
int color = info->texture[texNum][texHeight * texY + texX];
// make color darker for y-sides: R, G and B byte each divided through two with a "shift" and an "and"
if (side == 1)
color = (color >> 1) & 8355711;
info->buf[y][x] = color;
}
//FLOOR CASTING (vertical version, directly after drawing the vertical wall stripe for the current x)
double floorXWall, floorYWall; //x, y position of the floor texel at the bottom of the wall
//4 different wall directions possible
if(side == 0 && rayDirX > 0)
{
floorXWall = mapX;
floorYWall = mapY + wallX;
}
else if(side == 0 && rayDirX < 0)
{
floorXWall = mapX + 1.0;
floorYWall = mapY + wallX;
}
else if(side == 1 && rayDirY > 0)
{
floorXWall = mapX + wallX;
floorYWall = mapY;
}
else
{
floorXWall = mapX + wallX;
floorYWall = mapY + 1.0;
}
double distWall, distPlayer, currentDist;
distWall = perpWallDist;
distPlayer = 0.0;
if (drawEnd < 0) drawEnd = height; //becomes < 0 when the integer overflows
//draw the floor from drawEnd to the bottom of the screen
for(int y = drawEnd + 1; y < height; y++)
{
currentDist = height / (2.0 * y - height); //you could make a small lookup table for this instead
double weight = (currentDist - distPlayer) / (distWall - distPlayer);
double currentFloorX = weight * floorXWall + (1.0 - weight) * info->posX;
double currentFloorY = weight * floorYWall + (1.0 - weight) * info->posY;
int floorTexX, floorTexY;
floorTexX = (int)(currentFloorX * texWidth) % texWidth;
floorTexY = (int)(currentFloorY * texHeight) % texHeight;
int checkerBoardPattern = ((int)(currentFloorX) + (int)(currentFloorY)) % 2;
int floorTexture;
if(checkerBoardPattern == 0) floorTexture = 3;
else floorTexture = 4;
//floor
info->buf[y][x] = (info->texture[floorTexture][texWidth * floorTexY + floorTexX] >> 1) & 8355711;
//ceiling (symmetrical!)
info->buf[height - y][x] = info->texture[6][texWidth * floorTexY + floorTexX];
}
}
}
int main_loop(t_info *info)
{
calc(info);
draw(info);
return (0);
}
int key_press(int key, t_info *info)
{
if (key == K_W)
{
if (!worldMap[(int)(info->posX + info->dirX * info->moveSpeed)][(int)(info->posY)])
info->posX += info->dirX * info->moveSpeed;
if (!worldMap[(int)(info->posX)][(int)(info->posY + info->dirY * info->moveSpeed)])
info->posY += info->dirY * info->moveSpeed;
}
//move backwards if no wall behind you
if (key == K_S)
{
if (!worldMap[(int)(info->posX - info->dirX * info->moveSpeed)][(int)(info->posY)])
info->posX -= info->dirX * info->moveSpeed;
if (!worldMap[(int)(info->posX)][(int)(info->posY - info->dirY * info->moveSpeed)])
info->posY -= info->dirY * info->moveSpeed;
}
//rotate to the right
if (key == K_D)
{
//both camera direction and camera plane must be rotated
double oldDirX = info->dirX;
info->dirX = info->dirX * cos(-info->rotSpeed) - info->dirY * sin(-info->rotSpeed);
info->dirY = oldDirX * sin(-info->rotSpeed) + info->dirY * cos(-info->rotSpeed);
double oldPlaneX = info->planeX;
info->planeX = info->planeX * cos(-info->rotSpeed) - info->planeY * sin(-info->rotSpeed);
info->planeY = oldPlaneX * sin(-info->rotSpeed) + info->planeY * cos(-info->rotSpeed);
}
//rotate to the left
if (key == K_A)
{
//both camera direction and camera plane must be rotated
double oldDirX = info->dirX;
info->dirX = info->dirX * cos(info->rotSpeed) - info->dirY * sin(info->rotSpeed);
info->dirY = oldDirX * sin(info->rotSpeed) + info->dirY * cos(info->rotSpeed);
double oldPlaneX = info->planeX;
info->planeX = info->planeX * cos(info->rotSpeed) - info->planeY * sin(info->rotSpeed);
info->planeY = oldPlaneX * sin(info->rotSpeed) + info->planeY * cos(info->rotSpeed);
}
if (key == K_ESC)
exit(0);
return (0);
}
void load_image(t_info *info, int *texture, char *path, t_img *img)
{
img->img = mlx_xpm_file_to_image(info->mlx, path, &img->img_width, &img->img_height);
img->data = (int *)mlx_get_data_addr(img->img, &img->bpp, &img->size_l, &img->endian);
for (int y = 0; y < img->img_height; y++)
{
for (int x = 0; x < img->img_width; x++)
{
texture[img->img_width * y + x] = img->data[img->img_width * y + x];
}
}
mlx_destroy_image(info->mlx, img->img);
}
void load_texture(t_info *info)
{
t_img img;
load_image(info, info->texture[0], "textures/eagle.xpm", &img);
load_image(info, info->texture[1], "textures/redbrick.xpm", &img);
load_image(info, info->texture[2], "textures/purplestone.xpm", &img);
load_image(info, info->texture[3], "textures/greystone.xpm", &img);
load_image(info, info->texture[4], "textures/bluestone.xpm", &img);
load_image(info, info->texture[5], "textures/mossy.xpm", &img);
load_image(info, info->texture[6], "textures/wood.xpm", &img);
load_image(info, info->texture[7], "textures/colorstone.xpm", &img);
}
int main(void)
{
t_info info;
info.mlx = mlx_init();
info.posX = 22.0;
info.posY = 11.5;
info.dirX = -1.0;
info.dirY = 0.0;
info.planeX = 0.0;
info.planeY = 0.66;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
info.buf[i][j] = 0;
}
}
if (!(info.texture = (int **)malloc(sizeof(int *) * 8)))
return (-1);
for (int i = 0; i < 8; i++)
{
if (!(info.texture[i] = (int *)malloc(sizeof(int) * (texHeight * texWidth))))
return (-1);
}
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < texHeight * texWidth; j++)
{
info.texture[i][j] = 0;
}
}
load_texture(&info);
info.moveSpeed = 0.05;
info.rotSpeed = 0.05;
info.win = mlx_new_window(info.mlx, width, height, "mlx");
info.img.img = mlx_new_image(info.mlx, width, height);
info.img.data = (int *)mlx_get_data_addr(info.img.img, &info.img.bpp, &info.img.size_l, &info.img.endian);
mlx_loop_hook(info.mlx, &main_loop, &info);
mlx_hook(info.win, X_EVENT_KEY_PRESS, 0, &key_press, &info);
mlx_loop(info.mlx);
}