'Opengl shader to colour edges of square

I have a basic opengl app which is displaying a square, the squares colour is determined using interpolation. In my fragment shader I am trying to detect if the pixel runs along one of the edges of the square and if so to colour it black.

This is my vertex shader.

#version 330

layout (location = 0) in vec3 pos;

out vec4 vertColour;
uniform mat4 model;
uniform mat4 projection;


void main()
{
  gl_Position = projection * model * vec4(pos.x, pos.y, pos.z, 1.0);
  vertColour = vec4(clamp(pos, 0.0f, 1.0f), 1.0f);
}

And my fragment shader

#version 330

out vec4 colour;
in vec4 vertColour;

void main()
{
  float x = gl_FragCoord.x;
  float y = gl_FragCoord.y;
  float z = gl_FragCoord.z;
  if((x ==  1 && y ==  1) ||
  (x == -1 && y == -1) ||
  (x ==  1 && y == -1) ||
  (x == -1 && y ==  1) ||
  (x ==  1 && z ==  1) ||
  (x == -1 && z == -1) ||
  (x == -1 && z ==  1) ||
  (x ==  1 && z == -1) ||
  (y ==  1 && z ==  1) ||
  (y == -1 && z == -1) ||
  (y ==  1 && z == -1) ||
  (y == -1 && z ==  1))
  {
    colour = vertColour;
  } else {
    colour = vertColour;
  }
}

Here are my indices and vertices to form the square.

int[] indices = {
        0,1,4,
        4,5,1,
        1,0,3,
        3,2,1,
        1,5,6,
        6,1,2,
        2,6,7,
        7,6,5,
        5,4,7,
        7,2,3

};
float[] vertices = {
      -1.0f, -1.0f, -1.0f,
      -1.0f, -1.0f,  1.0f,
       1.0f, -1.0f,  1.0f,
       1.0f, -1.0f, -1.0f,
      -1.0f,  1.0f, -1.0f,
      -1.0f,  1.0f,  1.0f,
       1.0f,  1.0f,  1.0f,
       1.0f,  1.0f, -1.0f,
};

In effect I'm trying to determine if the location of the current fragment lies at either the max / minimum value of each edge. Here is what I see prior to trying the outline enter image description here

And when I try to apply the outline the whole cube turns black. I assume there is something wrong in my logic in the fragment shader but have not been able to spot the issue. Any help is welcome. enter image description here



Solution 1:[1]

gl_FragCoord.xy contains the window coordinates, the units are pixels (horizontal and vertical fragments). The bottom left coordinate is (0.5, 0.5) and the top right is (width-0.5, height-0.5).
If you want to get the vertex coordinates of a fragment on the cube, you need to pass pos from the vertex to the fragment shader:

Vertex shader:

layout (location = 0) in vec3 pos;
out vec3 vertPos;

void main()
{
    vertPos = pos;

    // [...]
}

Fragment shader:

in vec3 vertPos;

// [...]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rabbid76