From 723c4602c6ec9b74e841501754e651ef359f6385 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 12 May 2020 19:11:40 +0200 Subject: Added perspective matrix --- vendor/libft | 2 +- vendor/libftm/Makefile | 21 ++++++++++++------ vendor/libftm/inc/libftm_mat4.h | 27 ++++++++++++++++++++++- vendor/libftm/src/mat4/ftm_mat4init_frustum.c | 27 +++++++++++++++++++++++ vendor/libftm/src/mat4/ftm_mat4init_frustum_sym.c | 26 ++++++++++++++++++++++ vendor/libftm/src/mat4/ftm_mat4init_perspective.c | 25 +++++++++++++++++++++ vendor/libftm/src/mat4/ftm_mat4init_proj.c | 0 vendor/libftm/src/mat4/ftm_mat4rotate.c | 6 ++--- 8 files changed, 122 insertions(+), 12 deletions(-) create mode 100644 vendor/libftm/src/mat4/ftm_mat4init_frustum.c create mode 100644 vendor/libftm/src/mat4/ftm_mat4init_frustum_sym.c create mode 100644 vendor/libftm/src/mat4/ftm_mat4init_perspective.c create mode 100644 vendor/libftm/src/mat4/ftm_mat4init_proj.c (limited to 'vendor') diff --git a/vendor/libft b/vendor/libft index b9f000a..966eb29 160000 --- a/vendor/libft +++ b/vendor/libft @@ -1 +1 @@ -Subproject commit b9f000a80cbba38b8f21c9737a42f07573ec7b91 +Subproject commit 966eb29634a84496e0851ef2b5a7d64f413d33ed diff --git a/vendor/libftm/Makefile b/vendor/libftm/Makefile index 10b2bbd..c33bd78 100644 --- a/vendor/libftm/Makefile +++ b/vendor/libftm/Makefile @@ -6,12 +6,14 @@ # By: charles +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/05/09 20:44:53 by charles #+# #+# # -# Updated: 2020/05/12 11:43:06 by charles ### ########.fr # +# Updated: 2020/05/12 16:03:51 by charles ### ########.fr # # # # **************************************************************************** # LIB = ar rcs RM = rm -f +MAKE = make --no-print-directory +JOBS = 4 SRC_DIR = src INC_DIR = inc @@ -29,21 +31,26 @@ OBJ = $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) OBJ_SUB_DIR = $(shell find $(SRC_DIR) -type d | sed 's/$(SRC_DIR)/$(OBJ_DIR)/') -all: prebuild $(NAME) +all: prebuild + @$(MAKE) -j$(JOBS) $(NAME) prebuild: - mkdir -p $(OBJ_DIR) $(OBJ_SUB_DIR) + @mkdir -p $(OBJ_DIR) $(OBJ_SUB_DIR) $(NAME): $(OBJ) $(INC) - $(LIB) $@ $(OBJ) + @echo "Linking: $@" + @$(LIB) $@ $(OBJ) $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c - $(CC) $(CCFLAGS) -c -o $@ $< + @echo "Compiling: $@" + @$(CC) $(CCFLAGS) -c -o $@ $< clean: - $(RM) $(OBJ) + @echo "Removing objects" + @$(RM) $(OBJ) fclean: clean - $(RM) $(NAME) + @echo "Removing $(NAME)" + @$(RM) $(NAME) re: fclean all diff --git a/vendor/libftm/inc/libftm_mat4.h b/vendor/libftm/inc/libftm_mat4.h index f69d7e6..5fc183b 100644 --- a/vendor/libftm/inc/libftm_mat4.h +++ b/vendor/libftm/inc/libftm_mat4.h @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/12 11:04:59 by charles #+# #+# */ -/* Updated: 2020/05/12 13:53:40 by charles ### ########.fr */ +/* Updated: 2020/05/12 17:21:34 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,8 +22,33 @@ typedef struct float m[4 * 4]; } t_ftmmat4; +/* +** http://www.songho.ca/opengl/gl_projectionmatrix.html +*/ + +typedef struct +{ + float near; + float far; + float left; + float right; + float top; + float bottom; +} t_ftmfrustum; + +typedef struct +{ + float near; + float far; + float width; + float height; +} t_ftmfrustum_sym; + t_ftmmat4 *ftm_mat4init_eye(t_ftmmat4 *mat4, float x); t_ftmmat4 *ftm_mat4init_fill(t_ftmmat4 *mat4, float x); +t_ftmmat4 *ftm_mat4init_frustum(t_ftmmat4 *mat4, t_ftmfrustum *frustum); +t_ftmmat4 *ftm_mat4init_frustum_sym(t_ftmmat4 *mat4, t_ftmfrustum_sym *frustum); +t_ftmmat4 *ftm_mat4init_perspective(t_ftmmat4 *mat4, float fov, float aspect_ratio, float near, float far); t_ftmmat4 *ftm_mat4translate(t_ftmmat4 *mat4, float x, float y, float z); t_ftmmat4 *ftm_mat4rotate(t_ftmmat4 *mat4, float radian, t_ftmvec3 *axis); t_ftmmat4 *ftm_mat4scale(t_ftmmat4 *mat4, float x, float y, float z); diff --git a/vendor/libftm/src/mat4/ftm_mat4init_frustum.c b/vendor/libftm/src/mat4/ftm_mat4init_frustum.c new file mode 100644 index 0000000..1ba30b2 --- /dev/null +++ b/vendor/libftm/src/mat4/ftm_mat4init_frustum.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ftm_mat4init_frustum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/12 15:37:26 by charles #+# #+# */ +/* Updated: 2020/05/12 17:29:53 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libftm_mat4.h" + +t_ftmmat4 *ftm_mat4init_frustum(t_ftmmat4 *mat4, t_ftmfrustum *frustum) +{ + ftm_mat4init_fill(mat4, 0.0); + + ftm_mat4set(mat4, 0, 0, (2.0 * frustum->near) / (frustum->right - frustum->left)); + ftm_mat4set(mat4, 1, 1, (2.0 * frustum->near) / (frustum->top - frustum->bottom)); + ftm_mat4set(mat4, 2, 2, (-(frustum->far + frustum->near)) / (frustum->far - frustum->near)); + ftm_mat4set(mat4, 0, 2, (frustum->right + frustum->left) / (frustum->right - frustum->left)); + ftm_mat4set(mat4, 1, 2, (frustum->top + frustum->bottom) / (frustum->top - frustum->bottom)); + ftm_mat4set(mat4, 2, 3, (-2.0 * frustum->far * frustum->near) / (frustum->far - frustum->near)); + ftm_mat4set(mat4, 3, 2, -1.0); + return (mat4); +} diff --git a/vendor/libftm/src/mat4/ftm_mat4init_frustum_sym.c b/vendor/libftm/src/mat4/ftm_mat4init_frustum_sym.c new file mode 100644 index 0000000..2763765 --- /dev/null +++ b/vendor/libftm/src/mat4/ftm_mat4init_frustum_sym.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ftm_mat4init_frustum_sym.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/12 15:47:45 by charles #+# #+# */ +/* Updated: 2020/05/12 17:21:06 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libftm_mat4.h" + +t_ftmmat4 *ftm_mat4init_frustum_sym(t_ftmmat4 *mat4, t_ftmfrustum_sym *frustum) +{ + t_ftmfrustum f; + + f.right = frustum->width / 2; + f.left = -f.right; + f.top = frustum->height / 2; + f.bottom = -f.top; + f.near = frustum->near; + f.far = frustum->far; + return (ftm_mat4init_frustum(mat4, &f)); +} diff --git a/vendor/libftm/src/mat4/ftm_mat4init_perspective.c b/vendor/libftm/src/mat4/ftm_mat4init_perspective.c new file mode 100644 index 0000000..f04b39e --- /dev/null +++ b/vendor/libftm/src/mat4/ftm_mat4init_perspective.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ftm_mat4init_perspective.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/12 16:54:49 by charles #+# #+# */ +/* Updated: 2020/05/12 19:07:45 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libftm_mat4.h" + +t_ftmmat4 *ftm_mat4init_perspective(t_ftmmat4 *mat4, float fov, float aspect_ratio, + float near, float far) +{ + t_ftmfrustum_sym frustum; + + frustum.near = near; + frustum.far = far; + frustum.width = 2.0 * (near / tanf(M_PI_2 - (fov / 2.0))); + frustum.height = frustum.width / aspect_ratio; + return (ftm_mat4init_frustum_sym(mat4, &frustum)); +} diff --git a/vendor/libftm/src/mat4/ftm_mat4init_proj.c b/vendor/libftm/src/mat4/ftm_mat4init_proj.c new file mode 100644 index 0000000..e69de29 diff --git a/vendor/libftm/src/mat4/ftm_mat4rotate.c b/vendor/libftm/src/mat4/ftm_mat4rotate.c index 0a39c16..6d0432e 100644 --- a/vendor/libftm/src/mat4/ftm_mat4rotate.c +++ b/vendor/libftm/src/mat4/ftm_mat4rotate.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/12 11:44:18 by charles #+# #+# */ -/* Updated: 2020/05/12 14:05:18 by charles ### ########.fr */ +/* Updated: 2020/05/12 17:07:23 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,8 +46,8 @@ t_ftmmat4 *ftm_mat4rotate(t_ftmmat4 *mat4, float theta, t_ftmvec3 *axis) x = axis->v[0]; y = axis->v[1]; z = axis->v[2]; - sin_t = sin(theta); - cos_t = cos(theta); + sin_t = sinf(theta); + cos_t = cosf(theta); ftm_mat4init_fill(&rot, 0.0); ftm_mat4set(&rot, 0, 0, cos_t + x * x * (1 - cos_t)); ftm_mat4set(&rot, 1, 0, y * x * (1 - cos_t) + z * sin_t); -- cgit