Commit 4329b3d00c34048112e752f3f95429e741bee950

Thomas de Grivel 2023-12-23T22:58:39

wip OpenGL 4.6

diff --git a/libc3/buf.c b/libc3/buf.c
index 0e8c041..e5a24ed 100644
--- a/libc3/buf.c
+++ b/libc3/buf.c
@@ -499,6 +499,24 @@ sw buf_peek_str (s_buf *buf, const s_str *src)
   return src->size;
 }
 
+sw buf_peek_to_str (s_buf *buf, s_str *dest)
+{
+  sw size;
+  assert(buf);
+  assert(dest);
+  if (buf->rpos > buf->wpos)
+    return -1;
+  if (buf->wpos > buf->size)
+    return -1;
+  size = buf->wpos - buf->rpos;
+  if (size == 0) {
+    str_init_empty(dest);
+    return 0;
+  }
+  str_init_alloc(dest, size, buf->ptr.ps8 + buf->rpos);
+  return size;
+}
+
 sw buf_peek_u8 (s_buf *buf, u8 *p)
 {
   sw r;
diff --git a/libc3/window/sdl2/demo/window_sdl2_demo.c b/libc3/window/sdl2/demo/window_sdl2_demo.c
index 43249d1..f6d76e8 100644
--- a/libc3/window/sdl2/demo/window_sdl2_demo.c
+++ b/libc3/window/sdl2/demo/window_sdl2_demo.c
@@ -214,34 +214,27 @@ static void render_text (s_gl_text *text, f64 x, f64 y)
   assert(glGetError() == GL_NO_ERROR);
   gl_matrix_4d_translate(&g_ortho.model_matrix, 1.0, 0.0, 0.0);
   gl_ortho_update_model_matrix(&g_ortho);
-  assert(glGetError() == GL_NO_ERROR);
   gl_text_render(text);
   gl_matrix_4d_translate(&g_ortho.model_matrix, 1.0, 0.0, 0.0);
   gl_ortho_update_model_matrix(&g_ortho);
   gl_text_render(text);
-  gl_matrix_4d_translate(&g_ortho.model_matrix, 1.0, 0.0, 0.0);
+  gl_matrix_4d_translate(&g_ortho.model_matrix, 0.0, 1.0, 0.0);
   gl_ortho_update_model_matrix(&g_ortho);
-  glTranslatef( 0.0f,  1.0f, 0.0f);
   gl_text_render(text);
-  gl_matrix_4d_translate(&g_ortho.model_matrix, 1.0, 0.0, 0.0);
+  gl_matrix_4d_translate(&g_ortho.model_matrix, -1.0, 0.0, 0.0);
   gl_ortho_update_model_matrix(&g_ortho);
-  glTranslatef(-1.0f,  0.0f, 0.0f);
   gl_text_render(text);
-  gl_matrix_4d_translate(&g_ortho.model_matrix, 1.0, 0.0, 0.0);
+  gl_matrix_4d_translate(&g_ortho.model_matrix, -1.0, 0.0, 0.0);
   gl_ortho_update_model_matrix(&g_ortho);
-  glTranslatef(-1.0f,  0.0f, 0.0f);
   gl_text_render(text);
-  gl_matrix_4d_translate(&g_ortho.model_matrix, 1.0, 0.0, 0.0);
+  gl_matrix_4d_translate(&g_ortho.model_matrix, 0.0, 1.0, 0.0);
   gl_ortho_update_model_matrix(&g_ortho);
-  glTranslatef( 0.0f,  1.0f, 0.0f);
   gl_text_render(text);
   gl_matrix_4d_translate(&g_ortho.model_matrix, 1.0, 0.0, 0.0);
   gl_ortho_update_model_matrix(&g_ortho);
-  glTranslatef( 1.0f,  0.0f, 0.0f);
   gl_text_render(text);
   gl_matrix_4d_translate(&g_ortho.model_matrix, 1.0, 0.0, 0.0);
   gl_ortho_update_model_matrix(&g_ortho);
-  glTranslatef( 1.0f,  0.0f, 0.0f);
   gl_text_render(text);
   glBlendColor(0.0f, 0.0f, 0.0f, 1.0f);
   gl_matrix_4d_translate(&g_ortho.model_matrix, -1.0, -1.0, 0.0);
@@ -278,6 +271,7 @@ bool window_sdl2_demo_render (s_window_sdl2 *window, void *context)
   assert(glGetError() == GL_NO_ERROR);
   glBindTexture(GL_TEXTURE_2D, 0);
   assert(glGetError() == GL_NO_ERROR);
+  /*
   glRectd(19, 11,
           19 + (window->w - 40.0) * seq->t / seq->duration + 2,
           11 + 4);
@@ -286,6 +280,7 @@ bool window_sdl2_demo_render (s_window_sdl2 *window, void *context)
           20 + (window->w - 40.0) * seq->t / seq->duration,
           12 + 2);
   assert(glGetError() == GL_NO_ERROR);
+  */
   /* fps */
   s8 fps[32];
   snprintf(fps, sizeof(fps), "%.1f", (f64) seq->frame / seq->t);
diff --git a/libc3/window/sdl2/gl_object.c b/libc3/window/sdl2/gl_object.c
index 3547a0b..cf75f1d 100644
--- a/libc3/window/sdl2/gl_object.c
+++ b/libc3/window/sdl2/gl_object.c
@@ -56,35 +56,49 @@ void gl_object_render (const s_gl_object *object)
   assert(object);
   assert(glGetError() == GL_NO_ERROR);
   glBindVertexArray(object->gl_vao);
-  glBindBuffer(GL_ARRAY_BUFFER, object->gl_vbo);
+  assert(glGetError() == GL_NO_ERROR);
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object->gl_ebo);
-  glEnableClientState(GL_VERTEX_ARRAY);
-  glEnableClientState(GL_NORMAL_ARRAY);
-  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+  assert(glGetError() == GL_NO_ERROR);
   glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
   assert(glGetError() == GL_NO_ERROR);
 }
 
 bool gl_object_update (s_gl_object *object)
 {
+  //GLenum gl_error;
   assert(object);
+  assert(object->gl_vao);
+  assert(object->gl_vbo);
+  assert(object->gl_ebo);
+  assert(object->vertex.data);
+  assert(object->triangle.data);
   assert(glGetError() == GL_NO_ERROR);
   glBindVertexArray(object->gl_vao);
+  assert(glGetError() == GL_NO_ERROR);
   glBindBuffer(GL_ARRAY_BUFFER, object->gl_vbo);
-  glBufferData(GL_ARRAY_BUFFER, object->vertex.size,
+  assert(glGetError() == GL_NO_ERROR);
+  glBufferData(GL_ARRAY_BUFFER, object->vertex.count * sizeof(s_gl_vertex),
                object->vertex.data, GL_STATIC_DRAW);
+  assert(glGetError() == GL_NO_ERROR);
+  glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, sizeof(s_gl_vertex),
+                        (void *) 0);
+  assert(glGetError() == GL_NO_ERROR);
+  glEnableVertexAttribArray(0);
+  assert(glGetError() == GL_NO_ERROR);
+  glVertexAttribPointer(1, 3, GL_DOUBLE, GL_FALSE, sizeof(s_gl_vertex),
+                        (void *) (3 * sizeof(double)));
+  assert(glGetError() == GL_NO_ERROR);
+  glEnableVertexAttribArray(1);
+  assert(glGetError() == GL_NO_ERROR);
+  glVertexAttribPointer(2, 2, GL_DOUBLE, GL_FALSE, sizeof(s_gl_vertex),
+                        (void *) (6 * sizeof(double)));
+  assert(glGetError() == GL_NO_ERROR);
+  glEnableVertexAttribArray(2);
+  assert(glGetError() == GL_NO_ERROR);
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object->gl_ebo);
-  glBufferData(GL_ELEMENT_ARRAY_BUFFER, object->triangle.size,
+  assert(glGetError() == GL_NO_ERROR);
+  glBufferData(GL_ELEMENT_ARRAY_BUFFER, object->triangle.count * 3,
                object->triangle.data, GL_STATIC_DRAW);
-  glEnableClientState(GL_VERTEX_ARRAY);
-  glEnableClientState(GL_NORMAL_ARRAY);
-  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
-  glVertexPointer(3, GL_DOUBLE,   8 * sizeof(f64),
-                    (void *) 0);
-  glNormalPointer(GL_DOUBLE,      8 * sizeof(f64),
-                    (void *) (3 * sizeof(f64)));
-  glTexCoordPointer(2, GL_DOUBLE, 8 * sizeof(f64),
-                    (void *) (6 * sizeof(f64)));
   assert(glGetError() == GL_NO_ERROR);
   return true;
 }
