-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.c
138 lines (129 loc) · 4.65 KB
/
draw.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* draw.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: polmarti <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/27 11:40:57 by polmarti #+# #+# */
/* Updated: 2024/05/27 11:41:03 by polmarti ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
/**
* Constructs a 32-bit color value (RGBA) from
* the provided red, green, blue, and alpha components.
* Alpha (a) controls the transparency level:
* 0 is fully transparent, 255 is fully opaque.
*
* @param r Red component (0-255).
* @param g Green component (0-255).
* @param b Blue component (0-255).
* @param a Alpha component (0-255), controls transparency.
* @return Constructed RGBA color value.
*/
int get_rgba(int r, int g, int b, int a)
{
return (r << 24 | g << 16 | b << 8 | a);
}
/**
* Reverses the bytes of a 32-bit integer.
* Takes an integer and swaps its bytes to convert RGBA format
*
* @param c The 32-bit integer whose bytes are to be reversed.
* @return The 32-bit integer with its bytes reversed.
*/
int ft_torgba(int c)
{
unsigned int pix;
pix = 0;
pix |= (c & 0xFF) << 24;
pix |= (c & 0xFF00) << 8;
pix |= (c & 0xFF0000) >> 8;
pix |= (c & 0xFF000000) >> 24;
return (pix);
}
/**
* Safely puts a pixel of the specified color onto
* the image buffer at the given coordinates (x, y).
* Coordinates outside the bounds of the image buffer
* (0 to WIDTH-1 for x, 0 to HEIGHT-1 for y) are ignored.
*
* @param image Pointer to the image buffer (mlx_image_t structure).
* @param x X-coordinate of the pixel.
* @param y Y-coordinate of the pixel.
* @param color Color value to be placed at the specified coordinates
* (RGBA format).
*/
void safe_pixel_put(mlx_image_t *image, int x, int y, int color)
{
if (x < 0 || x >= WIDTH)
return ;
if (y < 0 || y >= HEIGHT)
return ;
mlx_put_pixel(image, x, y, color);
}
/**
* Draws a textured wall on the screen between the
* specified top and bottom pixels.
* Calculates the scaling factor for the texture based on the wall height.
* Sets the texture origin coordinates based on the
* collision point and ray direction.
* Draws the pixels obtained from the texture array,
* reversed to adapt to RGB format.
*
* @param d Pointer to the main data structure containing
* scene and graphics information.
* @param top_pix Top pixel index where the wall starts on the screen.
* @param bottom_pix Bottom pixel index where the wall ends on the screen.
* @param height_wall Height of the wall on the screen.
*/
void ft_draw_texture(t_data *d, int top_pix, int bottom_pix, \
double height_wall)
{
mlx_texture_t *tex;
double factor;
double x_o;
double y_o;
tex = get_texture_walls(d, d->cast_rays.collission);
d->map.tex.arr = (unsigned int *)tex->pixels;
factor = ((double)tex->height / height_wall);
if (d->cast_rays.collission == HORIZONTAL)
x_o = (t_32)fmodf(d->cast_rays.wall_hit_x_horizontal, TILE_SIZE);
else
x_o = (t_32)fmodf(d->cast_rays.wall_hit_y_vertical, tex->width);
y_o = (top_pix - (HEIGHT >> 1) + (height_wall * 0.5)) * factor;
if (y_o < 0)
y_o = 0;
while (top_pix < bottom_pix)
{
if ((t_32)y_o < TILE_SIZE && (t_32)x_o < TILE_SIZE)
safe_pixel_put(d->n_image, d->cast_rays.index, top_pix, \
ft_torgba(d->map.tex.arr[(t_32)y_o * TILE_SIZE + (t_32)x_o]));
y_o += factor;
top_pix++;
}
}
/**
* Renders the floor and ceiling for a given ray on the screen.
* The first loop renders the floor starting from the bottom pixel (`b_pix`)
* up to the screen height (`HEIGHT`).
* The second loop renders the ceiling from the top of
* the screen down to the top pixel (`t_pix`).
*
* @param d Pointer to the main data structure containing scene
* and graphics information.
* @param ray Index of the ray being processed.
* @param t_pix Top pixel index for rendering the ceiling.
* @param b_pix Bottom pixel index for rendering the floor.
*/
void ft_draw_fl_ce(t_data *d, int ray, int t_pix, int b_pix)
{
int i;
i = b_pix;
while (i < HEIGHT)
safe_pixel_put(d->n_image, ray, i++, d->map.f_color);
i = 0;
while (i < t_pix)
safe_pixel_put(d->n_image, ray, i++, d->map.c_color);
}