3

I`ve made a grid using a simple GLSL shader, passing texture coordinates to fragment shader. It was applied onto a large scaled plane.

Fragment shader:

#version 330 core

out vec4 fragColor;

smooth in vec2 f_TexCoord;

vec4 gridColor;

void main()
{
    if(fract(f_TexCoord.x / 0.0005f) < 0.025f || fract(f_TexCoord.y / 0.0005f) < 0.025f)
        gridColor = vec4(0.75, 0.75, 0.75, 1.0);
    else
        gridColor = vec4(0);
    // Check for alpha transparency
    if(gridColor.a != 1)
        discard;

    fragColor = gridColor;
}

enter image description here

As you can see the lines are not smooth and they start to "flickering" at the horizon. Is it possible to apply some sort of filtering/antialiasing on it? I've tried to increase number of samples (up to 4, because higher values gives me a qt error), but it has no affect on shader.

Artem
  • 563
  • 3
  • 17
  • 4
    Maybe this article about [filtering procedural textures](https://www.iquilezles.org/www/articles/filtering/filtering.htm) is helpful to you. – LJᛃ Aug 23 '20 at 11:03
  • I've worked with similar application which needed grid lines to be drawn. I ended up using geometric lines in a single draw call. This was much efficient and accurate. – codetiger Aug 24 '20 at 02:35

1 Answers1

2

Switch to GLSL version 4.20 (at least), activate multisampling and use the Auxiliary Storage Qualifier sample for the vertex shader output (and fragment shader input):

#version 420 core

sample smooth in vec2 f_TexCoord;

The qualifier causes per-sample interpolation.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    works quite well. Unfortunately, works only with version 420+. In other case, shader compilation fails with this error: `auxiliary storage qualifiers must come just before storage qualifiers`. Anyway, BIG thanks for this hint! – Artem Aug 26 '20 at 20:52
  • 1
    @Artem Thank you (fixed 4.00 -> 4.20). You're welcome. – Rabbid76 Aug 26 '20 at 20:54