scop/src/mat4_perspective.c

65 lines
2.0 KiB
C
Raw Normal View History

2020-12-23 15:11:35 +00:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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));
}