From 8532e9871a3d8e57fbebb152c3fd1bb09d09b960 Mon Sep 17 00:00:00 2001 From: Adam Date: Wed, 27 Nov 2024 21:35:07 +0000 Subject: [PATCH] library: fix vertex2d_xy y direction --- src/library/doc.texi | 17 +++++++++++++---- src/library/gl.c | 9 ++++++--- src/library/gl.h | 1 + 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/library/doc.texi b/src/library/doc.texi index a9445c0..d09e671 100644 --- a/src/library/doc.texi +++ b/src/library/doc.texi @@ -1925,7 +1925,8 @@ or smaller than that area, proportionally. @subsection vertexxy Given an index of a vertex in a batch, returns its X and Y position on -the screen, in pixel coordinates. +the screen, in pixel coordinates. As with all pixel coordinates in the +Bolt API, this is relative to the top-left pixel of the screen. @node batch2d-vertexatlasxy @subsection vertexatlasxy @@ -1943,7 +1944,13 @@ its associated image in the batch's texture atlas, in pixel coordinates. @subsection vertexuv Given an index of a vertex in a batch, returns the vertex's associated -"UV" coordinates. +"UV" coordinates. The values will be floating-point numbers in the range +0.0 - 1.0. + +Unlike with 3D render events, these UVs are relative to the entire +texture atlas, not to the sub-image. However they may fall outside the +bounds of the sub-image, in which case they're usually expected to wrap +around within it. The values will be floating-point numbers in the range 0.0 - 1.0. They are relative to image in the texture atlas. @@ -2202,8 +2209,10 @@ local x, y, w, h = event:atlasxywh(meta) Given a vertex number, returns the vertex's associated "UV" coordinates. -The values will be floating-point numbers in the range 0.0 - 1.0. They -are relative to image in the texture atlas. +The values will be floating-point numbers, usually in the range 0.0 - +1.0. They are relative to image in the texture atlas. They may fall +outside the image (and therefore outside the range 0.0 - 1.0), in which +case they're expected to wrap around within the image. @node render3d-vertexcolour @subsection vertexcolour diff --git a/src/library/gl.c b/src/library/gl.c index 642246d..31f29d2 100644 --- a/src/library/gl.c +++ b/src/library/gl.c @@ -1445,13 +1445,14 @@ void _bolt_gl_onDrawElements(GLenum mode, GLsizei count, GLenum type, const void vertex_userdata.atlas_size = &attributes[c->bound_program->loc_aTextureUVAtlasExtents]; vertex_userdata.tex_uv = &attributes[c->bound_program->loc_aTextureUV]; vertex_userdata.colour = &attributes[c->bound_program->loc_aVertexColour]; + vertex_userdata.screen_height = roundf(2.0 / projection_matrix[5]); struct GLPluginTextureUserData tex_userdata; tex_userdata.tex = tex; struct RenderBatch2D batch; batch.screen_width = roundf(2.0 / projection_matrix[0]); - batch.screen_height = roundf(2.0 / projection_matrix[5]); + batch.screen_height = vertex_userdata.screen_height; batch.index_count = count; batch.vertices_per_icon = 6; batch.is_minimap = tex_target && tex_target->is_minimap_tex_small; @@ -1687,11 +1688,13 @@ void _bolt_gl_onViewport(GLint x, GLint y, GLsizei width, GLsizei height) { static void _bolt_gl_plugin_drawelements_vertex2d_xy(size_t index, void* userdata, int32_t* out) { struct GLPluginDrawElementsVertex2DUserData* data = userdata; - if (!_bolt_get_attr_binding_int(data->c, data->position, data->indices[index], 2, out)) { + if (_bolt_get_attr_binding_int(data->c, data->position, data->indices[index], 2, out)) { + out[1] = (int32_t)data->screen_height - (out[1] + 1); + } else { float pos[2]; _bolt_get_attr_binding(data->c, data->position, data->indices[index], 2, pos); out[0] = (int32_t)roundf(pos[0]); - out[1] = (int32_t)roundf(pos[1]); + out[1] = (int32_t)data->screen_height - ((int32_t)roundf(pos[1]) + 1); } } diff --git a/src/library/gl.h b/src/library/gl.h index 6608569..db4e194 100644 --- a/src/library/gl.h +++ b/src/library/gl.h @@ -336,6 +336,7 @@ struct GLPluginDrawElementsVertex2DUserData { struct GLAttrBinding* atlas_size; struct GLAttrBinding* tex_uv; struct GLAttrBinding* colour; + uint32_t screen_height; }; struct GLPluginDrawElementsVertex3DUserData {