-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwall_raycasting.c
More file actions
90 lines (83 loc) · 3 KB
/
wall_raycasting.c
File metadata and controls
90 lines (83 loc) · 3 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* wall_raycasting.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jcorral- <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/27 09:29:36 by jcorral- #+# #+# */
/* Updated: 2020/02/27 09:29:38 by jcorral- ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
void wall_raycasting_perwall(t_m *s)
{
s->ray.perpwalldist = (s->ray.side == 0) ? (s->ray.mapx -
s->self.posx + (1 - s->ray.stepx) / 2) / s->ray.raydirx :
(s->ray.mapy - s->self.posy + (1 - s->ray.stepy) / 2) / s->ray.raydiry;
s->ray.lineheight = (int)(s->parser.h / s->ray.perpwalldist);
s->ray.drawstart = (-s->ray.lineheight / 2 + s->parser.h / 2 < 0) ? 0
: -s->ray.lineheight / 2 + s->parser.h / 2;
s->ray.drawend = (s->ray.lineheight / 2 + s->parser.h / 2 >= s->parser.h)
? s->parser.h - 1 : s->ray.lineheight / 2 + s->parser.h / 2;
}
void wall_raycasting_draw(t_m *s, int x)
{
int y;
y = 0;
while (y < s->ray.drawstart)
{
s->image.addr[y * s->parser.w + x] = s->parser.ceiling;
y++;
}
while (y < s->ray.drawend)
{
s->ray.texy = (int)s->ray.texpos & (s->ray.orient.he - 1);
s->ray.texpos += s->ray.step;
s->ray.color = s->ray.orient.addr[s->ray.orient.he *
s->ray.texy + s->ray.texx];
if (s->ray.side == 1)
s->ray.color = (s->ray.color >> 1) & 8355711;
s->image.addr[y * s->parser.w + x] = s->ray.color;
y++;
}
while (y < s->parser.h)
{
s->image.addr[y * s->parser.w + x] = s->parser.floor;
y++;
}
}
void wall_raycasting_text(t_m *s)
{
if (s->ray.side == 0)
s->ray.wallx = s->self.posy + s->ray.perpwalldist * s->ray.raydiry;
else
s->ray.wallx = s->self.posx + s->ray.perpwalldist * s->ray.raydirx;
s->ray.wallx -= floor((s->ray.wallx));
s->ray.texx = (int)(s->ray.wallx * (double)(s->ray.orient.wi));
if (s->ray.side == 0 && s->ray.raydirx > 0)
s->ray.texx = s->ray.orient.wi - s->ray.texx - 1;
if (s->ray.side == 1 && s->ray.raydiry < 0)
s->ray.texx = s->ray.orient.wi - s->ray.texx - 1;
s->ray.step = 1.0 * s->ray.orient.he
/ s->ray.lineheight;
s->ray.texpos = (s->ray.drawstart - s->parser.h / 2
+ s->ray.lineheight / 2) * s->ray.step;
}
void wall_raycasting(t_m *s)
{
int x;
x = 0;
while (x < s->parser.w)
{
wall_raycasting_init(s, x);
wall_raycasting_step(s);
wall_raycasting_hit(s);
wall_raycasting_perwall(s);
wall_raycasting_orientation(s);
wall_raycasting_text(s);
wall_raycasting_draw(s, x);
s->spr.zbuff[x] = s->ray.perpwalldist;
x++;
}
}