mat4_identity, mat4_inverse, minimal setup ready for OpenGL

This commit is contained in:
gbrochar 2020-12-23 20:38:33 +01:00
parent 7cc0265f3f
commit 7d9758728b
8 changed files with 131 additions and 8 deletions

View File

@ -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 17:36:57 by gbrochar ### ########.fr # # Updated: 2020/12/23 20:33:29 by gbrochar ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -17,10 +17,13 @@ SRC_FILE = main.c \
parse_line.c \ parse_line.c \
parse_token.c \ parse_token.c \
env.c \ env.c \
run.c \
mat4_transpose.c \ mat4_transpose.c \
mat4_multiply.c \ mat4_multiply.c \
mat4_perspective.c \ mat4_perspective.c \
mat4_lookat.c \ mat4_lookat.c \
mat4_identity.c \
mat4_inverse.c \
OBJ_FILE = $(SRC_FILE:.c=.o) OBJ_FILE = $(SRC_FILE:.c=.o)
@ -34,7 +37,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 = -lm LIB = -lm -lGL -lglut -lGLEW
CC = gcc CC = gcc

View File

@ -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 17:04:47 by gbrochar ### ########.fr */ /* Updated: 2020/12/23 20:17:35 by gbrochar ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -43,4 +43,8 @@ t_mat4 mat4_frustrum(
t_persp_tool tool, double near, double far); t_persp_tool tool, double near, double far);
t_mat4 mat4_frustrum_tmp(double x, double y, t_vec4 tool); t_mat4 mat4_frustrum_tmp(double x, double y, t_vec4 tool);
t_mat4 mat4_identity(void);
t_mat4 mat4_inverse(t_mat4 m);
#endif #endif

View File

@ -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/23 17:57:54 by gbrochar ### ########.fr */ /* Updated: 2020/12/23 20:37:01 by gbrochar ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,6 +17,9 @@
# include <string.h> # include <string.h>
# include <stdlib.h> # include <stdlib.h>
# include <GL/glew.h>
# include <GL/freeglut.h>
# define SUCCESS 0 # define SUCCESS 0
# define FAILURE -1 # define FAILURE -1
# define BREAK 1 # define BREAK 1
@ -112,4 +115,6 @@ void init_camera(t_env *e);
void init_object(t_env *e); void init_object(t_env *e);
void init_env(t_env *e); void init_env(t_env *e);
void run(t_env *e);
#endif #endif

View File

