From e2b1d027d8754673558b21c071c3159835502cea Mon Sep 17 00:00:00 2001 From: Eric-Paul Ickhorn Date: Tue, 10 Oct 2023 17:06:01 +0200 Subject: [PATCH] Got projection and view matrices working --- code/source-c/main.c | 168 +++++++++++++++++++++++++++++++++++++------ vertex_shader.glsl | 5 +- 2 files changed, 150 insertions(+), 23 deletions(-) diff --git a/code/source-c/main.c b/code/source-c/main.c index 087963f..760a469 100644 --- a/code/source-c/main.c +++ b/code/source-c/main.c @@ -7,6 +7,7 @@ SDL_Window *window = NULL; SDL_GLContext gl_context; int main_shader = 0; mat4x4 projection_matrix; +mat4x4 view_matrix; float triangle_vertices[9] = { 0.0f, 0.0f, -1.0f, @@ -22,6 +23,9 @@ typedef struct tc_vec3 } tc_vec3_s; +tc_vec3_s tc_camera_position = { 0.0f, 0.0f, 0.0f }; +tc_vec3_s tc_camera_rotation = { 0.0f, 0.0f, 0.0f }; + typedef struct tc_object { int vbo; @@ -149,16 +153,156 @@ void render() float color[4] = { 0.0f, 1.0f, 0.0f, 1.0f }; glClearBufferfv(GL_COLOR, 0, color); + mat4x4_perspective(projection_matrix, 120, 1200 / 800, 0.0001f, 1000.0f); + mat4x4_translate(view_matrix, -tc_camera_position.x, -tc_camera_position.y, tc_camera_position.z); + glUseProgram(main_shader); + int projection_uniform_location = glGetUniformLocation(main_shader, "projection_matrix"); - printf("%d\n", projection_uniform_location); glUniformMatrix4fv(projection_uniform_location, 1, GL_FALSE, &projection_matrix[0][0]); + int view_uniform_location = glGetUniformLocation(main_shader, "view_matrix"); + glUniformMatrix4fv(view_uniform_location, 1, GL_FALSE, &view_matrix[0][0]); + // printf("%d\n", view_uniform_location); + + // mat4x4_rotate_X(view_matrix, view_matrix, tc_camera_rotation.x); + // mat4x4_rotate_Y(view_matrix, view_matrix, tc_camera_rotation.y); + // mat4x4_rotate_Z(view_matrix, view_matrix, tc_camera_rotation.z); + render_block(block); SDL_GL_SwapWindow(window); } +bool shift_pressed = false; +bool go_up = false; +bool go_left = false; +bool go_right = false; +bool go_forward = false; +bool go_backwards = false; + +bool update() +{ + + SDL_Event event; + while(SDL_PollEvent(&event)) + { + switch(event.type) + { + case SDL_QUIT: + SDL_HideWindow(window); + return false; + break; + + case SDL_WINDOWEVENT: + { + if(event.window.event == SDL_WINDOWEVENT_RESIZED) + { + puts("RESIZED"); + } + } break; + case SDL_KEYDOWN: + + if(event.key.keysym.scancode == SDL_SCANCODE_W) + { + go_forward = true; + } + + if(event.key.keysym.scancode == SDL_SCANCODE_S) + { + go_backwards = true; + } + + if(event.key.keysym.scancode == SDL_SCANCODE_A) + { + go_left = true; + } + + if(event.key.keysym.scancode == SDL_SCANCODE_D) + { + go_right = true;; + } + + if(event.key.keysym.scancode == SDL_SCANCODE_SPACE) + { + go_up = true; + } + + if(event.key.keysym.scancode == SDL_SCANCODE_LSHIFT) + { + shift_pressed = true; + } + + break; + + case SDL_KEYUP: + + if(event.key.keysym.scancode == SDL_SCANCODE_W) + { + go_forward = false; + } + + if(event.key.keysym.scancode == SDL_SCANCODE_S) + { + go_backwards = false; + } + + if(event.key.keysym.scancode == SDL_SCANCODE_A) + { + go_left = false; + } + + if(event.key.keysym.scancode == SDL_SCANCODE_D) + { + go_right = false;; + } + + if(event.key.keysym.scancode == SDL_SCANCODE_SPACE) + { + go_up = false; + } + + if(event.key.keysym.scancode == SDL_SCANCODE_LSHIFT) + { + shift_pressed = false; + } + + break; + } + } + + if(shift_pressed) + { + tc_camera_position.y -= 0.25; + } + + if(go_up) + { + tc_camera_position.y += 0.25; + } + + if(go_left) + { + tc_camera_position.x -= 0.25; + } + + if(go_right) + { + tc_camera_position.x += 0.25; + } + + if(go_forward) + { + tc_camera_position.z += 0.25; + } + + if(go_backwards) + { + tc_camera_position.z -= 0.25; + } + return true; +} + int main(int argc, char **argv) { SDL_Init(SDL_INIT_EVERYTHING); @@ -176,7 +320,6 @@ int main(int argc, char **argv) gladLoadGLLoader(&SDL_GL_GetProcAddress); gladLoadGL(); - mat4x4_perspective(projection_matrix, 120, 1200 / 800, 0.0001f, 1000.0f); main_shader = setup_shaders("vertex_shader.glsl", "fragment_shader.glsl"); block = tc_new_block_at_3f(0.0, 0.0, 0.0); @@ -184,27 +327,10 @@ int main(int argc, char **argv) int64_t frame_index = 1; while(frame_index > 0) { - SDL_Event event; - while(SDL_PollEvent(&event)) + if(!update()) { - switch(event.type) - { - case SDL_QUIT: - SDL_HideWindow(window); - frame_index = -1; - break; - break; - - case SDL_WINDOWEVENT: - { - if(event.window.event == SDL_WINDOWEVENT_RESIZED) - { - puts("RESIZED"); - } - } break; - } + frame_index = -1; } - render(); if(frame_index == 1) diff --git a/vertex_shader.glsl b/vertex_shader.glsl index 8b47d36..dded497 100644 --- a/vertex_shader.glsl +++ b/vertex_shader.glsl @@ -3,9 +3,10 @@ layout(location=0) in vec3 pos; uniform mat4 projection_matrix; +uniform mat4 view_matrix; +uniform mat4 model_matrix; void main() { - gl_Position = vec4(pos, 1.0) * projection_matrix; -} + gl_Position = projection_matrix * view_matrix * vec4(pos, 1.0);