열렬히.뛰기

8. 일부 에러 수정

école 42 > cub3D > 진행상황 > 8. 일부 에러 수정

기존 문제

벽이 가까이 다가가면 일그러진다.

c
void	raycast_draw_line(t_box *tools, t_data *camera_image, int x)
{
	t_vec_f	texture;

	tools->line_height = (HEIGHT / tools->perpwalldist) / 2;
	tools->draw_start = HEIGHT / 2 - tools->line_height / 2;
	if (tools->draw_start < 0)
		tools->draw_start = 0;
	tools->draw_end = HEIGHT / 2 + tools->line_height / 2;
	if (tools->draw_end >= HEIGHT)
		tools->draw_end = HEIGHT - 1;
	texture.x = x;

	// 텍스쳐의 y값이 무조건 0으로 고정되어 있음.
	texture.y = 0;
	while (tools->draw_start <= tools->draw_end)
	{
		if (tools->side == 0)
			raycast_draw_eastwest(tools, camera_image, &texture, x);
		else if (tools->side == 1)
			raycast_draw_northsouth(tools, camera_image, &texture, x);
		(tools->draw_start)++;
	}
}
c
void	raycast_draw_northsouth(t_box *tools, t_data *camera_image, \
								t_vec_f *texture, int x)
{
	int	color;

	if (tools->raydir.y > 0) // 남쪽
	{
		set_texture_vector(tools, &tools->img_south, texture);
		color = get_color_of_texture(&tools->img_south, texture);
		my_mlx_pixel_put(camera_image, x, tools->draw_start, color);
		(texture->y) += (texture->dy);
		if (texture->y >= tools->img_south.height)
			texture->y = tools->img_south.height - 1;
	}
	else if (tools->raydir.y < 0) // 북쪽
	{
		set_texture_vector(tools, &tools->img_north, texture);
		color = get_color_of_texture(&tools->img_north, texture);
		my_mlx_pixel_put(camera_image, x, tools->draw_start, color);
		(texture->y) += (texture->dy);
		if (texture->y >= tools->img_north.height)
			texture->y = tools->img_north.height - 1;
	}
}

해결법

texture 구조체의 y값이 바뀌도록 수정한다.

이를 위해 기존 구조를 상당히 수정했다.

기존 구조는 다음과 같다.

  • while → if → 방향에 따른 그리기
    • set_texture_vector : dy 정하기
    • color 얻기 : get_color_of_texture
    • my_mlx_pixel_put
    • y += dy (y가 임계점을 넘을 때 조정)

<2. 고정된 이미지 출력> 파트는 이 부분에 맞게 수정했다. 2023.11.4 기준