remove the need for a second pair of texture coordinates

Sun, 01 Jun 2025 14:53:59 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 01 Jun 2025 14:53:59 +0200
changeset 135
062703d3f5cc
parent 134
42dcc8134a68
child 136
768e6eac1ab0

remove the need for a second pair of texture coordinates

shader/sprite_frag.glsl file | annotate | diff | comparison | revisions
shader/sprite_vtx.glsl file | annotate | diff | comparison | revisions
src/text.c file | annotate | diff | comparison | revisions
src/texture.c file | annotate | diff | comparison | revisions
--- a/shader/sprite_frag.glsl	Sun Jun 01 14:33:58 2025 +0200
+++ b/shader/sprite_frag.glsl	Sun Jun 01 14:53:59 2025 +0200
@@ -1,12 +1,11 @@
 #version 400 core
 
 layout(location = 0) out vec4 diffuse;
-in vec2 texcoord;
 in vec2 uvcoord;
 
 uniform sampler2D uv_tex;
 uniform sampler2DRect rect_tex;
 
 void main(void) {
-    diffuse = texture(rect_tex, texcoord) * texture(uv_tex, uvcoord);
+    diffuse = texture(rect_tex, uvcoord) * texture(uv_tex, uvcoord);
 }
--- a/shader/sprite_vtx.glsl	Sun Jun 01 14:33:58 2025 +0200
+++ b/shader/sprite_vtx.glsl	Sun Jun 01 14:53:59 2025 +0200
@@ -2,7 +2,6 @@
 
 layout(location = 0) in vec2 in_pos;
 layout(location = 1) in vec2 in_uv;
-out vec2 texcoord;
 out vec2 uvcoord;
 
 uniform mat4 projection;
@@ -13,6 +12,4 @@
     vec4 pos = projection*view*model*vec4(in_pos.xy, 0.0, 1.0);
     gl_Position = pos;
     uvcoord = in_uv;
-    // TODO: we don't need that in the future
-    texcoord = vec2(model[0].x, model[1].y)*in_pos;
 }
--- a/src/text.c	Sun Jun 01 14:33:58 2025 +0200
+++ b/src/text.c	Sun Jun 01 14:53:59 2025 +0200
@@ -60,16 +60,20 @@
         text->offx = newoffx;
     }
 
+    // Transfer Image Data
+    asc_texture_from_surface(sprite->texture, surface);
+
     // If dimensions changed, update the mesh
     if (text->dimension.x != (unsigned)surface->w || text->dimension.y != (unsigned)surface->h) {
         text->dimension.x = surface->w;
         text->dimension.y = surface->h;
-        asc_mesh_plane_2d(&text->base.mesh, .size = asc_vec2f_new(surface->w, surface->h));
+        const asc_vec2f uv_scale = asc_texture_calculate_uv_scale(sprite->texture, text->dimension, asc_vec2f_one);
+        asc_mesh_plane_2d(&text->base.mesh,
+            .size = asc_vec2f_new(surface->w, surface->h),
+            .uv_scale = uv_scale
+        );
     }
 
-    // Transfer Image Data
-    asc_texture_from_surface(sprite->texture, surface);
-
     // Free the surface
     SDL_FreeSurface(surface);
 
--- a/src/texture.c	Sun Jun 01 14:33:58 2025 +0200
+++ b/src/texture.c	Sun Jun 01 14:53:59 2025 +0200
@@ -168,9 +168,20 @@
         return factors;
     }
     asc_vec2f uv_scale = factors;
-    uv_scale.u *= (float) surface_dimension.width;
-    uv_scale.u /= (float) tex->width;
-    uv_scale.v *= (float) surface_dimension.height;
-    uv_scale.v /= (float) tex->height;
+    // non-normalized coordinates for rectangle textures
+    if (tex->target == GL_TEXTURE_RECTANGLE) {
+        uv_scale.x *= (float) tex->width;
+        uv_scale.y *= (float) tex->height;
+    }
+    // apply scaling
+    if (surface_dimension.width != tex->width) {
+        uv_scale.u *= (float) surface_dimension.width;
+        uv_scale.u /= (float) tex->width;
+    }
+    if (surface_dimension.height != tex->height) {
+        uv_scale.v *= (float) surface_dimension.height;
+        uv_scale.v /= (float) tex->height;
+    }
+
     return uv_scale;
 }

mercurial