Commit 510a21774f8686f3fe3d878ab3d72a8d4891ab91

Thomas de Grivel 2024-01-14T20:43:57

wip OpenGL 3.3

diff --git a/libc3/window/sdl2/demo/lightspeed.c b/libc3/window/sdl2/demo/lightspeed.c
index d6aa4d3..092e6ff 100644
--- a/libc3/window/sdl2/demo/lightspeed.c
+++ b/libc3/window/sdl2/demo/lightspeed.c
@@ -115,8 +115,6 @@ bool lightspeed_render (s_sequence *seq)
   assert(glGetError() == GL_NO_ERROR);
   glClear(GL_COLOR_BUFFER_BIT);
   assert(glGetError() == GL_NO_ERROR);
-  glLineWidth(2);
-  assert(glGetError() == GL_NO_ERROR);
   glBlendColor(1, 1, 1, 0.7f);
   assert(glGetError() == GL_NO_ERROR);
   glEnable(GL_BLEND);
diff --git a/libc3/window/sdl2/gl_camera.c b/libc3/window/sdl2/gl_camera.c
index 0ba3aaa..fb58028 100644
--- a/libc3/window/sdl2/gl_camera.c
+++ b/libc3/window/sdl2/gl_camera.c
@@ -17,6 +17,8 @@
 
 static const char * g_gl_camera_vertex_shader_src = "#version 330 core\n"
 "layout (location = 0) in vec3 aPos;\n"
+"layout (location = 1) in vec3 aNormal;\n"
+"layout (location = 2) in vec2 aTexCoord;\n"
 "uniform mat4 matrix;\n"
 "\n"
 "void main() {\n"
diff --git a/libc3/window/sdl2/gl_lines.c b/libc3/window/sdl2/gl_lines.c
index d6703fc..d72c240 100644
--- a/libc3/window/sdl2/gl_lines.c
+++ b/libc3/window/sdl2/gl_lines.c
@@ -74,20 +74,20 @@ bool gl_lines_update (s_gl_lines *lines, uw lines_count)
   glBufferData(GL_ARRAY_BUFFER, lines_count * 2 * sizeof(s_gl_vertex),
                lines->vertex.data, GL_DYNAMIC_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)));
+  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(s_gl_vertex),
+                        (void *) 0);
   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)));
+  glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(s_gl_vertex),
+                        (void *) (3 * sizeof(float)));
   assert(glGetError() == GL_NO_ERROR);
   glEnableVertexAttribArray(2);
   assert(glGetError() == GL_NO_ERROR);
+  glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(s_gl_vertex),
+                        (void *) (6 * sizeof(float)));
+  assert(glGetError() == GL_NO_ERROR);
   return true;
 }
diff --git a/libc3/window/sdl2/gl_ortho.c b/libc3/window/sdl2/gl_ortho.c
index ad506cb..e183ba4 100644
--- a/libc3/window/sdl2/gl_ortho.c
+++ b/libc3/window/sdl2/gl_ortho.c
@@ -168,9 +168,11 @@ 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);
+  assert(glGetError() == GL_NO_ERROR);
   glUniform1i(ortho->gl_enable_texture_loc, 0);
+  assert(glGetError() == GL_NO_ERROR);
   glUniform1i(ortho->gl_texture_loc, -1);
-  glUniform4f(ortho->gl_color_loc, 1.0f, 1.0f, 1.0f, 1.0f);
+  assert(glGetError() == GL_NO_ERROR);
   glDepthRange(ortho->clip_z_near, ortho->clip_z_far);
   assert(glGetError() == GL_NO_ERROR);
   err_puts("gl_ortho_render projection matrix");
