From 71c9d90ceeb0fe059e1c869a0c416ea5a22be3ae Mon Sep 17 00:00:00 2001 From: gbrochar Date: Wed, 23 Dec 2020 22:23:23 +0100 Subject: [PATCH] matrices not working --- inc/mat4.h | 10 +++++++++- inc/scop.h | 4 ++-- src/run.c | 42 +++++++++++++++++++++++++++++++++++------- src/shader.vs | 6 +++++- 4 files changed, 51 insertions(+), 11 deletions(-) diff --git a/inc/mat4.h b/inc/mat4.h index a734bfe..bde10cd 100644 --- a/inc/mat4.h +++ b/inc/mat4.h @@ -6,7 +6,7 @@ /* By: gbrochar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/22 20:35:00 by gbrochar #+# #+# */ -/* Updated: 2020/12/23 20:17:35 by gbrochar ### ########.fr */ +/* Updated: 2020/12/23 22:05:17 by gbrochar ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ # define MAT4_H # include "vec4.h" +# include "vec3.h" # include typedef struct s_mat4 t_mat4; @@ -43,6 +44,13 @@ t_mat4 mat4_frustrum( t_persp_tool tool, double near, double far); t_mat4 mat4_frustrum_tmp(double x, double y, t_vec4 tool); +t_mat4 mat4_lookat(t_vec3 eye, t_vec3 up, t_vec3 target); +t_mat4 mat4_lookat_make_mat( + t_vec3 x, t_vec3 y, t_vec3 z, t_vec3 eye); +t_vec3 mat4_lookat_make_y(t_vec3 x, t_vec3 z); +t_vec3 mat4_lookat_make_x(t_vec3 z, t_vec3 up); +t_vec3 mat4_lookat_make_z(t_vec3 eye, t_vec3 target); + t_mat4 mat4_identity(void); t_mat4 mat4_inverse(t_mat4 m); diff --git a/inc/scop.h b/inc/scop.h index 56c014f..7db2995 100644 --- a/inc/scop.h +++ b/inc/scop.h @@ -6,7 +6,7 @@ /* By: gbrochar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/22 12:08:22 by gbrochar #+# #+# */ -/* Updated: 2020/12/23 21:16:08 by gbrochar ### ########.fr */ +/* Updated: 2020/12/23 21:55:38 by gbrochar ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ # include # include # include - +# include # include # include diff --git a/src/run.c b/src/run.c index 7e99ac0..52f46f5 100644 --- a/src/run.c +++ b/src/run.c @@ -6,16 +6,20 @@ /* By: gbrochar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/23 19:38:05 by gbrochar #+# #+# */ -/* Updated: 2020/12/23 21:25:26 by gbrochar ### ########.fr */ +/* Updated: 2020/12/23 22:22:44 by gbrochar ### ########.fr */ /* */ /* ************************************************************************** */ #include "scop.h" - +#include "mat4.h" +#include "vec3.h" GLuint VBO; GLuint IBO; GLuint gScaleLocation; +GLuint gProjLocation; +GLuint gViewLocation; +GLuint gModelLocation; const char* pVSFileName = "src/shader.vs"; const char* pFSFileName = "src/shader.fs"; @@ -28,7 +32,27 @@ static void RenderSceneCB() Scale += 0.01f; - glUniform1f(gScaleLocation, sinf(Scale)); + t_vec3 eye; + t_vec3 up; + t_vec3 target; + + eye.x = 0; + eye.y = 0; + eye.z = 10; + up.x = 0; + up.y = 1; + up.z = 0; + target.x = 0; + target.y = 0; + target.z = 9; + + t_mat4 proj = mat4_perspective(1., 800.0 / 600.0, 0.001, 1000); + t_mat4 view = mat4_inverse(mat4_lookat(eye, up, target)); + t_mat4 model = mat4_identity(); + //glUniform1f(gScaleLocation, sinf(Scale)); + glUniformMatrix4fv(gProjLocation, 1, GL_FALSE, (GLfloat *)proj.data); + glUniformMatrix4fv(gViewLocation, 1, GL_FALSE, (GLfloat *)view.data); + glUniformMatrix4fv(gModelLocation, 1, GL_FALSE, (GLfloat *)model.data); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, VBO); @@ -121,8 +145,6 @@ static void CompileShaders() strcat(fs, buffer); } fclose(fp); - printf("%s", vs); - printf("%s", fs); AddShader(ShaderProgram, vs, GL_VERTEX_SHADER); AddShader(ShaderProgram, fs, GL_FRAGMENT_SHADER); @@ -149,8 +171,14 @@ static void CompileShaders() glUseProgram(ShaderProgram); - gScaleLocation = glGetUniformLocation(ShaderProgram, "gScale"); - // assert(gScaleLocation != 0xFFFFFFFF); + //gScaleLocation = glGetUniformLocation(ShaderProgram, "gScale"); + gProjLocation = glGetUniformLocation(ShaderProgram, "proj"); + gViewLocation = glGetUniformLocation(ShaderProgram, "view"); + gModelLocation = glGetUniformLocation(ShaderProgram, "model"); + //assert(gScaleLocation != 0xFFFFFFFF); + assert(gProjLocation != 0xFFFFFFFF); + assert(gViewLocation != 0xFFFFFFFF); + assert(gModelLocation != 0xFFFFFFFF); } void run(t_env *e) diff --git a/src/shader.vs b/src/shader.vs index 267f760..d119ba3 100644 --- a/src/shader.vs +++ b/src/shader.vs @@ -3,11 +3,15 @@ layout (location = 0) in vec3 Position; uniform float gScale; +uniform mat4 view; +uniform mat4 proj; +uniform mat4 model; flat out int vID; void main() { - gl_Position = vec4(gScale * Position.x,gScale * Position.y, gScale * Position.z, 1.0); + gl_Position = proj * view * model * vec4(Position, 1.0); + // gl_Position = vec4(gScale * Position.x,gScale * Position.y, gScale * Position.z, 1.0); vID = gl_VertexID; }