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: rasterization and fragment coordinates for lines

  1. #1
    Intern Newbie
    Join Date
    Jan 2013
    Posts
    42

    rasterization and fragment coordinates for lines

    Hi there,

    I've been reading a bit about using lines in openGL, but it's not quite clear to me yet how they work in the fragment shader. Specifically I don't understand how the 3d fragment coordinates (or other varying vars) are interpolated when the line has width > 1. A line only has 2 vertices, but basically a rectangular area is rasterized for lines with width > 1 (right?), so... how does the interpolation work then?
    Anyone knows how this is implemented?

    Thanks!
    Last edited by STTrife; 11-13-2013 at 02:08 AM.

  2. #2
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    For lines, interpolation is one-dimensional, i.e. the attribute values are interpolated based upon the distance "along" the line segment. The distance perpendicular to the line segment has no effect upon the interpolation. So if you draw e.g. a wide horizontal line (where the endpoints have the same y coordinate), a set of fragments forming a vertical line (i.e. all having the same x coordinate) will all have the same interpolated values for their inputs.

    If you view a wide line as a thin rectangle, each pair of vertices corresponding to a single endpoint will have identical values for their attributes, i.e. the values given for that endpoint.

    The exact details are given in 3.5 of the OpenGL 3.x specifications or 14.5 of the OpenGL 4.x specifications.

  3. #3
    Intern Newbie
    Join Date
    Jan 2013
    Posts
    42
    Thanks, that was very clear.
    Is there a good way then to find out the distance perpendicular to the line segment of the fragment in the fragment shader? That seems neccesary for anti-aliassing or other effects. I guess you would have to know the two endpoints of the line in the fragment shader then...? but how? using uniforms?

  4. #4
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    Quote Originally Posted by STTrife View Post
    Is there a good way then to find out the distance perpendicular to the line segment of the fragment in the fragment shader? That seems neccesary for anti-aliassing or other effects. I guess you would have to know the two endpoints of the line in the fragment shader then...? but how? using uniforms?
    One possibility:

    Pass the viewport parameters as a uniform. Have the vertex shader calculate the window-space coordinates of the vertex as an output (with the "noperspective" qualifier). For a fragment whose centre lies exactly on the line, the interpolated value should be exactly equal to gl_FragCoord.xy. In the general case, the difference between gl_FragCoord.xy and the interpolated value will be the perpendicular vector, and its length will be the distance of the fragment's centre from the line.

    Another possibility:

    Use a geometry shader to generate the coefficients of the plane equation (d=a*x+b*y+c) for each segment, and store these as an attribute with the "flat" qualifier. The fragment shader would feed the x and y components of gl_FragCoord into that equation to obtain the signed distance from the line.

  5. #5
    Intern Newbie
    Join Date
    Jan 2013
    Posts
    42
    Nice ideas I'll think I go for the first one.

Posting Permissions

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