diff --git a/inc/mat4.h b/inc/mat4.h index fffbba0..155d53c 100644 --- a/inc/mat4.h +++ b/inc/mat4.h @@ -6,7 +6,7 @@ /* By: gbrochar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/22 20:35:00 by gbrochar #+# #+# */ -/* Updated: 2020/12/23 10:50:09 by gbrochar ### ########.fr */ +/* Updated: 2020/12/23 16:11:16 by gbrochar ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,14 +14,30 @@ # define MAT4_H typedef struct s_mat4 t_mat4; +typedef struct s_persp_tool t_persp_tool; struct s_mat4 { double data[16]; }; +struct s_persp_tool +{ + double xmin; + double xmax; + double ymin; + double ymax; +}; + t_mat4 mat4_transpose(t_mat4 m); + t_mat4 mat4_multiply(t_mat4 m, t_mat4 rhs); t_mat4 mat4_multiply_tmp(t_mat4 m, t_mat4 rhs); +t_mat4 mat4_perspective( + double fov, double aspect, double near, double far); +t_mat4 mat4_frustrum( + t_persp_tool tool, double near, double far); +t_mat4 mat4_frustrum_tmp(double x, double y, t_vec4 tool); + #endif diff --git a/inc/vec4.h b/inc/vec4.h new file mode 100644 index 0000000..797ca55 --- /dev/null +++ b/inc/vec4.h @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec4.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/12/23 15:57:23 by gbrochar #+# #+# */ +/* Updated: 2020/12/23 16:00:12 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef VEC4_H +# define VEC4_H + +typedef struct s_vec4 t_vec4; + +struct s_vec4 +{ + double x; + double y; + double z; + double w; +}; + +#endif diff --git a/src/mat4_perspective.c b/src/mat4_perspective.c new file mode 100644 index 0000000..a32cac5 --- /dev/null +++ b/src/mat4_perspective.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mat4_perspective.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gbrochar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/12/23 15:52:55 by gbrochar #+# #+# */ +/* Updated: 2020/12/23 16:11:07 by gbrochar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "mat4.h" +#include "vec4.h" + +t_mat4 mat4_frustrum_tmp(double x, double y, t_vec4 tool) +{ + t_mat4 ret; + + ret.data[0] = x; + ret.data[4] = 0; + ret.data[8] = tool.x; + ret.data[12] = 0; + ret.data[1] = 0; + ret.data[5] = y; + ret.data[9] = tool.y; + ret.data[13] = 0; + ret.data[2] = 0; + ret.data[6] = 0; + ret.data[10] = tool.z; + ret.data[14] = tool.w; + ret.data[3] = 0; + ret.data[7] = 0; + ret.data[11] = -1; + ret.data[15] = 0; + return (ret); +} + +t_mat4 mat4_frustrum(t_persp_tool tool, double near, double far) +{ + double x; + double y; + t_vec4 tool2; + + x = (2.0 * near) / (tool.xmax - tool.xmin); + y = (2.0 * near) / (tool.ymax - tool.ymin); + tool2.x = (tool.xmax + tool.xmin) / (tool.xmax - tool.xmin); + tool2.y = (tool.ymax + tool.ymin) / (tool.ymax - tool.ymin); + tool2.z = -(far + near) / (far - near); + tool2.w = (-2.0 * far * near) / (far - near); + return (mat4_frustrum_tmp(x, y, tool2)); +} + +t_mat4 mat4_perspective( + double fov, double aspect, double near, double far) +{ + t_persp_tool tool; + + tool.ymax = near * tan(fov / 2.0); + tool.ymin = -tool.ymax; + tool.xmin = tool.ymin * aspect; + tool.xmax = tool.ymax * aspect; + return (mat4_frustrum(tool, near, far)); +}