#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;
uniform int AnchorPoint;
uniform bool FaceScreen;
uniform float aspect;
uniform vec2 displacement;
uniform float size;

const vec3[9] AnchorDeltas3 = vec3[]
  (
    vec3( 0.0,  0.0,  0.0),
    vec3( 0.0, -0.5,  0.0),
    vec3( 0.0, -1.0,  0.0),
    vec3(-0.5,  0.0,  0.0),
    vec3(-0.5, -0.5,  0.0),
    vec3(-0.5, -1.0,  0.0),
    vec3(-1.0,  0.0,  0.0),
    vec3(-1.0, -0.5,  0.0),
    vec3(-1.0, -1.0,  0.0)
  );

const vec2[9] AnchorDeltas2 = vec2[]
  (
    vec2( 0.0,  0.0),
    vec2(-0.5,  0.0),
    vec2(-1.0,  0.0),
    vec2( 0.0,  0.5),
    vec2(-0.5,  0.5),
    vec2(-1.0,  0.5),
    vec2( 0.0,  1.0),
    vec2(-0.5,  1.0),
    vec2(-1.0,  1.0)
  );

void main()
{
  if (FaceScreen)
  {
    gl_Position = MVP * OM * vec4(0.0, 0.0, 0.0, 1.0);
    gl_Position.xy = gl_Position.xy +
      (vec2(0.0, -1.0) + texcoords + AnchorDeltas2[AnchorPoint]) * vec2(aspect, 1.0) * pow(determinant(OM) / pow(aspect, 2), 1.0/3.0) * size +
      displacement;
    v_texcoords = texcoords;
  }
  else
  {
    gl_Position = MVP * OM * vec4(size * (coords + AnchorDeltas3[AnchorPoint]), 1.0);
    v_texcoords = texcoords;
  }
};

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

uniform sampler2D screentex;
uniform vec3 color;
uniform float opacity;

void main()
{
  FragColor = texture(screentex, v_texcoords) * opacity + vec4(color, 0.0);
  if (FragColor.a == 0.0) discard;
};
