-
Notifications
You must be signed in to change notification settings - Fork 0
/
render3d.c
93 lines (85 loc) · 2.47 KB
/
render3d.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render3d.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hgrissen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/07 12:36:01 by hgrissen #+# #+# */
/* Updated: 2021/02/15 16:00:19 by hgrissen ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
unsigned int shadow(unsigned int color, int col)
{
unsigned int r;
unsigned int g;
unsigned int b;
float fact;
unsigned int dark;
if (BON == 0)
return (color);
col = 0;
fact = LGHT / g_col.perp_dist;
if (fact > 1)
return (color);
r = (((color >> 16) & 0xFF)) * fact;
g = (((color >> 8) & 0xFF)) * fact;
b = ((color) & (0xFF)) * fact;
dark = rgb_to_int(0, r, g, b);
if (dark > color)
dark = color;
return (dark);
}
int shadedcolor(int r, int g, int b, float percent)
{
r = clamp_clr(r - floorf(r * percent));
g = clamp_clr(g - floorf(g * percent));
b = clamp_clr(b - floorf(b * percent));
return ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff);
}
int shade(int i, char x)
{
int crouch;
float percent;
crouch = g_p.iscrouch ? CROUCH : 0;
if (x == 'c')
{
if (BON == 0)
return (rgb_to_int(00, g_prm.cr, g_prm.cg, g_prm.cb));
percent = ((float)i + g_p.crouch) / (g_prm.h / 2);
return (shadedcolor(g_prm.cr, g_prm.cg, g_prm.cb, percent));
}
else
{
if (BON == 0)
return (rgb_to_int(00, g_prm.fr, g_prm.fg, g_prm.fb));
percent = ((float)i + g_p.crouch) - (g_prm.h / 2);
percent /= (g_prm.h - (g_prm.h / 2));
percent = 1 - percent;
return (shadedcolor(g_prm.fr, g_prm.fg, g_prm.fb, percent));
}
}
void render_flr_cei(void)
{
int i;
int j;
int fc;
int cc;
int crouch;
crouch = g_p.crouch * 2;
i = -1;
while (++i < g_prm.h)
{
fc = shade(i, 'f');
cc = shade(i, 'c');
j = -1;
while (++j < g_prm.w)
{
if (i > (g_prm.h - crouch) / 2)
my_mlx_pixel_put(&g_img, j, i, fc);
else
my_mlx_pixel_put(&g_img, j, i, cc);
}
}
}