mat4_lookat
This commit is contained in:
parent
3aa9bf6fe3
commit
be37bd8c3e
6
Makefile
6
Makefile
|
@ -6,7 +6,7 @@
|
||||||
# By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ #
|
# By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2020/12/17 19:47:03 by gbrochar #+# #+# #
|
# Created: 2020/12/17 19:47:03 by gbrochar #+# #+# #
|
||||||
# Updated: 2020/12/23 10:49:53 by gbrochar ### ########.fr #
|
# Updated: 2020/12/23 17:05:36 by gbrochar ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
@ -18,6 +18,8 @@ SRC_FILE = main.c \
|
||||||
env.c \
|
env.c \
|
||||||
mat4_transpose.c \
|
mat4_transpose.c \
|
||||||
mat4_multiply.c \
|
mat4_multiply.c \
|
||||||
|
mat4_perspective.c \
|
||||||
|
mat4_lookat.c \
|
||||||
|
|
||||||
OBJ_FILE = $(SRC_FILE:.c=.o)
|
OBJ_FILE = $(SRC_FILE:.c=.o)
|
||||||
|
|
||||||
|
@ -31,7 +33,7 @@ SRC = $(addprefix $(SRC_DIR), $(SRC_FILE))
|
||||||
OBJ = $(addprefix $(OBJ_DIR), $(OBJ_FILE))
|
OBJ = $(addprefix $(OBJ_DIR), $(OBJ_FILE))
|
||||||
INC = $(addprefix $(INC_DIR), $(INC_FILE))
|
INC = $(addprefix $(INC_DIR), $(INC_FILE))
|
||||||
|
|
||||||
LIB =
|
LIB = -lm
|
||||||
|
|
||||||
CC = gcc
|
CC = gcc
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2020/12/22 20:35:00 by gbrochar #+# #+# */
|
/* Created: 2020/12/22 20:35:00 by gbrochar #+# #+# */
|
||||||
/* Updated: 2020/12/23 16:12:55 by gbrochar ### ########.fr */
|
/* Updated: 2020/12/23 17:04:47 by gbrochar ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@
|
||||||
# define MAT4_H
|
# define MAT4_H
|
||||||
|
|
||||||
# include "vec4.h"
|
# include "vec4.h"
|
||||||
|
# include <math.h>
|
||||||
|
|
||||||
typedef struct s_mat4 t_mat4;
|
typedef struct s_mat4 t_mat4;
|
||||||
typedef struct s_persp_tool t_persp_tool;
|
typedef struct s_persp_tool t_persp_tool;
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2020/12/22 12:08:22 by gbrochar #+# #+# */
|
/* Created: 2020/12/22 12:08:22 by gbrochar #+# #+# */
|
||||||
/* Updated: 2020/12/22 20:15:56 by gbrochar ### ########.fr */
|
/* Updated: 2020/12/23 16:23:16 by gbrochar ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -25,6 +25,8 @@
|
||||||
|
|
||||||
# define BUFFER_SIZE 4096
|
# define BUFFER_SIZE 4096
|
||||||
|
|
||||||
|
# define EPSILON 0.000001
|
||||||
|
|
||||||
typedef enum e_gl_buf_type t_gl_buf_type;
|
typedef enum e_gl_buf_type t_gl_buf_type;
|
||||||
|
|
||||||
enum e_gl_buf_type
|
enum e_gl_buf_type
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* vec3.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2020/12/23 16:14:13 by gbrochar #+# #+# */
|
||||||
|
/* Updated: 2020/12/23 16:14:28 by gbrochar ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef VEC3_H
|
||||||
|
# define VEC3_H
|
||||||
|
|
||||||
|
typedef struct s_vec3 t_vec3;
|
||||||
|
|
||||||
|
struct s_vec3
|
||||||
|
{
|
||||||
|
double x;
|
||||||
|
double y;
|
||||||
|
double z;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,125 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* mat4_lookat.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2020/12/23 16:14:47 by gbrochar #+# #+# */
|
||||||
|
/* Updated: 2020/12/23 17:05:21 by gbrochar ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "mat4.h"
|
||||||
|
#include "vec3.h"
|
||||||
|
#include "scop.h"
|
||||||
|
|
||||||
|
t_mat4 mat4_lookat_make_mat(t_vec3 x, t_vec3 y, t_vec3 z, t_vec3 eye)
|
||||||
|
{
|
||||||
|
t_mat4 ret;
|
||||||
|
|
||||||
|
ret.data[0] = x.x;
|
||||||
|
ret.data[1] = x.y;
|
||||||
|
ret.data[2] = x.z;
|
||||||
|
ret.data[3] = 0;
|
||||||
|
ret.data[4] = y.x;
|
||||||
|
ret.data[5] = y.y;
|
||||||
|
ret.data[6] = y.z;
|
||||||
|
ret.data[7] = 0;
|
||||||
|
ret.data[8] = z.x;
|
||||||
|
ret.data[9] = z.y;
|
||||||
|
ret.data[10] = z.z;
|
||||||
|
ret.data[11] = 0;
|
||||||
|
ret.data[12] = -(x.x * eye.x + x.y * eye.y + x.z * eye.z);
|
||||||
|
ret.data[13] = -(y.x * eye.x + y.y * eye.y + y.z * eye.z);
|
||||||
|
ret.data[14] = -(z.x * eye.x + z.y * eye.y + z.z * eye.z);
|
||||||
|
ret.data[15] = 1;
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_vec3 mat4_lookat_make_y(t_vec3 x, t_vec3 z)
|
||||||
|
{
|
||||||
|
t_vec3 y;
|
||||||
|
double len;
|
||||||
|
|
||||||
|
y.x = z.y * x.z - z.z * x.y;
|
||||||
|
y.y = z.z * x.x - z.x * x.z;
|
||||||
|
y.z = z.x * x.y - z.y * x.x;
|
||||||
|
len = y.x * y.x + y.y * y.y + y.z * y.z;
|
||||||
|
if (len - EPSILON < 0 && len + EPSILON > 0)
|
||||||
|
{
|
||||||
|
y.x = 0;
|
||||||
|
y.y = 0;
|
||||||
|
y.z = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
len = 1.0 / sqrt(len);
|
||||||
|
y.x *= len;
|
||||||
|
y.y *= len;
|
||||||
|
y.z *= len;
|
||||||
|
}
|
||||||
|
return (y);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_vec3 mat4_lookat_make_x(t_vec3 z, t_vec3 up)
|
||||||
|
{
|
||||||
|
t_vec3 x;
|
||||||
|
double len;
|
||||||
|
|
||||||
|
x.x = up.y * z.z - up.z * z.y;
|
||||||
|
x.y = up.z * z.x - up.x * z.z;
|
||||||
|
x.z = up.x * z.y - up.y * z.x;
|
||||||
|
len = x.x * x.x + x.y * x.y + x.z * x.z;
|
||||||
|
if (len - EPSILON < 0 && len + EPSILON > 0)
|
||||||
|
{
|
||||||
|
x.x = 0;
|
||||||
|
x.y = 0;
|
||||||
|
x.z = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
len = 1.0 / sqrt(len);
|
||||||
|
x.x *= len;
|
||||||
|
x.y *= len;
|
||||||
|
x.z *= len;
|
||||||
|
}
|
||||||
|
return (x);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_vec3 mat4_lookat_make_z(t_vec3 eye, t_vec3 target)
|
||||||
|
{
|
||||||
|
t_vec3 z;
|
||||||
|
double len;
|
||||||
|
|
||||||
|
z.x = eye.x - target.x;
|
||||||
|
z.y = eye.y - target.y;
|
||||||
|
z.z = eye.z - target.z;
|
||||||
|
len = z.x * z.x + z.y * z.y + z.z * z.z;
|
||||||
|
if (len - EPSILON < 0 && len + EPSILON > 0)
|
||||||
|
{
|
||||||
|
z.x = 0;
|
||||||
|
z.y = 0;
|
||||||
|
z.z = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
len = 1.0 / sqrt(len);
|
||||||
|
z.x *= len;
|
||||||
|
z.y *= len;
|
||||||
|
z.z *= len;
|
||||||
|
}
|
||||||
|
return (z);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_mat4 mat4_lookat(t_vec3 eye, t_vec3 up, t_vec3 target)
|
||||||
|
{
|
||||||
|
t_vec3 z;
|
||||||
|
t_vec3 x;
|
||||||
|
t_vec3 y;
|
||||||
|
|
||||||
|
z = mat4_lookat_make_z(eye, target);
|
||||||
|
x = mat4_lookat_make_x(z, up);
|
||||||
|
y = mat4_lookat_make_y(x, z);
|
||||||
|
return (mat4_lookat_make_mat(x, y, z, eye));
|
||||||
|
}
|
Loading…
Reference in New Issue