Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Wide line endcaps in a fragment shader?

  1. #1
    Intern Contributor
    Join Date
    Nov 2010
    Posts
    61

    Wide line endcaps in a fragment shader?

    I've been looking into various ways to draw wide lines. A couple of times I have run across references to rendering rounded endcaps in the fragment shader, instead of with geometry (by adding a little fan of triangles forming a half-circle). But I haven't seen any concrete examples of how to do this. I get that you would measure the distance away from the line endpoint, and anything beyond that is discarded, which would form a round shape. But how do you do that in practice? I've only used fragment shaders to deal with color and lighting, not anything using distance or position.

    Thanks!

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,515
    Are you converting the lines to triangles (either in client code or a geometry shader), or just using glLineWidth() to render thick lines?

    Either way, you need to set appropriate attributes. One option is to simply store the start and end coordinates as attributes, then find the distance between gl_FragCoord and the line segment (after conversion to window space; but note that with a perspective projection, endpoints might not have window-space coordinates).

  3. #3
    Intern Contributor
    Join Date
    Nov 2010
    Posts
    61
    Yes, converting to triangles. And yes I just want to know how to find the distance between the fragment coordinate and the segment. Basically, how do I get the fragment coordinate?

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,515
    Quote Originally Posted by bsabiston View Post
    Yes, converting to triangles. And yes I just want to know how to find the distance between the fragment coordinate and the segment. Basically, how do I get the fragment coordinate?
    Add appropriate attributes to each vertex, containing "line space" coordinates. These will be interpolated during rasterisation, so the fragment shader knows the fragment's location relative to the line.

    Typically, the attribute would be a vec3, where the values are:
    1. The perpendicular distance from the line.
    2. The distance along the line from one endpoint.
    3. The distance along the line from the other endpoint.


    If either of the last two values are negative, then it means that you're inside the line cap, and should use the smaller value and the perpendicular distance to calculate the distance from the endpoint. If that's less than half the width of the line then the fragment is inside the semicircle, otherwise it's outside.
    Code :
    if (pos.y < 0 && length(pos.xy) < 0.5*line_width)
        discard;
    if (pos.z < 0 && length(pos.xz) < 0.5*line_width)
        discard;

  5. #5
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,515
    Here's a diagram to explain the coordinate system:
    Click image for larger version. 

Name:	text5734.jpg 
Views:	12 
Size:	13.3 KB 
ID:	2510

    If you want polylines with rounded or mitred joins, you have to calculate the distance along the edge of the stroke. If you just want caps, you can measure the distance along the centre line.

    The parallel and perpendicular distances must have the same scale to get circular (rather than elliptical) caps.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •