2

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

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Asonic84
  • 79
  • 6
  • 1
    What's wrong with the output? And what is wrong with the output that using MSAA wouldn't help with? – Nicol Bolas Jun 25 '21 at 16:48
  • @Nicol Bolas how can I add thickness using MSAA? Line is extremely thin. – Asonic84 Jun 25 '21 at 17:34
  • You need to draw polygons instead of lines. You can smooth the polygon lines in the fragment shader by discarding and/or blending fragments. – Rabbid76 Jun 25 '21 at 17:36
  • @Rabbid76 to draw a "polyline" , I will have to calculate adjacent vertices either on the cpu side or in the vertex/tessellation/geometry shader. But my question was can I do the same calculation in Fragment shader instead and fill the result with required color and hence bypass all new geometry creation steps? I think it will be much faster also , since I don't need any extra control like variable width of the line etc. it would be perfect for my needs? – Asonic84 Jun 25 '21 at 17:58
  • *"But my question was can I do the same calculation in Fragment shader instead and fill the result with required color and hence bypass all new geometry creation steps?"* No. You need to draw geometry (primitives) that enclose all fragments of the line. All you can do in the fragment shader is discard fragments, but you cannot magically change the color of some additional fragments – Rabbid76 Jun 25 '21 at 18:04
  • @Rabbid76 ok , got it, so that means I have to use geometry shader to further process these "isolines" (points if I am not wrong) as geometry shader input and calculate thickness( adjacent vertices ) and draw poly(triangle strips) then pass that to FS? – Asonic84 Jun 25 '21 at 18:17
  • @Asonic84 see [rendering 2D Cubics in GLSL](https://stackoverflow.com/a/60113617/2521214) – Spektre Jun 28 '21 at 07:04

1 Answers1

2

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).

Rabbid76
  • 202,892
  • 27
  • 131
  • 174