mat4_perspective

This commit is contained in:
gbrochar 2020-12-23 16:11:35 +01:00
parent c2b5bb8de0
commit 5e4e8a768e
3 changed files with 107 additions and 1 deletions

View File

@ -6,7 +6,7 @@
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

26
inc/vec4.h Normal file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec4.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

64
src/mat4_perspective.c Normal file
View File

@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mat4_perspective.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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));
}