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 path01_untextured_raycast.c
257 lines (229 loc) · 7.33 KB
/
01_untextured_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 01_untextured_raycast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yohlee <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/15 12:12:25 by yohan #+# #+# */
/* Updated: 2020/07/21 08:08:19 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 mapWidth 24
#define mapHeight 24
#define width 640
#define height 480
typedef struct s_info
{
double posX;
double posY;
double dirX;
double dirY;
double planeX;
double planeY;
void *mlx;
void *win;
double moveSpeed;
double rotS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{1,4,0,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,4,4,4,4,4,4,4,4,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}
};
void verLine(t_info *info, int x, int y1, int y2, int color)
{
int y;
y = y1;
while (y <= y2)
{
mlx_pixel_put(info->mlx, info->win, x, y, color);
y++;
}
}
void calc(t_info *info)
{
int x;
x = 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;
int color;
if (worldMap[mapY][mapX] == 1)
color = 0xFF0000;
else if (worldMap[mapY][mapX] == 2)
color = 0x00FF00;
else if (worldMap[mapY][mapX] == 3)
color = 0x0000FF;
else if (worldMap[mapY][mapX] == 4)
color = 0xFFFFFF;
else
color = 0xFFFF00;
if (side == 1)
color = color / 2;
verLine(info, x, drawStart, drawEnd, color);
x++;
}
}
int main_loop(t_info *info)
{
calc(info);
// mlx_put_image_to_window(info->mlx, info->win, &info->img, 0, 0);
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);
}
int main(void)
{
t_info info;
info.mlx = mlx_init();
info.posX = 12;
info.posY = 5;
info.dirX = -1;
info.dirY = 0;
info.planeX = 0;
info.planeY = 0.66;
info.moveSpeed = 0.05;
info.rotSpeed = 0.05;
info.win = mlx_new_window(info.mlx, width, height, "mlx");
mlx_loop_hook(info.mlx, &main_loop, &info);
mlx_hook(info.win, X_EVENT_KEY_PRESS, 0, &key_press, &info);
mlx_loop(info.mlx);
}