added orthographic matrix
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 18 Oct 2020 19:10:39 +0000 (14:10 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 18 Oct 2020 19:10:39 +0000 (14:10 -0500)
include/log.h
include/vecmath.h
res/shaders/planet_vert.glsl
src/log.c
src/sim.c
src/utils.c
src/vecmath.c

index 18609414c76f3ead2ef658478b56e75cab832bc5..9f29b7bdd3f334749d6bd832dc68b02b50f02a83 100644 (file)
@@ -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
index 88531765a61e6454a6296149d04944b0e2f52888..cf40c7e1f8138a9d015ff39883834392e13ee4e5 100644 (file)
@@ -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
index 594d9662c3846cf09b1d45fbb09682cc848a68fd..67afc250294a01e41989dba8554a7ee6ef88217f 100644 (file)
@@ -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
index e3d49c02bf56f5201464d642ffb14990e63b5cc1..36218c2f2e1936dbe14226183ad9c350244c995f 100644 (file)
--- 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]);
index 39b48b275649ebcf087160a25652aa913ae773ea..4929b5ccf43eaa7e7bcd31cd0011f5f81e218f5e 100644 (file)
--- 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
index 7cbace5d533e9934bd302de5fce528bb46f29190..254ed677401db0cd6c17f53aba62a601661ea455 100644 (file)
@@ -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;
index 8f9382c7447f1d151113f94f5403f634e4a9b6a7..a523fa3ce3994ba6c3cc05f9ecce2d6c72a3c398 100644 (file)
@@ -1,36 +1,76 @@
 #include "vecmath.h"
 #include <math.h>
 
-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);
+}