# 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!

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.

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?

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=ax+by+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.

Nice ideas I’ll think I go for the first one.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.