diff options
-rw-r--r-- | inc/cub3d_structs.h | 5 | ||||
-rw-r--r-- | src/ft_floor_cast.c | 30 | ||||
-rw-r--r-- | src/ft_raycasting.c | 39 | ||||
-rw-r--r-- | src/ft_rgb_to_hex.c | 7 |
4 files changed, 54 insertions, 27 deletions
diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h index 49ede14..d9deb8a 100644 --- a/inc/cub3d_structs.h +++ b/inc/cub3d_structs.h @@ -168,9 +168,10 @@ typedef struct s_ray float row_dist; float x_floor; float y_floor; - int32_t x_cell; - int32_t y_cell; int32_t *tex_x_tab; + float *x_floor_tab; + float *y_floor_tab; + float *row_dist_tab; int16_t *wall_t_tab; uint8_t *w_side_tab; uint16_t *line_h_tab; diff --git a/src/ft_floor_cast.c b/src/ft_floor_cast.c index 180f401..aa89f99 100644 --- a/src/ft_floor_cast.c +++ b/src/ft_floor_cast.c @@ -18,7 +18,8 @@ static void { float dist; - dist = (cl->rlist.row_dist > 0) ? (cl->rlist.row_dist) : (0.0001); + if ((dist = cl->rlist.row_dist_tab[y]) <= 0) + dist = 0.0001; *(int*)(cl->img.ptr + (x * 4 + (y * cl->img.sizeline))) = ft_rgb_to_hex(dist, rgb, cl); } @@ -29,7 +30,8 @@ static void float dist; t_rgb rgb; - dist = (cl->rlist.row_dist > 0) ? (cl->rlist.row_dist) : (0.0001); + if ((dist = cl->rlist.row_dist_tab[y]) <= 0) + dist = 0.0001; rgb.b = (uint8_t)cl->tlist[tid].ptr[cl->tlist[tid].tex_x * 4 + 4 * cl->tlist[tid].img_h * cl->tlist[tid].tex_y]; rgb.g = (uint8_t)cl->tlist[tid].ptr[cl->tlist[tid].tex_x @@ -41,33 +43,30 @@ static void } static void - ft_set_tex_xy(uint8_t tid, t_ray *rl, t_cub *cl) + ft_set_tex_xy(uint8_t tid, uint16_t x, t_ray *rl, t_cub *cl) { - cl->tlist[tid].tex_y = (int32_t)(cl->tlist[tid].img_w - * (rl->x_floor - rl->x_cell)); + const int32_t x_cell = (int32_t)(rl->x_floor_tab[x]); + const int32_t y_cell = (int32_t)(rl->y_floor_tab[x]); + cl->tlist[tid].tex_x = (int32_t)(cl->tlist[tid].img_h - * (rl->y_floor - rl->y_cell)); - cl->tlist[tid].tex_x = (cl->tlist[tid].tex_x > 0) - ? (cl->tlist[tid].tex_x) : (-cl->tlist[tid].tex_x); - cl->tlist[tid].tex_y = (cl->tlist[tid].tex_y > 0) - ? (cl->tlist[tid].tex_y) : (-cl->tlist[tid].tex_y); + * (rl->y_floor_tab[x] - y_cell)); + cl->tlist[tid].tex_y = (int32_t)(cl->tlist[tid].img_w + * (rl->x_floor_tab[x] - x_cell)); } static void ft_floor_cast_loop(uint16_t y, uint16_t x, t_ray *rl, t_cub *cl) { - rl->x_cell = (int32_t)(rl->x_floor); - rl->y_cell = (int32_t)(rl->y_floor); if (cl->mlist.isftex) { - ft_set_tex_xy(6, rl, cl); + ft_set_tex_xy(6, x, rl, cl); ft_draw_extra_tex(6, y, x, cl); } else ft_draw_plain_horizontal(cl->f_rgb, cl, y, x); if (cl->mlist.isctex && !cl->mlist.isskybox) { - ft_set_tex_xy(7, rl, cl); + ft_set_tex_xy(7, x, rl, cl); ft_draw_extra_tex(7, cl->wlist.y_size - y - 1, x, cl); } else if (!cl->mlist.isctex && !cl->mlist.isskybox) @@ -85,14 +84,11 @@ void y = (cl->wlist.y_size / 2); while (y < cl->wlist.y_size) { - ft_floor_cast_inits(y, &cl->rlist, cl); x = 0; while (x < cl->wlist.x_size) { if (cl->rlist.wall_bz[x] <= y) ft_floor_cast_loop(y, x, &cl->rlist, cl); - cl->rlist.x_floor += cl->mlist.x_floor_step; - cl->rlist.y_floor += cl->mlist.y_floor_step; x++; } y++; diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c index 89cc61f..73aaf30 100644 --- a/src/ft_raycasting.c +++ b/src/ft_raycasting.c @@ -91,7 +91,8 @@ void ft_castray(t_cub *cl) { pthread_t tid[2]; - uint16_t i; + uint16_t y; + uint16_t x; if (!(cl->rlist.wall_dist_tab = (float*)malloc(cl->wlist.x_size * sizeof(float))) || @@ -104,13 +105,36 @@ void !(cl->rlist.wall_t_tab = (int16_t*)malloc(cl->wlist.x_size * sizeof(int16_t))) || !(cl->rlist.tex_x_tab = (int32_t*)malloc(cl->wlist.x_size * - sizeof(int32_t)))) + sizeof(int32_t))) || + !(cl->rlist.y_floor_tab = (float*)malloc(cl->wlist.x_size * + sizeof(float))) || + !(cl->rlist.x_floor_tab = (float*)malloc(cl->wlist.x_size * + sizeof(float))) || + !(cl->rlist.row_dist_tab = (float*)malloc(cl->wlist.y_size * + sizeof(float)))) return ; - i = 0; - while (i < cl->wlist.x_size) + x = 0; + while (x < cl->wlist.x_size) + { + ft_castray_loop(x, &cl->wlist, cl); + x++; + } + y = cl->wlist.y_size / 2; + while (y < cl->wlist.y_size) { - ft_castray_loop(i, &cl->wlist, cl); - i++; + x = 0; + ft_floor_cast_inits(y, &cl->rlist, cl); + while (x < cl->wlist.x_size) + { + cl->rlist.x_floor += cl->mlist.x_floor_step; + cl->rlist.y_floor += cl->mlist.y_floor_step; + cl->rlist.x_floor_tab[x] = cl->rlist.x_floor; + cl->rlist.y_floor_tab[x] = cl->rlist.y_floor; + x++; + } + cl->rlist.row_dist_tab[y] = cl->rlist.row_dist; + cl->rlist.row_dist_tab[cl->wlist.y_size - y] = cl->rlist.row_dist; + y++; } pthread_create(&tid[0], 0x0, ft_wall_cast, (void*)cl); pthread_create(&tid[1], 0x0, ft_floor_cast, (void*)cl); @@ -123,6 +147,9 @@ void if (cl->plist.handles_weapon > -1) ft_draw_handweap(cl); ft_memdel((void*)&cl->rlist.tex_x_tab); + ft_memdel((void*)&cl->rlist.y_floor_tab); + ft_memdel((void*)&cl->rlist.x_floor_tab); + ft_memdel((void*)&cl->rlist.row_dist_tab); ft_memdel((void*)&cl->rlist.wall_t_tab); ft_memdel((void*)&cl->rlist.w_side_tab); ft_memdel((void*)&cl->rlist.line_h_tab); diff --git a/src/ft_rgb_to_hex.c b/src/ft_rgb_to_hex.c index 2b72287..2ef1862 100644 --- a/src/ft_rgb_to_hex.c +++ b/src/ft_rgb_to_hex.c @@ -29,7 +29,10 @@ uint32_t rgb.g = (rgb.g < 0) ? (0) : (rgb.g); rgb.b = (rgb.b < 0) ? (0) : (rgb.b); res = 0; - res += (((uint8_t)(rgb.r / calc) << 16) + ((uint8_t)(rgb.g / calc) << 8) - + (uint8_t)(rgb.b / calc)); + if (calc <= 1) + res += ((rgb.r << 16) + (rgb.g << 8) + rgb.b); + else + res += (((uint8_t)(rgb.r / calc) << 16) + ((uint8_t)(rgb.g / calc) << 8) + + (uint8_t)(rgb.b / calc)); return (res); } |