some matrix-related functions

This commit is contained in:
Gregory 2017-02-21 07:09:36 +02:00
parent 50d70eb260
commit a84634e6de
3 changed files with 204 additions and 15 deletions

View file

@ -14,13 +14,18 @@
#include <math.h>
#include <fcntl.h>
typedef struct s_point
typedef struct s_vec
{
int x;
int y;
int z;
float x;
float y;
float z;
int color;
} t_point;
} t_vec;
typedef struct s_mat4
{
float mx[4][4];
} t_mat4;
typedef struct s_mlx
{
@ -34,6 +39,6 @@ typedef struct s_swap
int swap_coord;
} t_swap;
void line(t_point *p1, t_point *p2, t_mlx *m);
void line(t_vec *p1, t_vec *p2, t_mlx *m);
#endif

View file

@ -1,7 +1,7 @@
#include "fdf.h"
static void swap_x_y(t_point *p1, t_point *p2)
static void swap_x_y(t_vec *p1, t_vec *p2)
{
int temp;
@ -13,9 +13,9 @@ static void swap_x_y(t_point *p1, t_point *p2)
p2->y = temp;
}
static void swap_points(t_point *p1, t_point *p2)
static void swap_points(t_vec *p1, t_vec *p2)
{
t_point temp;
t_vec temp;
temp = *p1;
*p1 = *p2;
@ -23,7 +23,7 @@ static void swap_points(t_point *p1, t_point *p2)
}
static void draw_line(t_point *p1, t_point *p2, t_mlx *m, t_swap *s)
static void draw_line(t_vec *p1, t_vec *p2, t_mlx *m, t_swap *s)
{
int dx = abs(p2->x - p1->x);
int dy = abs(p2->y - p1->y);
@ -44,7 +44,7 @@ static void draw_line(t_point *p1, t_point *p2, t_mlx *m, t_swap *s)
}
}
void line(t_point *p1, t_point *p2, t_mlx *m)
void line(t_vec *p1, t_vec *p2, t_mlx *m)
{
int dx;
int dy;

View file

@ -7,10 +7,194 @@
#include "fdf.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 *mat;
mat = NULL;
if ((mat = (t_mat4 *)malloc(sizeof(t_mat4))))
{
mat->mx[0][0] = 1.f;
mat->mx[1][1] = 1.f;
mat->mx[2][2] = 1.f;
mat->mx[3][3] = 1.f;
}
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_z_rot(t_mat4 *m, float angle)
{
angle = angle * M_1_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, float angle)
{
angle = angle * M_1_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, float angle)
{
angle = angle * M_1_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, t_vec *v2)
{
float vec4;
v2->x = v->x * m->mx[0][0] + v->y * m->mx[1][0] + v->z * m->mx[2][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] +
m->mx[3][1];
v2->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)
{
v2->x = v2->x / vec4;
v2->y = v2->y / vec4;
v2->z = v2->z / vec4;
}
}
void test_line(t_mlx mlx)
{
t_point p1;
t_point p2;
t_vec p1;
t_vec p2;
int temp;
p1.color = 0xC100E5;
@ -55,8 +239,8 @@ void test_line(t_mlx mlx)
void circle(t_mlx m)
{
t_point p1;
t_point p2;
t_vec p1;
t_vec p2;
double angle;