Search code examples
openglglsl

Draw anti-aliased thick tessellated curves


So I am able to draw curved lines using tessellation shaders , but the lines coming out are very thin and jaggy(aliased). I was confused as how can I process these new points( isoline ), from tessellation eval shader to fragment shader and make it thicker and aliased.

I know their are multiple ways like using geometry shader and even vertex shader to calculate adjacent vertices and create polyline. But my goal isn't creating polyline, just a constant thickness and anti-aliased lines(edges). For which I think fragment shader is enough.

Kindly advise what will be the best and fastest way to achieve this and how can I pass "isolines" data from tessellation shader to fragment shader and manipulate their. Any small code which shows transfer of data from TES to FS would be really helpful. And pardon me, since I am beginner, so many of my assumptions above might be incorrect.

Vertex Shader: This is a simple pass through shader so I am not adding here.

Tessellation Eval Shader:

#version 450

layout( isolines ) in;

uniform mat4 MVP;

void main()
{

    vec3 p0 = gl_in[0].gl_Position.xyz;
    vec3 p1 = gl_in[1].gl_Position.xyz;
    vec3 p2 = gl_in[2].gl_Position.xyz;

    float t = gl_TessCoord.x;
    float one_minus_t = (1.0 - t);
    float one_minus_t_square = one_minus_t * one_minus_t;
    float t_square = t * t;
    float two_times_one_minus_t = 2 * one_minus_t;

    // Bezier interpolation
    vec3 p = one_minus_t_square * p0 + two_times_one_minus_t*t*p1 + t_square * p2;

    gl_Position = vec4(p, 1.0);

}

Fragment Shader : Basic shader which assigns uniform color to gl_FragColor

Output:

enter image description here


Solution

  • If you want to draw a thick smooth line, you have 3 options:

    1. Draw a highly tessellated polygon with many vertices.

    2. Draw a quad over the entire viewport and discard any fragments that are not on the line (in the fragment shader).

    3. Mix options 1 and 2. Draw a rough polygon that is larger than the line and encloses the line. Discard fragments at the edges and corners to smooth out the line (in the fragment shader).