scop/src/mat4_perspective.c

60 lines
2.0 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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(t_persp_tool tool, double near, double far)
{
double x;
double y;
t_vec4 tool2;
t_mat4 frustrum;
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);
frustrum.data[0] = x;
frustrum.data[4] = 0;
frustrum.data[8] = tool2.x;
frustrum.data[12] = 0;
frustrum.data[1] = 0;
frustrum.data[5] = y;
frustrum.data[9] = tool2.y;
frustrum.data[13] = 0;
frustrum.data[2] = 0;
frustrum.data[6] = 0;
frustrum.data[10] = tool2.z;
frustrum.data[14] = tool2.w;
frustrum.data[3] = 0;
frustrum.data[7] = 0;
frustrum.data[11] = -1;
frustrum.data[15] = 0;
return frustrum;
}
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);
}