Skip to content

ImDrawList::AddPolyline() misses pixels #9360

@memononen

Description

@memononen

Version/Branch of Dear ImGui:

1.92.7

Back-ends:

n/a

Compiler, OS:

win11, msvc

Full config/build information:

No response

Details:

When drawing a line using ImDrawList::AddPolyline(), from pixel center to pixel center, one of the end pixels may not be filled.

This is due to AddPolyline() not drawing line caps. A butt cap would fix the issue.

Butt caps are simple to implement, we just need to extend the first and last point along the line direction.

void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 col, ImDrawFlags flags, float thickness)
{
    ...

        if (use_texture || !thick_line)
        {

            const float cap_size = thickness * 0.5f;

            // If line is not closed, the first and last points need to be generated differently as there are no normals to blend
            if (!closed)
            {
                const ImVec2 dir0(-temp_normals[0].y, temp_normals[0].x); // Normal to direction
                temp_points[0] = points[0] - dir0 * cap_size + temp_normals[0] * half_draw_size;
                temp_points[1] = points[0] - dir0 * cap_size - temp_normals[0] * half_draw_size;
                // The other end point cannot be done here, since it will be overridden by the loop below!
            }

            // The loop...

            if (!closed)
            {
                const ImVec2 dir1(-temp_normals[points_count-1].y, temp_normals[points_count-1].x);  // Normal to direction
                temp_points[(points_count-1)*2+0] = points[points_count-1] + dir1 * cap_size + temp_normals[points_count-1] * half_draw_size;
                temp_points[(points_count-1)*2+1] = points[points_count-1] + dir1 * cap_size - temp_normals[points_count-1] * half_draw_size;
            }
        }
    ...
}

The end cap could be made anti-aliased by adding extra division that is faded to zero alpha. Or the whole endcap could be baked into texture in which case round caps could be supported.

Screenshots/Video:

The image tagged after is rendered with butt caps as per above explanation.

Image

Minimal, Complete and Verifiable Example code:

No response

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions