diff --git a/CMakeLists.txt b/CMakeLists.txt index 9bc2478..ce809a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ set(SOURCE_FILES src/line.c src/map_init.c src/pnt_init.c - src/free_tab.c) # sources + src/help_func.c src/render.c src/mat4.c) # sources add_executable(fdf ${SOURCE_FILES}) # compilation diff --git a/Makefile b/Makefile index f9102a7..6779c06 100644 --- a/Makefile +++ b/Makefile @@ -18,11 +18,13 @@ SRCDIR = ./src/ OBJDIR = ./obj/ -SRC_FILES = main.c \ - line.c \ - map_init.c \ - pnt_init.c \ - free_tab.c +SRC_FILES = main.c \ + line.c \ + map_init.c \ + pnt_init.c \ + help_func.c \ + mat4.c \ + render.c OBJ_FILES = $(SRC_FILES:.c=.o) diff --git a/inc/fdf.h b/inc/fdf.h index 2b1cfca..31566a3 100644 --- a/inc/fdf.h +++ b/inc/fdf.h @@ -15,54 +15,83 @@ #include #include -typedef struct s_map -{ - t_list *lst_map; - t_list *lst_pnt; - int width; - int height; -} t_map; - typedef struct s_vec { double x; double y; double z; - int color; } t_vec; typedef struct s_pnt { int x; int y; + int color; } t_pnt; -typedef struct s_edg -{ - int p1; - int p2; -} t_edg; - -typedef struct s_mat4 -{ - double mx[4][4]; -} t_mat4; - typedef struct s_mlx { void *mlx; void *win; } t_mlx; +typedef struct s_map +{ + t_list *lst_map; + t_pnt **arr_pnt; + int width; + int height; +} t_map; + +typedef struct s_mat4 +{ + double mx[4][4]; +} t_mat4; + +typedef struct s_trnsf +{ + t_mat4 *tr; + t_mat4 *xr; + t_mat4 *yr; + t_mat4 *zr; + t_mat4 *fin; +} t_trnsf; + typedef struct s_swap { int swap_x; int swap_coord; } t_swap; -void line(t_vec p1, t_vec p2, t_mlx *m); -t_map *map_init(char *path); -t_list *pnt_init(t_list *lst_map); -void free_tab(char **tab); +//delete + +typedef struct s_edg +{ + int p1; + int p2; +} t_edg; + +void line(t_pnt p1, t_pnt p2, t_mlx *m); + +t_map *map_init(char *path); +void pnt_init(t_map *map); + +t_mat4 *mat4_init(void); +t_mat4 *mat4_mult(t_mat4 *m1, t_mat4 *m2); +void mat4_translate(t_mat4 *m, double x, double y, double z); +void mat4_scale(t_mat4 *m, double x, double y, double z); +void mat4_z_rot(t_mat4 *m, double angle); +void mat4_x_rot(t_mat4 *m, double angle); +void mat4_y_rot(t_mat4 *m, double angle); +void vec_mat_mult(t_mat4 *m, t_vec *v, t_pnt *result); + +void map_transform(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 free_tab(char **tab); +void quit(t_mlx *m); #endif diff --git a/src/free_tab.c b/src/help_func.c similarity index 55% rename from src/free_tab.c rename to src/help_func.c index c5e29bc..0f5e445 100644 --- a/src/free_tab.c +++ b/src/help_func.c @@ -5,4 +5,10 @@ void free_tab(char **tab) while (*tab) free(*tab++); free(*tab); +} + +void quit(t_mlx *m) +{ + mlx_destroy_window(m->mlx, m->win); + exit(0); } \ No newline at end of file diff --git a/src/line.c b/src/line.c index a482c58..4b62237 100644 --- a/src/line.c +++ b/src/line.c @@ -1,7 +1,7 @@ #include "fdf.h" -static void swap_x_y(t_vec *p1, t_vec *p2) +static void swap_x_y(t_pnt *p1, t_pnt *p2) { int temp; @@ -13,9 +13,9 @@ static void swap_x_y(t_vec *p1, t_vec *p2) p2->y = temp; } -static void swap_points(t_vec *p1, t_vec *p2) +static void swap_points(t_pnt *p1, t_pnt *p2) { - t_vec temp; + t_pnt temp; temp = *p1; *p1 = *p2; @@ -23,7 +23,7 @@ static void swap_points(t_vec *p1, t_vec *p2) } -static void draw_line(t_vec *p1, t_vec *p2, t_mlx *m, t_swap *s) +static void draw_line(t_pnt *p1, t_pnt *p2, t_mlx *m, t_swap *s) { int dx = abs(p2->x - p1->x); int dy = abs(p2->y - p1->y); @@ -31,9 +31,9 @@ static void draw_line(t_vec *p1, t_vec *p2, t_mlx *m, t_swap *s) while (p1->x < p2->x) { if (s->swap_coord) - mlx_pixel_put(m->mlx, m->win, p1->y, p1->x, p1->color); + mlx_pixel_put(m->mlx, m->win, p1->y, p1->x, 0xFFFFFF); else - mlx_pixel_put(m->mlx, m->win, p1->x, p1->y, p1->color); + mlx_pixel_put(m->mlx, m->win, p1->x, p1->y, 0xFFFFFF); if (derr > 0) { p1->y += s->swap_x; @@ -44,7 +44,7 @@ static void draw_line(t_vec *p1, t_vec *p2, t_mlx *m, t_swap *s) } } -void line(t_vec p1, t_vec p2, t_mlx *m) +void line(t_pnt p1, t_pnt p2, t_mlx *m) { int dx; int dy; diff --git a/src/main.c b/src/main.c index 841611b..c241235 100644 --- a/src/main.c +++ b/src/main.c @@ -10,144 +10,13 @@ #include -t_mat4 *mat4_init(void) -{ - t_mat4 *mat; - mat = NULL; - if ((mat = (t_mat4 *)malloc(sizeof(t_mat4)))) - { - mat->mx[0][0] = 1; - mat->mx[0][1] = 0; - mat->mx[0][2] = 0; - mat->mx[0][3] = 0; - mat->mx[1][0] = 0; - mat->mx[1][1] = 1; - mat->mx[1][2] = 0; - mat->mx[1][3] = 0; - mat->mx[2][0] = 0; - mat->mx[2][1] = 0; - mat->mx[2][2] = 1; - mat->mx[2][3] = 0; - mat->mx[3][0] = 0; - mat->mx[3][1] = 0; - mat->mx[3][2] = 0; - mat->mx[3][3] = 1; - } - return (mat); -} - -t_mat4 *mat4_mult(t_mat4 *m1, t_mat4 *m2) -{ - t_mat4 *m3; - int i; - int j; - - i = 0; - m3 = NULL; - if ((m3 = (t_mat4 *)malloc(sizeof(t_mat4)))) - { - while(i < 4) - { - j = 0; - while (j < 4) - { - m3->mx[i][j] = m1->mx[i][0] * m2->mx[0][j] + - m1->mx[i][1] * m2->mx[1][j] + - m1->mx[i][2] * m2->mx[2][j] + - m1->mx[i][3] * m2->mx[3][j]; - j++; - } - i++; - } - } - return (m3); -} - -void mat4_translate(t_mat4 *m, double x, double y, double z) -{ - m->mx[0][0] = 1.f; - m->mx[1][1] = 1.f; - m->mx[2][2] = 1.f; - m->mx[3][3] = 1.f; - m->mx[3][0] = x; - m->mx[3][1] = y; - m->mx[3][2] = z; -} - -void mat4_z_rot(t_mat4 *m, double angle) -{ - angle = angle * M_PI / 180; - m->mx[0][0] = cos(angle); - m->mx[0][1] = sin(angle); - m->mx[0][2] = 0; - m->mx[1][0] = -(sin(angle)); - m->mx[1][1] = cos(angle); - m->mx[1][2] = 0; - m->mx[2][0] = 0; - m->mx[2][1] = 0; - m->mx[2][2] = 1; -} - -void mat4_x_rot(t_mat4 *m, double angle) -{ - angle = angle * M_PI / 180; - m->mx[0][0] = 1; - m->mx[0][1] = 0; - m->mx[0][2] = 0; - m->mx[1][0] = 0; - m->mx[1][1] = cos(angle); - m->mx[1][2] = sin(angle); - m->mx[2][0] = 0; - m->mx[2][1] = -(sin(angle)); - m->mx[2][2] = cos(angle); -} - -void mat4_y_rot(t_mat4 *m, double angle) -{ - angle = angle * M_PI / 180; - m->mx[0][0] = cos(angle); - m->mx[0][1] = 0; - m->mx[0][2] = -(sin(angle)); - m->mx[1][0] = 0; - m->mx[1][1] = 1; - m->mx[1][2] = 0; - m->mx[2][0] = sin(angle); - m->mx[2][1] = 0; - m->mx[2][2] = cos(angle); -} - -void vec_mat_mult(t_mat4 *m, t_vec *v) -{ - double vec4; - double x; - double y; - double z; - - x = v->x * m->mx[0][0] + v->y * m->mx[1][0] + v->z * m->mx[2][0] + - m->mx[3][0]; - y = v->x * m->mx[0][1] + v->y * m->mx[1][1] + v->z * m->mx[2][1] + - m->mx[3][1]; - z = v->x * m->mx[0][2] + v->y * m->mx[1][2] + v->z * m->mx[2][2] + - m->mx[3][2]; - vec4 = v->x * m->mx[0][3] + v->y * m->mx[1][3] + v->z * m->mx[2][3] + - m->mx[3][3]; - if (vec4 != 1 && vec4 != 0) - { - x = x / vec4; - y = y / vec4; - z = z / vec4; - } - v->x = x; - v->y = y; - v->z = z; -} void test_line(t_mlx mlx) { - t_vec p1; - t_vec p2; - double temp; + t_pnt p1; + t_pnt p2; + int temp; p1.color = 0xC100E5; p2.color = 0xC100E5; @@ -191,13 +60,11 @@ void test_line(t_mlx mlx) void circle(t_mlx m) { - t_vec p1; - t_vec p2; + t_pnt p1; + t_pnt p2; double angle; - p1.color = 0xFFFFFF; - p2.color = 0xFFFFFF; for (int i = 0; i < 360; i += 4) { p1.x = 250; @@ -209,181 +76,6 @@ void circle(t_mlx m) } } -void fill_vec3(double x, double y, double z, t_vec *new) -{ - new->x = x; - new->y = y; - new->z = z; - new->color = 0xFFFFFFFF; -} - -void fill_edg(int p1, int p2, t_edg *edg) -{ - edg->p1 = p1; - edg->p2 = p2; -} - -void perspective_transform(t_list *vec_list, t_mlx *mlx, t_vec **points) -{ - double xmin = -0.5; - double xmax = 0.5; - double ymin = -0.5; - double ymax = 0.5; - double x; - double y; - double z; - t_vec *point; - int i; - - i = 0; - while(vec_list) - { - x = ((t_vec *)vec_list->content)->x; - y = ((t_vec *)vec_list->content)->y; - z = ((t_vec *)vec_list->content)->z; - x = x / z; - y = y / z; - point = (t_vec *)malloc(sizeof(t_vec)); - points[i] = point; - points[i]->x = 500 * (1 - (x - xmin) / (xmax - xmin)); - points[i]->y = 500 * (1 - (y - ymin) / (ymax - ymin)); - points[i]->color = 0xFFFFFF; - mlx_pixel_put(mlx->mlx, mlx->win, (int)points[i]->x, (int)points[i]->y, 0xFFFFFF); - vec_list = vec_list->next; - i++; - } - points[i] = 0; -} - -void edges(t_vec **points, t_list *edg, t_mlx *m) -{ - int p1; - int p2; -// int i; - -// i = 0; -// while (points[i]) { -// printf("point x - %f, y - %f\n", points[i]->x, points[i]->y); -// i++; -// } - while (edg) - { - p1 = ((t_edg *)edg->content)->p1; - p2 = ((t_edg *)edg->content)->p2; - line( - *points[p1], - *points[p2], - m - ); - edg = edg->next; - } -} - -void transform(t_mat4 *mat, t_list *v_list) -{ - int i; - - i = 0; - while(v_list) - { - vec_mat_mult(mat, v_list->content); - i++; - v_list = v_list->next; - } -} - -//void cube(t_mlx *m) -//{ -// t_list *vec_list; -// t_list *edg_list; -// t_vec vec; -// t_vec **points; -// t_edg edg; -// int vec_list_length; -// t_mat4 *temp; -// t_mat4 *temp1; -// t_mat4 *temp2; -// t_mat4 *temp3; -// t_mat4 *final_mat; -// int deg; -// -//// struct timespec tw = {0,125000000}; -//// struct timespec tr; -// -// -// vec_list = NULL; -// edg_list = NULL; -// fill_vec3(-0.5, -0.5, 2.5, &vec, 0); -// ft_lstadd_back(&vec_list, &vec, sizeof(t_vec)); -// fill_vec3(-0.5, 0.5, 2.5, &vec, 1); -// ft_lstadd_back(&vec_list, &vec, sizeof(t_vec)); -// fill_vec3(0.5, 0.5, 2.5, &vec, 2); -// ft_lstadd_back(&vec_list, &vec, sizeof(t_vec)); -// fill_vec3(0.5, -0.5, 2.5, &vec, 3); -// ft_lstadd_back(&vec_list, &vec, sizeof(t_vec)); -// fill_vec3(-0.5, -0.5, 3.5, &vec, 4); -// ft_lstadd_back(&vec_list, &vec, sizeof(t_vec)); -// fill_vec3(-0.5, 0.5, 3.5, &vec, 5); -// ft_lstadd_back(&vec_list, &vec, sizeof(t_vec)); -// fill_vec3(0.5, 0.5, 3.5, &vec, 6); -// ft_lstadd_back(&vec_list, &vec, sizeof(t_vec)); -// fill_vec3(0.5, -0.5, 3.5, &vec, 7); -// ft_lstadd_back(&vec_list, &vec, sizeof(t_vec)); -// -// vec_list_length = ft_lst_len(vec_list); -// points = (t_vec **)malloc(sizeof(t_vec *) * (vec_list_length + 1)); -// fill_edg(0, 1, &edg); -// ft_lstadd_back(&edg_list, &edg, sizeof(int[2])); -// fill_edg(1, 2, &edg); -// ft_lstadd_back(&edg_list, &edg, sizeof(int[2])); -// fill_edg(2, 3, &edg); -// ft_lstadd_back(&edg_list, &edg, sizeof(int[2])); -// fill_edg(3, 0, &edg); -// ft_lstadd_back(&edg_list, &edg, sizeof(int[2])); -// fill_edg(0, 4, &edg); -// ft_lstadd_back(&edg_list, &edg, sizeof(int[2])); -// fill_edg(1, 5, &edg); -// ft_lstadd_back(&edg_list, &edg, sizeof(int[2])); -// fill_edg(2, 6, &edg); -// ft_lstadd_back(&edg_list, &edg, sizeof(int[2])); -// fill_edg(3, 7, &edg); -// ft_lstadd_back(&edg_list, &edg, sizeof(int[2])); -// fill_edg(4, 5, &edg); -// ft_lstadd_back(&edg_list, &edg, sizeof(int[2])); -// fill_edg(5, 6, &edg); -// ft_lstadd_back(&edg_list, &edg, sizeof(int[2])); -// fill_edg(6, 7, &edg); -// ft_lstadd_back(&edg_list, &edg, sizeof(int[2])); -// fill_edg(7, 4, &edg); -// ft_lstadd_back(&edg_list, &edg, sizeof(int[2])); -// -// final_mat = mat4_init(); -// temp = mat4_init(); -// temp1 = mat4_init(); -// temp2 = mat4_init(); -// temp3 = mat4_init(); -// -// deg = 0; -// -// mat4_translate(temp, 0, 0, -3.5); -// final_mat = mat4_mult(final_mat, temp); -// mat4_x_rot(temp1, deg); -// final_mat = mat4_mult(final_mat, temp1); -// mat4_y_rot(temp2, deg); -// final_mat = mat4_mult(final_mat, temp2); -// mat4_translate(temp3, 0, 0, 3.5); -// final_mat = mat4_mult(final_mat, temp3); -// transform(final_mat, vec_list); -// perspective_transform(vec_list, m, points); -// edges(points, edg_list, m); -//} - -void quit(t_mlx *m) -{ - mlx_destroy_window(m->mlx, m->win); - exit(0); -} - int key_hook(int keycode, void *m) { printf("%d\n", keycode); @@ -400,51 +92,18 @@ int key_hook(int keycode, void *m) int main(int argc, char **argv) { -// t_mlx m; + t_mlx m; t_map *map; - int i = 0; if (argc != 2) return (0); + m.mlx = mlx_init(); + m.win = mlx_new_window(m.mlx, 500, 500, "fdf"); map = map_init(argv[1]); -// while (map->lst_map) -// { -// printf("x=%d, y=%d, z=%d", -// (int)(((t_vec *)(map->lst_map->content))->x), -// (int)(((t_vec *)(map->lst_map->content))->y), -// (int)(((t_vec *)(map->lst_map->content))->z)); -// if ((int)(((t_vec *)(map->lst_map->content))->z) == 0) -// printf(" "); -// else -// printf(" "); -// i++; -// if (i == map->width) -// { -// printf("\n"); -// i = 0; -// } -// map->lst_map = map->lst_map->next; -// } - map->lst_pnt = pnt_init(map->lst_map); - while (map->lst_map) - { - i++; - map->lst_map = map->lst_map->next; - } - printf("%d\n", i); - i = 0; - while (map->lst_pnt) - { - i++; - map->lst_pnt = map->lst_pnt->next; - } - printf("%d\n", i); -// m.mlx = mlx_init(); -// m.win = mlx_new_window(m.mlx, 1000, 1000, "fdf"); -// test_line(m); -// circle(m); -// cube(&m); -// mlx_key_hook(m.win, key_hook, &m); -// mlx_loop(m.mlx); + pnt_init(map); + //circle(m); + render(map, &m, 500, 500); + mlx_key_hook(m.win, key_hook, &m); + mlx_loop(m.mlx); return (0); } diff --git a/src/mat4.c b/src/mat4.c new file mode 100644 index 0000000..3012fcb --- /dev/null +++ b/src/mat4.c @@ -0,0 +1,184 @@ +#include "fdf.h" + +t_mat4 *mat4_init(void) +{ + t_mat4 *mat; + + mat = NULL; + if ((mat = (t_mat4 *)malloc(sizeof(t_mat4)))) + { + mat->mx[0][0] = 1; + mat->mx[0][1] = 0; + mat->mx[0][2] = 0; + mat->mx[0][3] = 0; + mat->mx[1][0] = 0; + mat->mx[1][1] = 1; + mat->mx[1][2] = 0; + mat->mx[1][3] = 0; + mat->mx[2][0] = 0; + mat->mx[2][1] = 0; + mat->mx[2][2] = 1; + mat->mx[2][3] = 0; + mat->mx[3][0] = 0; + mat->mx[3][1] = 0; + mat->mx[3][2] = 0; + mat->mx[3][3] = 1; + } + return (mat); +} + +t_mat4 *mat4_mult(t_mat4 *m1, t_mat4 *m2) +{ + t_mat4 *m3; + int i; + int j; + + i = 0; + m3 = NULL; + if ((m3 = (t_mat4 *)malloc(sizeof(t_mat4)))) + { + while(i < 4) + { + j = 0; + while (j < 4) + { + m3->mx[i][j] = m1->mx[i][0] * m2->mx[0][j] + + m1->mx[i][1] * m2->mx[1][j] + + m1->mx[i][2] * m2->mx[2][j] + + m1->mx[i][3] * m2->mx[3][j]; + j++; + } + i++; + } + } + return (m3); +} + +void mat4_translate(t_mat4 *m, double x, double y, double z) +{ + m->mx[0][0] = 1; + m->mx[0][1] = 0; + m->mx[0][2] = 0; + m->mx[0][3] = 0; + m->mx[1][0] = 0; + m->mx[1][1] = 1; + m->mx[1][2] = 0; + m->mx[1][3] = 0; + m->mx[2][0] = 0; + m->mx[2][1] = 0; + m->mx[2][2] = 1; + m->mx[2][3] = 0; + m->mx[3][0] = x; + m->mx[3][1] = y; + m->mx[3][2] = z; + m->mx[3][3] = 1; +} + +void mat4_scale(t_mat4 *m, double x, double y, double z) +{ + m->mx[0][0] = x; + m->mx[0][1] = 0; + m->mx[0][2] = 0; + m->mx[0][3] = 0; + m->mx[1][0] = 0; + m->mx[1][1] = y; + m->mx[1][2] = 0; + m->mx[1][3] = 0; + m->mx[2][0] = 0; + m->mx[2][1] = 0; + m->mx[2][2] = z; + m->mx[2][3] = 0; + m->mx[3][0] = 0; + m->mx[3][1] = 0; + m->mx[3][2] = 0; + m->mx[3][3] = 1; +} + +void mat4_z_rot(t_mat4 *m, double angle) +{ + angle = angle * M_PI / 180; + m->mx[0][0] = cos(angle); + m->mx[0][1] = -(sin(angle)); + m->mx[0][2] = 0; + m->mx[0][3] = 0; + m->mx[1][0] = sin(angle); + m->mx[1][1] = cos(angle); + m->mx[1][2] = 0; + m->mx[1][3] = 0; + m->mx[2][0] = 0; + m->mx[2][1] = 0; + m->mx[2][2] = 1; + m->mx[2][3] = 0; + m->mx[3][0] = 0; + m->mx[3][1] = 0; + m->mx[3][2] = 0; + m->mx[3][3] = 1; +} + +void mat4_x_rot(t_mat4 *m, double angle) +{ + angle = angle * M_PI / 180; + m->mx[0][0] = 1; + m->mx[0][1] = 0; + m->mx[0][2] = 0; + m->mx[0][3] = 0; + m->mx[1][0] = 0; + m->mx[1][1] = cos(angle); + m->mx[1][2] = (sin(angle)); + m->mx[1][3] = 0; + m->mx[2][0] = 0; + m->mx[2][1] = -sin(angle); + m->mx[2][2] = cos(angle); + m->mx[2][3] = 0; + m->mx[3][0] = 0; + m->mx[3][1] = 0; + m->mx[3][2] = 0; + m->mx[3][3] = 1; +} + +void mat4_y_rot(t_mat4 *m, double angle) +{ + angle = angle * M_PI / 180; + m->mx[0][0] = cos(angle); + m->mx[0][1] = 0; + m->mx[0][2] = -(sin(angle)); + m->mx[0][3] = 0; + m->mx[1][0] = 0; + m->mx[1][1] = 1; + m->mx[1][2] = 0; + m->mx[1][3] = 0; + m->mx[2][0] = sin(angle); + m->mx[2][1] = 0; + m->mx[2][2] = cos(angle); + m->mx[2][3] = 0; + m->mx[3][0] = 0; + m->mx[3][1] = 0; + m->mx[3][2] = 0; + m->mx[3][3] = 1; +} + +void vec_mat_mult(t_mat4 *m, t_vec *v, t_pnt *result) +{ +// double vec4; + double x; + double y; +// double z; + + x = v->x * m->mx[0][0] + v->y * m->mx[1][0] + v->z * m->mx[2][0] + + m->mx[3][0]; + y = v->x * m->mx[0][1] + v->y * m->mx[1][1] + v->z * m->mx[2][1] + + m->mx[3][1]; +// z = v->x * m->mx[0][2] + v->y * m->mx[1][2] + v->z * m->mx[2][2] + +// m->mx[3][2]; +// vec4 = v->x * m->mx[0][3] + v->y * m->mx[1][3] + v->z * m->mx[2][3] + +// m->mx[3][3]; +// if (vec4 != 1 && vec4 != 0) +// { +// x = x / vec4; +// y = y / vec4; +// z = z / vec4; +// } + result->x = x; + result->y = y; +// result->z = z; +} \ No newline at end of file diff --git a/src/pnt_init.c b/src/pnt_init.c index b410720..91d316b 100644 --- a/src/pnt_init.c +++ b/src/pnt_init.c @@ -1,17 +1,16 @@ #include "fdf.h" -t_list *pnt_init(t_list *lst_map) +void pnt_init(t_map *map) { - t_list *lst_pnt; - t_pnt *pnt; + t_pnt **arr_pnt; + int i; - lst_pnt = NULL; - - pnt = malloc(sizeof(t_pnt)); - while (lst_map) + arr_pnt = malloc(sizeof(t_pnt *) * map->height); + i = 0; + while (i < map->height) { - ft_lstadd_back(&lst_pnt, pnt, sizeof(t_pnt)); - lst_map = lst_map->next; + arr_pnt[i] = malloc(sizeof(t_pnt) * (map->width)); + i++; } - return (lst_pnt); + map->arr_pnt = arr_pnt; } diff --git a/src/render.c b/src/render.c new file mode 100644 index 0000000..42575fd --- /dev/null +++ b/src/render.c @@ -0,0 +1,172 @@ +#include "fdf.h" + +t_trnsf *transfor_mat_init() +{ + t_trnsf *trnsf; + + trnsf = NULL; + if ((trnsf = malloc(sizeof(t_trnsf)))) + { + trnsf->tr = mat4_init(); + trnsf->xr = mat4_init(); + trnsf->yr = mat4_init(); + trnsf->zr = mat4_init(); + trnsf->fin = mat4_init(); + } + 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) +{ + t_list *vec_list; + int i; + int j; + + vec_list = map->lst_map; + i = 0; + while (i < map->height) + { + j = 0; + while (j < map->width) + { + vec_mat_mult(mat, vec_list->content, &map->arr_pnt[i][j]); + vec_list = vec_list->next; + j++; + } + i++; + } +} + +t_mat4 *viewport_mat_init(int x, int y) +{ + t_mat4 *m; + + if ((m = malloc(sizeof(t_mat4)))) + { + m->mx[0][0] = (double)x / 2; + m->mx[0][1] = 0; + m->mx[0][2] = 0; + m->mx[0][3] = 0; + m->mx[1][0] = 0; + m->mx[1][1] = (double)y / 2; + m->mx[1][2] = 0; + m->mx[1][3] = 0; + m->mx[2][0] = 0; + m->mx[2][1] = 0; + m->mx[2][2] = 1; + m->mx[2][3] = 0; + m->mx[3][0] = ((double)x - 1) / 2; + m->mx[3][1] = ((double)y - 1) / 2; + m->mx[3][2] = 0; + m->mx[3][3] = 1; + } + return (m); +} + +t_mat4 *orth_mat_init(int l, int r, int b, int t, int n, int f) +{ + t_mat4 *orth; + + if ((orth = malloc(sizeof(t_mat4)))) + { + orth->mx[0][0] = 2 / ((double)r - (double)l); + orth->mx[0][1] = 0; + orth->mx[0][2] = 0; + orth->mx[0][3] = 0; + orth->mx[1][0] = 0; + orth->mx[1][1] = 2 / ((double)t - (double)b); + orth->mx[1][2] = 0; + orth->mx[1][3] = 0; + orth->mx[2][0] = 0; + orth->mx[2][1] = 0; + orth->mx[2][2] = 2 / ((double)n - (double)f); + orth->mx[2][3] = 0; + 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][2] = - (((double)n + (double)f) / ((double)n - (double)f)); + orth->mx[3][3] = 1; + } + return (orth); +} + +void draw(t_map *map, t_mlx *m) +{ + int x; + int y; + int i; + int j; + + i = 0; + while (i < map->height) + { + j = 0; + while (j < map->width) + { + x = map->arr_pnt[i][j].x; + y = map->arr_pnt[i][j].y; + mlx_pixel_put(m->mlx, m->win, x, y, 0xFFFFFF); + if (i == map->height - 1 && j < map->width - 1) + line(map->arr_pnt[i][j], map->arr_pnt[i][j + 1], m); + 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][j + 1], m); + } + j++; + } + i++; + } +} + +void render(t_map *map, t_mlx *m, int x, int y) +{ + t_mat4 *vp; + t_mat4 *orth; + t_mat4 *final; + t_mat4 *rot_z; + t_mat4 *rot_y; + t_mat4 *rot_x; + t_mat4 *translate; + t_mat4 *scale; + + rot_x = mat4_init(); + rot_y = mat4_init(); + rot_z = mat4_init(); + translate = mat4_init(); + scale = mat4_init(); + vp = viewport_mat_init(x, y); + orth = orth_mat_init(0, map->width + 1, 0, map->height + 1, 0, 20); + mat4_x_rot(rot_x, 20); + mat4_z_rot(rot_z, 45); + mat4_y_rot(rot_y, 20); + mat4_scale(scale, 1, 1, 1); + mat4_translate(translate, 0, 0, 0); + final = mat4_mult(vp, orth); + final = mat4_mult(rot_x, final); + final = mat4_mult(translate, final); + final = mat4_mult(rot_y ,final); + final = mat4_mult(scale, final); + transform(final, map); + draw(map, m); +} + diff --git a/tests/simple.fdf b/tests/simple.fdf new file mode 100644 index 0000000..5a00d4f --- /dev/null +++ b/tests/simple.fdf @@ -0,0 +1,3 @@ +0 0 +1 1 +0 0