Commit 006d7cb5f64f19360f5cad8397fe8677fc988dd7

Thomas de Grivel 2024-01-09T12:35:30

wip matrix

diff --git a/libc3/buf_inspect.c b/libc3/buf_inspect.c
index 4a0ad59..cf8ac3c 100644
--- a/libc3/buf_inspect.c
+++ b/libc3/buf_inspect.c
@@ -680,7 +680,7 @@ sw buf_inspect_f32 (s_buf *buf, const f32 *f)
   if ((r = buf_write_1(buf, ".")) <= 0)
     return r;
   result += r;
-  j = 7;
+  j = 6;
   do {
     x *= 10;
     i = (u8) x;
@@ -758,7 +758,7 @@ sw buf_inspect_f64 (s_buf *buf, const f64 *f)
   if ((r = buf_write_1(buf, ".")) <= 0)
     return r;
   result += r;
-  j = 15;
+  j = 14;
   do {
     x *= 10;
     i = (u8) x;
diff --git a/libc3/window/sdl2/demo/lightspeed.c b/libc3/window/sdl2/demo/lightspeed.c
index 88bff51..6141f3f 100644
--- a/libc3/window/sdl2/demo/lightspeed.c
+++ b/libc3/window/sdl2/demo/lightspeed.c
@@ -92,11 +92,9 @@ bool lightspeed_render (s_sequence *seq)
   window = seq->window;
   assert(window);
   gl_matrix_4f_init_identity(&g_ortho.model_matrix);
-  /*
   gl_matrix_4f_scale(&g_ortho.model_matrix, window->w / 2.0,
                      window->h / 2.0, 1);
   gl_matrix_4f_translate(&g_ortho.model_matrix, 1, 1, 0);
-  */
   gl_ortho_update_model_matrix(&g_ortho);
   star_count = window->w * window->h * LIGHTSPEED_STAR_PROBABILITY;
   if (star_count > LIGHTSPEED_STAR_MAX)
diff --git a/libc3/window/sdl2/gl_matrix_4f.c b/libc3/window/sdl2/gl_matrix_4f.c
index 33d6f87..9232cd9 100644
--- a/libc3/window/sdl2/gl_matrix_4f.c
+++ b/libc3/window/sdl2/gl_matrix_4f.c
@@ -176,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.tx = - (x1 + x2) / dx;
+  ortho.ty = - (y1 + y2) / dy;
+  ortho.tz = - (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_ortho.c b/libc3/window/sdl2/gl_ortho.c
index a563960..0b5fe38 100644
--- a/libc3/window/sdl2/gl_ortho.c
+++ b/libc3/window/sdl2/gl_ortho.c
@@ -30,7 +30,7 @@ static const s8 * g_gl_ortho_vertex_shader_src = "#version 330 core\n"
   "                     model_matrix * vec4(aPos, 1.0));\n"
   "  FragNormal = vec3(mat3(transpose(inverse(model_matrix))) *\n"
   "                    aNorm);\n"
-  "  TexCoord = vec2(aTexCoord);\n"
+  "  TexCoord = aTexCoord;\n"
   "}\n";
 
 void gl_ortho_clean (s_gl_ortho *ortho)
@@ -51,13 +51,16 @@ s_gl_ortho * gl_ortho_init (s_gl_ortho *ortho)
   u32 vertex_shader;
   assert(ortho);
   gl_matrix_4f_init_identity(&ortho->projection_matrix);
-  gl_matrix_4f_ortho(&ortho->projection_matrix, -1, 1, -1, 1, -1, 1);
-  ortho->position.x = 0.0;
-  ortho->position.y = 0.0;
-  ortho->position.z = 0.0;
-  ortho->rotation.x = 0.0;
-  ortho->rotation.y = 0.0;
-  ortho->rotation.z = 0.0;
+  gl_matrix_4f_ortho(&ortho->projection_matrix, -1, 1, -1, 1, 0, 1);
+  ortho->position.x = 0.0f;
+  ortho->position.y = 0.0f;
+  ortho->position.z = 0.0f;
+  ortho->rotation.x = M_PI;
+  ortho->rotation.y = 0.0f;
+  ortho->rotation.z = 0.0f;
+  ortho->scale.x = 1.0f;
+  ortho->scale.y = 1.0f;
+  ortho->scale.z = 1.0f;
   gl_matrix_4f_init_identity(&ortho->view_matrix);
   gl_matrix_4f_init_identity(&ortho->model_matrix);
   vertex_shader = glCreateShader(GL_VERTEX_SHADER);
@@ -144,10 +147,10 @@ void gl_ortho_resize (s_gl_ortho *ortho, f32 x1, f32 x2, f32 y1, f32 y2,
 {
   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);
+  buf_flush(&g_c3_env.err);
 }
 
 void gl_ortho_update_model_matrix (s_gl_ortho *ortho)
@@ -172,6 +175,8 @@ void gl_ortho_update_view_matrix (s_gl_ortho *ortho)
                            &(s_gl_point_3f) { 0.0f, 1.0f, 0.0f });
   gl_matrix_4f_rotate_axis(&ortho->view_matrix, ortho->rotation.z,
                            &(s_gl_point_3f) { 0.0f, 0.0f, 1.0f });
+  gl_matrix_4f_scale(&ortho->view_matrix, ortho->scale.x,
+                     ortho->scale.y, ortho->scale.z);
   glUniformMatrix4fv(ortho->gl_view_matrix_loc, 1, GL_FALSE,
                      &ortho->view_matrix.xx);
   assert(glGetError() == GL_NO_ERROR);
diff --git a/libc3/window/sdl2/types.h b/libc3/window/sdl2/types.h
index 43b0152..ec3d2f1 100644
--- a/libc3/window/sdl2/types.h
+++ b/libc3/window/sdl2/types.h
@@ -274,6 +274,7 @@ struct gl_ortho {
   f32 clip_z_far;
   s_gl_point_3f position;
   s_gl_point_3f rotation;
+  s_gl_point_3f scale;
   s_gl_matrix_4f projection_matrix;
   u32         gl_projection_matrix_loc;
   s_gl_matrix_4f view_matrix;