Search code examples
opengltexturesshader

opengl - flickering of fragments even with disabled depth test


I'm trying to render a quad with a coloured border. I'm using texture coordinates to detect whether the fragment should be considered part of border or not. If it is part of border, then render it with green colour or else with black colour.

Here are my vertices / normals / tex coordinates.

    float vertices[] = {
        // posistions          // normals           // texture coords
      -0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  0.0f,  0.0f,
       0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  1.0f,  0.0f,
       0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  1.0f,  1.0f,
       0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  1.0f,  1.0f,
      -0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  0.0f,  1.0f,
      -0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,  0.0f,  0.0f,
    }

Here's my fragment shader

#version 330 core

in vec3 frag_pos;
in vec3 frag_nor;
in vec2 frag_tex;

out vec4 frag_color;

void main()
{
    vec2 origin = vec2(0.01, 0.01);

    float width =  1.0 - origin.x * 2.0;
    float height = 1.0 - origin.y * 2.0;

    if( (frag_tex.x >= origin.x && frag_tex.x < origin.x + width) &&
        (frag_tex.y >= origin.y && frag_tex.y < origin.y + height) )
    {
        frag_color = vec4(0.0);
    }
    else
    {
        frag_color = vec4(0.0, 1.0, 0.0, 0.0);
    }
}

And this is how I'm rendering

glDisable(GL_DEPTH_TEST);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 6);

GIF LINK

In right, I'm drawing the same quad with another pass-through fragment shader in wireframe mode.

As you can see the left quad is flickering while moving the camera. Any ideas how to fix this.


Solution

  • The problem exist even with applying a 2D texture. To fix that, I used Mipmaps with these filters.

        glGenerateMipmap(GL_TEXTURE_2D);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);