Search code examples
openglglslshaderfragment-shaderpyopengl

OpenGL procedural texture antialiasing


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.


Solution

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