Commit 8dd274b26e9f492a36102fcd2993db4d97050863

Thomas de Grivel 2024-01-16T22:51:03

wip textures

diff --git a/libc3/window/sdl2/demo/earth.c b/libc3/window/sdl2/demo/earth.c
index 6c93c53..e078cae 100644
--- a/libc3/window/sdl2/demo/earth.c
+++ b/libc3/window/sdl2/demo/earth.c
@@ -117,10 +117,10 @@ bool earth_render (s_sequence *seq)
     assert(glGetError() == GL_NO_ERROR);
     //glEnable(GL_TEXTURE_2D);
     assert(glGetError() == GL_NO_ERROR);
-    gl_camera_bind_texture(camera,
-                           gl_sprite_texture(&g_sprite_earth, 0));
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
-                    GL_LINEAR_MIPMAP_LINEAR);
+    //gl_camera_bind_texture(camera,
+    //                       gl_sprite_texture(&g_sprite_earth, 0));
+    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
+    //                GL_LINEAR_MIPMAP_LINEAR);
     assert(glGetError() == GL_NO_ERROR);
     glBlendColor(1.0f, 1.0f, 1.0f, 1.0f);
     gl_sphere_render(sphere);
diff --git a/libc3/window/sdl2/demo/flies.c b/libc3/window/sdl2/demo/flies.c
index 99ae8d5..866ebd0 100644
--- a/libc3/window/sdl2/demo/flies.c
+++ b/libc3/window/sdl2/demo/flies.c
@@ -201,7 +201,7 @@ bool flies_render (s_sequence *seq)
   gl_matrix_4f_init_identity(&g_ortho.model_matrix);
   gl_matrix_4f_translate(&g_ortho.model_matrix, board_x, 60.0, 0.0);
   gl_ortho_update_model_matrix(&g_ortho);
-  glBlendColor(1.0f, 1.0f, 1.0f, 1.0f);
+  gl_ortho_color(&g_ortho, 1.0f, 1.0f, 1.0f, 1.0f);
   buf_init(&buf, false, sizeof(a), a);
   buf_write_1(&buf, "In ");
   buf_inspect_uw(&buf, fly_in);
@@ -213,15 +213,15 @@ bool flies_render (s_sequence *seq)
   buf_write_1(&buf, "Out ");
   buf_inspect_uw(&buf, fly_out);
   buf_write_u8(&buf, 0);
