using orthographic matrix
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 12 Sep 2020 02:18:10 +0000 (21:18 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sat, 12 Sep 2020 02:18:10 +0000 (21:18 -0500)
src/gfx/quad_renderer.onyx
src/main.onyx
src/shaders/basic.vert
tags

index 1bd9c203507c6a099594bedef9cff747bf49fa89..5af29244cbba628bea995300cbc4a1ac0cd6e772 100644 (file)
@@ -4,14 +4,14 @@ use package core
 use package gl as gl
 use package gl_utils as gl_utils
 
+use package main { window_width, window_height }
+
 Vec2 :: struct {
     x : f32;
     y : f32;
 }
 
 QuadRenderer :: struct {
-    is_initialized : bool = false;
-
     quad_data   : [..] Quad;
 
     vertexArray  : gl.GLVertexArrayObject;
@@ -21,6 +21,8 @@ QuadRenderer :: struct {
 
     program : gl.GLProgram;
 
+    u_proj_loc : gl.GLUniformLocation;
+
     is_data_dirty : bool = false;
 }
 
@@ -87,16 +89,10 @@ quad_renderer_init :: proc (use qr: ^QuadRenderer, initial_quads := 10) {
         data = cast(^void) quad_data.data,
     }, gl.DYNAMIC_DRAW);
 
-    gl.enableVertexAttribArray(1);
-    gl.enableVertexAttribArray(2);
-    gl.enableVertexAttribArray(3);
-    gl.enableVertexAttribArray(4);
-    gl.enableVertexAttribArray(5);
-    gl.vertexAttribDivisor(1, 1);
-    gl.vertexAttribDivisor(2, 1);
-    gl.vertexAttribDivisor(3, 1);
-    gl.vertexAttribDivisor(4, 1);
-    gl.vertexAttribDivisor(5, 1);
+    for i: 1 .. 6 {
+        gl.enableVertexAttribArray(i);
+        gl.vertexAttribDivisor(i, 1);
+    }
     gl.vertexAttribPointer(1, 2, gl.FLOAT, false, sizeof Quad, 0);
     gl.vertexAttribPointer(2, 2, gl.FLOAT, false, sizeof Quad, 2 * sizeof f32);
     gl.vertexAttribPointer(3, 2, gl.FLOAT, false, sizeof Quad, 4 * sizeof f32);
@@ -126,19 +122,8 @@ quad_renderer_init :: proc (use qr: ^QuadRenderer, initial_quads := 10) {
     program          = gl_utils.link_program(vertex_shader, fragment_shader);
     gl.useProgram(program);
 
-    u_proj_loc := gl.getUniformLocation(program, "u_proj");
-    proj_mat : [9] gl.GLfloat;
-    proj_mat[3 * 0 + 0] = 2.0f;
-    proj_mat[3 * 0 + 1] = 0.0f;
-    proj_mat[3 * 0 + 2] = 0.0f;
-    proj_mat[3 * 1 + 0] = 0.0f;
-    proj_mat[3 * 1 + 1] = -2.0f;
-    proj_mat[3 * 1 + 2] = 0.0f;
-    proj_mat[3 * 2 + 0] = -1.0f;
-    proj_mat[3 * 2 + 1] = 1.0f;
-    proj_mat[3 * 2 + 2] = 1.0f;
-
-    gl.uniformMatrix3(u_proj_loc, false, proj_mat);
+    u_proj_loc = gl.getUniformLocation(program, "u_proj");
+    quad_renderer_update_view(qr);
 }
 
 quad_renderer_draw :: proc (use qr: ^QuadRenderer) {
@@ -147,6 +132,29 @@ quad_renderer_draw :: proc (use qr: ^QuadRenderer) {
     gl.bindVertexArray(-1);
 }
 
+quad_renderer_update_view :: proc (use qr: ^QuadRenderer) {
+    proj_mat : [4 * 4] gl.GLfloat;
+    for ^it: proj_mat do *it = 0.0f;
+
+    // Orthographic projection matrix samelessly stolen from:
+    //     https://en.wikipedia.org/wiki/Orthographic_projection
+    r :: cast(f32) window_width;
+    l :: 0.0f;
+    t :: 0.0f;
+    b :: cast(f32) window_height;
+
+    proj_mat[0 * 4 + 0] = 2.0f / (r - l);
+    proj_mat[0 * 4 + 3] = -(r + l) / (r - l);
+    proj_mat[1 * 4 + 1] = 2.0f / (t - b);
+    proj_mat[1 * 4 + 3] = -(t + b) / (t - b);
+    proj_mat[2 * 4 + 2] = -2.0f;
+    proj_mat[2 * 4 + 3] = -1.0f;
+    proj_mat[3 * 4 + 3] = 1.0f;
+
+    // This matrix uses the wrong major axis so a transpose is necessary.
+    gl.uniformMatrix4(u_proj_loc, true, proj_mat);
+}
+
 quad_update_at_index :: proc (use qr: ^QuadRenderer, idx: i32, quad: Quad) {
     quad_data[idx] = quad;
     is_data_dirty = true;
index 76154fab7f6d24415b47e9f8646b1490aec686d5..64b8272c7d6fd33769a6ebea2b4d40b7ab630759 100644 (file)
@@ -19,7 +19,7 @@ NUM_QUADS :: 1 << 7
 
 GameState :: struct {
     quad_renderer : QuadRenderer;
-    input_state : input.InputState;
+    input_state   : input.InputState;
 
     player : Player;
 }
@@ -49,6 +49,8 @@ poll_events :: proc (use gs: ^GameState) {
 
             gl.canvasSize(window_width, window_height);
             gl.viewport(0, 0, window_width, window_height);
+
+            quad_renderer_update_view(^quad_renderer);
         }
     }
 }
@@ -58,36 +60,18 @@ update :: proc (use gs: ^GameState) {
     defer input.postupdate(^input_state);
     poll_events(gs);
 
-    player_speed :: 0.004f;
+    player_speed :: 4f;
 
     if input.key_down(^input_state, Key.ArrowUp)    do player.y -= player_speed;
     if input.key_down(^input_state, Key.ArrowDown)  do player.y += player_speed;
     if input.key_down(^input_state, Key.ArrowLeft)  do player.x -= player_speed;
     if input.key_down(^input_state, Key.ArrowRight) do player.x += player_speed;
 
-    /* for i: 0 .. NUM_QUADS {
-        t := random_float();
-        d := random_float(0.8f, 1.0f);
-
-        quad_update_at_index(^quad_renderer, i, Quad.{
-            x = d * cos(t * 2.0f * PI) / 2.2f + 0.5f,
-            y = d * sin(t * 2.0f * PI) / 2.2f + 0.5f,
-
-            w = random_float(0.006f, 0.01f),
-            h = random_float(0.006f, 0.01f),
-
-            r = random_float(),
-            g = random_float(0.6f, 1.0f),
-            b = random_float(0.6f, 1.0f),
-            a = 1.0f
-        });
-    } */
-
     quad_update_at_index(^quad_renderer, NUM_QUADS - 1, Quad.{
-        x = cast(f32) input_state.mouse.x / cast(f32) window_width - 0.025f,
-        y = cast(f32) input_state.mouse.y / cast(f32) window_height - 0.025f,
+        x = input_state.mouse.x,
+        y = cast(f32) input_state.mouse.y,
 
-        w = 0.05f, h = 0.05f,
+        w = 32.0f, h = 32.0f,
 
         u = 0.25f, v = 0.25f, tw = 0.25f, th = 0.25f,
 
@@ -98,7 +82,7 @@ update :: proc (use gs: ^GameState) {
         x = player.x,
         y = player.y,
 
-        w = 0.1f, h = 0.1f,
+        w = 100.0f, h = 100.0f,
 
         u = 0.0f, v = 0.0f, tw = 1.0f, th = 1.0f,
 
index f467066df068ed54cf6c2cc61eae585cb418be5b..cb8a34b1b206504161113f913cd0b5113fce12fe 100644 (file)
@@ -10,13 +10,13 @@ layout(location = 3) in vec2 a_tex_pos;
 layout(location = 4) in vec2 a_tex_size;
 layout(location = 5) in vec4 a_col;
 
-uniform mat3 u_proj;
+uniform mat4 u_proj;
 
 out vec2 v_tex_pos;
 out vec4 v_col;
 
 void main() {
-       gl_Position = vec4(u_proj * vec3(a_vert_pos * a_size + a_pos, 1), 1);
+       gl_Position = u_proj * vec4(a_vert_pos * a_size + a_pos, 0, 1);
     v_col = a_col;
 
     v_tex_pos = a_tex_pos + a_vert_pos * a_tex_size;
diff --git a/tags b/tags
index 9bae4261636860e2ebc0302445db9cc11d6a7cae..175ecf3ea4fd9730874099266f87c1045701ffbb 100644 (file)
--- a/tags
+++ b/tags
@@ -309,7 +309,7 @@ NICEST      /usr/share/onyx/core/js/webgl.onyx      /^NICEST                         :: 0x
 NONE   /usr/share/onyx/core/js/webgl.onyx      /^NONE                           :: 0$/
 NOTEQUAL       /usr/share/onyx/core/js/webgl.onyx      /^NOTEQUAL                       :: 0x0205$/
 NO_ERROR       /usr/share/onyx/core/js/webgl.onyx      /^NO_ERROR                       :: 0$/
-NUM_QUADS      src/main.onyx   /^NUM_QUADS :: 100$/
+NUM_QUADS      src/main.onyx   /^NUM_QUADS :: 1 << 7$/
 OBJECT_TYPE    /usr/share/onyx/core/js/webgl.onyx      /^OBJECT_TYPE                                   :: 0x9112$/
 ONE    /usr/share/onyx/core/js/webgl.onyx      /^ONE                            :: 1$/
 ONE_MINUS_CONSTANT_ALPHA       /usr/share/onyx/core/js/webgl.onyx      /^ONE_MINUS_CONSTANT_ALPHA       :: 0x8004$/
@@ -332,6 +332,7 @@ POINTS      /usr/share/onyx/core/js/webgl.onyx      /^POINTS                         :: 0x
 POLYGON_OFFSET_FACTOR  /usr/share/onyx/core/js/webgl.onyx      /^POLYGON_OFFSET_FACTOR          :: 0x8038$/
 POLYGON_OFFSET_FILL    /usr/share/onyx/core/js/webgl.onyx      /^POLYGON_OFFSET_FILL            :: 0x8037$/
 POLYGON_OFFSET_UNITS   /usr/share/onyx/core/js/webgl.onyx      /^POLYGON_OFFSET_UNITS           :: 0x2A00$/
+Player src/main.onyx   /^Player :: struct {$/
 PtrMap /usr/share/onyx/core/ptrmap.onyx        /^PtrMap :: struct {$/
 PtrMapEntry    /usr/share/onyx/core/ptrmap.onyx        /^PtrMapEntry :: struct {$/
 PtrMapLookupResult     /usr/share/onyx/core/ptrmap.onyx        /^PtrMapLookupResult :: struct {$/
@@ -662,6 +663,7 @@ bufferDataNoData    /usr/share/onyx/core/js/webgl.onyx      /^bufferDataNoData
 bufferDataWithData     /usr/share/onyx/core/js/webgl.onyx      /^bufferDataWithData             :: proc (target: GLenum, buffer: Buffer, usage: GLenum) #foreign "gl" "bufferDataWithData" ---$/
 bufferSubData  /usr/share/onyx/core/js/webgl.onyx      /^bufferSubData                  :: proc (target: GLenum, offset: GLsizei, data: Buffer) #foreign "gl" "bufferSubData" ---$/
 calloc /usr/share/onyx/core/builtin.onyx       /^calloc  :: proc (size: u32) -> rawptr do return alloc(context.allocator, size);$/
+canvasSize     /usr/share/onyx/core/js/webgl.onyx      /^canvasSize                     :: proc (width: GLsizei, height: GLsizei) #foreign "gl" "canvasSize" ---$/
 ceil_f32       /usr/share/onyx/core/intrinsics.onyx    /^ceil_f32     :: proc (val: f32) -> f32 #intrinsic ---$/
 ceil_f64       /usr/share/onyx/core/intrinsics.onyx    /^ceil_f64     :: proc (val: f64) -> f64 #intrinsic ---$/
 cfree  /usr/share/onyx/core/builtin.onyx       /^cfree   :: proc (ptr: rawptr) do free(context.allocator, ptr);$/
@@ -901,6 +903,7 @@ vertexAttrib4f      /usr/share/onyx/core/js/webgl.onyx      /^vertexAttrib4f
 vertexAttribDivisor    /usr/share/onyx/core/js/webgl.onyx      /^vertexAttribDivisor            :: proc (idx: GLuint, divisor: GLuint) #foreign "gl" "vertexAttribDivisor" ---$/
 vertexAttribPointer    /usr/share/onyx/core/js/webgl.onyx      /^vertexAttribPointer            :: proc (idx: GLuint, size: GLint, type: GLenum, normalized: GLboolean, stride: GLsizei, offset: GLint) #foreign "gl" "vertexAttribPointer" ---$/
 viewport       /usr/share/onyx/core/js/webgl.onyx      /^viewport                       :: proc (x: GLint, y: GLint, width: GLsizei, height: GLsizei) #foreign "gl" "viewport" ---      $/
-window_width   src/main.onyx   /^window_width := 0$/
+window_height  src/main.onyx   /^window_height := 0$/
+window_width   src/main.onyx   /^window_width  := 0$/
 xor_i32        /usr/share/onyx/core/intrinsics.onyx    /^xor_i32      :: proc (lhs: i32, rhs: i32) -> i32 #intrinsic ---$/
 xor_i64        /usr/share/onyx/core/intrinsics.onyx    /^xor_i64      :: proc (lhs: i64, rhs: i64) -> i64 #intrinsic ---$/