@ -6,11 +6,12 @@
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */ /* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/22 12:02:14 by gbrochar #+# #+# */ /* Created: 2020/12/22 12:02:14 by gbrochar #+# #+# */
/* Updated: 2020/12/23 17:57:34 by gbrochar ### ########.fr */ /* Updated: 2020/12/23 20:30:48 by gbrochar ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "scop.h" #include "scop.h"
#include "mat4.h"
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
@ -18,8 +19,7 @@ int main(int argc, char **argv)
init_env(&e); init_env(&e);
if (parse(&e, argc, argv) == SUCCESS) if (parse(&e, argc, argv) == SUCCESS)
{ run(&e);
}
else else
{ {
free_env(&e); free_env(&e);

37
src/mat4_identity.c Normal file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mat4_identity.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/23 20:08:04 by gbrochar #+# #+# */
/* Updated: 2020/12/23 20:16:32 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "mat4.h"
t_mat4 mat4_identity(void)
{
t_mat4 identity;
identity.data[0] = 1;
identity.data[1] = 0;
identity.data[2] = 0;
identity.data[3] = 0;
identity.data[4] = 0;
identity.data[5] = 1;
identity.data[6] = 0;
identity.data[7] = 0;
identity.data[8] = 0;
identity.data[9] = 0;
identity.data[10] = 1;
identity.data[11] = 0;
identity.data[12] = 0;
identity.data[13] = 0;
identity.data[14] = 0;
identity.data[15] = 1;
return (identity);
}

55
src/mat4_inverse.c Normal file
View File

@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mat4_inverse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/23 20:06:45 by gbrochar #+# #+# */
/* Updated: 2020/12/23 20:26:07 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "mat4.h"
#include "scop.h"
t_mat4 mat4_inverse(t_mat4 m)
{
t_mat4 ret;
double b00 = m.data[0] * m.data[5] - m.data[1] * m.data[4];
double b01 = m.data[0] * m.data[6] - m.data[2] * m.data[4];
double b02 = m.data[0] * m.data[7] - m.data[3] * m.data[4];
double b03 = m.data[1] * m.data[6] - m.data[2] * m.data[5];
double b04 = m.data[1] * m.data[7] - m.data[3] * m.data[5];
double b05 = m.data[2] * m.data[7] - m.data[3] * m.data[6];
double b06 = m.data[8] * m.data[13] - m.data[9] * m.data[12];
double b07 = m.data[8] * m.data[14] - m.data[10] * m.data[12];
double b08 = m.data[8] * m.data[15] - m.data[11] * m.data[12];
double b09 = m.data[9] * m.data[14] - m.data[10] * m.data[13];
double b10 = m.data[9] * m.data[15] - m.data[11] * m.data[13];
double b11 = m.data[10] * m.data[15] - m.data[11] * m.data[14];
double det =
b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
if (det - EPSILON < 0 && det + EPSILON > 0)
return (mat4_identity());
det = 1.0 / det;
ret.data[0] = (m.data[5] * b11 - m.data[6] * b10 + m.data[7] * b09) * det;
ret.data[1] = (m.data[2] * b10 - m.data[1] * b11 + m.data[3] * b09) * det;
ret.data[2] = (m.data[13] * b05 - m.data[14] * b04 + m.data[15] * b03) * det;
ret.data[3] = (m.data[10] * b04 - m.data[9] * b05 + m.data[11] * b03) * det;
ret.data[4] = (m.data[6] * b08 - m.data[4] * b11 + m.data[7] * b07) * det;
ret.data[5] = (m.data[0] * b11 - m.data[2] * b08 + m.data[3] * b07) * det;
ret.data[6] = (m.data[14] * b02 - m.data[12] * b05 + m.data[15] * b01) * det;
ret.data[7] = (m.data[8] * b05 - m.data[10] * b02 + m.data[11] * b01) * det;
ret.data[8] = (m.data[4] * b10 - m.data[5] * b08 + m.data[7] * b06) * det;
ret.data[9] = (m.data[1] * b08 - m.data[0] * b10 + m.data[3] * b06) * det;
ret.data[10] = (m.data[12] * b04 - m.data[13] * b02 + m.data[15] * b00) * det;
ret.data[11] = (m.data[9] * b02 - m.data[8] * b04 + m.data[11] * b00) * det;
ret.data[12] = (m.data[5] * b07 - m.data[4] * b09 + m.data[6] * b06) * det;
ret.data[13] = (m.data[0] * b09 - m.data[1] * b07 + m.data[2] * b06) * det;
ret.data[14] = (m.data[13] * b01 - m.data[12] * b03 + m.data[14] * b00) * det;
ret.data[15] = (m.data[8] * b03 - m.data[9] * b01 + m.data[10] * b00) * det;
return (ret);
}

View File

@ -6,7 +6,7 @@
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */ /* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/22 18:41:28 by gbrochar #+# #+# */ /* Created: 2020/12/22 18:41:28 by gbrochar #+# #+# */
/* Updated: 2020/12/23 17:57:40 by gbrochar ### ########.fr */ /* Updated: 2020/12/23 20:31:41 by gbrochar ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -62,6 +62,7 @@ void parse_triangulate(t_buf_i *indices, int vertex_count)
i++; i++;
} }
parse_append_data_tmp(indices, vertex_count, data_tmp); parse_append_data_tmp(indices, vertex_count, data_tmp);
free(data_tmp);
} }
int parse_line(t_env *e, char *line) int parse_line(t_env *e, char *line)

18
src/run.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* run.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/23 19:38:05 by gbrochar #+# #+# */
/* Updated: 2020/12/23 20:28:50 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "scop.h"
void run(t_env *e)
{
(void)e;
}