+  gl_text_update_1(&g_text_flies_out, a);
   matrix = g_ortho.model_matrix; {
     x = board_item_w * (BOARD_SIZE / 2 + 1);
     gl_matrix_4f_translate(&g_ortho.model_matrix, x, 0.0, 0.0);
     gl_ortho_update_model_matrix(&g_ortho);
-    gl_text_update_1(&g_text_flies_out, a);
     gl_ortho_text_render(&g_ortho, &g_text_flies_out);
     g_ortho.model_matrix = matrix;
     gl_matrix_4f_translate(&g_ortho.model_matrix, 0.0, board_item_h, 0.0);
-    glBlendColor(0.6f, 0.7f, 0.9f, 1.0f);
+    gl_ortho_color(&g_ortho, 0.6f, 0.7f, 0.9f, 1.0f);
     gl_ortho_rect(&g_ortho, 0, 0, board_w, board_h);
     address[1] = 0;
     while (address[1] < BOARD_SIZE) {
@@ -239,7 +239,7 @@ bool flies_render (s_sequence *seq)
             break;
           case BOARD_ITEM_BLOCK:
             gl_ortho_bind_texture(&g_ortho, 0);
-            glBlendColor(0.0f, 0.0f, 1.0f, 1.0f);
+            gl_ortho_color(&g_ortho, 0.0f, 0.0f, 1.0f, 1.0f);
             gl_ortho_rect(&g_ortho, 0, 0, board_item_w + 1.0, board_item_h + 1.0);
             break;
           case BOARD_ITEM_FLY:
@@ -252,6 +252,7 @@ bool flies_render (s_sequence *seq)
               gl_ortho_update_model_matrix(&g_ortho);
               gl_ortho_bind_texture(&g_ortho,
                                     gl_sprite_texture(&g_sprite_fly, 0));
+              gl_ortho_color(&g_ortho, 1.0f, 1.0f, 1.0f, 1.0f);
               gl_ortho_rect(&g_ortho, 0, 0, g_sprite_fly.pt_w,
                             g_sprite_fly.pt_h);
               gl_ortho_bind_texture(&g_ortho, 0);
@@ -332,6 +333,7 @@ bool flies_render (s_sequence *seq)
               gl_ortho_update_model_matrix(&g_ortho);
               gl_ortho_bind_texture(&g_ortho,
                                     gl_sprite_texture(&g_sprite_dead_fly, 0));
+              gl_ortho_color(&g_ortho, 1.0f, 1.0f, 1.0f, 1.0f);
               gl_ortho_rect(&g_ortho, 0, 0, g_sprite_dead_fly.pt_w,
                             g_sprite_dead_fly.pt_h);
               gl_ortho_bind_texture(&g_ortho, 0);
diff --git a/libc3/window/sdl2/demo/window_sdl2_demo.c b/libc3/window/sdl2/demo/window_sdl2_demo.c
index 84107f7..d149790 100644
--- a/libc3/window/sdl2/demo/window_sdl2_demo.c
+++ b/libc3/window/sdl2/demo/window_sdl2_demo.c
@@ -219,7 +219,7 @@ static void render_text (s_gl_text *text, f64 x, f64 y)
   matrix = g_ortho.model_matrix;
   gl_matrix_4f_translate(&g_ortho.model_matrix, x - 1.0, y - 1.0, 0.0);
   gl_ortho_update_model_matrix(&g_ortho);
-  glBlendColor(1.0f, 1.0f, 1.0f, 1.0f);
+  gl_ortho_color(&g_ortho, 1.0f, 1.0f, 1.0f, 1.0f);
   assert(glGetError() == GL_NO_ERROR);
   gl_ortho_text_render(&g_ortho, text);
   assert(glGetError() == GL_NO_ERROR);
@@ -249,7 +249,7 @@ static void render_text (s_gl_text *text, f64 x, f64 y)
   gl_ortho_update_model_matrix(&g_ortho);
   gl_ortho_text_render(&g_ortho, text);
   assert(glGetError() == GL_NO_ERROR);
-  glBlendColor(0.0f, 0.0f, 0.0f, 1.0f);
+  gl_ortho_color(&g_ortho, 0.0f, 0.0f, 0.0f, 1.0f);
   assert(glGetError() == GL_NO_ERROR);
   gl_matrix_4f_translate(&g_ortho.model_matrix, -1.0, -1.0, 0.0);
   gl_ortho_update_model_matrix(&g_ortho);
@@ -293,11 +293,11 @@ bool window_sdl2_demo_render (s_window_sdl2 *window)
   assert(glGetError() == GL_NO_ERROR);
   gl_ortho_bind_texture(&g_ortho, 0);
   assert(glGetError() == GL_NO_ERROR);
-  glBlendColor(1.0f, 1.0f, 1.0f, 1.0f);
+  gl_ortho_color(&g_ortho, 1.0f, 1.0f, 1.0f, 1.0f);
   gl_ortho_rect(&g_ortho, 19, 11,
                 (window->w - 40.0) * seq->t / seq->duration + 2,
                 4);
-  glBlendColor(0.0f, 0.0f, 0.0f, 1.0f);
+  gl_ortho_color(&g_ortho, 0.0f, 0.0f, 0.0f, 1.0f);
   gl_ortho_rect(&g_ortho, 20, 12,
                 (window->w - 40.0) * seq->t / seq->duration,
                 2);
diff --git a/libc3/window/sdl2/gl_camera.c b/libc3/window/sdl2/gl_camera.c
index 5bb5e71..6189528 100644
--- a/libc3/window/sdl2/gl_camera.c
+++ b/libc3/window/sdl2/gl_camera.c
@@ -40,13 +40,14 @@ static const char * g_gl_camera_fragment_shader_src =
   "out vec4 oFragColor;\n"
   "uniform bool uEnableTex2D;\n"
   "uniform sampler2D uTex2D;\n"
+  "uniform vec4 uColor;\n"
   "void main() {\n"
+  "  vec4 texColor = texture(uTex2D, iTexCoord);\n"
   "  if (uEnableTex2D) {\n"
-  "    vec4 texColor = texture(uTex2D, iTexCoord);\n"
-  "    oFragColor = texColor;\n"
+  "    oFragColor = texColor * uColor;\n"
   "  }\n"
   "  else\n"
-  "    oFragColor = vec4(1, 1, 1, 1);\n"
+  "    oFragColor = uColor;\n"
   "}\n";
 
 void gl_camera_bind_texture (s_gl_camera *camera, GLuint texture)
@@ -129,6 +130,15 @@ s_gl_camera * gl_camera_init (s_gl_camera *camera, uw w, uw h)
   camera->gl_model_matrix_loc =
     glGetUniformLocation(camera->gl_shader_program,
                          "uModelMatrix");
+  camera->gl_enable_tex2d_loc =
+    glGetUniformLocation(camera->gl_shader_program,
+                         "uEnableTex2D");
+  camera->gl_tex2d_loc =
+    glGetUniformLocation(camera->gl_shader_program,
+                         "uTex2D");
+  camera->gl_color_loc =
+    glGetUniformLocation(camera->gl_shader_program,
+                         "uColor");
   return camera;
 }
 
@@ -149,7 +159,9 @@ s_gl_camera * gl_camera_new (uw w, uw h)
 
 void gl_camera_render (s_gl_camera *camera)
 {
+  const s_rgba color = {1, 1, 1, 1};
   assert(camera);
+  assert(glGetError() == GL_NO_ERROR);
   gl_matrix_4f_init_identity(&camera->projection_matrix);
   gl_matrix_4f_perspective(&camera->projection_matrix, camera->fov_y,
                            camera->aspect_ratio, camera->clip_z_near,
@@ -165,12 +177,22 @@ void gl_camera_render (s_gl_camera *camera)
                            &(s_gl_point_3f) { 0.0f, 0.0f, 1.0f });
   gl_matrix_4f_init_identity(&camera->model_matrix);
   glUseProgram(camera->gl_shader_program);
+  assert(glGetError() == GL_NO_ERROR);
   glUniformMatrix4fv(camera->gl_projection_matrix_loc, 1, GL_FALSE,
                      &camera->projection_matrix.xx);
+  assert(glGetError() == GL_NO_ERROR);
   glUniformMatrix4fv(camera->gl_view_matrix_loc, 1, GL_FALSE,
                      &camera->view_matrix.xx);
+  assert(glGetError() == GL_NO_ERROR);
   glUniformMatrix4fv(camera->gl_model_matrix_loc, 1, GL_FALSE,
                      &camera->model_matrix.xx);
+  assert(glGetError() == GL_NO_ERROR);
+  glUniform1i(camera->gl_enable_tex2d_loc, 0);
+  assert(glGetError() == GL_NO_ERROR);
+  glUniform1i(camera->gl_tex2d_loc, 0);
+  assert(glGetError() == GL_NO_ERROR);
+  glUniform4fv(camera->gl_color_loc, 1, &color.r);
+  assert(glGetError() == GL_NO_ERROR);
 }
 
 void gl_camera_render_end (s_gl_camera *camera)
diff --git a/libc3/window/sdl2/gl_ortho.c b/libc3/window/sdl2/gl_ortho.c
index 7214c5e..623f30c 100644
--- a/libc3/window/sdl2/gl_ortho.c
+++ b/libc3/window/sdl2/gl_ortho.c
@@ -42,13 +42,14 @@ static const char * g_gl_ortho_fragment_shader_src =
   "out vec4 oFragColor;\n"
   "uniform bool uEnableTex2D;\n"
   "uniform sampler2D uTex2D;\n"
+  "uniform vec4 uColor;\n"
   "void main() {\n"
+  "  vec4 texColor = texture(uTex2D, iTexCoord);\n"
   "  if (uEnableTex2D) {\n"
-  "    vec4 texColor = texture(uTex2D, iTexCoord);\n"
   "    oFragColor = texColor;\n"
   "  }\n"
   "  else\n"
-  "    oFragColor = vec4(1, 1, 1, 1);\n"
+  "    oFragColor = uColor;\n"
   "}\n";
 
 void gl_ortho_bind_texture (s_gl_ortho *ortho, GLuint texture)
@@ -56,21 +57,43 @@ void gl_ortho_bind_texture (s_gl_ortho *ortho, GLuint texture)
   assert(ortho);
   assert(glGetError() == GL_NO_ERROR);
   if (! texture) {
+    glActiveTexture(GL_TEXTURE0);
+    assert(glGetError() == GL_NO_ERROR);
+    glBindTexture(GL_TEXTURE_2D, 0);
+    assert(glGetError() == GL_NO_ERROR);
     glUniform1i(ortho->gl_enable_tex2d_loc, 0);
     assert(glGetError() == GL_NO_ERROR);
+    glUniform1i(ortho->gl_tex2d_loc, 0);
+    assert(glGetError() == GL_NO_ERROR);
     return;
   }
+  glActiveTexture(GL_TEXTURE0);
+  assert(glGetError() == GL_NO_ERROR);
+  glBindTexture(GL_TEXTURE_2D, texture);
+  assert(glGetError() == GL_NO_ERROR);
   glUniform1i(ortho->gl_enable_tex2d_loc, 1);
   assert(glGetError() == GL_NO_ERROR);
-  glUniform1i(ortho->gl_tex2d_loc, texture);  
+  glUniform1i(ortho->gl_tex2d_loc, 0);
   assert(glGetError() == GL_NO_ERROR);
 }
 
 void gl_ortho_clean (s_gl_ortho *ortho)
 {
   assert(ortho);
+  assert(glGetError() == GL_NO_ERROR);
   glDeleteProgram(ortho->gl_shader_program);
+  assert(glGetError() == GL_NO_ERROR);
   gl_square_clean(&ortho->square);
+  assert(glGetError() == GL_NO_ERROR);
+}
+
+void gl_ortho_color (s_gl_ortho *ortho, f32 r, f32 g, f32 b, f32 a)
+{
+  s_rgba color = {r, g, b, a};
+  assert(ortho);
+  assert(glGetError() == GL_NO_ERROR);
+  glUniform4fv(ortho->gl_color_loc, 1, &color.r);    
+  assert(glGetError() == GL_NO_ERROR);
 }
 
 void gl_ortho_delete (s_gl_ortho *ortho)
