reading map

This commit is contained in:
Gregory 2017-03-03 21:51:09 +02:00
parent 226c42e66a
commit 92736963a6
8 changed files with 319 additions and 102 deletions

View file

@ -7,7 +7,11 @@ include_directories(inc libft/includes minilibx) # headers
link_directories(libft minilibx) # libraries link_directories(libft minilibx) # libraries
set(SOURCE_FILES set(SOURCE_FILES
src/main.c src/line.c libft/ft_lst_len.c) # sources src/main.c
src/line.c
src/map_init.c
src/pnt_init.c
src/free_tab.c) # sources
add_executable(fdf ${SOURCE_FILES}) # compilation add_executable(fdf ${SOURCE_FILES}) # compilation

View file

@ -18,8 +18,11 @@ SRCDIR = ./src/
OBJDIR = ./obj/ OBJDIR = ./obj/
SRC_FILES = main.c \ SRC_FILES = main.c \
line.c line.c \
map_init.c \
pnt_init.c \
free_tab.c
OBJ_FILES = $(SRC_FILES:.c=.o) OBJ_FILES = $(SRC_FILES:.c=.o)

View file

@ -13,16 +13,30 @@
#include "mlx.h" #include "mlx.h"
#include <math.h> #include <math.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h>
typedef struct s_map
{
t_list *lst_map;
t_list *lst_pnt;
int width;
int height;
} t_map;
typedef struct s_vec typedef struct s_vec
{ {
double x; double x;
double y; double y;
double z; double z;
int pos;
int color; int color;
} t_vec; } t_vec;
typedef struct s_pnt
{
int x;
int y;
} t_pnt;
typedef struct s_edg typedef struct s_edg
{ {
int p1; int p1;
@ -47,5 +61,8 @@ typedef struct s_swap
} t_swap; } t_swap;
void line(t_vec p1, t_vec p2, t_mlx *m); 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);
#endif #endif

View file

