Commit 93f7cdbd2875f082c8259b11b97ea343eaa6f737

Thomas de Grivel 2024-01-28T22:57:23

transform

diff --git a/libc3/window/sdl2/demo/earth.c b/libc3/window/sdl2/demo/earth.c
index e078cae..1dc4bb0 100644
--- a/libc3/window/sdl2/demo/earth.c
+++ b/libc3/window/sdl2/demo/earth.c
@@ -13,9 +13,11 @@
 #include <math.h>
 #include <libc3/c3.h>
 #include "../window_sdl2.h"
-#include "../gl_sprite.h"
 #include "../gl_camera.h"
+#include "../gl_matrix_4f.h"
+#include "../gl_object.h"
 #include "../gl_sphere.h"
+#include "../gl_sprite.h"
 #include "earth.h"
 
 #define EARTH_CAMERA_ROTATION_Z_SPEED 0.1
@@ -27,8 +29,10 @@ s_gl_sprite g_sprite_earth = {0};
 bool earth_load (s_sequence *seq)
 {
   s_map *map;
+  s_gl_matrix_4f matrix;
   s_gl_camera *camera;
   s_gl_sphere *sphere;
+  const f32    sphere_radius = 5.0;
   s_window_sdl2 *window;
   assert(seq);
   window = seq->window;
@@ -39,6 +43,9 @@ bool earth_load (s_sequence *seq)
   sphere = gl_sphere_new(EARTH_SEGMENTS_U, EARTH_SEGMENTS_V);
   if (! sphere)
     return false;
+  gl_matrix_4f_init_scale(&matrix, sphere_radius, sphere_radius,
+                          sphere_radius);
+  gl_object_transform(&sphere->object, &matrix);
   if (! tag_map(&seq->tag, 3))
     return false;
   map = &seq->tag.data.map;
@@ -111,18 +118,17 @@ bool earth_render (s_sequence *seq)
   assert(glGetError() == GL_NO_ERROR);
   //glPushMatrix();
   {
-    //sphere_radius = 5.0;
     assert(glGetError() == GL_NO_ERROR);
-    //glScalef(sphere_radius, sphere_radius, sphere_radius);
     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));
