Commit 134f9f5884aba34eb4b2ec7e3abcb9fe90840d40

Thomas de Grivel 2023-12-21T19:12:01

wip OpenGL

diff --git a/libc3/window/sdl2/gl_matrix_4d.c b/libc3/window/sdl2/gl_matrix_4d.c
index af89ad6..f176272 100644
--- a/libc3/window/sdl2/gl_matrix_4d.c
+++ b/libc3/window/sdl2/gl_matrix_4d.c
@@ -114,6 +114,13 @@ s_gl_matrix_4d * gl_matrix_4d_new_zero (void)
   return m;
 }
 
+s_gl_matrix_4d * gl_matrix_4d_ortho (s_gl_matrix_4d *m, f64 x1, f64 x2,
+                                     f64 y1, f64 y2, f64 clip_z_near,
+                                     f64 clip_z_far)
+{
+  
+}
+
 s_gl_matrix_4d * gl_matrix_4d_perspective (s_gl_matrix_4d *m, f64 fov_y,
                                            f64 aspect_ratio,
                                            f64 z_near,
diff --git a/libc3/window/sdl2/gl_matrix_4d.h b/libc3/window/sdl2/gl_matrix_4d.h
index ef26286..4d4be24 100644
--- a/libc3/window/sdl2/gl_matrix_4d.h
+++ b/libc3/window/sdl2/gl_matrix_4d.h
@@ -33,6 +33,9 @@ s_gl_matrix_4d * gl_matrix_4d_new_matrix_mult (const s_gl_matrix_4d *a,
 s_gl_matrix_4d * gl_matrix_4d_new_zero (void);
 
 /* Operators. */
+s_gl_matrix_4d * gl_matrix_4d_ortho (s_gl_matrix_4d *m, f64 x1, f64 x2,
+                                     f64 y1, f64 y2, f64 clip_z_near,
+                                     f64 clip_z_far);
 s_gl_matrix_4d * gl_matrix_4d_perspective (s_gl_matrix_4d *m, f64 fov_y,
                                            f64 aspect_ratio,
                                            f64 clip_z_near,
diff --git a/libc3/window/sdl2/gl_ortho.c b/libc3/window/sdl2/gl_ortho.c
new file mode 100644
index 0000000..e23de22
--- /dev/null
+++ b/libc3/window/sdl2/gl_ortho.c
@@ -0,0 +1,118 @@
+/* c3
+ * Copyright 2022,2023 kmx.io <contact@kmx.io>
+ *
+ * Permission is hereby granted to use this software granted the above
+ * copyright notice and this permission paragraph are included in all
+ * copies and substantial portions of this software.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
+ * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
+ * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
+ * THIS SOFTWARE.
+ */
+#include <math.h>
+#include <libc3/c3.h>
+#include "gl_ortho.h"
+#include "gl_matrix_4d.h"
+
+static const s8 * g_gl_ortho_vertex_shader_src = "#version 460 core\n"
+"layout (location = 0) in dvec3 aPos;\n"
+"uniform dmat4 matrix;\n"
+"\n"
+"void main() {\n"
+"  gl_Position = vec4(matrix * dvec4(aPos, 1.0));\n"
+"}\n";
+
+void gl_ortho_clean (s_gl_ortho *ortho)
+{
+  assert(ortho);
+  glDeleteProgram(ortho->gl_shader_program);
+}
+
+void gl_ortho_delete (s_gl_ortho *ortho)
+{
+  gl_ortho_clean(ortho);
+  free(ortho);
+}
+
+s_gl_ortho * gl_ortho_init (s_gl_ortho *ortho, uw w, uw h)
+{
+  GLint success;
+  u32 vertex_shader;
+  assert(ortho);
+  ortho->clip_z_far = 1.0;
+  ortho->clip_z_near = -1.0;
+  ortho->fov_y = 90.0;
+  ortho->position.x = 0.0;
+  ortho->position.y = 0.0;
+  ortho->position.z = 0.0;
+  ortho->rotation = 0.0;
+  vertex_shader = glCreateShader(GL_VERTEX_SHADER);
+  glShaderSource(vertex_shader, 1, &g_gl_ortho_vertex_shader_src,
+                 NULL);
+  glCompileShader(vertex_shader);
+  glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success);
+  if (! success) {
+    s8 info_log[512];
+    glGetShaderInfoLog(vertex_shader, sizeof(info_log), NULL, info_log);
+    err_write_1("gl_ortho_init: shader compilation failed: ");
+    err_puts(info_log);
+  }
+  ortho->gl_shader_program = glCreateProgram();
+  glAttachShader(ortho->gl_shader_program, vertex_shader);
+  glLinkProgram(ortho->gl_shader_program);
+  glDeleteShader(vertex_shader);
+  ortho->gl_matrix_loc =
+    glGetUniformLocation(ortho->gl_shader_program, "matrix");
+  return ortho;
+}
+
+s_gl_ortho * gl_ortho_new (uw w, uw h)
+{
+  s_gl_ortho *ortho;
+  ortho = calloc(1, sizeof(s_gl_ortho));
+  if (! ortho) {
+    err_puts("gl_ortho_new: failed to allocate memory");
+    return NULL;
+  }
+  if (! gl_ortho_init(ortho, w, h)) {
+    free(ortho);
+    return NULL;
+  }
+  return ortho;
+}
+
+void gl_ortho_render (s_gl_ortho *ortho)
+{
+  assert(ortho);
+  gl_matrix_4d_init_identity(&ortho->matrix);
+  gl_matrix_4d_ortho(&ortho->matrix, ortho->x1, ortho->x2,
+                     ortho->y1, ortho->y2,
+                     ortho->clip_z_near, ortho->clip_z_far);
+  gl_matrix_4d_translate(&ortho->matrix, ortho->position.x,
+                         ortho->position.y, ortho->position.z);
+  gl_matrix_4d_rotate_axis(&ortho->matrix, ortho->rotation.x,
+                           &(s_gl_point_3d) { 1.0, 0.0, 0.0 });
+  gl_matrix_4d_rotate_axis(&ortho->matrix, ortho->rotation.y,
+                           &(s_gl_point_3d) { 0.0, 1.0, 0.0 });
+  gl_matrix_4d_rotate_axis(&ortho->matrix, ortho->rotation.z,
+                           &(s_gl_point_3d) { 0.0, 0.0, 1.0 });
+  glUseProgram(ortho->gl_shader_program);
+  glUniformMatrix4dv(ortho->gl_matrix_loc, 1, GL_FALSE,
+                     &ortho->matrix.xx);
+}
+
+void gl_ortho_render_end (s_gl_ortho *ortho)
+{
+  assert(ortho);
+  (void) ortho;
+  glUseProgram(0);
+}
+
+s_gl_ortho * gl_ortho_set_aspect_ratio (s_gl_ortho *ortho, uw w,
+                                          uw h)
+{
+  assert(ortho);
+  ortho->aspect_ratio = (f64) (w ? w : 1) / (h ? h : 1);
+  return ortho;
+}
diff --git a/libc3/window/sdl2/sdl2_sprite.c b/libc3/window/sdl2/sdl2_sprite.c
index 863e349..5f365f8 100644
--- a/libc3/window/sdl2/sdl2_sprite.c
+++ b/libc3/window/sdl2/sdl2_sprite.c
@@ -95,9 +95,9 @@ s_sdl2_sprite * sdl2_sprite_init (s_sdl2_sprite *sprite,
 {
   u8 *data;
   FILE *fp;
-  GLenum gl_format = 0;
-  GLint  gl_internal_format = 0;
-  GLenum gl_type = 0;
+  GLenum gl_format;
+  GLint  gl_internal_format;
+  GLenum gl_type;
   uw i;
   s32         png_bit_depth;
   s32         png_color_type;
@@ -175,6 +175,7 @@ s_sdl2_sprite * sdl2_sprite_init (s_sdl2_sprite *sprite,
   }
   gl_internal_format = 0;
   gl_format = 0;
+  gl_type = 0;
   png_components = 0;
   if (setjmp(png_jmpbuf(png_read))) {
     png_destroy_read_struct(&png_read, &png_info, NULL);
diff --git a/libc3/window/sdl2/types.h b/libc3/window/sdl2/types.h
index 00ff063..8db1e38 100644
--- a/libc3/window/sdl2/types.h
+++ b/libc3/window/sdl2/types.h
@@ -18,13 +18,8 @@
 #ifndef LIBC3_WINDOW_SDL2_TYPES_H
 #define LIBC3_WINDOW_SDL2_TYPES_H
 
-#include <SDL.h>
 #include <GL/glew.h>
-#if defined(__APPLE__)
-# include <OpenGL/glcorearb.h>
-# include <OpenGL/glu.h>
-#else
-#endif
+#include <SDL.h>
 #include <FTGL/ftgl.h>
 #include <png.h>
 #include <libc3/types.h>
@@ -35,6 +30,7 @@ typedef struct gl_cylinder  s_gl_cylinder;
 typedef struct gl_matrix_3d s_gl_matrix_3d;
 typedef struct gl_matrix_4d s_gl_matrix_4d;
 typedef struct gl_object    s_gl_object;
+typedef struct gl_ortho     s_gl_ortho;
 typedef struct gl_point_2d  s_gl_point_2d;
 typedef struct gl_point_3d  s_gl_point_3d;
 typedef struct gl_sphere    s_gl_sphere;
@@ -203,6 +199,20 @@ struct gl_cylinder {
   uw segments_v;
 };
 
+struct gl_ortho {
+  f64 x1;
+  f64 x2;
+  f64 y1;
+  f64 y2;
+  f64 clip_z_near;
+  f64 clip_z_far;
+  s_gl_point_3d position;
+  s_gl_point_3d rotation;
+  s_gl_matrix_4d matrix;
+  u32 gl_matrix_loc;
+  u32 gl_shader_program;
+};
+
 struct gl_sphere {
   s_gl_object object;
   uw segments_u;