diff --git a/include/meta.h b/include/meta.h index 65991ed..2187e6a 100644 --- a/include/meta.h +++ b/include/meta.h @@ -138,11 +138,13 @@ typedef struct s_ray { t_vec2d direction; t_vec2d end; t_vec2d map_pos; - t_vec2i texture; + t_vec2d texture_point; + t_vec2d line_point; t_side hit_side; + + double line_height; double length; double wall_x; - double line_height; double texture_position; double step; } t_ray; @@ -254,7 +256,7 @@ void meta_free(t_meta *meta); int set_textures(t_attr *attributes); //pixel_picker.c -uint32_t pixel_picker(t_tex texture, uint32_t x, uint32_t y); -void wall_texture_position(t_tex texture, t_ray *ray, t_vec2d line_points, uint32_t h); +uint32_t pixel_picker(mlx_texture_t *texture, int32_t x, int32_t y); +void wall_texture_position(mlx_texture_t *texture, t_ray *ray, t_vec2d line_points, uint32_t h); #endif diff --git a/src/game/raycaster.c b/src/game/raycaster.c index 035038d..8960b66 100644 --- a/src/game/raycaster.c +++ b/src/game/raycaster.c @@ -117,6 +117,15 @@ t_ray raycaster_cast(t_vec2d pp, t_vec2d dir, t_ray_hitfunc hit, const void *par r.direction = dir; r.end = r.map_pos; + r.line_height = (int)(WINDOW_HEIGHT / r.length); + if (r.line_height > WINDOW_HEIGHT) + r.line_height = WINDOW_HEIGHT; + + // draw start and draw end + r.line_point.x = -r.line_height / 2 + ((double)WINDOW_HEIGHT) / 2; + r.line_point.y = r.line_height / 2 + ((double)WINDOW_HEIGHT) / 2; + if (r.line_point.y >= WINDOW_HEIGHT) + r.line_point.y = WINDOW_HEIGHT - 1; if (r.hit_side == SIDE_N || r.hit_side == SIDE_S) r.wall_x = r.map_pos.y + r.length * r.direction.y; else diff --git a/src/game/render_viewport.c b/src/game/render_viewport.c index e2a5bf4..146df72 100644 --- a/src/game/render_viewport.c +++ b/src/game/render_viewport.c @@ -12,7 +12,7 @@ #include "meta.h" -static t_vec2d calculate_line_points(double ray_length, uint32_t h, double line_height) +static t_vec2d calculate_line_points(uint32_t h, double line_height) { //calculate lowest and highest pixel to fill in current stripe uint32_t start; @@ -68,10 +68,10 @@ void render_viewport(mlx_image_t *image, t_player *p) uint32_t i = 0; while(i < image->width) { - p->rays[i].line_height = calculate_line_height(p->rays[i].length, image->height); - t_vec2d line = calculate_line_points(p->rays[i].length, image->height, p->rays[i].line_height); + // p->rays[i].line_height = calculate_line_height(p->rays[i].length, image->height); + // t_vec2d line = calculate_line_points(image->height, p->rays[i].line_height); - draw_column(p->meta, line, &p->rays[i], i, image->height); + draw_column(p->meta, p->rays[i].line_point, &p->rays[i], i, image->height); i++; } } diff --git a/src/renderer/pixel_picker.c b/src/renderer/pixel_picker.c index 6d998a9..e579601 100644 --- a/src/renderer/pixel_picker.c +++ b/src/renderer/pixel_picker.c @@ -15,35 +15,36 @@ #include // calculate texture position -void wall_texture_position(t_tex texture, t_ray *ray, t_vec2d line_points, uint32_t h) +void wall_texture_position(mlx_texture_t *texture, t_ray *ray, t_vec2d line_points, uint32_t h) { - // printf("wall x: %f\n", ray->wall_x); - ray->texture.x = (int)(ray->wall_x * (double)texture.tex->width); + ray->texture_point.x = (int)(ray->wall_x * (double)texture->width); if ((ray->hit_side == SIDE_N || ray->hit_side == SIDE_S) && ray->direction.x > 0) { - ray->texture.x = texture.tex->width - ray->texture.x - 1; + ray->texture_point.x = texture->width - ray->texture_point.x - 1; } if ((ray->hit_side == SIDE_E || ray->hit_side == SIDE_W) && ray->direction.y < 0) { - ray->texture.x = texture.tex->width - ray->texture.x - 1; + ray->texture_point.x = texture->width - ray->texture_point.x - 1; } - ray->step = 1.0 * texture.tex->height / ray->line_height; - // printf("step: %f\n", ray->step); + ray->step = 1.0 * texture->height / ray->line_height; // x is draw start and y is draw end ray->texture_position = (line_points.x + (ray->line_height - h )/ 2) * ray->step; } -uint32_t pixel_picker(t_tex texture, uint32_t x, uint32_t y) +uint32_t pixel_picker(mlx_texture_t *texture, int32_t x, int32_t y) { - uint32_t color = 0; + int32_t r; + int32_t g; + int32_t b; + int32_t a; int32_t index; // printf("x: %u | y: %u\n", x, y); - index = (y % texture.tex->width) * texture.tex->width * 4 +(x % texture.tex->width) * 4; + index = y * texture->width * 4 + x * 4; // printf("index: %d\n", index); - color |= (uint32_t)texture.tex->pixels[index] << 24; - color |= (uint32_t)texture.tex->pixels[index + 1] << 16; - color |= (uint32_t)texture.tex->pixels[index + 2] << 8; - color |= (uint32_t)texture.tex->pixels[index + 3]; - return (color); + r = (uint32_t)texture->pixels[index]; + g = (uint32_t)texture->pixels[index + 1]; + b = (uint32_t)texture->pixels[index + 2]; + a = (uint32_t)texture->pixels[index + 3]; + return (r << 24 | g << 16 | b << 8 | a); } \ No newline at end of file diff --git a/src/utils/colors.c b/src/utils/colors.c index b9fe8bd..5872265 100644 --- a/src/utils/colors.c +++ b/src/utils/colors.c @@ -12,6 +12,7 @@ #include "meta.h" #include +#include int32_t set_color(int32_t r, int32_t g, int32_t b, int32_t a) { @@ -33,30 +34,27 @@ int32_t set_color(int32_t r, int32_t g, int32_t b, int32_t a) // return (color); // } -t_tex get_texture(t_side side, t_attr attributes) +mlx_texture_t *get_texture(t_side side, t_attr attributes) { if (side == SIDE_N) - return (attributes.n); + return (attributes.n.tex); else if (side == SIDE_S) - return (attributes.s); + return (attributes.s.tex); else if (side == SIDE_E) - return (attributes.e); - return (attributes.w); + return (attributes.e.tex); + return (attributes.w.tex); } int32_t find_wall_color(t_attr atrributes, t_ray *ray, t_vec2d line_points, uint32_t h) { - int32_t color; - t_tex texture; + int32_t color; + mlx_texture_t *texture; texture = get_texture(ray->hit_side, atrributes); wall_texture_position(texture, ray, line_points, h); - ray->texture.y = (int)ray->texture_position & (texture.tex->height - 1); + ray->texture_point.y = (int)ray->texture_position & (texture->height - 1); ray->texture_position += ray->step; - printf("h: %d\n",h); - // printf("texture position: %f\n", ray->texture_position); - // printf("x: %d | y: %d\n", ray->texture.x, ray->texture.y); - color = pixel_picker(texture, ray->texture.x, ray->texture.y); + color = pixel_picker(texture, (int)round(ray->texture_point.x), (int)round(ray->texture_point.y)); return (color); }