transformations using keys
This commit is contained in:
parent
304ec993c8
commit
f89c6be4f8
4
Makefile
4
Makefile
|
@ -6,7 +6,7 @@
|
|||
# By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2020/12/17 19:47:03 by gbrochar #+# #+# #
|
||||
# Updated: 2020/12/26 06:11:43 by gbrochar ### ########.fr #
|
||||
# Updated: 2020/12/26 09:13:13 by gbrochar ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
|
@ -26,6 +26,8 @@ SRC_FILE = main.c \
|
|||
mat4_identity.c \
|
||||
mat4_inverse.c \
|
||||
mat4_rotate.c \
|
||||
mat4_translate.c \
|
||||
mat4_scale.c \
|
||||
|
||||
OBJ_FILE = $(SRC_FILE:.c=.o)
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2020/12/22 20:35:00 by gbrochar #+# #+# */
|
||||
/* Updated: 2020/12/24 12:30:50 by gbrochar ### ########.fr */
|
||||
/* Updated: 2020/12/26 09:16:18 by gbrochar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -57,5 +57,11 @@ t_mat4 mat4_inverse(t_mat4 m);
|
|||
|
||||
t_mat4 mat4_rotatex(t_mat4 m, double theta);
|
||||
t_mat4 mat4_rotatey(t_mat4 m, double theta);
|
||||
t_mat4 mat4_rotatez(t_mat4 m, double theta);
|
||||
t_mat4 mat4_rotatexyz(t_mat4 m, t_vec3 theta);
|
||||
|
||||
t_mat4 mat4_translate(t_mat4 m, t_vec3 t);
|
||||
|
||||
t_mat4 mat4_scale(t_mat4 m, t_vec3 s);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2020/12/22 12:08:22 by gbrochar #+# #+# */
|
||||
/* Updated: 2020/12/26 06:32:21 by gbrochar ### ########.fr */
|
||||
/* Updated: 2020/12/26 09:16:30 by gbrochar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -22,6 +22,8 @@
|
|||
# include <GL/glew.h>
|
||||
# include <GLFW/glfw3.h>
|
||||
|
||||
# include "vec3.h"
|
||||
|
||||
# define SUCCESS 0
|
||||
# define FAILURE -1
|
||||
# define BREAK 1
|
||||
|
@ -96,6 +98,9 @@ struct s_env
|
|||
GLuint shader_program;
|
||||
GLuint vertex_shader;
|
||||
GLuint fragment_shader;
|
||||
t_vec3 translate;
|
||||
t_vec3 rotate;
|
||||
t_vec3 scale;
|
||||
};
|
||||
|
||||
int parse(t_env *e, int argc, char **argv);
|
||||
|
|
16
src/env.c
16
src/env.c
|
@ -6,7 +6,7 @@
|
|||
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2020/12/22 18:56:21 by gbrochar #+# #+# */
|
||||
/* Updated: 2020/12/24 14:32:22 by gbrochar ### ########.fr */
|
||||
/* Updated: 2020/12/26 09:37:56 by gbrochar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -52,9 +52,23 @@ void init_object(t_env *e)
|
|||
e->object.indices.len = BUFFER_SIZE;
|
||||
}
|
||||
|
||||
void init_transformations(t_env *e)
|
||||
{
|
||||
e->translate.x = 0;
|
||||
e->translate.y = 0;
|
||||
e->translate.z = 0;
|
||||
e->rotate.x = 0;
|
||||
e->rotate.y = 0;
|
||||
e->rotate.z = 0;
|
||||
e->scale.x = 1;
|
||||
e->scale.y = 1;
|
||||
e->scale.z = 1;
|
||||
}
|
||||
|
||||
void init_env(t_env *e)
|
||||
{
|
||||
init_window(e);
|
||||
init_camera(e);
|
||||
init_object(e);
|
||||
init_transformations(e);
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2020/12/24 12:24:54 by gbrochar #+# #+# */
|
||||
/* Updated: 2020/12/24 12:32:22 by gbrochar ### ########.fr */
|
||||
/* Updated: 2020/12/26 09:10:47 by gbrochar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -61,3 +61,34 @@ t_mat4 mat4_rotatey(t_mat4 m, double theta)
|
|||
ret.data[15] = m.data[15];
|
||||
return (ret);
|
||||
}
|
||||
|
||||
t_mat4 mat4_rotatez(t_mat4 m, double theta)
|
||||
{
|
||||
double s = sin(theta);
|
||||
double c = cos(theta);
|
||||
t_mat4 ret;
|
||||
|
||||
ret.data[0] = m.data[0] * c + m.data[4] * s;
|
||||
ret.data[1] = m.data[1] * c + m.data[5] * s;
|
||||
ret.data[2] = m.data[2] * c + m.data[6] * s;
|
||||
ret.data[3] = m.data[3] * c + m.data[7] * s;
|
||||
ret.data[4] = m.data[4] * c - m.data[0] * s;
|
||||
ret.data[5] = m.data[5] * c - m.data[1] * s;
|
||||
ret.data[6] = m.data[6] * c - m.data[2] * s;
|
||||
ret.data[7] = m.data[7] * c - m.data[3] * s;
|
||||
ret.data[8] = m.data[8];
|
||||
ret.data[9] = m.data[9];
|
||||
ret.data[10] = m.data[10];
|
||||
ret.data[11] = m.data[11];
|
||||
ret.data[12] = m.data[12];
|
||||
ret.data[13] = m.data[13];
|
||||
ret.data[14] = m.data[14];
|
||||
ret.data[15] = m.data[15];
|
||||
return (ret);
|
||||
}
|
||||
|
||||
t_mat4 mat4_rotatexyz(t_mat4 m, t_vec3 theta)
|
||||
{
|
||||
return (
|
||||
mat4_rotatey(mat4_rotatex(mat4_rotatez(m, theta.z), theta.x), theta.y));
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* mat4_scale.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2020/12/26 09:13:18 by gbrochar #+# #+# */
|
||||
/* Updated: 2020/12/26 09:17:36 by gbrochar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "mat4.h"
|
||||
|
||||
t_mat4 mat4_scale(t_mat4 m, t_vec3 s)
|
||||
{
|
||||
t_mat4 ret;
|
||||
|
||||
ret.data[0] = m.data[0] * s.x;
|
||||
ret.data[1] = m.data[1] * s.x;
|
||||
ret.data[2] = m.data[2] * s.x;
|
||||
ret.data[3] = m.data[3] * s.x;
|
||||
ret.data[4] = m.data[4] * s.y;
|
||||
ret.data[5] = m.data[5] * s.y;
|
||||
ret.data[6] = m.data[6] * s.y;
|
||||
ret.data[7] = m.data[7] * s.y;
|
||||
ret.data[8] = m.data[8] * s.z;
|
||||
ret.data[9] = m.data[9] * s.z;
|
||||
ret.data[10] = m.data[10] * s.z;
|
||||
ret.data[11] = m.data[11] * s.z;
|
||||
ret.data[12] = m.data[12];
|
||||
ret.data[13] = m.data[13];
|
||||
ret.data[14] = m.data[14];
|
||||
ret.data[15] = m.data[15];
|
||||
return (ret);
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* mat4_translate.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2020/12/26 09:03:05 by gbrochar #+# #+# */
|
||||
/* Updated: 2020/12/26 09:17:27 by gbrochar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "mat4.h"
|
||||
|
||||
t_mat4 mat4_translate(t_mat4 m, t_vec3 t)
|
||||
{
|
||||
t_mat4 ret;
|
||||
|
||||
ret.data[0] = m.data[0];
|
||||
ret.data[1] = m.data[1];
|
||||
ret.data[2] = m.data[2];
|
||||
ret.data[3] = m.data[3];
|
||||
ret.data[4] = m.data[4];
|
||||
ret.data[5] = m.data[5];
|
||||
ret.data[6] = m.data[6];
|
||||
ret.data[7] = m.data[7];
|
||||
ret.data[8] = m.data[8];
|
||||
ret.data[9] = m.data[9];
|
||||
ret.data[10] = m.data[10];
|
||||
ret.data[11] = m.data[11];
|
||||
ret.data[12] =
|
||||
m.data[0] * t.x + m.data[4] * t.y + m.data[8] * t.z + m.data[12];
|
||||
ret.data[13] =
|
||||
m.data[1] * t.x + m.data[5] * t.y + m.data[9] * t.z + m.data[13];
|
||||
ret.data[14] =
|
||||
m.data[2] * t.x + m.data[6] * t.y + m.data[10] * t.z + m.data[14];
|
||||
ret.data[15] =
|
||||
m.data[3] * t.x + m.data[7] * t.y + m.data[11] * t.z + m.data[15];
|
||||
return (ret);
|
||||
}
|
56
src/run.c
56
src/run.c
|
@ -6,7 +6,7 @@
|
|||
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2020/12/23 19:38:05 by gbrochar #+# #+# */
|
||||
/* Updated: 2020/12/26 06:26:36 by gbrochar ### ########.fr */
|
||||
/* Updated: 2020/12/26 09:47:39 by gbrochar ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -27,7 +27,7 @@ GLuint index_count;
|
|||
const char* pVSFileName = "src/shader.vs";
|
||||
const char* pFSFileName = "src/shader.fs";
|
||||
|
||||
static void RenderSceneCB()
|
||||
static void RenderSceneCB(t_env *e)
|
||||
{
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
|
@ -53,7 +53,10 @@ static void RenderSceneCB()
|
|||
t_mat4 proj = mat4_perspective(45.0 * M_PI / 180.0, 800.0 / 600.0, 0.001, 1000);
|
||||
t_mat4 view = mat4_inverse(mat4_lookat(eye, up, target));
|
||||
// t_mat4 model = mat4_rotatey(mat4_rotatex(mat4_identity(), Scale), Scale / 1.3);
|
||||
t_mat4 model = mat4_rotatey(mat4_identity(), Scale);
|
||||
//t_mat4 model = mat4_rotatey(mat4_identity(), Scale);
|
||||
t_mat4 model = mat4_scale(
|
||||
mat4_rotatexyz(
|
||||
mat4_translate(mat4_identity(), e->translate), e->rotate), e->scale);
|
||||
glUniform1f(gScaleIntLocation, Scale);
|
||||
glUniformMatrix4fv(gProjLocation, 1, GL_FALSE, (GLfloat *)proj.data);
|
||||
glUniformMatrix4fv(gViewLocation, 1, GL_FALSE, (GLfloat *)view.data);
|
||||
|
@ -192,6 +195,49 @@ void free_shaders(t_env *e)
|
|||
glDeleteProgram(e->shader_program);
|
||||
}
|
||||
|
||||
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
|
||||
{
|
||||
t_env *e = (t_env *)glfwGetWindowUserPointer(window);
|
||||
if (key == GLFW_KEY_Q && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||
e->translate.x += 0.03;
|
||||
if (key == GLFW_KEY_A && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||
e->translate.x -= 0.03;
|
||||
if (key == GLFW_KEY_W && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||
e->translate.y += 0.03;
|
||||
if (key == GLFW_KEY_S && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||
e->translate.y -= 0.03;
|
||||
if (key == GLFW_KEY_E && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||
e->translate.z += 0.03;
|
||||
if (key == GLFW_KEY_D && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||
e->translate.z -= 0.03;
|
||||
if (key == GLFW_KEY_R && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||
e->rotate.x += 0.03;
|
||||
if (key == GLFW_KEY_F && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||
e->rotate.x -= 0.03;
|
||||
if (key == GLFW_KEY_T && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||
e->rotate.y += 0.03;
|
||||
if (key == GLFW_KEY_G && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||
e->rotate.y -= 0.03;
|
||||
if (key == GLFW_KEY_Y && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||
e->rotate.z += 0.03;
|
||||
if (key == GLFW_KEY_H && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||
e->rotate.z -= 0.03;
|
||||
if (key == GLFW_KEY_U && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||
e->scale.x += 0.03;
|
||||
if (key == GLFW_KEY_J && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||
e->scale.x -= 0.03;
|
||||
if (key == GLFW_KEY_I && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||
e->scale.y += 0.03;
|
||||
if (key == GLFW_KEY_K && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||
e->scale.y -= 0.03;
|
||||
if (key == GLFW_KEY_O && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||
e->scale.z += 0.03;
|
||||
if (key == GLFW_KEY_L && (action == GLFW_PRESS || action == GLFW_REPEAT))
|
||||
e->scale.z -= 0.03;
|
||||
(void)scancode;
|
||||
(void)mods;
|
||||
}
|
||||
|
||||
int run(t_env *e)
|
||||
{
|
||||
GLFWwindow* window;
|
||||
|
@ -202,6 +248,8 @@ int run(t_env *e)
|
|||
|
||||
/* Create a windowed mode window and its OpenGL context */
|
||||
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
|
||||
glfwSetWindowUserPointer(window, e);
|
||||
glfwSetKeyCallback(window, key_callback);
|
||||
if (!window)
|
||||
{
|
||||
glfwTerminate();
|
||||
|
@ -229,7 +277,7 @@ int run(t_env *e)
|
|||
/* Render here */
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
RenderSceneCB();
|
||||
RenderSceneCB(e);
|
||||
/* Swap front and back buffers */
|
||||
glfwSwapBuffers(window);
|
||||
|
||||
|
|
Loading…
Reference in New Issue