@ -12,7 +12,7 @@
#include "libft.h" #include "libft.h"
char *read_file(const int fd) char *ft_read_file(const int fd)
{ {
long readed; long readed;
long old_size; long old_size;
@ -24,7 +24,7 @@ char *read_file(const int fd)
{ {
buf[readed] = '\0'; buf[readed] = '\0';
old_size = ft_strlen(string); old_size = ft_strlen(string);
string = ft_realloc(string, old_size + readed, old_size); string = ft_realloc(string, old_size + readed + 1, old_size);
ft_strcat(string, buf); ft_strcat(string, buf);
} }
return (string); return (string);

8
src/free_tab.c Normal file
View file

@ -0,0 +1,8 @@
#include "fdf.h"
void free_tab(char **tab)
{
while (*tab)
free(*tab++);
free(*tab);
}

View file

@ -7,6 +7,7 @@
#include "fdf.h" #include "fdf.h"
#include <stdio.h> #include <stdio.h>
#include <time.h>
t_mat4 *mat4_init(void) t_mat4 *mat4_init(void)
@ -16,10 +17,22 @@ t_mat4 *mat4_init(void)
mat = NULL; mat = NULL;
if ((mat = (t_mat4 *)malloc(sizeof(t_mat4)))) if ((mat = (t_mat4 *)malloc(sizeof(t_mat4))))
{ {
mat->mx[0][0] = 1.f; mat->mx[0][0] = 1;
mat->mx[1][1] = 1.f; mat->mx[0][1] = 0;
mat->mx[2][2] = 1.f; mat->mx[0][2] = 0;
mat->mx[3][3] = 1.f; 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); return (mat);
} }
@ -51,9 +64,20 @@ t_mat4 *mat4_mult(t_mat4 *m1, t_mat4 *m2)
return (m3); 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) void mat4_z_rot(t_mat4 *m, double angle)
{ {
angle = angle * M_1_PI / 180; angle = angle * M_PI / 180;
m->mx[0][0] = cos(angle); m->mx[0][0] = cos(angle);
m->mx[0][1] = sin(angle); m->mx[0][1] = sin(angle);
m->mx[0][2] = 0; m->mx[0][2] = 0;
@ -67,7 +91,7 @@ void mat4_z_rot(t_mat4 *m, double angle)
void mat4_x_rot(t_mat4 *m, double angle) void mat4_x_rot(t_mat4 *m, double angle)
{ {
angle = angle * M_1_PI / 180; angle = angle * M_PI / 180;
m->mx[0][0] = 1; m->mx[0][0] = 1;
m->mx[0][1] = 0; m->mx[0][1] = 0;
m->mx[0][2] = 0; m->mx[0][2] = 0;
@ -81,7 +105,7 @@ void mat4_x_rot(t_mat4 *m, double angle)
void mat4_y_rot(t_mat4 *m, double angle) void mat4_y_rot(t_mat4 *m, double angle)
{ {
angle = angle * M_1_PI / 180; angle = angle * M_PI / 180;
m->mx[0][0] = cos(angle); m->mx[0][0] = cos(angle);
m->mx[0][1] = 0; m->mx[0][1] = 0;
m->mx[0][2] = -(sin(angle)); m->mx[0][2] = -(sin(angle));
@ -185,12 +209,11 @@ void circle(t_mlx m)
} }
} }
void fill_vec3(double x, double y, double z, t_vec *new, int pos) void fill_vec3(double x, double y, double z, t_vec *new)
{ {
new->x = x; new->x = x;
new->y = y; new->y = y;
new->z = z; new->z = z;
new->pos = pos;
new->color = 0xFFFFFFFF; new->color = 0xFFFFFFFF;
} }
@ -224,24 +247,25 @@ void perspective_transform(t_list *vec_list, t_mlx *mlx, t_vec **points)
points[i] = point; points[i] = point;
points[i]->x = 500 * (1 - (x - xmin) / (xmax - xmin)); points[i]->x = 500 * (1 - (x - xmin) / (xmax - xmin));
points[i]->y = 500 * (1 - (y - ymin) / (ymax - ymin)); points[i]->y = 500 * (1 - (y - ymin) / (ymax - ymin));
points[i]->color = 0xFFFFFFFF; points[i]->color = 0xFFFFFF;
mlx_pixel_put(mlx->mlx, mlx->win, (int)points[i]->x, (int)points[i]->y, 0xFFFFFFFF); mlx_pixel_put(mlx->mlx, mlx->win, (int)points[i]->x, (int)points[i]->y, 0xFFFFFF);
vec_list = vec_list->next; vec_list = vec_list->next;
i++; i++;
} }
points[i] = 0;
} }
void edges(t_vec **points, t_list *edg, t_mlx *m) void edges(t_vec **points, t_list *edg, t_mlx *m)
{ {
int p1; int p1;
int p2; int p2;
int i; // int i;
i = 0; // i = 0;
while (points[i]) { // while (points[i]) {
printf("point x - %f, y - %f\n", points[i]->x, points[i]->y); // printf("point x - %f, y - %f\n", points[i]->x, points[i]->y);
i++; // i++;
} // }
while (edg) while (edg)
{ {
p1 = ((t_edg *)edg->content)->p1; p1 = ((t_edg *)edg->content)->p1;
@ -268,89 +292,159 @@ void transform(t_mat4 *mat, t_list *v_list)
} }
} }
//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);
if (keycode == 65307)
quit(m);
// if (keycode == 123)
// left(mlx);
// if (keycode == 126)
// up(mlx);
// if (keycode == 125)
// down(mlx);
return (0);
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
t_mlx m; // t_mlx m;
t_list *vec_list; t_map *map;
t_list *edg_list; int i = 0;
t_vec vec;
t_vec **points;
t_edg edg;
int vec_list_length;
t_mat4 *mat;
// int fd; if (argc != 2)
// char *line; return (0);
// map = map_init(argv[1]);
// if (argc != 2) // while (map->lst_map)
// return (0);
// fd = open(argv[1], O_RDONLY);
// // add new get_next_line
// while (get_next_line(fd, &line))
// { // {
// ft_putstr(line); // printf("x=%d, y=%d, z=%d",
// ft_putstr("\n"); // (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);
m.mlx = mlx_init(); while (map->lst_map)
m.win = mlx_new_window(m.mlx, 500, 500, "fdf"); {
//test_line(m); i++;
//circle(m); map->lst_map = map->lst_map->next;
}
argc = argc; printf("%d\n", i);
argv = argv; i = 0;
vec_list = NULL; while (map->lst_pnt)
edg_list = NULL; {
fill_vec3(-0.5, -0.5, 2.5, &vec, 0); i++;
ft_lstadd_back(&vec_list, &vec, sizeof(t_vec)); map->lst_pnt = map->lst_pnt->next;
fill_vec3(-0.5, 0.5, 2.5, &vec, 1); }
ft_lstadd_back(&vec_list, &vec, sizeof(t_vec)); printf("%d\n", i);
fill_vec3(0.5, 0.5, 2.5, &vec, 2); // m.mlx = mlx_init();
ft_lstadd_back(&vec_list, &vec, sizeof(t_vec)); // m.win = mlx_new_window(m.mlx, 1000, 1000, "fdf");
fill_vec3(0.5, -0.5, 2.5, &vec, 3); // test_line(m);
ft_lstadd_back(&vec_list, &vec, sizeof(t_vec)); // circle(m);
fill_vec3(-0.5, -0.5, 3.5, &vec, 4); // cube(&m);
ft_lstadd_back(&vec_list, &vec, sizeof(t_vec)); // mlx_key_hook(m.win, key_hook, &m);
fill_vec3(-0.5, 0.5, 3.5, &vec, 5); // mlx_loop(m.mlx);
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]));
mat = mat4_init();
mat4_z_rot(mat, 360);
transform(mat, vec_list);
perspective_transform(vec_list, &m, points);
edges(points, edg_list, &m);
mlx_loop(m.mlx);
return (0); return (0);
} }

