Makefile upgrade and mlx_image_put heuristic
This commit is contained in:
parent
ef401ebeae
commit
1cdd38aca7
10 changed files with 236 additions and 219 deletions
|
@ -11,7 +11,9 @@ set(SOURCE_FILES
|
||||||
src/line.c
|
src/line.c
|
||||||
src/map_init.c
|
src/map_init.c
|
||||||
src/pnt_init.c
|
src/pnt_init.c
|
||||||
src/help_func.c src/render.c src/mat4.c) # sources
|
src/help_func.c
|
||||||
|
src/render.c
|
||||||
|
src/mat4.c) # sources
|
||||||
|
|
||||||
add_executable(fdf ${SOURCE_FILES}) # compilation
|
add_executable(fdf ${SOURCE_FILES}) # compilation
|
||||||
|
|
||||||
|
|
100
Makefile
100
Makefile
|
@ -10,83 +10,93 @@
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
NAME = fdf
|
# clor constants
|
||||||
|
|
||||||
|
SWITCH = \033[
|
||||||
|
|
||||||
SRCDIR = ./src/
|
NORMAL = $(SWITCH)0m
|
||||||
|
GREEN = $(SWITCH)32m
|
||||||
|
BLUE = $(SWITCH)34m
|
||||||
|
CYAN = $(SWITCH)36m
|
||||||
|
|
||||||
OBJDIR = ./obj/
|
# final binary name
|
||||||
|
|
||||||
|
NAME = fdf
|
||||||
|
|
||||||
SRC_FILES = main.c \
|
# static libraries
|
||||||
line.c \
|
|
||||||
map_init.c \
|
|
||||||
pnt_init.c \
|
|
||||||
help_func.c \
|
|
||||||
mat4.c \
|
|
||||||
render.c
|
|
||||||
|
|
||||||
OBJ_FILES = $(SRC_FILES:.c=.o)
|
LIBFT = libft.a
|
||||||
|
MLX = libmlx.a
|
||||||
|
|
||||||
|
# directories
|
||||||
|
|
||||||
SRC = $(addprefix $(SRCDIR), $(SRC_FILES))
|
SRC_DIR = ./src/
|
||||||
|
OBJ_DIR = ./obj/
|
||||||
|
LIBFT_DIR = ./libft/
|
||||||
|
MLX_DIR = ./minilibx/
|
||||||
|
|
||||||
OBJ = $(addprefix $(OBJDIR), $(OBJ_FILES))
|
# src and obj files
|
||||||
|
|
||||||
|
SRC_FILES = $(shell ls $(SRC_DIR))
|
||||||
|
OBJ_FILES = $(SRC_FILES:.c=.o)
|
||||||
|
|
||||||
INC = -I $(LIBFT_FOLDER)includes/ \
|
SRC = $(addprefix $(SRC_DIR), $(SRC_FILES))
|
||||||
-I $(MLX_FOLDER) \
|
OBJ = $(addprefix $(OBJ_DIR), $(OBJ_FILES))
|
||||||
-I ./inc
|
|
||||||
|
|
||||||
|
# header files
|
||||||
|
|
||||||
LIBFT = libft.a
|
INC = -I $(LIBFT_DIR)includes/ \
|
||||||
|
-I $(MLX_DIR) \
|
||||||
|
-I ./inc
|
||||||
|
|
||||||
MLX = libmlx.a
|
# compiler flags
|
||||||
|
|
||||||
|
LIBFT_FLAGS = -lft -L $(LIBFT_DIR)
|
||||||
|
MLX_FLAGS = -lmlx -lXext -lX11 -L $(MLX_DIR)
|
||||||
|
OTHER_FLAGS = -lm
|
||||||
|
CC_FLAGS = -Werror -Wextra -Wall -O3
|
||||||
|
FLAGS := $(CC_FLAGS) $(LIBFT_FLAGS) $(MLX_FLAGS) $(OTHER_FLAGS)
|
||||||
|
|
||||||
LIBFT_FLAGS = -lft -L $(LIBFT_FOLDER)
|
# compiler
|
||||||
|
|
||||||
MLX_FLAGS = -lmlx -lXext -lX11 -L $(MLX_FOLDER)
|
CC = gcc
|
||||||
|
|
||||||
OTHER_FLAGS = -lm
|
|
||||||
|
|
||||||
LIBFT_FOLDER = ./libft/
|
|
||||||
|
|
||||||
MLX_FOLDER = ./minilibx/
|
|
||||||
|
|
||||||
|
|
||||||
FLAGS = -Werror -Wextra -Wall -O3
|
|
||||||
|
|
||||||
CC = gcc
|
|
||||||
|
|
||||||
|
# rules
|
||||||
|
|
||||||
all: $(NAME)
|
all: $(NAME)
|
||||||
|
|
||||||
$(NAME): $(OBJ) $(LIBFT_FOLDER)$(LIBFT) $(MLX_FOLDER)$(MLX)
|
$(NAME): $(OBJ) $(LIBFT_DIR)$(LIBFT) $(MLX_DIR)$(MLX)
|
||||||
$(CC) $(OBJ) $(FLAGS) $(LIBFT_FLAGS) $(MLX_FLAGS) $(OTHER_FLAGS) -o $(NAME)
|
@echo "$(CYAN)Linking fdf binary...$(NORMAL)"
|
||||||
|
@$(CC) $(OBJ) $(FLAGS) -o $(NAME)
|
||||||
|
@echo "$(GREEN)Done!$(NORMAL)"
|
||||||
|
|
||||||
$(OBJDIR)%.o : $(SRCDIR)%.c
|
$(OBJ_DIR)%.o : $(SRC_DIR)%.c
|
||||||
$(CC) $(FLAGS) $(INC) -c $< -o $@
|
@echo "$(CYAN)Compiling object files: $(BLUE)$@$(NORMAL)"
|
||||||
|
@$(CC) $(CC_FLAGS) $(INC) -c $< -o $@
|
||||||
|
|
||||||
$(LIBFT_FOLDER)$(LIBFT):
|
$(LIBFT_DIR)$(LIBFT):
|
||||||
make -C $(LIBFT_FOLDER)
|
@echo "$(CYAN)Compiling libft library...$(NORMAL)"
|
||||||
|
@make -s -C $(LIBFT_DIR)
|
||||||
|
|
||||||
$(MLX_FOLDER)$(MLX):
|
$(MLX_DIR)$(MLX):
|
||||||
make -C $(MLX_FOLDER)
|
@echo "$(CYAN)Compiling libmlx library...$(NORMAL)"
|
||||||
|
@make -s -C $(MLX_DIR)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(OBJ)
|
@rm -rf $(OBJ)
|
||||||
|
|
||||||
fclean: clean
|
fclean: clean
|
||||||
rm -rf $(NAME)
|
@rm -rf $(NAME)
|
||||||
|
|
||||||
libclean:
|
libclean:
|
||||||
make clean -C $(LIBFT_FOLDER)
|
@make clean -s -C $(LIBFT_DIR)
|
||||||
make clean -C $(MLX_FOLDER)
|
@make clean -s -C $(MLX_DIR)
|
||||||
|
|
||||||
libfclean:
|
libfclean:
|
||||||
make fclean -C $(LIBFT_FOLDER)
|
@make fclean -s -C $(LIBFT_DIR)
|
||||||
|
|
||||||
re: fclean libfclean all
|
re: fclean libfclean all
|
||||||
|
|
||||||
pub: fclean libfclean libclean
|
pu: fclean libfclean libclean
|
||||||
|
@echo "$(CYAN)Cleaning for push...$(NORMAL)"
|
||||||
|
|
||||||
|
|
36
inc/fdf.h
36
inc/fdf.h
|
@ -15,32 +15,38 @@
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
# define HEIGHT 500
|
||||||
|
# define WIDTH 500
|
||||||
|
|
||||||
typedef struct s_vec
|
typedef struct s_vec
|
||||||
{
|
{
|
||||||
double x;
|
double x;
|
||||||
double y;
|
double y;
|
||||||
double z;
|
double z;
|
||||||
|
int color;
|
||||||
} t_vec;
|
} t_vec;
|
||||||
|
|
||||||
typedef struct s_pnt
|
typedef struct s_pnt
|
||||||
{
|
{
|
||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
|
int z;
|
||||||
int color;
|
int color;
|
||||||
} t_pnt;
|
} t_pnt;
|
||||||
|
|
||||||
typedef struct s_mlx
|
typedef struct s_mw
|
||||||
{
|
{
|
||||||
void *mlx;
|
void *mlx;
|
||||||
void *win;
|
void *win;
|
||||||
} t_mlx;
|
} t_mw;
|
||||||
|
|
||||||
typedef struct s_map
|
typedef struct s_map
|
||||||
{
|
{
|
||||||
t_list *lst_map;
|
t_list *lst_map;
|
||||||
t_pnt **arr_pnt;
|
t_pnt **arr_pnt;
|
||||||
int width;
|
int x_max;
|
||||||
int height;
|
int y_max;
|
||||||
|
int z_max;
|
||||||
} t_map;
|
} t_map;
|
||||||
|
|
||||||
typedef struct s_mat4
|
typedef struct s_mat4
|
||||||
|
@ -57,21 +63,22 @@ typedef struct s_trnsf
|
||||||
t_mat4 *fin;
|
t_mat4 *fin;
|
||||||
} t_trnsf;
|
} t_trnsf;
|
||||||
|
|
||||||
|
typedef struct s_FDF
|
||||||
|
{
|
||||||
|
t_map *map;
|
||||||
|
t_mw *mw;
|
||||||
|
int w_heigth;
|
||||||
|
int w_width;
|
||||||
|
} t_FDF;
|
||||||
|
|
||||||
typedef struct s_swap
|
typedef struct s_swap
|
||||||
{
|
{
|
||||||
int swap_x;
|
int swap_x;
|
||||||
int swap_coord;
|
int swap_coord;
|
||||||
} t_swap;
|
} t_swap;
|
||||||
|
|
||||||
//delete
|
|
||||||
|
|
||||||
typedef struct s_edg
|
void line(t_pnt p1, t_pnt p2, t_mw *mw);
|
||||||
{
|
|
||||||
int p1;
|
|
||||||
int p2;
|
|
||||||
} t_edg;
|
|
||||||
|
|
||||||
void line(t_pnt p1, t_pnt p2, t_mlx *m);
|
|
||||||
|
|
||||||
t_map *map_init(char *path);
|
t_map *map_init(char *path);
|
||||||
void pnt_init(t_map *map);
|
void pnt_init(t_map *map);
|
||||||
|
@ -87,11 +94,10 @@ void vec_mat_mult(t_mat4 *m, t_vec *v, t_pnt *result);
|
||||||
|
|
||||||
void map_transform(t_map *map);
|
void map_transform(t_map *map);
|
||||||
void transform(t_mat4 *mat, t_map *map);
|
void transform(t_mat4 *mat, t_map *map);
|
||||||
void perspective_transform(t_map *map, t_mlx *mlx);
|
|
||||||
|
|
||||||
void render(t_map *map, t_mlx *m, int x, int y);
|
void render(t_FDF *FDF);
|
||||||
|
|
||||||
void free_tab(char **tab);
|
void free_tab(char **tab);
|
||||||
void quit(t_mlx *m);
|
void quit(t_FDF *FDF);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -14,9 +14,9 @@
|
||||||
all : do_configure
|
all : do_configure
|
||||||
|
|
||||||
do_configure :
|
do_configure :
|
||||||
./configure
|
./configure > /dev/null 2>&1
|
||||||
|
|
||||||
clean :
|
clean :
|
||||||
./configure clean
|
./configure clean > /dev/null 2>&1
|
||||||
|
|
||||||
re : clean all
|
re : clean all
|
||||||
|
|
|
@ -7,8 +7,8 @@ void free_tab(char **tab)
|
||||||
free(*tab);
|
free(*tab);
|
||||||
}
|
}
|
||||||
|
|
||||||
void quit(t_mlx *m)
|
void quit(t_FDF *FDF)
|
||||||
{
|
{
|
||||||
mlx_destroy_window(m->mlx, m->win);
|
mlx_destroy_window(FDF->mw->mlx, FDF->mw->win);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
10
src/line.c
10
src/line.c
|
@ -23,7 +23,7 @@ static void swap_points(t_pnt *p1, t_pnt *p2)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void draw_line(t_pnt *p1, t_pnt *p2, t_mlx *m, t_swap *s)
|
static void draw_line(t_pnt *p1, t_pnt *p2, t_mw *mw, t_swap *s)
|
||||||
{
|
{
|
||||||
int dx = abs(p2->x - p1->x);
|
int dx = abs(p2->x - p1->x);
|
||||||
int dy = abs(p2->y - p1->y);
|
int dy = abs(p2->y - p1->y);
|
||||||
|
@ -31,9 +31,9 @@ static void draw_line(t_pnt *p1, t_pnt *p2, t_mlx *m, t_swap *s)
|
||||||
while (p1->x < p2->x)
|
while (p1->x < p2->x)
|
||||||
{
|
{
|
||||||
if (s->swap_coord)
|
if (s->swap_coord)
|
||||||
mlx_pixel_put(m->mlx, m->win, p1->y, p1->x, 0xFFFFFF);
|
mlx_pixel_put(mw->mlx, mw->win, p1->y, p1->x, 0xFFFFFF);
|
||||||
else
|
else
|
||||||
mlx_pixel_put(m->mlx, m->win, p1->x, p1->y, 0xFFFFFF);
|
mlx_pixel_put(mw->mlx, mw->win, p1->x, p1->y, 0xFFFFFF);
|
||||||
if (derr > 0)
|
if (derr > 0)
|
||||||
{
|
{
|
||||||
p1->y += s->swap_x;
|
p1->y += s->swap_x;
|
||||||
|
@ -44,7 +44,7 @@ static void draw_line(t_pnt *p1, t_pnt *p2, t_mlx *m, t_swap *s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void line(t_pnt p1, t_pnt p2, t_mlx *m)
|
void line(t_pnt p1, t_pnt p2, t_mw *mw)
|
||||||
{
|
{
|
||||||
int dx;
|
int dx;
|
||||||
int dy;
|
int dy;
|
||||||
|
@ -63,6 +63,6 @@ void line(t_pnt p1, t_pnt p2, t_mlx *m)
|
||||||
swap_points(&p1, &p2);
|
swap_points(&p1, &p2);
|
||||||
if (p1.y > p2.y)
|
if (p1.y > p2.y)
|
||||||
swap.swap_x = -1;
|
swap.swap_x = -1;
|
||||||
draw_line(&p1, &p2, m, &swap);
|
draw_line(&p1, &p2, mw, &swap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
138
src/main.c
138
src/main.c
|
@ -3,83 +3,16 @@
|
||||||
// FILE: main.c
|
// FILE: main.c
|
||||||
// ROLE: to rule them all
|
// ROLE: to rule them all
|
||||||
// CREATED: 2017-01-30 21:51:28
|
// CREATED: 2017-01-30 21:51:28
|
||||||
// MODIFIED: 2017-02-13 20:30:48
|
// MODIFIED: 2017-03-09 20:51:10
|
||||||
|
|
||||||
#include "fdf.h"
|
#include "fdf.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void test_line(t_mlx mlx)
|
|
||||||
{
|
|
||||||
t_pnt p1;
|
|
||||||
t_pnt p2;
|
|
||||||
int temp;
|
|
||||||
|
|
||||||
p1.color = 0xC100E5;
|
|
||||||
p2.color = 0xC100E5;
|
|
||||||
for (p2.y = 0; p2.y <= 500; p2.y += 4)
|
|
||||||
{
|
|
||||||
p1.x = 0;
|
|
||||||
p1.y = 0;
|
|
||||||
p2.x = 500;
|
|
||||||
line(p1, p2, &mlx);
|
|
||||||
}
|
|
||||||
for (p2.x = 500; p2.x >= 0; p2.x -= 4)
|
|
||||||
{
|
|
||||||
p1.x = 0;
|
|
||||||
p1.y = 0;
|
|
||||||
p2.y = 500;
|
|
||||||
temp = p2.x;
|
|
||||||
line(p1, p2, &mlx);
|
|
||||||
p2.x = temp;
|
|
||||||
}
|
|
||||||
p1.color = 0x1EBF00;
|
|
||||||
p2.color = 0x1EBF00;
|
|
||||||
for (p2.y = 0; p2.y <= 500; p2.y += 4)
|
|
||||||
{
|
|
||||||
p1.x = 500;
|
|
||||||
p1.y = 0;
|
|
||||||
p2.x = 0;
|
|
||||||
temp = p2.y;
|
|
||||||
line(p1, p2, &mlx);
|
|
||||||
p2.y = temp;
|
|
||||||
}
|
|
||||||
for (p2.x = 0; p2.x <= 500; p2.x += 4)
|
|
||||||
{
|
|
||||||
p1.x = 500;
|
|
||||||
p1.y = 0;
|
|
||||||
p2.y = 500;
|
|
||||||
temp = p2.x;
|
|
||||||
line(p1, p2, &mlx);
|
|
||||||
p2.x = temp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void circle(t_mlx m)
|
|
||||||
{
|
|
||||||
t_pnt p1;
|
|
||||||
t_pnt p2;
|
|
||||||
double angle;
|
|
||||||
|
|
||||||
|
|
||||||
for (int i = 0; i < 360; i += 4)
|
|
||||||
{
|
|
||||||
p1.x = 250;
|
|
||||||
p1.y = 250;
|
|
||||||
angle = i * M_PI / 180;
|
|
||||||
p2.x = p1.x + 200 * sin(angle);
|
|
||||||
p2.y = p1.y + 200 * cos(angle);
|
|
||||||
line(p1, p2, &m);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int key_hook(int keycode, void *m)
|
int key_hook(int keycode, void *m)
|
||||||
{
|
{
|
||||||
printf("%d\n", keycode);
|
printf("%d\n", keycode);
|
||||||
if (keycode == 65307)
|
if (keycode == 53 || keycode == 65307)
|
||||||
quit(m);
|
quit(m);
|
||||||
// if (keycode == 123)
|
// if (keycode == 123)
|
||||||
// left(mlx);
|
// left(mlx);
|
||||||
|
@ -90,20 +23,67 @@ int key_hook(int keycode, void *m)
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t_mw *mlx_and_win_ptr_init(int x, int y)
|
||||||
|
{
|
||||||
|
t_mw *mw;
|
||||||
|
|
||||||
|
mw = NULL;
|
||||||
|
if ((mw = malloc(sizeof(t_mw))))
|
||||||
|
{
|
||||||
|
mw->mlx = mlx_init();
|
||||||
|
mw->win = mlx_new_window(mw->mlx, x, y, "FDF");
|
||||||
|
}
|
||||||
|
return (mw);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_FDF *FDF_init(char *path)
|
||||||
|
{
|
||||||
|
t_FDF *FDF;
|
||||||
|
|
||||||
|
FDF = NULL;
|
||||||
|
if ((FDF = malloc(sizeof(t_FDF))))
|
||||||
|
{
|
||||||
|
FDF->w_heigth = HEIGHT;
|
||||||
|
FDF->w_width = WIDTH;
|
||||||
|
FDF->mw = mlx_and_win_ptr_init(WIDTH, HEIGHT);
|
||||||
|
FDF->map = map_init(path);
|
||||||
|
pnt_init(FDF->map);
|
||||||
|
}
|
||||||
|
return (FDF);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
t_mlx m;
|
t_FDF *FDF;
|
||||||
t_map *map;
|
void *image;
|
||||||
|
char *data;
|
||||||
|
int i;
|
||||||
|
int bpp;
|
||||||
|
int endian;
|
||||||
|
int j;
|
||||||
|
int line;
|
||||||
|
|
||||||
if (argc != 2)
|
if (argc != 2)
|
||||||
return (0);
|
return (0);
|
||||||
m.mlx = mlx_init();
|
FDF = FDF_init(argv[1]);
|
||||||
m.win = mlx_new_window(m.mlx, 500, 500, "fdf");
|
i = 0;
|
||||||
map = map_init(argv[1]);
|
bpp = 4;
|
||||||
pnt_init(map);
|
endian = 1;
|
||||||
//circle(m);
|
image = mlx_new_image(FDF->mw->mlx, FDF->w_heigth, FDF->w_heigth);
|
||||||
render(map, &m, 500, 500);
|
data = mlx_get_data_addr(image, &bpp, &line, &endian);
|
||||||
mlx_key_hook(m.win, key_hook, &m);
|
while (i < FDF->w_heigth)
|
||||||
mlx_loop(m.mlx);
|
{
|
||||||
|
j = 0;
|
||||||
|
while (j < FDF->w_width)
|
||||||
|
{
|
||||||
|
*((int *)data + j + i * FDF->w_width) = mlx_get_color_value(FDF->mw->mlx, 0x4286f4);
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, image, 0, 0);
|
||||||
|
//render(FDF);
|
||||||
|
mlx_key_hook(FDF->mw->win, key_hook, FDF);
|
||||||
|
mlx_loop(FDF->mw->mlx);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#include "fdf.h"
|
#include "fdf.h"
|
||||||
|
|
||||||
void fill_row(t_list **list, int i, char *row, t_map *map)
|
void fill_row(t_list **list, int i, char *row)
|
||||||
{
|
{
|
||||||
int j;
|
int j;
|
||||||
char **row_numbers;
|
char **row_numbers;
|
||||||
|
@ -16,11 +16,10 @@ void fill_row(t_list **list, int i, char *row, t_map *map)
|
||||||
ft_lstadd_back(list, &vec3, sizeof(t_vec));
|
ft_lstadd_back(list, &vec3, sizeof(t_vec));
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
free(row_numbers);
|
free_tab(row_numbers);
|
||||||
map->width = j;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t_list *to_lst_map(char **tab_map, t_map *map)
|
t_list *to_lst_map(char **tab_map)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
t_list *lst_map;
|
t_list *lst_map;
|
||||||
|
@ -29,7 +28,7 @@ t_list *to_lst_map(char **tab_map, t_map *map)
|
||||||
i = 0;
|
i = 0;
|
||||||
while (tab_map[i])
|
while (tab_map[i])
|
||||||
{
|
{
|
||||||
fill_row(&lst_map, i, tab_map[i], map);
|
fill_row(&lst_map, i, tab_map[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (lst_map);
|
return (lst_map);
|
||||||
|
@ -45,15 +44,30 @@ char *to_str_map(char *path)
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int map_heigth(char *str_map)
|
void map_dimensions(t_map *map)
|
||||||
{
|
{
|
||||||
int h;
|
int x;
|
||||||
|
int y;
|
||||||
|
int z;
|
||||||
|
t_list *l;
|
||||||
|
|
||||||
h = 0;
|
l = map->lst_map;
|
||||||
while (*str_map++)
|
x = 0;
|
||||||
if (*str_map == '\n')
|
y = 0;
|
||||||
h++;
|
z = 0;
|
||||||
return (h);
|
while (l)
|
||||||
|
{
|
||||||
|
if (((t_vec *)(l->content))->x > x)
|
||||||
|
x = (int)((t_vec *)(l->content))->x;
|
||||||
|
if (((t_vec *)(l->content))->y > y)
|
||||||
|
y = (int)((t_vec *)(l->content))->y;
|
||||||
|
if (((t_vec *)(l->content))->z > z)
|
||||||
|
z = (int)((t_vec *)(l->content))->z;
|
||||||
|
l = l->next;
|
||||||
|
}
|
||||||
|
map->x_max = x;
|
||||||
|
map->y_max = y;
|
||||||
|
map->z_max = z;
|
||||||
}
|
}
|
||||||
|
|
||||||
t_map *map_init(char *path)
|
t_map *map_init(char *path)
|
||||||
|
@ -64,10 +78,9 @@ t_map *map_init(char *path)
|
||||||
|
|
||||||
map = malloc(sizeof(t_map));
|
map = malloc(sizeof(t_map));
|
||||||
str_map = to_str_map(path);
|
str_map = to_str_map(path);
|
||||||
printf("%s\n", str_map);
|
|
||||||
map->height = map_heigth(str_map);
|
|
||||||
tab_map = ft_strsplit(str_map, '\n');
|
tab_map = ft_strsplit(str_map, '\n');
|
||||||
map->lst_map = to_lst_map(tab_map, map);
|
map->lst_map = to_lst_map(tab_map);
|
||||||
|
map_dimensions(map);
|
||||||
free(str_map);
|
free(str_map);
|
||||||
free_tab(tab_map);
|
free_tab(tab_map);
|
||||||
return (map);
|
return (map);
|
||||||
|
|
|
@ -5,11 +5,11 @@ void pnt_init(t_map *map)
|
||||||
t_pnt **arr_pnt;
|
t_pnt **arr_pnt;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
arr_pnt = malloc(sizeof(t_pnt *) * map->height);
|
arr_pnt = malloc(sizeof(t_pnt *) * (map->y_max + 1));
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < map->height)
|
while (i <= map->y_max)
|
||||||
{
|
{
|
||||||
arr_pnt[i] = malloc(sizeof(t_pnt) * (map->width));
|
arr_pnt[i] = malloc(sizeof(t_pnt) * (map->x_max + 1));
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
map->arr_pnt = arr_pnt;
|
map->arr_pnt = arr_pnt;
|
||||||
|
|
110
src/render.c
110
src/render.c
|
@ -16,24 +16,6 @@ t_trnsf *transfor_mat_init()
|
||||||
return (trnsf);
|
return (trnsf);
|
||||||
}
|
}
|
||||||
|
|
||||||
int max_z(t_map *map)
|
|
||||||
{
|
|
||||||
int z;
|
|
||||||
t_list *tmp;
|
|
||||||
|
|
||||||
z = 0;
|
|
||||||
tmp = map->lst_map;
|
|
||||||
while (tmp)
|
|
||||||
{
|
|
||||||
if (((t_vec *)(tmp->content))->z > z)
|
|
||||||
{
|
|
||||||
z = (int)((t_vec *)(tmp->content))->z;
|
|
||||||
}
|
|
||||||
tmp = tmp->next;
|
|
||||||
}
|
|
||||||
return (z);
|
|
||||||
}
|
|
||||||
|
|
||||||
void transform(t_mat4 *mat, t_map *map)
|
void transform(t_mat4 *mat, t_map *map)
|
||||||
{
|
{
|
||||||
t_list *vec_list;
|
t_list *vec_list;
|
||||||
|
@ -42,10 +24,10 @@ void transform(t_mat4 *mat, t_map *map)
|
||||||
|
|
||||||
vec_list = map->lst_map;
|
vec_list = map->lst_map;
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < map->height)
|
while (i <= map->y_max)
|
||||||
{
|
{
|
||||||
j = 0;
|
j = 0;
|
||||||
while (j < map->width)
|
while (j <= map->x_max)
|
||||||
{
|
{
|
||||||
vec_mat_mult(mat, vec_list->content, &map->arr_pnt[i][j]);
|
vec_mat_mult(mat, vec_list->content, &map->arr_pnt[i][j]);
|
||||||
vec_list = vec_list->next;
|
vec_list = vec_list->next;
|
||||||
|
@ -61,20 +43,20 @@ t_mat4 *viewport_mat_init(int x, int y)
|
||||||
|
|
||||||
if ((m = malloc(sizeof(t_mat4))))
|
if ((m = malloc(sizeof(t_mat4))))
|
||||||
{
|
{
|
||||||
m->mx[0][0] = (double)x / 2;
|
m->mx[0][0] = (double)x / 2.0f;
|
||||||
m->mx[0][1] = 0;
|
m->mx[0][1] = 0;
|
||||||
m->mx[0][2] = 0;
|
m->mx[0][2] = 0;
|
||||||
m->mx[0][3] = 0;
|
m->mx[0][3] = 0;
|
||||||
m->mx[1][0] = 0;
|
m->mx[1][0] = 0;
|
||||||
m->mx[1][1] = (double)y / 2;
|
m->mx[1][1] = (double)y / 2.0f;
|
||||||
m->mx[1][2] = 0;
|
m->mx[1][2] = 0;
|
||||||
m->mx[1][3] = 0;
|
m->mx[1][3] = 0;
|
||||||
m->mx[2][0] = 0;
|
m->mx[2][0] = 0;
|
||||||
m->mx[2][1] = 0;
|
m->mx[2][1] = 0;
|
||||||
m->mx[2][2] = 1;
|
m->mx[2][2] = 1;
|
||||||
m->mx[2][3] = 0;
|
m->mx[2][3] = 0;
|
||||||
m->mx[3][0] = ((double)x - 1) / 2;
|
m->mx[3][0] = ((double)x - 1) / 2.0f;
|
||||||
m->mx[3][1] = ((double)y - 1) / 2;
|
m->mx[3][1] = ((double)y - 1) / 2.0f;
|
||||||
m->mx[3][2] = 0;
|
m->mx[3][2] = 0;
|
||||||
m->mx[3][3] = 1;
|
m->mx[3][3] = 1;
|
||||||
}
|
}
|
||||||
|
@ -87,17 +69,17 @@ t_mat4 *orth_mat_init(int l, int r, int b, int t, int n, int f)
|
||||||
|
|
||||||
if ((orth = malloc(sizeof(t_mat4))))
|
if ((orth = malloc(sizeof(t_mat4))))
|
||||||
{
|
{
|
||||||
orth->mx[0][0] = 2 / ((double)r - (double)l);
|
orth->mx[0][0] = 2.0f / ((double)r - (double)l);
|
||||||
orth->mx[0][1] = 0;
|
orth->mx[0][1] = 0;
|
||||||
orth->mx[0][2] = 0;
|
orth->mx[0][2] = 0;
|
||||||
orth->mx[0][3] = 0;
|
orth->mx[0][3] = 0;
|
||||||
orth->mx[1][0] = 0;
|
orth->mx[1][0] = 0;
|
||||||
orth->mx[1][1] = 2 / ((double)t - (double)b);
|
orth->mx[1][1] = 2.0f / ((double)t - (double)b);
|
||||||
orth->mx[1][2] = 0;
|
orth->mx[1][2] = 0;
|
||||||
orth->mx[1][3] = 0;
|
orth->mx[1][3] = 0;
|
||||||
orth->mx[2][0] = 0;
|
orth->mx[2][0] = 0;
|
||||||
orth->mx[2][1] = 0;
|
orth->mx[2][1] = 0;
|
||||||
orth->mx[2][2] = 2 / ((double)n - (double)f);
|
orth->mx[2][2] = 2.0f / ((double)n - (double)f);
|
||||||
orth->mx[2][3] = 0;
|
orth->mx[2][3] = 0;
|
||||||
orth->mx[3][0] = - (((double)r + (double)l) / ((double)r - (double)l));
|
orth->mx[3][0] = - (((double)r + (double)l) / ((double)r - (double)l));
|
||||||
orth->mx[3][1] = - (((double)t + (double)b) / ((double)t - (double)b));
|
orth->mx[3][1] = - (((double)t + (double)b) / ((double)t - (double)b));
|
||||||
|
@ -107,30 +89,25 @@ t_mat4 *orth_mat_init(int l, int r, int b, int t, int n, int f)
|
||||||
return (orth);
|
return (orth);
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw(t_map *map, t_mlx *m)
|
void draw(t_map *map, t_mw *mw)
|
||||||
{
|
{
|
||||||
int x;
|
|
||||||
int y;
|
|
||||||
int i;
|
int i;
|
||||||
int j;
|
int j;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < map->height)
|
while (i <= map->y_max)
|
||||||
{
|
{
|
||||||
j = 0;
|
j = 0;
|
||||||
while (j < map->width)
|
while (j <= map->x_max)
|
||||||
{
|
{
|
||||||
x = map->arr_pnt[i][j].x;
|
if (i == map->y_max && j < map->x_max)
|
||||||
y = map->arr_pnt[i][j].y;
|
line(map->arr_pnt[i][j], map->arr_pnt[i][j + 1], mw);
|
||||||
mlx_pixel_put(m->mlx, m->win, x, y, 0xFFFFFF);
|
else if (i < map->y_max && j == map->x_max)
|
||||||
if (i == map->height - 1 && j < map->width - 1)
|
line(map->arr_pnt[i][j], map->arr_pnt[i + 1][j], mw);
|
||||||
line(map->arr_pnt[i][j], map->arr_pnt[i][j + 1], m);
|
else if (i < map->y_max && j < map->x_max)
|
||||||
else if (i < map->height - 1 && j == map->width - 1)
|
|
||||||
line(map->arr_pnt[i][j], map->arr_pnt[i + 1][j], m);
|
|
||||||
else if (i < map->height - 1 && j < map->width - 1)
|
|
||||||
{
|
{
|
||||||
line(map->arr_pnt[i][j], map->arr_pnt[i + 1][j], m);
|
line(map->arr_pnt[i][j], map->arr_pnt[i + 1][j], mw);
|
||||||
line(map->arr_pnt[i][j], map->arr_pnt[i][j + 1], m);
|
line(map->arr_pnt[i][j], map->arr_pnt[i][j + 1], mw);
|
||||||
}
|
}
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
|
@ -138,7 +115,24 @@ void draw(t_map *map, t_mlx *m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void render(t_map *map, t_mlx *m, int x, int y)
|
void center(t_map *map)
|
||||||
|
{
|
||||||
|
t_list *l;
|
||||||
|
double d_width;
|
||||||
|
double d_height;
|
||||||
|
|
||||||
|
d_width = map->x_max / 2.0f;
|
||||||
|
d_height = map->y_max / 2.0f;
|
||||||
|
l = map->lst_map;
|
||||||
|
while (l)
|
||||||
|
{
|
||||||
|
((t_vec *)(l->content))->x = ((t_vec *)(l->content))->x - d_width;
|
||||||
|
((t_vec *)(l->content))->y = ((t_vec *)(l->content))->y - d_height;
|
||||||
|
l = l->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void render(t_FDF *FDF)
|
||||||
{
|
{
|
||||||
t_mat4 *vp;
|
t_mat4 *vp;
|
||||||
t_mat4 *orth;
|
t_mat4 *orth;
|
||||||
|
@ -154,19 +148,31 @@ void render(t_map *map, t_mlx *m, int x, int y)
|
||||||
rot_z = mat4_init();
|
rot_z = mat4_init();
|
||||||
translate = mat4_init();
|
translate = mat4_init();
|
||||||
scale = mat4_init();
|
scale = mat4_init();
|
||||||
vp = viewport_mat_init(x, y);
|
vp = viewport_mat_init(FDF->w_width, FDF->w_heigth);
|
||||||
orth = orth_mat_init(0, map->width + 1, 0, map->height + 1, 0, 20);
|
orth = orth_mat_init(
|
||||||
mat4_x_rot(rot_x, 20);
|
0,
|
||||||
|
FDF->map->x_max + 2,
|
||||||
|
0,
|
||||||
|
FDF->map->x_max + 2,
|
||||||
|
0,
|
||||||
|
FDF->map->z_max + 2);
|
||||||
|
mat4_x_rot(rot_x, 45);
|
||||||
mat4_z_rot(rot_z, 45);
|
mat4_z_rot(rot_z, 45);
|
||||||
mat4_y_rot(rot_y, 20);
|
mat4_y_rot(rot_y, 45);
|
||||||
mat4_scale(scale, 1, 1, 1);
|
mat4_scale(scale, 1, 1, 1);
|
||||||
mat4_translate(translate, 0, 0, 0);
|
mat4_translate(
|
||||||
|
translate,
|
||||||
|
(((double)FDF->map->x_max) / 2.0f),
|
||||||
|
(((double)FDF->map->x_max) / 2.0f),
|
||||||
|
(((double)FDF->map->z_max) / 2.0f));
|
||||||
final = mat4_mult(vp, orth);
|
final = mat4_mult(vp, orth);
|
||||||
final = mat4_mult(rot_x, final);
|
final = mat4_mult(rot_x, final);
|
||||||
final = mat4_mult(translate, final);
|
|
||||||
final = mat4_mult(rot_y ,final);
|
final = mat4_mult(rot_y ,final);
|
||||||
final = mat4_mult(scale, final);
|
final = mat4_mult(translate, final);
|
||||||
transform(final, map);
|
// final = mat4_mult(scale, final);
|
||||||
draw(map, m);
|
|
||||||
|
center(FDF->map);
|
||||||
|
transform(final, FDF->map);
|
||||||
|
draw(FDF->map, FDF->mw);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue