Cub3D
Loading...
Searching...
No Matches
draw_vertical_line.c
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* draw_vertical_line.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/08/26 22:47:33 by kamitsui #+# #+# */
9/* Updated: 2024/12/21 12:08:33 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "cub3d.h"
14
15static void draw_ceiling(t_frame *frame, int x, int ceiling_end)
16{
17 int y;
18
19 y = 0;
20 while (y < ceiling_end)
21 {
22 my_mlx_pixel_put(frame->img_3d, x, y, *frame->ceiling_color);
23 y++;
24 }
25}
26
27static void draw_floor(t_frame *frame, int x, int floor_start)
28{
29 int y;
30
31 y = floor_start + 1;
32 while (y < IMG_3D_HEIGHT)
33 {
34 my_mlx_pixel_put(frame->img_3d, x, y, *frame->floor_color);
35 y++;
36 }
37}
38
39static int get_color_texture_pixel(t_frame *frame, int tex_x, int tex_y)
40{
41 int offset;
42 int color;
43 t_img img_tex;
44
45 img_tex = frame->dda.texture.img_tex;
46 offset = (tex_y * img_tex.line_length) + (tex_x * (img_tex.bpp / 8));
47 color = *(int *)(img_tex.addr + offset);
48 return (color);
49}
50// color = *(int *)(img_tex.addr + offset);// compatibility ??
51
52int get_texture_y_coordinate(t_frame *frame, int y)
53{
54 int64_t d;
55 int64_t line_height;
56 int64_t tex_height;
57 int64_t tex_y;
58
59 line_height = frame->wall_slice.line_height;
60 tex_height = frame->dda.texture.height;
61 if (line_height == 0)
62 line_height = 1;
63 d = (y * 256) - (IMG_3D_HEIGHT * 128) + (line_height * 128);
64 if (d < 0)
65 d = 0;
66 tex_y = ((d * tex_height) / line_height) / 256;
67 if (tex_y < 0)
68 tex_y = 0;
69 if (tex_y > tex_height)
70 tex_y = tex_height;
71 return (tex_y);
72}
73
77void draw_vertical_line(t_frame *frame, int x)
78{
79 int y;
80 int tex_x;
81 int tex_y;
82 int color;
83
84 tex_x = frame->dda.tex_x;
85 if (x < 0 || x >= IMG_3D_WIDTH)
86 return ;
87 if (frame->wall_slice.draw_start < 0)
88 frame->wall_slice.draw_start = 0;
89 if (frame->wall_slice.draw_end >= IMG_3D_HEIGHT)
90 frame->wall_slice.draw_end = IMG_3D_HEIGHT;
91 y = frame->wall_slice.draw_start;
92 draw_ceiling(frame, x, y);
93 while (y <= frame->wall_slice.draw_end)
94 {
95 tex_y = get_texture_y_coordinate(frame, y);
96 color = get_color_texture_pixel(frame, tex_x, tex_y);
97 my_mlx_pixel_put(frame->img_3d, x, y, color);
98 y++;
99 }
100 draw_floor(frame, x, frame->wall_slice.draw_end);
101}
102 //debug_texture_y_coordinate_overflow(frame, y);
single still image for render_frame
Definition type_cub3d.h:247
3D or 2D or Texture image
Definition type_cub3d.h:82