+    assert(glGetError() == GL_NO_ERROR);
+    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_camera_color(camera, 1.0f, 1.0f, 1.0f, 1.0f);
     gl_sphere_render(sphere);
     /*
     glDisable(GL_TEXTURE_2D);
diff --git a/libc3/window/sdl2/gl_camera.c b/libc3/window/sdl2/gl_camera.c
index 6189528..e0590ff 100644
--- a/libc3/window/sdl2/gl_camera.c
+++ b/libc3/window/sdl2/gl_camera.c
@@ -20,29 +20,29 @@ static const char * g_gl_camera_vertex_shader_src =
   "layout (location = 0) in vec3 iPos;\n"
   "layout (location = 1) in vec3 iNormal;\n"
   "layout (location = 2) in vec2 iTexCoord;\n"
-  "out vec3 oFragNormal;\n"
-  "out vec2 oTexCoord;\n"
+  "out vec3 ioFragNormal;\n"
+  "out vec2 ioTexCoord;\n"
   "uniform mat4 uProjectionMatrix;\n"
   "uniform mat4 uViewMatrix;\n"
   "uniform mat4 uModelMatrix;\n"
   "void main() {\n"
   "  gl_Position = vec4(uProjectionMatrix * uViewMatrix *\n"
   "                     uModelMatrix * vec4(iPos, 1.0));\n"
-  "  oFragNormal = vec3(mat3(transpose(inverse(uModelMatrix))) *\n"
-  "                     iNormal);\n"
-  "  oTexCoord = iTexCoord;\n"
+  "  ioFragNormal = vec3(mat3(transpose(inverse(uModelMatrix))) *\n"
+  "                      iNormal);\n"
+  "  ioTexCoord = iTexCoord;\n"
   "}\n";
 
 static const char * g_gl_camera_fragment_shader_src =
   "#version 330 core\n"
-  "in vec3 iFragNormal;\n"
-  "in vec2 iTexCoord;\n"
+  "in vec3 ioFragNormal;\n"
+  "in vec2 ioTexCoord;\n"
   "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"
+  "  vec4 texColor = texture(uTex2D, ioTexCoord);\n"
   "  if (uEnableTex2D) {\n"
   "    oFragColor = texColor * uColor;\n"
   "  }\n"
@@ -55,13 +55,23 @@ void gl_camera_bind_texture (s_gl_camera *camera, GLuint texture)
   assert(camera);
   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(camera->gl_enable_tex2d_loc, 0);
     assert(glGetError() == GL_NO_ERROR);
+    glUniform1i(camera->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(camera->gl_enable_tex2d_loc, 1);
   assert(glGetError() == GL_NO_ERROR);
-  glUniform1i(camera->gl_tex2d_loc, texture);  
+  glUniform1i(camera->gl_tex2d_loc, 0);
   assert(glGetError() == GL_NO_ERROR);
 }
 
@@ -71,6 +81,15 @@ void gl_camera_clean (s_gl_camera *camera)
   glDeleteProgram(camera->gl_shader_program);
 }
 
+void gl_camera_color (s_gl_camera *camera, f32 r, f32 g, f32 b, f32 a)
+{
+  s_rgba color = {r, g, b, a};
+  assert(camera);
+  assert(glGetError() == GL_NO_ERROR);
+  glUniform4fv(camera->gl_color_loc, 1, &color.r);    
+  assert(glGetError() == GL_NO_ERROR);
+}
+
 void gl_camera_delete (s_gl_camera *camera)
 {
   gl_camera_clean(camera);
diff --git a/libc3/window/sdl2/gl_camera.h b/libc3/window/sdl2/gl_camera.h
index 7ccdc65..7aabf3c 100644
--- a/libc3/window/sdl2/gl_camera.h
+++ b/libc3/window/sdl2/gl_camera.h
@@ -27,6 +27,7 @@ s_gl_camera * gl_camera_new (uw w, uw h);
 /* Operators. */
 void          gl_camera_bind_texture (s_gl_camera *camera,
                                       GLuint texture);
+void          gl_camera_color (s_gl_camera *camera, f32 r, f32 g, f32 b, f32 a);
 void          gl_camera_render (s_gl_camera *camera);
 s_gl_camera * gl_camera_set_aspect_ratio (s_gl_camera *camera, uw w,
                                           uw h);
diff --git a/libc3/window/sdl2/gl_matrix_4f.c b/libc3/window/sdl2/gl_matrix_4f.c
index 8cb4279..3d77b3e 100644
--- a/libc3/window/sdl2/gl_matrix_4f.c
+++ b/libc3/window/sdl2/gl_matrix_4f.c
@@ -105,6 +105,17 @@ s_gl_matrix_4f * gl_matrix_4f_init_identity (s_gl_matrix_4f *m)
   return m;
 }
 
