#pragma language glsl3
#ifdef VERTEX
vec4 position(mat4 transform_projection, vec4 vertex_position) {return transform_projection * vertex_position; }
#endif

#ifdef PIXEL

uniform float zNear = 0.0001;
uniform float zFar = 4.0;
float linearDepth(float depthSample)
{
    depthSample = 2.0 * depthSample - 1.0;
    float zLinear = 2.0 * zNear * zFar / (zFar + zNear - depthSample * (zFar - zNear));
    return zLinear;
}
float depthSample(float linearDepth)
{
    float nonLinearDepth = (zFar + zNear - 2.0 * zNear * zFar / linearDepth) / (zFar - zNear);
    nonLinearDepth = (nonLinearDepth + 1.0) / 2.0;
    return nonLinearDepth;
}

uniform vec3 eyePosition;

uniform sampler2D gbuffer_diffuse;
uniform sampler2D gbuffer_position;
uniform sampler2D gbuffer_normal;
uniform sampler2D gbuffer_depth;

uniform float ambientLight;
uniform vec3 ambientColor;

mat4 bias = mat4(0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0,
                 0.5, 0.5, 0.5, 1.0);

vec4 effect(vec4 color, sampler2D tex, vec2 textureCoords, vec2 screen_coords) {
  vec2 coord = screen_coords / love_ScreenSize.xy;
  vec4 position = Texel(gbuffer_position, textureCoords);
  vec4 diffuse = Texel(gbuffer_diffuse, textureCoords);
  vec4 depth= Texel(gbuffer_depth,   textureCoords);
  vec3 light = Texel(tex, textureCoords).rgb;

  if (diffuse.a == 0) {
    discard;
  }

  vec4 result = vec4(ambientLight * ambientColor * diffuse.rgb + diffuse.rgb * light , 1);
  return result;
}

#endif
