Commit aa2bb65eebf2b29a6cca371cd58b63f43f47c28c

Thomas de Grivel 2024-01-08T21:43:05

wip ortho

diff --git a/.ic3_history b/.ic3_history
index 0bc63d8..6f974c0 100644
--- a/.ic3_history
+++ b/.ic3_history
@@ -1,7 +1,3 @@
-"\U+1FAB0"
-1 + 10000000000000000000000000000
-Map.map
-Map.to_list
 make
 %GL.Sphere{}
 a = (U8) {0, 1}
@@ -97,3 +93,7 @@ facts_next_id
 facts_next_id()
 4 * 27
 24 * 7
+4.0/1920.0
+4.0 / 1920.0
+4.0f / 1920.0f
+4.0f / 1200.0f
diff --git a/libc3/window/sdl2/demo/lightspeed.c b/libc3/window/sdl2/demo/lightspeed.c
index 0e82aac..88bff51 100644
--- a/libc3/window/sdl2/demo/lightspeed.c
+++ b/libc3/window/sdl2/demo/lightspeed.c
@@ -19,6 +19,7 @@
 #include "lightspeed.h"
 
 s_gl_lines g_lines_stars = {0};
+s_gl_ortho g_ortho_lightspeed = {0};
 
 static void star_init (s_tag *star)
 {
diff --git a/libc3/window/sdl2/demo/window_sdl2_demo.c b/libc3/window/sdl2/demo/window_sdl2_demo.c
index 91ce296..65cd7d8 100644
--- a/libc3/window/sdl2/demo/window_sdl2_demo.c
+++ b/libc3/window/sdl2/demo/window_sdl2_demo.c
@@ -151,7 +151,7 @@ bool window_sdl2_demo_load (s_window_sdl2 *window)
   }
   if (! gl_ortho_init(&g_ortho))
     return false;
-  gl_ortho_resize(&g_ortho, 0, window->w, 0, window->h, -1, 1);
+  gl_ortho_resize(&g_ortho, 0, window->w, 0, window->h, 0, 1);
   if (! gl_font_init(&g_font_courier_new,
                      "fonts/Courier New/Courier New.ttf"))
     return false;
@@ -257,7 +257,6 @@ bool window_sdl2_demo_render (s_window_sdl2 *window)
   if (! window_animate((s_window *) window))
     return false;
   seq = window->seq;
-  gl_ortho_resize(&g_ortho, 0, window->w, 0, window->h, -1, 1);
   gl_matrix_4f_init_identity(&g_ortho.model_matrix);
   gl_ortho_render(&g_ortho);
   glEnable(GL_BLEND);
@@ -326,8 +325,7 @@ bool window_sdl2_demo_resize (s_window_sdl2 *window,
   assert(window);
   (void) window;
   assert(glGetError() == GL_NO_ERROR);
-  gl_ortho_resize(&g_ortho, 0, w, 0, h, -1, 1);
-  glViewport(0, 0, w, h);
+  gl_ortho_resize(&g_ortho, 0, w, 0, h, 0, 1);
   assert(glGetError() == GL_NO_ERROR);
   return true;
 }