+s_gl_matrix_4f * gl_matrix_4f_init_scale (s_gl_matrix_4f *m, f32 x,
+                                          f32 y, f32 z)
+{
+  assert(m);
+  m->xx = x;   m->xy = 0.0; m->xz = 0.0; m->xt = 0.0;
+  m->yx = 0.0; m->yy = y;   m->yz = 0.0; m->yt = 0.0;
+  m->zx = 0.0; m->zy = 0.0; m->zz = z;   m->zt = 0.0;
+  m->tx = 0.0; m->ty = 0.0; m->tz = 0.0; m->tt = 1.0;
+  return m;
+}
+
 s_gl_matrix_4f * gl_matrix_4f_init_zero (s_gl_matrix_4f *m)
 {
   assert(m);
diff --git a/libc3/window/sdl2/gl_matrix_4f.h b/libc3/window/sdl2/gl_matrix_4f.h
index 8b38aa3..f85f339 100644
--- a/libc3/window/sdl2/gl_matrix_4f.h
+++ b/libc3/window/sdl2/gl_matrix_4f.h
@@ -22,6 +22,8 @@ s_gl_matrix_4f * gl_matrix_4f_init_identity (s_gl_matrix_4f *m);
 s_gl_matrix_4f * gl_matrix_4f_init_product (s_gl_matrix_4f *m,
                                             const s_gl_matrix_4f *a,
                                             const s_gl_matrix_4f *b);
+s_gl_matrix_4f * gl_matrix_4f_init_scale (s_gl_matrix_4f *m, f32 x,
+                                          f32 y, f32 z);
 s_gl_matrix_4f * gl_matrix_4f_init_zero (s_gl_matrix_4f *m);
 
 /* Heap-allocation functions, call gl_matrix_4f_delete after use. */
diff --git a/libc3/window/sdl2/gl_object.h b/libc3/window/sdl2/gl_object.h
index 023406c..679146e 100644
--- a/libc3/window/sdl2/gl_object.h
+++ b/libc3/window/sdl2/gl_object.h
@@ -27,6 +27,8 @@ s_gl_object * gl_object_new (void);
 /* Operators. */
 s_gl_object * gl_object_allocate (s_gl_object *object, uw vertex_count,
                                   uw index_count);
+void          gl_object_transform (s_gl_object *object,
+                                   const s_gl_matrix_4f *matrix);
 bool          gl_object_update (s_gl_object *object);
 
 /* Observers. */
diff --git a/libc3/window/sdl2/gl_ortho.c b/libc3/window/sdl2/gl_ortho.c
index b8ec10a..2463179 100644
--- a/libc3/window/sdl2/gl_ortho.c
+++ b/libc3/window/sdl2/gl_ortho.c
@@ -22,29 +22,29 @@ static const char * g_gl_ortho_vertex_shader_src =
   "layout (location = 0) in vec3 iPos;\n"
   "layout (location = 1) in vec3 iNormal;\n"
   "layout (location = 2) in vec2 iTexCoord;\n"
-  "out vec3 fragNormal;\n"
-  "out vec2 texCoord;\n"
+  "out vec3 ioFragNormal;\n"
+  "out vec2 ioTexCoord;\n"
   "uniform mat4 uProjectionMatrix;\n"
   "uniform mat4 uViewMatrix;\n"
   "uniform mat4 uModelMatrix;\n"
   "void main() {\n"
   "  gl_Position = vec4(uProjectionMatrix * uViewMatrix * \n"
   "                     uModelMatrix * vec4(iPos, 1.0));\n"
-  "  texCoord = iTexCoord;\n"
-  "  fragNormal = vec3(mat3(transpose(inverse(uModelMatrix))) *\n"
-  "                    iNormal);\n"
+  "  ioTexCoord = iTexCoord;\n"
+  "  ioFragNormal = vec3(mat3(transpose(inverse(uModelMatrix))) *\n"
+  "                      iNormal);\n"
   "}\n";
 
 static const char * g_gl_ortho_fragment_shader_src =
   "#version 330 core\n"
-  "in vec3 fragNormal;\n"
-  "in vec2 texCoord;\n"
+  "in vec3 ioFragNormal;\n"
+  "in vec2 ioTexCoord;\n"
   "out vec4 oFragColor;\n"
   "uniform vec4 uColor;\n"
   "uniform bool uEnableTex2D;\n"
   "uniform sampler2D uTex2D;\n"
   "void main() {\n"
-  "  vec4 texColor = texture(uTex2D, texCoord);\n"
+  "  vec4 texColor = texture(uTex2D, ioTexCoord);\n"
   "  if (uEnableTex2D) {\n"
   "    oFragColor = vec4(texColor[0] * uColor[0],\n"
   "                      texColor[1] * uColor[1],\n"