@@ -149,6 +172,9 @@ s_gl_ortho * gl_ortho_init (s_gl_ortho *ortho)
   ortho->gl_tex2d_loc =
     glGetUniformLocation(ortho->gl_shader_program, "uTex2D");
   assert(glGetError() == GL_NO_ERROR);
+  ortho->gl_color_loc =
+    glGetUniformLocation(ortho->gl_shader_program, "uColor");
+  assert(glGetError() == GL_NO_ERROR);
   return ortho;
 }
 
@@ -173,8 +199,8 @@ void gl_ortho_rect (s_gl_ortho *ortho, f32 x, f32 y, f32 w, f32 h)
   assert(ortho);
   assert(glGetError() == GL_NO_ERROR);
   matrix = ortho->model_matrix;
-  gl_matrix_4f_translate(&ortho->model_matrix, x - 0.5, y - 0.5, 0.0f);
-  gl_matrix_4f_scale(&ortho->model_matrix, w + 1.0f, h + 1.0f, 1.0f);
+  gl_matrix_4f_translate(&ortho->model_matrix, x, y, 0.0f);
+  gl_matrix_4f_scale(&ortho->model_matrix, w, h, 1.0f);
   gl_ortho_update_model_matrix(ortho);
   assert(glGetError() == GL_NO_ERROR);
   gl_square_render(&ortho->square);
