Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ys-zm committed Jan 15, 2024
1 parent 837855b commit 10b2c2e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 35 deletions.
10 changes: 6 additions & 4 deletions include/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions src/game/raycaster.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/game/render_viewport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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++;
}
}
Expand Down
31 changes: 16 additions & 15 deletions src/renderer/pixel_picker.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,36 @@
#include <string.h>

// 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);
}
22 changes: 10 additions & 12 deletions src/utils/colors.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "meta.h"
#include <stdio.h>
#include <math.h>

int32_t set_color(int32_t r, int32_t g, int32_t b, int32_t a)
{
Expand All @@ -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);
}

Expand Down

0 comments on commit 10b2c2e

Please sign in to comment.