diff --git a/libc3/window/sdl2/gl_text.c b/libc3/window/sdl2/gl_text.c
index 525103c..56ec215 100644
--- a/libc3/window/sdl2/gl_text.c
+++ b/libc3/window/sdl2/gl_text.c
@@ -30,16 +30,18 @@ s_gl_text * gl_text_init (s_gl_text *text, const s_gl_font *font)
   s_gl_text tmp = {0};
   assert(glGetError() == GL_NO_ERROR);
   tmp.font = font;
-  *text = tmp;  
-  glGenTextures(1, &text->texture);
+  glGenTextures(1, &tmp.texture);
   assert(glGetError() == GL_NO_ERROR);
-  gl_object_init(&text->object);
+  gl_object_init(&tmp.object);
   dimension = 4;
-  array_init(&text->object.vertex, sym_1("GL.Vertex"), 1,
+  array_init(&tmp.object.vertex, sym_1("GL.Vertex"), 1,
              &dimension);
+  array_allocate(&tmp.object.vertex);
   dimension = 2;
-  array_init(&text->object.triangle, sym_1("GL.Triangle"), 1,
+  array_init(&tmp.object.triangle, sym_1("GL.Triangle"), 1,
              &dimension);
+  array_allocate(&tmp.object.triangle);
+  *text = tmp;  
   return text;
 }
 
@@ -55,7 +57,8 @@ s_gl_text * gl_text_init_str (s_gl_text *text, const s_gl_font *font,
                               const s_str *str)
 {
   s_gl_text tmp = {0};
-  tmp.font = font;
+  if (! gl_text_init(&tmp, font))
+    return NULL;
   if (! str_init_copy(&tmp.str, str))
     return NULL;
   *text = tmp;
@@ -81,6 +84,7 @@ bool gl_text_render_to_texture (s_gl_text *text)
   uw  data_y;
   FT_Vector delta;
   const s_gl_font *font;
+  FT_GlyphSlot glyph;
   FT_UInt glyph_index;
   uw i;
   uw j;
@@ -103,7 +107,7 @@ bool gl_text_render_to_texture (s_gl_text *text)
     if (prev_glyph_index && glyph_index) {
       FT_Get_Kerning(font->ft_face, prev_glyph_index, glyph_index,
                      FT_KERNING_DEFAULT, &delta);
-      total_width += (f64) delta.x / (1 << 6);
+      total_width += ceil((f64) delta.x / (1 << 6));
     }
     if (FT_Load_Glyph(font->ft_face, glyph_index, FT_LOAD_RENDER)) {
       err_write_1("gl_font_render_to_texture: failed to load glyph: ");
@@ -111,15 +115,17 @@ bool gl_text_render_to_texture (s_gl_text *text)
       err_write_1("\n");
       continue;
     }
-    total_width += font->ft_face->glyph->bitmap.width;
-    max_height = (font->ft_face->glyph->bitmap.rows > max_height) ?
-      font->ft_face->glyph->bitmap.rows : max_height;
+    glyph = font->ft_face->glyph;
+    total_width += glyph->bitmap.width;
+    max_height =
+      (glyph->bitmap.rows + glyph->bitmap_top > max_height) ?
+      (glyph->bitmap.rows + glyph->bitmap_top) : max_height;
     prev_glyph_index = glyph_index;
   }
   data_w = ceil(total_width);
   data_h = ceil(max_height);
-  data_size = data_w * data_h;
-  data = calloc(4, data_size);
+  data_size = data_w * data_h * 4;
+  data = calloc(1, data_size);
   f64 x = 0;
   prev_glyph_index = 0;
   s = text->str;
@@ -136,7 +142,6 @@ bool gl_text_render_to_texture (s_gl_text *text)
     if (FT_Load_Glyph(font->ft_face, glyph_index, FT_LOAD_RENDER)) {
       continue;
     }
-    FT_GlyphSlot glyph;
     glyph = font->ft_face->glyph;
     i = 0;
     while (i < glyph->bitmap.width) {
@@ -203,7 +208,7 @@ bool gl_text_set_text_buf (s_gl_text *text, s_buf *buf)
   bool result;
   s_str str;
   assert(text);
-  assert(p);
+  assert(buf);
   buf_peek_to_str(buf, &str);
   result = gl_text_set_text(text, &str);
   str_clean(&str);
@@ -232,6 +237,7 @@ bool gl_text_update (s_gl_text *text)
   triangle = text->object.triangle.data;
   gl_triangle_init(triangle + 0, 0, 1, 2);
   gl_triangle_init(triangle + 1, 1, 3, 2);
+  gl_object_update(&text->object);
   return true;
 }
 
diff --git a/libc3/window/sdl2/gl_triangle.c b/libc3/window/sdl2/gl_triangle.c
index 6a43a4c..c2200f1 100644
--- a/libc3/window/sdl2/gl_triangle.c
+++ b/libc3/window/sdl2/gl_triangle.c
@@ -10,6 +10,7 @@
  * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
  * THIS SOFTWARE.
  */
+#include <libc3/c3.h>
 #include "gl_triangle.h"
 
 s_gl_triangle * gl_triangle_init (s_gl_triangle *triangle, u32 a, u32 b,
diff --git a/libc3/window/sdl2/sources.mk b/libc3/window/sdl2/sources.mk
index 0c7ee2b..281244d 100644
--- a/libc3/window/sdl2/sources.mk
+++ b/libc3/window/sdl2/sources.mk
@@ -12,6 +12,7 @@ HEADERS = \
 	"gl_point_3d.h" \
 	"gl_sphere.h" \
 	"gl_text.h" \
+	"gl_triangle.h" \
 	"sdl2_sprite.h" \
 	"types.h" \
 	"window_sdl2.h" \
@@ -27,6 +28,7 @@ SOURCES = \
 	"gl_point_3d.c" \
 	"gl_sphere.c" \
 	"gl_text.c" \
+	"gl_triangle.c" \
 	"sdl2_sprite.c" \
 	"window_sdl2.c" \
 
diff --git a/libc3/window/sdl2/sources.sh b/libc3/window/sdl2/sources.sh
index 8080ffa..f41cb08 100644
--- a/libc3/window/sdl2/sources.sh
+++ b/libc3/window/sdl2/sources.sh
@@ -1,3 +1,3 @@
 # sources.sh generated by update_sources
-HEADERS='gl_camera.h gl_cylinder.h gl_font.h gl_ft2.h gl_matrix_3d.h gl_matrix_4d.h gl_object.h gl_ortho.h gl_point_2d.h gl_point_3d.h gl_sphere.h gl_text.h sdl2_sprite.h types.h window_sdl2.h '
-SOURCES='gl_camera.c gl_cylinder.c gl_font.c gl_matrix_4d.c gl_object.c gl_ortho.c gl_point_2d.c gl_point_3d.c gl_sphere.c gl_text.c sdl2_sprite.c window_sdl2.c '
+HEADERS='gl_camera.h gl_cylinder.h gl_font.h gl_ft2.h gl_matrix_3d.h gl_matrix_4d.h gl_object.h gl_ortho.h gl_point_2d.h gl_point_3d.h gl_sphere.h gl_text.h gl_triangle.h sdl2_sprite.h types.h window_sdl2.h '
+SOURCES='gl_camera.c gl_cylinder.c gl_font.c gl_matrix_4d.c gl_object.c gl_ortho.c gl_point_2d.c gl_point_3d.c gl_sphere.c gl_text.c gl_triangle.c sdl2_sprite.c window_sdl2.c '