@@ -184,6 +210,7 @@ void gl_ortho_rect (s_gl_ortho *ortho, f32 x, f32 y, f32 w, f32 h)
 
 void gl_ortho_render (s_gl_ortho *ortho)
 {
+  const s_rgba color = {1, 1, 1, 1};
   GLenum error;
   assert(ortho);
   assert(glGetError() == GL_NO_ERROR);
@@ -207,6 +234,8 @@ void gl_ortho_render (s_gl_ortho *ortho)
   assert(glGetError() == GL_NO_ERROR);
   glUniform1i(ortho->gl_tex2d_loc, 0);
   assert(glGetError() == GL_NO_ERROR);
+  glUniform4fv(ortho->gl_color_loc, 1, &color.r);
+  assert(glGetError() == GL_NO_ERROR);
   glDepthRange(ortho->clip_z_near, ortho->clip_z_far);
   assert(glGetError() == GL_NO_ERROR);
   /*
diff --git a/libc3/window/sdl2/gl_ortho.h b/libc3/window/sdl2/gl_ortho.h
index 8e06d15..c31264a 100644
--- a/libc3/window/sdl2/gl_ortho.h
+++ b/libc3/window/sdl2/gl_ortho.h
@@ -26,6 +26,8 @@ s_gl_ortho * gl_ortho_new (void);
 
 /* Operators. */
 void         gl_ortho_bind_texture (s_gl_ortho *ortho, GLuint texture);
+void         gl_ortho_color (s_gl_ortho *ortho, f32 r, f32 g, f32 b,
+                             f32 a);
 void         gl_ortho_rect (s_gl_ortho *ortho, f32 x1, f32 x2, f32 y1,
                             f32 y2);
 void         gl_ortho_render (s_gl_ortho *ortho);
diff --git a/libc3/window/sdl2/gl_text.c b/libc3/window/sdl2/gl_text.c
index a04617b..e42c7d1 100644
--- a/libc3/window/sdl2/gl_text.c
+++ b/libc3/window/sdl2/gl_text.c
@@ -118,7 +118,7 @@ bool gl_text_render_to_texture (s_gl_text *text)
       FT_Vector delta;
       FT_Get_Kerning(font->ft_face, prev_glyph_index, glyph_index,
                      FT_KERNING_DEFAULT, &delta);
-      x += (f32) delta.x / (1 << 6);
+      x += delta.x >> 6;
     }
     if (FT_Load_Glyph(font->ft_face, glyph_index, FT_LOAD_RENDER)) {
       continue;
@@ -128,13 +128,13 @@ bool gl_text_render_to_texture (s_gl_text *text)
     while (i < glyph->bitmap.width) {
       j = 0;
       while (j < glyph->bitmap.rows) {
-        data_x = round(x) + i;
+        data_x = x + i;
         data_y = j + glyph->bitmap_top;
         data_pixel = data + (data_y * data_w + data_x) * 4;
         u8 value = glyph->bitmap.buffer[j * glyph->bitmap.width + i];
-        data_pixel[0] = value;
-        data_pixel[1] = value;
-        data_pixel[2] = value;
+        data_pixel[0] = 255;
+        data_pixel[1] = 255;
+        data_pixel[2] = 255;
         data_pixel[3] = value;
         j++;
       }
diff --git a/libc3/window/sdl2/types.h b/libc3/window/sdl2/types.h
index fef23ea..95bf885 100644
--- a/libc3/window/sdl2/types.h
+++ b/libc3/window/sdl2/types.h
@@ -274,14 +274,15 @@ struct gl_camera {
   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;
-  u32         gl_view_matrix_loc;
   s_gl_matrix_4f model_matrix;
-  u32         gl_model_matrix_loc;
-  u32 gl_enable_tex2d_loc;
-  u32 gl_tex2d_loc;
-  u32 gl_shader_program;
+  GLuint gl_projection_matrix_loc;
+  GLuint gl_view_matrix_loc;
+  GLuint gl_model_matrix_loc;
+  GLuint gl_enable_tex2d_loc;
+  GLuint gl_tex2d_loc;
+  GLuint gl_color_loc;
+  GLuint gl_shader_program;
 };
 
 struct gl_cylinder {
@@ -302,13 +303,11 @@ struct gl_square {
   uw segments_v;
 };
 
-#pragma pack(push, 1)
 struct gl_vertex {
   s_gl_point_3f position;
   s_gl_point_3f normal;
   s_gl_point_2f tex_coord;
 };
-#pragma pack(pop)
 
 struct sdl2_sprite {
   s_gl_object object;
@@ -339,14 +338,15 @@ struct gl_ortho {
   s_gl_point_3f rotation;
   s_gl_point_3f scale;
   s_gl_matrix_4f projection_matrix;
-  u32         gl_projection_matrix_loc;
+  GLuint      gl_projection_matrix_loc;
   s_gl_matrix_4f view_matrix;
-  u32         gl_view_matrix_loc;
+  GLuint      gl_view_matrix_loc;
   s_gl_matrix_4f model_matrix;
-  u32         gl_model_matrix_loc;
-  u32 gl_enable_tex2d_loc;
-  u32 gl_tex2d_loc;
-  u32 gl_shader_program;
+  GLuint      gl_model_matrix_loc;
+  GLuint gl_enable_tex2d_loc;
+  GLuint gl_tex2d_loc;
+  GLuint gl_color_loc;
+  GLuint gl_shader_program;
   s_gl_square square;
 };