Commit bcc2695eb96b1281e27f06426d9c1bdb01201f94

Thomas de Grivel 2024-01-11T16:29:42

opengl translation

diff --git a/libc3/window/sdl2/gl_matrix_4f.c b/libc3/window/sdl2/gl_matrix_4f.c
index dfca8d9..7236514 100644
--- a/libc3/window/sdl2/gl_matrix_4f.c
+++ b/libc3/window/sdl2/gl_matrix_4f.c
@@ -175,9 +175,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;
@@ -230,9 +230,9 @@ s_gl_matrix_4f * gl_matrix_4f_scale (s_gl_matrix_4f *m, f32 x, f32 y,
 s_gl_matrix_4f * gl_matrix_4f_translate (s_gl_matrix_4f *m, f32 x,
                                          f32 y, f32 z)
 {
-  m->xt += x;
-  m->yt += y;
-  m->zt += z;
+  m->tx += x;
+  m->ty += y;
+  m->tz += z;
   return m;
 }
 
diff --git a/libc3/window/sdl2/gl_ortho.c b/libc3/window/sdl2/gl_ortho.c
index 434ecaf..fd7ef06 100644
--- a/libc3/window/sdl2/gl_ortho.c
+++ b/libc3/window/sdl2/gl_ortho.c
@@ -15,7 +15,8 @@
 #include "gl_ortho.h"
 #include "gl_matrix_4f.h"
 
-static const char * g_gl_ortho_vertex_shader_src = "#version 330 core\n"
+static const char * g_gl_ortho_vertex_shader_src =
+  "#version 330 core\n"
   "layout (location = 0) in vec3 aPos;\n"
   "layout (location = 1) in vec3 aNorm;\n"
   "layout (location = 2) in vec2 aTexCoord;\n"
@@ -33,6 +34,17 @@ static const char * g_gl_ortho_vertex_shader_src = "#version 330 core\n"
   "  TexCoord = aTexCoord;\n"
   "}\n";
 
+static const char * g_gl_ortho_fragment_shader_src =
+  "#version 330 core\n"
+  "in vec3 FragNormal;\n"
+  "in vec2 TexCoord;\n"
+  "out vec4 FragColor;\n"
+  "uniform sampler2D texture2D;\n"
+  "void main() {\n"
+  "  vec4 textureColor = texture(texture2D, TexCoord);\n"
+  "  FragColor = textureColor;"
+  "}\n";
+
 void gl_ortho_clean (s_gl_ortho *ortho)
 {
   assert(ortho);
@@ -47,8 +59,9 @@ void gl_ortho_delete (s_gl_ortho *ortho)
 
 s_gl_ortho * gl_ortho_init (s_gl_ortho *ortho)
 {
+  GLuint fragment_shader;
   GLint success;
-  u32 vertex_shader;
+  GLuint vertex_shader;
   assert(ortho);
   gl_matrix_4f_init_identity(&ortho->projection_matrix);
   gl_matrix_4f_ortho(&ortho->projection_matrix, -1, 1, -1, 1, 0, 1);
@@ -74,14 +87,29 @@ s_gl_ortho * gl_ortho_init (s_gl_ortho *ortho)
     err_write_1("gl_ortho_init: shader compilation failed: ");
     err_puts(info_log);
   }
+  fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
+  glShaderSource(fragment_shader, 1, &g_gl_ortho_fragment_shader_src,
+                 NULL);
+  glCompileShader(fragment_shader);
+  glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success);
+  if (! success) {
+    char info_log[512];
+    glGetShaderInfoLog(fragment_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();
   assert(glGetError() == GL_NO_ERROR);
   glAttachShader(ortho->gl_shader_program, vertex_shader);
   assert(glGetError() == GL_NO_ERROR);
+  glAttachShader(ortho->gl_shader_program, fragment_shader);
+  assert(glGetError() == GL_NO_ERROR);
   glLinkProgram(ortho->gl_shader_program);
   assert(glGetError() == GL_NO_ERROR);
   glDeleteShader(vertex_shader);
   assert(glGetError() == GL_NO_ERROR);
+  glDeleteShader(fragment_shader);
+  assert(glGetError() == GL_NO_ERROR);
   ortho->gl_projection_matrix_loc =
     glGetUniformLocation(ortho->gl_shader_program, "projection_matrix");
   assert(glGetError() == GL_NO_ERROR);
@@ -91,6 +119,9 @@ s_gl_ortho * gl_ortho_init (s_gl_ortho *ortho)
   ortho->gl_model_matrix_loc =
     glGetUniformLocation(ortho->gl_shader_program, "model_matrix");
   assert(glGetError() == GL_NO_ERROR);
+  ortho->gl_texture_loc =
+    glGetUniformLocation(ortho->gl_shader_program, "texture2D");
+  assert(glGetError() == GL_NO_ERROR);
   return ortho;
 }
 
@@ -129,6 +160,8 @@ 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);
+  glUniform1i(ortho->gl_texture_loc, 0);
+  glUniform4f(ortho->gl_color_loc, 1.0f, 1.0f, 1.0f, 1.0f);
   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_point_3f.c b/libc3/window/sdl2/gl_point_3f.c
index 03ed4f4..f49f67d 100644
--- a/libc3/window/sdl2/gl_point_3f.c
+++ b/libc3/window/sdl2/gl_point_3f.c
@@ -56,9 +56,9 @@ s_gl_point_3f * gl_point_3f_init_product (s_gl_point_3f *p,
   assert(p);
   assert(m);
   assert(s);
-  p->x = m->xx * s->x + m->xy * s->y + m->xz * s->z + m->xt;
-  p->y = m->xy * s->x + m->yy * s->y + m->yz * s->z + m->yt;
-  p->z = m->xz * s->x + m->zy * s->y + m->zz * s->z + m->zt;
+  p->x = m->xx * s->x + m->yx * s->y + m->zx * s->z + m->tx;
+  p->y = m->xy * s->x + m->yy * s->y + m->zy * s->z + m->ty;
+  p->z = m->xz * s->x + m->yz * s->y + m->zz * s->z + m->tz;
   return p;
 }
 
diff --git a/libc3/window/sdl2/types.h b/libc3/window/sdl2/types.h
index 58039b3..8536967 100644
--- a/libc3/window/sdl2/types.h
+++ b/libc3/window/sdl2/types.h
@@ -123,39 +123,39 @@ struct gl_matrix_3f {
 
 struct gl_matrix_4d {
   f64 xx;
-  f64 yx;
-  f64 zx;
-  f64 tx;
   f64 xy;
-  f64 yy;
-  f64 zy;
-  f64 ty;
   f64 xz;
-  f64 yz;
-  f64 zz;
-  f64 tz;
   f64 xt;
+  f64 yx;
+  f64 yy;
+  f64 yz;
   f64 yt;
+  f64 zx;
+  f64 zy;
+  f64 zz;
   f64 zt;
+  f64 tx;
+  f64 ty;
+  f64 tz;
   f64 tt;
 };
 
 struct gl_matrix_4f {
   f32 xx;
-  f32 yx;
-  f32 zx;
-  f32 tx;
   f32 xy;
-  f32 yy;
-  f32 zy;
-  f32 ty;
   f32 xz;
-  f32 yz;
-  f32 zz;
-  f32 tz;
   f32 xt;
+  f32 yx;
+  f32 yy;
+  f32 yz;
   f32 yt;
+  f32 zx;
+  f32 zy;
+  f32 zz;
   f32 zt;
+  f32 tx;
+  f32 ty;
+  f32 tz;
   f32 tt;
 };
 
@@ -281,6 +281,8 @@ struct gl_ortho {
   u32         gl_view_matrix_loc;
   s_gl_matrix_4f model_matrix;
   u32         gl_model_matrix_loc;
+  u32 gl_color_loc;
+  u32 gl_texture_loc;
   u32 gl_shader_program;
 };