diff --git a/libc3/window/sdl2/gl_sprite.c b/libc3/window/sdl2/gl_sprite.c
index 2b68e92..63dc6b3 100644
--- a/libc3/window/sdl2/gl_sprite.c
+++ b/libc3/window/sdl2/gl_sprite.c
@@ -134,14 +134,14 @@ s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
     (dim_x * dim_y);
   str_init_copy_1(&tmp.path, path);
   if (! file_search(&tmp.path, sym_1("r"), &tmp.real_path)) {
-    err_write_1("sdl2_sprite_init: file not found: ");
+    err_write_1("gl_sprite_init: file not found: ");
     err_puts(path);
     str_clean(&tmp.path);
     return NULL;
   }
   fp = fopen(tmp.real_path.ptr.pchar, "rb");
   if (! fp) {
-    err_write_1("sdl2_sprite_init: fopen: ");
+    err_write_1("gl_sprite_init: fopen: ");
     err_puts(tmp.real_path.ptr.pchar);
     str_clean(&tmp.path);
     str_clean(&tmp.real_path);
@@ -149,7 +149,7 @@ s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
   }
   if (fread(png_header, 1, sizeof(png_header), fp) !=
       sizeof(png_header)) {
-    err_write_1("sdl2_sprite_init: fread: ");
+    err_write_1("gl_sprite_init: fread: ");
     err_puts(tmp.real_path.ptr.pchar);
     fclose(fp);
     str_clean(&tmp.path);
@@ -157,7 +157,7 @@ s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
     return NULL;
   }
   if (png_sig_cmp(png_header, 0, sizeof(png_header))) {
-    err_write_1("sdl2_sprite_init: not a png: ");
+    err_write_1("gl_sprite_init: not a png: ");
     err_puts(tmp.real_path.ptr.pchar);
     fclose(fp);
     str_clean(&tmp.path);
@@ -167,7 +167,7 @@ s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
   png_read = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL,
 				    NULL);
   if (! png_read) {
-    err_write_1("sdl2_sprite_init: png_create_read_struct: ");
+    err_write_1("gl_sprite_init: png_create_read_struct: ");
     err_puts(tmp.real_path.ptr.pchar);
     fclose(fp);
     str_clean(&tmp.path);
@@ -176,7 +176,7 @@ s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
   }
   png_info = png_create_info_struct(png_read);
   if (! png_info) {
-    err_write_1("sdl2_sprite_init: png_create_info_struct: ");
+    err_write_1("gl_sprite_init: png_create_info_struct: ");
     err_puts(tmp.real_path.ptr.pchar);
     png_destroy_read_struct(&png_read, NULL, NULL);
     fclose(fp);
@@ -207,17 +207,17 @@ s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
 			    &gl_internal_format, &gl_type,
 			    &png_components)) {
     if (! gl_format || ! png_components) {
-      err_write_1("sdl2_sprite_init: unknown PNG color type ");
+      err_write_1("gl_sprite_init: unknown PNG color type ");
       err_inspect_s32(&png_color_type);
       err_write_1(": ");
       err_puts(tmp.real_path.ptr.pchar);
     }
     if (! gl_internal_format) {
-      err_write_1("sdl2_sprite_init: unknown OpenGL internal format: ");
+      err_write_1("gl_sprite_init: unknown OpenGL internal format: ");
       err_puts(tmp.real_path.ptr.pchar);
     }
     if (! gl_type) {
-      err_write_1("sdl2_sprite_init: unknown OpenGL type: ");
+      err_write_1("gl_sprite_init: unknown OpenGL type: ");
       err_puts(tmp.real_path.ptr.pchar);
     }
     png_destroy_read_struct(&png_read, &png_info, NULL);
@@ -251,16 +251,17 @@ s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
   tmp.h = tmp.total_h / dim_y;
   tmp.texture = calloc(tmp.frame_count, sizeof(GLuint));
   if (! tmp.texture) {
-    err_puts("sdl2_sprite_init: tmp.texture:"
+    err_puts("gl_sprite_init: tmp.texture:"
              " failed to allocate memory");
     str_clean(&tmp.path);
     str_clean(&tmp.real_path);
     return NULL;
   }
+  assert(glGetError() == GL_NO_ERROR);
   glGenTextures(tmp.frame_count, tmp.texture);
   GLenum gl_error = glGetError();
   if (gl_error != GL_NO_ERROR) {
-    err_write_1("sdl2_sprite_init: ");
+    err_write_1("gl_sprite_init: ");
     err_inspect_str(&tmp.real_path);
     err_write_1(": glGenTextures: ");
     err_puts(gl_error_string(gl_error));
@@ -272,7 +273,7 @@ s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
   sprite_stride = tmp.w * png_pixel_size;
   data = malloc(tmp.h * sprite_stride);
   if (! data) {
-    err_write_1("sdl2_sprite_init: failed to allocate memory: ");
+    err_write_1("gl_sprite_init: failed to allocate memory: ");
     err_puts(tmp.real_path.ptr.pchar);
     free(tmp.texture);
     str_clean(&tmp.path);
@@ -296,7 +297,7 @@ s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
       glBindTexture(GL_TEXTURE_2D, tmp.texture[i]);
       gl_error = glGetError();
       if (gl_error != GL_NO_ERROR) {
-	err_write_1("sdl2_sprite_init: ");
+	err_write_1("gl_sprite_init: ");
         err_inspect_str(&tmp.real_path);
         err_write_1(": glBindTexture: ");
         err_puts(gl_error_string(gl_error));
@@ -311,7 +312,7 @@ s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
       gl_error = glGetError();
       if (gl_error != GL_NO_ERROR) {
-	err_write_1("sdl2_sprite_init: ");
+	err_write_1("gl_sprite_init: ");
         err_inspect_str(&tmp.real_path);
         err_write_1(": glTexParameteri: ");
         err_puts(gl_error_string(gl_error));
@@ -323,7 +324,7 @@ s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
       //             0, GL_RGBA, GL_UNSIGNED_BYTE, data);
       gl_error = glGetError();
       if (gl_error != GL_NO_ERROR) {
-	err_write_1("sdl2_sprite_init: ");
+	err_write_1("gl_sprite_init: ");
         err_inspect_str(&tmp.real_path);
         err_write_1(": glTexImage2D: ");
         err_puts(gl_error_string(gl_error));
diff --git a/libc3/window/sdl2/window_sdl2.c b/libc3/window/sdl2/window_sdl2.c
index 42c2b15..a7d94ed 100644
--- a/libc3/window/sdl2/window_sdl2.c
+++ b/libc3/window/sdl2/window_sdl2.c
@@ -197,9 +197,10 @@ bool window_sdl2_run (s_window_sdl2 *window)
     err_puts("window_sdl2_run: failed to retrieve OpenGL version");
     goto ko;
   }
-  glEnable(GL_DEBUG_OUTPUT);
-  if (glDebugMessageCallback)
+  if (glDebugMessageCallback) {
+    glEnable(GL_DEBUG_OUTPUT);
     glDebugMessageCallback(gl_debug, NULL);
+  }
   if (SDL_GL_MakeCurrent(sdl_window, context) < 0) {
     warnx("window_sdl2_run: failed to make OpenGL context current: %s",
           SDL_GetError());