#vertex shader
#version 330 core
layout (location = 0) in vec3 coords;
layout (location = 1) in vec2 texcoords;

out vec2 v_texcoords;

uniform mat4 MVP;
uniform mat4 OM;

void main()
{
  gl_Position = MVP * OM * vec4(coords, 1.0);
  v_texcoords = texcoords;
};

#fragment shader
#version 330 core
out vec4 FragColor;
in vec2 v_texcoords;

uniform sampler2D screentex;
uniform int transpmode;
uniform vec3 transpcolor;
uniform vec3 opaquecolor;

const int tcmOff      = 0;
const int tcmEqual    = 1;
const int tcmDistance = 2;
const int tcmBipolar  = 3;

const vec3 epsilon = vec3(1.0 / 255.0);

void main()
{
  FragColor = texture(screentex, v_texcoords);
  switch (transpmode)
  {
    case tcmOff:
      break;
    case tcmEqual:
      if (all(lessThanEqual(abs(FragColor.rgb - transpcolor), epsilon))) discard;
      break;
    case tcmDistance:
      FragColor.a = distance(FragColor.rgb, transpcolor) / sqrt(3);
      break;
    case tcmBipolar:
      vec3 delta = opaquecolor - transpcolor;
      float t = dot(FragColor.rgb - transpcolor, delta) / dot(delta, delta);
      FragColor.a = t;
      break;
  }
};
