new stuff

This commit is contained in:
Gregory 2017-02-28 07:42:53 +02:00
parent a84634e6de
commit 226c42e66a
7 changed files with 235 additions and 127 deletions

View file

@ -1,13 +1,13 @@
cmake_minimum_required(VERSION 3.6) cmake_minimum_required(VERSION 3.6)
project(fdf) project(fdf)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -Wextra -O3") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -Wextra ") #-O3
include_directories(inc libft/includes minilibx) # headers 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) # sources src/main.c src/line.c libft/ft_lst_len.c) # sources
add_executable(fdf ${SOURCE_FILES}) # compilation add_executable(fdf ${SOURCE_FILES}) # compilation

View file

@ -16,15 +16,22 @@
typedef struct s_vec typedef struct s_vec
{ {
float x; double x;
float y; double y;
float z; double z;
int pos;
int color; int color;
} t_vec; } t_vec;
typedef struct s_edg
{
int p1;
int p2;
} t_edg;
typedef struct s_mat4 typedef struct s_mat4
{ {
float mx[4][4]; double mx[4][4];
} t_mat4; } t_mat4;
typedef struct s_mlx typedef struct s_mlx
@ -39,6 +46,6 @@ typedef struct s_swap
int swap_coord; int swap_coord;
} 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);
#endif #endif

View file

@ -76,6 +76,7 @@ SRC = ft_memset.c \
ft_lstfind.c \ ft_lstfind.c \
ft_lst_at.c \ ft_lst_at.c \
ft_lstadd_back.c \ ft_lstadd_back.c \
ft_lst_len.c \
\ \
ft_realloc.c \ ft_realloc.c \
ft_read_file.c \ ft_read_file.c \