diff --git a/libc3/window/sdl2/gl_matrix_4f.c b/libc3/window/sdl2/gl_matrix_4f.c
index e202fb2..33d6f87 100644
--- a/libc3/window/sdl2/gl_matrix_4f.c
+++ b/libc3/window/sdl2/gl_matrix_4f.c
@@ -18,6 +18,7 @@
 sw gl_matrix_4f_buf_inspect (s_buf *buf, const s_gl_matrix_4f *matrix)
 {
   u8 i;
+  u8 j;
   sw r;
   sw result = 0;
   assert(buf);
@@ -28,14 +29,33 @@ sw gl_matrix_4f_buf_inspect (s_buf *buf, const s_gl_matrix_4f *matrix)
   result += r;
   m = &matrix->xx;
   i = 0;
-  while (i < 16) {
-    if ((r = buf_inspect_f32(buf, m)) < 0)
-      return r;
-    result += r;
-    m++;
+  while (i < 4) {
+    j = 0;
+    while (j < 4) {
+      if ((r = buf_inspect_f32(buf, m)) < 0)
+        return r;
+      result += r;
+      if (i < 3 || j < 3) {
+        if ((r = buf_write_1(buf, ",")) < 0)
+          return r;
+        result += r;
+        if (j < 3) {
+          if ((r = buf_write_1(buf, " ")) < 0)
+            return r;
+          result += r;
+        }
+      }
+      m++;
+      j++;
+    }
+    if (i < 3) {
+      if ((r = buf_write_1(buf, "\n       ")) < 0)
+        return r;
+      result += r;
+    }
     i++;
   }
-  if ((r = buf_write_1(buf, "}")) < 0)
+  if ((r = buf_write_1(buf, "}\n")) < 0)
     return r;
   result += r;
   return result;
@@ -156,9 +176,9 @@ s_gl_matrix_4f * gl_matrix_4f_ortho (s_gl_matrix_4f *m, f32 x1, f32 x2,
   ortho.xx = 2.0 / dx;
   ortho.yy = 2.0 / dy;
   ortho.zz = -2.0 / dz;
-  ortho.xt = -(x1 + x2) / dx;
-  ortho.yt = -(y1 + y2) / dy;
-  ortho.zt = -(clip_z_near + clip_z_far) / dz;
+  ortho.xt = (x1 + x2) / dx;
+  ortho.yt = (y1 + y2) / dy;
+  ortho.zt = (clip_z_near + clip_z_far) / dz;
   ortho.tt = 1.0;
   gl_matrix_4f_product(m, &ortho);
   return m;
diff --git a/libc3/window/sdl2/gl_matrix_4f.h b/libc3/window/sdl2/gl_matrix_4f.h
index 77b8fba..8b38aa3 100644
--- a/libc3/window/sdl2/gl_matrix_4f.h
+++ b/libc3/window/sdl2/gl_matrix_4f.h
@@ -49,4 +49,7 @@ s_gl_matrix_4f * gl_matrix_4f_scale (s_gl_matrix_4f *m, f32 x, f32 y,
 s_gl_matrix_4f * gl_matrix_4f_translate (s_gl_matrix_4f *m, f32 x,
                                          f32 y, f32 z);
 
+/* Observers. */
+sw gl_matrix_4f_buf_inspect (s_buf *buf, const s_gl_matrix_4f *matrix);
+
 #endif /* GL_MATRIX_4F_H */
diff --git a/libc3/window/sdl2/gl_ortho.c b/libc3/window/sdl2/gl_ortho.c
index 2a42f6e..a563960 100644
--- a/libc3/window/sdl2/gl_ortho.c
+++ b/libc3/window/sdl2/gl_ortho.c
@@ -126,18 +126,10 @@ void gl_ortho_render (s_gl_ortho *ortho)
   assert(glGetError() == GL_NO_ERROR);
   glUniformMatrix4fv(ortho->gl_model_matrix_loc, 1, GL_FALSE,
                      &ortho->model_matrix.xx);
+  glDepthRange(ortho->clip_z_near, ortho->clip_z_far);
   assert(glGetError() == GL_NO_ERROR);
 }
 
-void gl_ortho_resize (s_gl_ortho *ortho, f32 x1, f32 x2, f32 y1, f32 y2,
-                      f32 clip_z_near, f32 clip_z_far)
-{
-  assert(ortho);
-  gl_matrix_4f_init_identity(&ortho->projection_matrix);
-  gl_matrix_4f_ortho(&ortho->projection_matrix, x1, x2, y1, y2,
-                     clip_z_near, clip_z_far);
-}
-
 void gl_ortho_render_end (s_gl_ortho *ortho)
 {
   assert(ortho);
@@ -147,6 +139,17 @@ void gl_ortho_render_end (s_gl_ortho *ortho)
   assert(glGetError() == GL_NO_ERROR);
 }
 
+void gl_ortho_resize (s_gl_ortho *ortho, f32 x1, f32 x2, f32 y1, f32 y2,
+                      f32 clip_z_near, f32 clip_z_far)
+{
+  assert(ortho);
+  gl_matrix_4f_init_identity(&ortho->projection_matrix);
+  gl_matrix_4f_buf_inspect(&g_c3_env.err, &ortho->projection_matrix);
+  gl_matrix_4f_ortho(&ortho->projection_matrix, x1, x2, y1, y2,
+                     clip_z_near, clip_z_far);
+  gl_matrix_4f_buf_inspect(&g_c3_env.err, &ortho->projection_matrix);
+}
+
 void gl_ortho_update_model_matrix (s_gl_ortho *ortho)
 {
   assert(ortho);
diff --git a/libc3/window/sdl2/window_sdl2.c b/libc3/window/sdl2/window_sdl2.c
index fc885cd..c46bc66 100644
--- a/libc3/window/sdl2/window_sdl2.c
+++ b/libc3/window/sdl2/window_sdl2.c
@@ -276,6 +276,8 @@ bool window_sdl2_run (s_window_sdl2 *window)
           assert(glGetError() == GL_NO_ERROR);
           window->gl_w = gl_w;
           window->gl_h = gl_h;
+          glViewport(0, 0, sdl_event.window.data1,
+                     sdl_event.window.data2);
         }
         break;
       default: