From a0348fd64b7416e583eae3d97bb56cb95300f63f Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Sun, 18 Oct 2020 14:10:39 -0500 Subject: [PATCH] added orthographic matrix --- include/log.h | 2 +- include/vecmath.h | 9 ++++++ res/shaders/planet_vert.glsl | 4 ++- src/log.c | 6 ++-- src/sim.c | 27 ++++++++++------- src/utils.c | 3 +- src/vecmath.c | 58 ++++++++++++++++++++++++++++++------ 7 files changed, 85 insertions(+), 24 deletions(-) diff --git a/include/log.h b/include/log.h index 1860941..9f29b7b 100644 --- a/include/log.h +++ b/include/log.h @@ -14,4 +14,4 @@ extern LogLevel LOG_LEVEL_MINIMUM; void logprint(LogLevel level, const char* format, ...); void logvprint(LogLevel level, const char* format, va_list va); -#endif //LOG_H +#endif //LOG_H \ No newline at end of file diff --git a/include/vecmath.h b/include/vecmath.h index 8853176..cf40c7e 100644 --- a/include/vecmath.h +++ b/include/vecmath.h @@ -14,5 +14,14 @@ f32 v2f_mag (V2f a); V2f v2f_norm(V2f a); V2f v2f_proj(V2f a, V2f onto); +typedef float mat4[16]; + +void mat4_identity(mat4 mat); +void mat4_ortho(mat4 mat, + f32 left, f32 right, + f32 top, f32 bottom, + f32 near, f32 far); +void mat4_proj(mat4 mat); + #endif //VECMATH_H diff --git a/res/shaders/planet_vert.glsl b/res/shaders/planet_vert.glsl index 594d966..67afc25 100644 --- a/res/shaders/planet_vert.glsl +++ b/res/shaders/planet_vert.glsl @@ -6,6 +6,8 @@ layout(location = 0) in vec2 a_shape_pos; layout(location = 1) in vec2 a_obj_pos; layout(location = 2) in float a_mass; +uniform mat4 u_proj; + void main() { - gl_Position = vec4(a_shape_pos, 0, 1); + gl_Position = u_proj * vec4(a_shape_pos, 0, 1); } \ No newline at end of file diff --git a/src/log.c b/src/log.c index e3d49c0..36218c2 100644 --- a/src/log.c +++ b/src/log.c @@ -12,7 +12,8 @@ internal const char* log_level_strs[LOG_LEVEL_COUNT] = { "ERROR", }; -void logprint(LogLevel level, const char *format, ...) { +void +logprint(LogLevel level, const char *format, ...) { va_list va; va_start(va, format); logvprint(level, format, va); @@ -20,7 +21,8 @@ void logprint(LogLevel level, const char *format, ...) { } // NOTE(Brendan): This always prints a newline. -void logvprint(LogLevel level, const char* format, va_list va) { +void +logvprint(LogLevel level, const char* format, va_list va) { if (level < LOG_LEVEL_MINIMUM) return; printf("[%s] ", log_level_strs[level]); diff --git a/src/sim.c b/src/sim.c index 39b48b2..4929b5c 100644 --- a/src/sim.c +++ b/src/sim.c @@ -56,9 +56,9 @@ init_glfw() { glfwSetKeyCallback(window, glfw_key_handler); glfwSetFramebufferSizeCallback(window, glfw_resize_handler); - glEnable(GL_CULL_FACE); - glFrontFace(GL_CCW); - glCullFace(GL_BACK); + //glEnable(GL_CULL_FACE); + //glFrontFace(GL_CW); + //glCullFace(GL_BACK); } internal GLuint @@ -144,8 +144,8 @@ create_circle_mesh() { V2f circle_points[CIRCLE_POINT_COUNT] = {}; foreach (i, 0, CIRCLE_POINT_COUNT) { f32 t = (f32) i / (f32) CIRCLE_POINT_COUNT; - circle_points[i].x = cos(t * 2 * PI) / 2.0f; - circle_points[i].y = sin(t * 2 * PI) / 2.0f; + circle_points[i].x = cos(t * 2 * PI) * 100.0f + 400.0f; + circle_points[i].y = sin(t * 2 * PI) * 100.0f + 200.0f; } GLsizei vertex_buffer; @@ -213,18 +213,25 @@ loop() { } } -i32 main(i32 argc, char* argv[]) { +i32 +main(i32 argc, char* argv[]) { init_glfw(); circle_mesh = create_circle_mesh(NULL); GLuint v_shader = load_shader(GL_VERTEX_SHADER, "res/shaders/planet_vert.glsl"); GLuint f_shader = load_shader(GL_FRAGMENT_SHADER, "res/shaders/planet_frag.glsl"); GLuint program = create_program(v_shader, f_shader); - glUseProgram(program); - loop(); - deinit_opengl(); + mat4 ortho_mat; + mat4_ortho(ortho_mat, 0, 800, 0, 600, 0.0f, 100.0f); + + GLuint ortho_mat_loc = glGetUniformLocation(program, "u_proj"); + glUniformMatrix4fv(ortho_mat_loc, 1, false, (f32 *) ortho_mat); + + loop(); + + deinit_opengl(); - return 0; + return 0; } \ No newline at end of file diff --git a/src/utils.c b/src/utils.c index 7cbace5..254ed67 100644 --- a/src/utils.c +++ b/src/utils.c @@ -4,7 +4,8 @@ #include "log.h" -void panic_and_die(const char* msg, ...) { +void +panic_and_die(const char* msg, ...) { puts("************ PANIC ************"); va_list va; diff --git a/src/vecmath.c b/src/vecmath.c index 8f9382c..a523fa3 100644 --- a/src/vecmath.c +++ b/src/vecmath.c @@ -1,36 +1,76 @@ #include "vecmath.h" #include -V2f v2f_add(V2f a, V2f b) { +#include "utils.h" + +V2f +v2f_add(V2f a, V2f b) { return (V2f) { .x = a.x + b.x, .y = a.y + b.y }; } -V2f v2f_sub(V2f a, V2f b) { +V2f +v2f_sub(V2f a, V2f b) { return (V2f) { .x = a.x - b.x, .y = a.y - b.y }; } -V2f v2f_mul(V2f a, f32 scalar) { +V2f +v2f_mul(V2f a, f32 scalar) { return (V2f) { .x = a.x * scalar, .y = a.y * scalar }; } -f32 v2f_dot(V2f a, V2f b) { +f32 +v2f_dot(V2f a, V2f b) { return a.x * b.x + a.y * b.y; } -f32 v2f_smag(V2f a) { +f32 +v2f_smag(V2f a) { return v2f_dot(a, a); } -f32 v2f_mag(V2f a) { +f32 +v2f_mag(V2f a) { return sqrt(v2f_smag(a)); } -V2f v2f_norm(V2f a) { +V2f +v2f_norm(V2f a) { const f32 mag = v2f_mag(a); return v2f_mul(a, 1.0f / mag); } -V2f v2f_proj(V2f a, V2f onto) { +V2f +v2f_proj(V2f a, V2f onto) { const f32 dp = v2f_dot(a, onto) / v2f_mag(onto); return v2f_mul(onto, dp); -} \ No newline at end of file +} + + + +void +mat4_identity(mat4 mat) +{ + foreach(i, 0, 16) mat[i] = 0.0f; + mat[0 * 4 + 0] = 1.0f; + mat[1 * 4 + 1] = 1.0f; + mat[2 * 4 + 2] = 1.0f; + mat[3 * 4 + 3] = 1.0f; +} + +void +mat4_ortho(mat4 mat, + f32 left, f32 right, + f32 top, f32 bottom, + f32 near, f32 far) +{ + foreach(i, 0, 16) mat[i] = 0.0; + + mat[0 * 4 + 0] = 2.0f / (right - left); + mat[1 * 4 + 1] = 2.0f / (top - bottom); + mat[2 * 4 + 2] = -2.0f / (far - near); + mat[3 * 4 + 3] = 1.0f; + + mat[3 * 4 + 0] = -(right + left) / (right - left); + mat[3 * 4 + 1] = -(top + bottom) / (top - bottom); + mat[3 * 4 + 2] = -(far + near) / (far - near); +} -- 2.25.1