From eb7e7f89d647445abebc54d9e5bebb9eff3d66e7 Mon Sep 17 00:00:00 2001 From: Caden Parker Date: Tue, 17 Nov 2026 19:37:59 -0600 Subject: [PATCH 1/2] [rshapes] Fix half-pixel offsets in DrawRectangleLines --- src/rshapes.c | 60 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/src/rshapes.c b/src/rshapes.c index 687d98094a2d..a0c01c5bb41c 100644 --- a/src/rshapes.c +++ b/src/rshapes.c @@ -79,6 +79,11 @@ static Texture2D texShapes = { 1, 1, 1, 1, 7 }; // Texture used on shapes drawing (white pixel loaded by rlgl) static Rectangle texShapesRec = { 0.0f, 0.0f, 1.0f, 1.0f }; // Texture source rectangle used on shapes drawing +//---------------------------------------------------------------------------------- +// Module Internal Functions Declaration +//---------------------------------------------------------------------------------- +static Vector2 CalculateHalfPixelOffsets(void); + //---------------------------------------------------------------------------------- // Module Functions Definition //---------------------------------------------------------------------------------- @@ -745,9 +750,9 @@ void DrawRectangleGradientEx(Rectangle rec, Color col1, Color col2, Color col3, // but it solves another issue: https://github.com/raysan5/raylib/issues/3884 void DrawRectangleLines(int posX, int posY, int width, int height, Color color) { - Matrix mat = rlGetMatrixTransform(); - float xOffset = 0.5f/mat.m0; - float yOffset = 0.5f/mat.m5; + Vector2 halfPixelOffsets = CalculateHalfPixelOffsets(); + float xOffset = halfPixelOffsets.x; + float yOffset = halfPixelOffsets.y; rlBegin(RL_LINES); rlColor4ub(color.r, color.g, color.b, color.a); @@ -1142,17 +1147,21 @@ void DrawRectangleRoundedLines(Rectangle rec, float roundness, int segments, Col P5 ------------------ P4 */ + Vector2 halfPixelOffsets = CalculateHalfPixelOffsets(); + float xOffset = halfPixelOffsets.x; + float yOffset = halfPixelOffsets.y; + // The x-coordinates used for the outline - float x0 = rec.x + radius + 0.5f; - float x1 = (rec.x + rec.width) - radius - 0.5f; - float x2 = (rec.x + rec.width) - 0.5f; - float x3 = rec.x + 0.5f; + float x0 = rec.x + radius + xOffset; + float x1 = (rec.x + rec.width) - radius - xOffset; + float x2 = (rec.x + rec.width) - xOffset; + float x3 = rec.x + xOffset; // The y-coordinates used for the outline - float y0 = rec.y + 0.5f; - float y1 = rec.y + radius + 0.5f; - float y2 = (rec.y + rec.height) - radius - 0.5f; - float y3 = (rec.y + rec.height) - 0.5f; + float y0 = rec.y + yOffset; + float y1 = rec.y + radius + yOffset; + float y2 = (rec.y + rec.height) - radius - yOffset; + float y3 = (rec.y + rec.height) - yOffset; Vector2 points[8] = { { x0, y0 }, // P0 @@ -4548,4 +4557,33 @@ Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2) return overlap; } +//---------------------------------------------------------------------------------- +// Module Internal Functions Definition +//---------------------------------------------------------------------------------- + +// Calculate how much a 2D vertex position should move to shift it to the center of a pixel +static Vector2 CalculateHalfPixelOffsets(void) +{ + Matrix t = rlGetMatrixTransform(); + Matrix m = rlGetMatrixModelview(); + + // Compute upper-left 2x2 of M*T + // ax and ay are the first column of the matrix; bx and by are the second + float ax = m.m0*t.m0 + m.m4*t.m1; + float ay = m.m1*t.m0 + m.m5*t.m1; + float bx = m.m0*t.m4 + m.m4*t.m5; + float by = m.m1*t.m4 + m.m5*t.m5; + + // Column lengths are pixels per world unit along world x- and y-axes + float column0Length = sqrtf(ax*ax + ay*ay); + float column1Length = sqrtf(bx*bx + by*by); + + // Take inverse column lengths: how many world units translate to one pixel + // Divide by two: world units to half-pixel + float xOffset = 0.5f/column0Length; + float yOffset = 0.5f/column1Length; + + return (Vector2){ xOffset, yOffset }; +} + #endif // SUPPORT_MODULE_RSHAPES From 829e7bb2681212fbe2e5b10b934076841daca4c7 Mon Sep 17 00:00:00 2001 From: Caden Parker Date: Mon, 23 Nov 2026 13:00:19 -0600 Subject: [PATCH 2/2] Calculate half-pixel offsets using 2x2 inverse matrix --- src/rshapes.c | 68 +++++++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 40 deletions(-) diff --git a/src/rshapes.c b/src/rshapes.c index a0c01c5bb41c..1f8ce275f27f 100644 --- a/src/rshapes.c +++ b/src/rshapes.c @@ -79,11 +79,6 @@ static Texture2D texShapes = { 1, 1, 1, 1, 7 }; // Texture used on shapes drawing (white pixel loaded by rlgl) static Rectangle texShapesRec = { 0.0f, 0.0f, 1.0f, 1.0f }; // Texture source rectangle used on shapes drawing -//---------------------------------------------------------------------------------- -// Module Internal Functions Declaration -//---------------------------------------------------------------------------------- -static Vector2 CalculateHalfPixelOffsets(void); - //---------------------------------------------------------------------------------- // Module Functions Definition //---------------------------------------------------------------------------------- @@ -750,9 +745,20 @@ void DrawRectangleGradientEx(Rectangle rec, Color col1, Color col2, Color col3, // but it solves another issue: https://github.com/raysan5/raylib/issues/3884 void DrawRectangleLines(int posX, int posY, int width, int height, Color color) { - Vector2 halfPixelOffsets = CalculateHalfPixelOffsets(); - float xOffset = halfPixelOffsets.x; - float yOffset = halfPixelOffsets.y; + Matrix t = rlGetMatrixTransform(); + Matrix m = rlGetMatrixModelview(); + + // Compute upper-left 2x2 of M*T + float ax = m.m0*t.m0 + m.m4*t.m1; + float ay = m.m1*t.m0 + m.m5*t.m1; + float bx = m.m0*t.m4 + m.m4*t.m5; + float by = m.m1*t.m4 + m.m5*t.m5; + + // Use the 2x2 inverse of M*T to convert 0.5 px screen-space to world units + // Using max(|bx|, |by|) and max(|ax|, |ay|) matches OpenGL's diamond-exit rule + float invDet = 0.5f/fabsf(ax*by - ay*bx); + float xOffset = ((fabsf(bx) > fabsf(by))? fabsf(bx) : fabsf(by))*invDet; + float yOffset = ((fabsf(ax) > fabsf(ay))? fabsf(ax) : fabsf(ay))*invDet; rlBegin(RL_LINES); rlColor4ub(color.r, color.g, color.b, color.a); @@ -1147,9 +1153,20 @@ void DrawRectangleRoundedLines(Rectangle rec, float roundness, int segments, Col P5 ------------------ P4 */ - Vector2 halfPixelOffsets = CalculateHalfPixelOffsets(); - float xOffset = halfPixelOffsets.x; - float yOffset = halfPixelOffsets.y; + Matrix t = rlGetMatrixTransform(); + Matrix m = rlGetMatrixModelview(); + + // Compute upper-left 2x2 of M*T + float ax = m.m0*t.m0 + m.m4*t.m1; + float ay = m.m1*t.m0 + m.m5*t.m1; + float bx = m.m0*t.m4 + m.m4*t.m5; + float by = m.m1*t.m4 + m.m5*t.m5; + + // Use the 2x2 inverse of M*T to convert 0.5 px screen-space to world units + // Using max(|bx|, |by|) and max(|ax|, |ay|) matches OpenGL's diamond-exit rule + float invDet = 0.5f/fabsf(ax*by - ay*bx); + float xOffset = ((fabsf(bx) > fabsf(by))? fabsf(bx) : fabsf(by))*invDet; + float yOffset = ((fabsf(ax) > fabsf(ay))? fabsf(ax) : fabsf(ay))*invDet; // The x-coordinates used for the outline float x0 = rec.x + radius + xOffset; @@ -4557,33 +4574,4 @@ Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2) return overlap; } -//---------------------------------------------------------------------------------- -// Module Internal Functions Definition -//---------------------------------------------------------------------------------- - -// Calculate how much a 2D vertex position should move to shift it to the center of a pixel -static Vector2 CalculateHalfPixelOffsets(void) -{ - Matrix t = rlGetMatrixTransform(); - Matrix m = rlGetMatrixModelview(); - - // Compute upper-left 2x2 of M*T - // ax and ay are the first column of the matrix; bx and by are the second - float ax = m.m0*t.m0 + m.m4*t.m1; - float ay = m.m1*t.m0 + m.m5*t.m1; - float bx = m.m0*t.m4 + m.m4*t.m5; - float by = m.m1*t.m4 + m.m5*t.m5; - - // Column lengths are pixels per world unit along world x- and y-axes - float column0Length = sqrtf(ax*ax + ay*ay); - float column1Length = sqrtf(bx*bx + by*by); - - // Take inverse column lengths: how many world units translate to one pixel - // Divide by two: world units to half-pixel - float xOffset = 0.5f/column0Length; - float yOffset = 0.5f/column1Length; - - return (Vector2){ xOffset, yOffset }; -} - #endif // SUPPORT_MODULE_RSHAPES