74
src/map_init.c Normal file
View file

@ -0,0 +1,74 @@
#include "fdf.h"
void fill_row(t_list **list, int i, char *row, t_map *map)
{
int j;
char **row_numbers;
t_vec vec3;
j = 0;
vec3.y = i;
row_numbers = ft_strsplit(row, ' ');
while (row_numbers[j])
{
vec3.x = j;
vec3.z = ft_atoi(row_numbers[j]);
ft_lstadd_back(list, &vec3, sizeof(t_vec));
j++;
}
free(row_numbers);
map->width = j;
}
t_list *to_lst_map(char **tab_map, t_map *map)
{
int i;
t_list *lst_map;
lst_map = NULL;
i = 0;
while (tab_map[i])
{
fill_row(&lst_map, i, tab_map[i], map);
i++;
}
return (lst_map);
}
char *to_str_map(char *path)
{
int fd;
fd = open(path, O_RDONLY);
if (fd != -1)
return (ft_read_file(fd));
return (0);
}
int map_heigth(char *str_map)
{
int h;
h = 0;
while (*str_map++)
if (*str_map == '\n')
h++;
return (h);
}
t_map *map_init(char *path)
{
char *str_map;
char **tab_map;
t_map *map;
map = malloc(sizeof(t_map));
str_map = to_str_map(path);
printf("%s\n", str_map);
map->height = map_heigth(str_map);
tab_map = ft_strsplit(str_map, '\n');
map->lst_map = to_lst_map(tab_map, map);
free(str_map);
free_tab(tab_map);
return (map);
}

17
src/pnt_init.c Normal file
View file

@ -0,0 +1,17 @@
#include "fdf.h"
t_list *pnt_init(t_list *lst_map)
{
t_list *lst_pnt;
t_pnt *pnt;
lst_pnt = NULL;
pnt = malloc(sizeof(t_pnt));
while (lst_map)
{
ft_lstadd_back(&lst_pnt, pnt, sizeof(t_pnt));
lst_map = lst_map->next;
}
return (lst_pnt);
}