render: implement coordinate-based anchor positioning

Not to be confused with Minecraft coordinates.

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Ifdb90fc92a1565ba1d30b85c91d6e1ab6a6a6964
This commit is contained in:
raf 2026-04-15 11:54:30 +03:00
commit dadba853e8
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
4 changed files with 168 additions and 85 deletions

View file

@ -43,7 +43,7 @@ static void get_gl_filter_params(chroma_filter_quality_t quality,
// Calculate texture coordinates based on scaling mode and anchor position
static void calculate_texture_coords(chroma_scale_mode_t scale_mode,
chroma_anchor_t anchor,
float anchor_x, float anchor_y,
int image_width, int image_height,
int output_width, int output_height,
float tex_coords[8]) {
@ -143,53 +143,13 @@ static void calculate_texture_coords(chroma_scale_mode_t scale_mode,
break;
}
// Apply anchor-based offset by shifting the visible crop region
// For fill: shift which part of image is shown
// For fit/center: shift where the image is positioned
//
// Positive shift reveals left/bottom portion
// Negative shift reveals right/top portion
float u_shift = 0.0f;
float v_shift = 0.0f;
switch (anchor) {
case CHROMA_ANCHOR_CENTER:
u_shift = 0.0f;
v_shift = 0.0f;
break;
case CHROMA_ANCHOR_TOP:
u_shift = 0.0f;
v_shift = -0.5f; // shift up to show top of image
break;
case CHROMA_ANCHOR_BOTTOM:
u_shift = 0.0f;
v_shift = 0.5f; // shift down to show bottom of image
break;
case CHROMA_ANCHOR_LEFT:
u_shift = -0.5f; // shift left to show left of image
v_shift = 0.0f;
break;
case CHROMA_ANCHOR_RIGHT:
u_shift = 0.5f; // shift right to show right of image
v_shift = 0.0f;
break;
case CHROMA_ANCHOR_TOP_LEFT:
u_shift = -0.5f;
v_shift = -0.5f;
break;
case CHROMA_ANCHOR_TOP_RIGHT:
u_shift = 0.5f;
v_shift = -0.5f;
break;
case CHROMA_ANCHOR_BOTTOM_LEFT:
u_shift = -0.5f;
v_shift = 0.5f;
break;
case CHROMA_ANCHOR_BOTTOM_RIGHT:
u_shift = 0.5f;
v_shift = 0.5f;
break;
}
// Apply anchor-based offset using anchor_x and anchor_y (0-100, 50=center)
// anchor_x: 0=left edge, 50=center, 100=right edge
// anchor_y: 0=top edge, 50=center, 100=bottom edge
// u_shift: negative moves view left (shows more right side of image)
// v_shift: negative moves view up (shows more top of image)
float u_shift = (anchor_x - 50.0f) / 50.0f; // -1 to 1
float v_shift = (50.0f - anchor_y) / 50.0f; // -1 to 1 (inverted for top-down)
// Calculate the shift amount based on crop/border space available
float u_crop = 1.0f - (u2 - u1);
@ -755,11 +715,13 @@ int chroma_render_wallpaper(chroma_state_t *state, chroma_output_t *output) {
// Update VBO only if needed. E.g, image changed, scale mode changed, or first
// render
if (output->vbo_dirty) {
// Calculate texture coordinates based on scaling mode and anchor
// Calculate texture coordinates based on scaling mode, anchor, and anchor
// coords
float tex_coords[8];
calculate_texture_coords(output->scale_mode, output->anchor,
output->image->width, output->image->height,
output->width, output->height, tex_coords);
calculate_texture_coords(output->scale_mode, output->anchor_x,
output->anchor_y, output->image->width,
output->image->height, output->width,
output->height, tex_coords);
// Create dynamic vertex data with calculated texture coordinates
float dynamic_vertices[] = {