26
libft/ft_lst_len.c Normal file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lst_len.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/04 19:13:25 by gtertysh #+# #+# */
/* Updated: 2016/12/06 19:47:50 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_lst_len(t_list *lst)
{
int len;
len = 0;
while (lst)
{
lst = lst->next;
len++;
}
return (len);
}

View file

@ -99,6 +99,7 @@ void ft_lstadd(t_list **alst, t_list *new);
void ft_lstiter(t_list *lst, void (*f)(t_list *elem)); void ft_lstiter(t_list *lst, void (*f)(t_list *elem));
t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)); t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem));
int ft_lst_len(t_list *lst);
t_list *ft_lststrsplit(const char *s, char c); t_list *ft_lststrsplit(const char *s, char c);
t_list *ft_lstfind(t_list *lst, void *data, size_t size); t_list *ft_lstfind(t_list *lst, void *data, size_t size);
t_list *ft_lst_at(t_list *l, unsigned int at); t_list *ft_lst_at(t_list *l, unsigned int at);

View file

@ -44,25 +44,25 @@ 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_vec p1, t_vec p2, t_mlx *m)
{ {
int dx; int dx;
int dy; int dy;
t_swap swap; t_swap swap;
dx = abs(p2->x - p1->x); dx = abs(p2.x - p1.x);
dy = abs(p2->y - p1->y); dy = abs(p2.y - p1.y);
swap.swap_coord = 0; swap.swap_coord = 0;
swap.swap_x = 1; swap.swap_x = 1;
if (dy > dx) if (dy > dx)
{ {
swap_x_y(p1, p2); swap_x_y(&p1, &p2);
swap.swap_coord= 1; swap.swap_coord= 1;
} }
if (p1->x > p2->x) if (p1.x > p2.x)
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, m, &swap);
} }

View file

@ -6,86 +6,8 @@
// MODIFIED: 2017-02-13 20:30:48 // MODIFIED: 2017-02-13 20:30:48
#include "fdf.h" #include "fdf.h"
#include <stdio.h>
float length(t_vec v)
{
return (sqrt(v.x * v.x + v.y * v.y + v.z * v.z));
}
void normalize(t_vec *vec)
{
float len;
len = length(*vec);
if (len > 0)
{
len = 1 / len;
vec->x *= len;
vec->y *= len;
vec->z *= len;
}
}
float dot(t_vec v1, t_vec v2)
{
return (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z);
}
t_vec *cross(t_vec *v1, t_vec *v2)
{
t_vec *v3;
v3 = NULL;
if ((v3 = (t_vec *)malloc(sizeof(t_vec))))
{
v3->x = v1->y * v2->z - v1->z * v2->y;
v3->y = v1->z * v2->x - v1->x * v2->z;
v3->z = v1->x * v2->y - v1->y * v2->z;
}
return (v3);
}
t_vec *vec_plus(t_vec *v1, t_vec *v2)
{
t_vec *v3;
v3 = NULL;
if ((v3 = (t_vec *)malloc(sizeof(t_vec))))
{
v3->x = v1->x + v2->x;
v3->y = v1->y + v2->y;
v3->z = v1->z + v2->z;
}
return (v3);
}
t_vec *vec_minux(t_vec *v1, t_vec *v2)
{
t_vec *v3;
v3 = NULL;
if ((v3 = (t_vec *)malloc(sizeof(t_vec))))
{
v3->x = v1->x - v2->x;
v3->y = v1->y - v2->y;
v3->z = v1->z - v2->z;
}
return (v3);
}
t_vec *scalar_mult(t_vec *v1, float i)
{
t_vec *v3;
v3 = NULL;
if ((v3 = (t_vec *)malloc(sizeof(t_vec))))
{
v3->x = v1->x * i;
v3->y = v1->y * i;
v3->z = v1->z * i;
}
return (v3);
}
t_mat4 *mat4_init(void) t_mat4 *mat4_init(void)
{ {
@ -129,7 +51,7 @@ t_mat4 *mat4_mult(t_mat4 *m1, t_mat4 *m2)
return (m3); return (m3);
} }
void mat4_z_rot(t_mat4 *m, float angle) void mat4_z_rot(t_mat4 *m, double angle)
{ {
angle = angle * M_1_PI / 180; angle = angle * M_1_PI / 180;
m->mx[0][0] = cos(angle); m->mx[0][0] = cos(angle);
@ -143,7 +65,7 @@ void mat4_z_rot(t_mat4 *m, float angle)
m->mx[2][2] = 1; m->mx[2][2] = 1;
} }
void mat4_x_rot(t_mat4 *m, float angle) void mat4_x_rot(t_mat4 *m, double angle)
{ {
angle = angle * M_1_PI / 180; angle = angle * M_1_PI / 180;
m->mx[0][0] = 1; m->mx[0][0] = 1;
@ -157,7 +79,7 @@ void mat4_x_rot(t_mat4 *m, float angle)
m->mx[2][2] = cos(angle); m->mx[2][2] = cos(angle);
} }
void mat4_y_rot(t_mat4 *m, float angle) void mat4_y_rot(t_mat4 *m, double angle)
{ {
angle = angle * M_1_PI / 180; angle = angle * M_1_PI / 180;
m->mx[0][0] = cos(angle); m->mx[0][0] = cos(angle);
@ -171,31 +93,37 @@ void mat4_y_rot(t_mat4 *m, float angle)
m->mx[2][2] = cos(angle); m->mx[2][2] = cos(angle);
} }
void vec_mat_mult(t_mat4 *m, t_vec *v, t_vec *v2) void vec_mat_mult(t_mat4 *m, t_vec *v)
{ {
float vec4; double vec4;
double x;
double y;
double z;
v2->x = v->x * m->mx[0][0] + v->y * m->mx[1][0] + v->z * m->mx[2][0] + x = v->x * m->mx[0][0] + v->y * m->mx[1][0] + v->z * m->mx[2][0] +
m->mx[3][0]; m->mx[3][0];
v2->y = v->x * m->mx[0][1] + v->y * m->mx[1][1] + v->z * m->mx[2][1] + y = v->x * m->mx[0][1] + v->y * m->mx[1][1] + v->z * m->mx[2][1] +
m->mx[3][1]; m->mx[3][1];
v2->z = v->x * m->mx[0][2] + v->y * m->mx[1][2] + v->z * m->mx[2][2] + z = v->x * m->mx[0][2] + v->y * m->mx[1][2] + v->z * m->mx[2][2] +
m->mx[3][2]; m->mx[3][2];
vec4 = v->x * m->mx[0][3] + v->y * m->mx[1][3] + v->z * m->mx[2][3] + vec4 = v->x * m->mx[0][3] + v->y * m->mx[1][3] + v->z * m->mx[2][3] +
m->mx[3][3]; m->mx[3][3];
if (vec4 != 1 && vec4 != 0) if (vec4 != 1 && vec4 != 0)
{ {
v2->x = v2->x / vec4; x = x / vec4;
v2->y = v2->y / vec4; y = y / vec4;
v2->z = v2->z / vec4; z = z / vec4;
} }
v->x = x;
v->y = y;
v->z = z;
} }
void test_line(t_mlx mlx) void test_line(t_mlx mlx)
{ {
t_vec p1; t_vec p1;
t_vec p2; t_vec p2;
int temp; double temp;
p1.color = 0xC100E5; p1.color = 0xC100E5;
p2.color = 0xC100E5; p2.color = 0xC100E5;
@ -204,7 +132,7 @@ void test_line(t_mlx mlx)
p1.x = 0; p1.x = 0;
p1.y = 0; p1.y = 0;
p2.x = 500; p2.x = 500;
line(&p1, &p2, &mlx); line(p1, p2, &mlx);
} }
for (p2.x = 500; p2.x >= 0; p2.x -= 4) for (p2.x = 500; p2.x >= 0; p2.x -= 4)
{ {
@ -212,7 +140,7 @@ void test_line(t_mlx mlx)
p1.y = 0; p1.y = 0;
p2.y = 500; p2.y = 500;
temp = p2.x; temp = p2.x;
line(&p1, &p2, &mlx); line(p1, p2, &mlx);
p2.x = temp; p2.x = temp;
} }
p1.color = 0x1EBF00; p1.color = 0x1EBF00;
@ -223,7 +151,7 @@ void test_line(t_mlx mlx)
p1.y = 0; p1.y = 0;
p2.x = 0; p2.x = 0;
temp = p2.y; temp = p2.y;
line(&p1, &p2, &mlx); line(p1, p2, &mlx);
p2.y = temp; p2.y = temp;
} }
for (p2.x = 0; p2.x <= 500; p2.x += 4) for (p2.x = 0; p2.x <= 500; p2.x += 4)
@ -232,7 +160,7 @@ void test_line(t_mlx mlx)
p1.y = 0; p1.y = 0;
p2.y = 500; p2.y = 500;
temp = p2.x; temp = p2.x;
line(&p1, &p2, &mlx); line(p1, p2, &mlx);
p2.x = temp; p2.x = temp;
} }
} }
@ -253,31 +181,176 @@ void circle(t_mlx m)
angle = i * M_PI / 180; angle = i * M_PI / 180;
p2.x = p1.x + 200 * sin(angle); p2.x = p1.x + 200 * sin(angle);
p2.y = p1.y + 200 * cos(angle); p2.y = p1.y + 200 * cos(angle);
line(&p1, &p2, &m); line(p1, p2, &m);
}
}
void fill_vec3(double x, double y, double z, t_vec *new, int pos)
{
new->x = x;
new->y = y;
new->z = z;
new->pos = pos;
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 = 0xFFFFFFFF;
mlx_pixel_put(mlx->mlx, mlx->win, (int)points[i]->x, (int)points[i]->y, 0xFFFFFFFF);
vec_list = vec_list->next;
i++;
}
}
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;
} }
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
t_mlx m; t_mlx m;
int fd; t_list *vec_list;
char *line; t_list *edg_list;
t_vec vec;
if (argc != 2) t_vec **points;
return (0); t_edg edg;
fd = open(argv[1], O_RDONLY); int vec_list_length;
// add new get_next_line t_mat4 *mat;
while (get_next_line(fd, &line))
{
ft_putstr(line);
ft_putstr("\n");
}
// int fd;
// char *line;
//
// if (argc != 2)
// return (0);
// fd = open(argv[1], O_RDONLY);
// // add new get_next_line
// while (get_next_line(fd, &line))
// {
// ft_putstr(line);
// ft_putstr("\n");
// }
m.mlx = mlx_init(); m.mlx = mlx_init();
m.win = mlx_new_window(m.mlx, 500, 500, "fdf"); m.win = mlx_new_window(m.mlx, 500, 500, "fdf");
test_line(m); //test_line(m);
circle(m); //circle(m);
argc = argc;
argv = argv;
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]));
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); mlx_loop(m.mlx);
return (0); return (0);
} }