This repository has been archived by the owner on Aug 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path02_textured_raycast.c
349 lines (316 loc) · 10.2 KB
/
02_textured_raycast.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 02_textured_raycast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yohlee <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/26 15:18:03 by yohlee #+# #+# */
/* Updated: 2020/07/21 08:09:54 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 1920
#define height 1080
typedef struct s_img
{
void *img;
int *data;
int size_l;
int bpp;
int endian;
} 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;
int texture[8][texHeight * texWidth];
double moveSpeed;
double rotSpeed;
int re_buf;
} t_info;
int worldMap[mapWidth][mapHeight] =
{
{4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,7,7,7,7,7,7,7,7},
{4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,0,0,7},
{4,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7},
{4,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7},
{4,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,0,0,7},
{4,0,4,0,0,0,0,5,5,5,5,5,5,5,5,5,7,7,0,7,7,7,7,7},
{4,0,5,0,0,0,0,5,0,5,0,5,0,5,0,5,7,0,0,0,7,7,7,1},
{4,0,6,0,0,0,0,5,0,0,0,0,0,0,0,5,7,0,0,0,0,0,0,8},
{4,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,7,1},
{4,0,8,0,0,0,0,5,0,0,0,0,0,0,0,5,7,0,0,0,0,0,0,8},
{4,0,0,0,0,0,0,5,0,0,0,0,0,0,0,5,7,0,0,0,7,7,7,1},
{4,0,0,0,0,0,0,5,5,5,5,0,5,5,5,5,7,7,7,7,7,7,7,1},
{6,6,6,6,6,6,6,6,6,6,6,0,6,6,6,6,6,6,6,6,6,6,6,6},
{8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4},
{6,6,6,6,6,6,0,6,6,6,6,0,6,6,6,6,6,6,6,6,6,6,6,6},
{4,4,4,4,4,4,0,4,4,4,6,0,6,2,2,2,2,2,2,2,3,3,3,3},
{4,0,0,0,0,0,0,0,0,4,6,0,6,2,0,0,0,0,0,2,0,0,0,2},
{4,0,0,0,0,0,0,0,0,0,0,0,6,2,0,0,5,0,0,2,0,0,0,2},
{4,0,0,0,0,0,0,0,0,4,6,0,6,2,0,0,0,0,0,2,2,0,2,2},
{4,0,6,0,6,0,0,0,0,4,6,0,0,0,0,0,5,0,0,0,0,0,0,2},
{4,0,0,5,0,0,0,0,0,4,6,0,6,2,0,0,0,0,0,2,2,0,2,2},
{4,0,6,0,6,0,0,0,0,4,6,0,6,2,0,0,5,0,0,2,0,0,0,2},
{4,0,0,0,0,0,0,0,0,4,6,0,6,2,0,0,0,0,0,2,0,0,0,2},
{4,4,4,4,4,4,4,4,4,4,1,1,1,2,2,2,2,2,2,3,3,3,3,3}
};
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)
{
int x;
x = 0;
if (info->re_buf == 1)
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
info->buf[i][j] = 0;
}
}
info->re_buf = 0;
}
while (x < width)
{
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];
// 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;
info->re_buf = 1;
}
x++;
}
}
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);
mlx_clear_window(info->mlx, info->win);
main_loop(info);
return (0);
}
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;
info.re_buf = 0;
info.buf = (int **)malloc(sizeof(int *) * height);
for (int i = 0; i < height; i++)
{
info.buf[i] = (int *)malloc(sizeof(int) * width);
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
info.buf[i][j] = 0;
}
}
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < texHeight * texWidth; j++)
{
info.texture[i][j] = 0;
}
}
for (int x = 0; x < texWidth; x++)
{
for (int y = 0; y < texHeight; y++)
{
int xorcolor = (x * 256 / texWidth) ^ (y * 256 / texHeight);
int ycolor = y * 256 / texHeight;
int xycolor = y * 128 / texHeight + x * 128 / texWidth;
info.texture[0][texWidth * y + x] = 65536 * 254 * (x != y && x != texWidth - y); //flat red texture with black cross
info.texture[1][texWidth * y + x] = xycolor + 256 * xycolor + 65536 * xycolor; //sloped greyscale
info.texture[2][texWidth * y + x] = 256 * xycolor + 65536 * xycolor; //sloped yellow gradient
info.texture[3][texWidth * y + x] = xorcolor + 256 * xorcolor + 65536 * xorcolor; //xor greyscale
info.texture[4][texWidth * y + x] = 256 * xorcolor; //xor green
info.texture[5][texWidth * y + x] = 65536 * 192 * (x % 16 && y % 16); //red bricks
info.texture[6][texWidth * y + x] = 65536 * ycolor; //red gradient
info.texture[7][texWidth * y + x] = 128 + 256 * 128 + 65536 * 128; //flat grey texture
}
}
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);
main_loop(&info);
mlx_key_hook(info.win, &key_press, &info);
// mlx_loop_hook(info.mlx, &main_loop, &info);
// mlx_hook(info.win, X_EVENT_KEY_PRESS, 0, &key_press, &info);
mlx_loop(info.mlx);
}