#include "/lib/uniforms.glsl"

#include "/lib/taa/res_params.glsl"
varying vec3 np3;

varying vec4 color;
varying vec2 texcoord;
#define diagonal3(m) vec3((m)[0].x, (m)[1].y, m[2].z)
#define projMAD(m, v) (diagonal3(m) * (v) + (m)[3].xyz)
vec4 toClipSpace3(vec3 viewSpacePosition)
{
    return vec4(projMAD(gl_ProjectionMatrix, viewSpacePosition), -viewSpacePosition.z);
}

uniform mat4 gbufferModelViewInverse;

vec3 viewToWorld(vec3 viewPos)
{
    return (gbufferModelViewInverse * vec4(viewPos, 0.0)).xyz;
}

void main()
{

#if CLOUD_QUALITY == 3
    float skysize = 1.0;
#endif

#if CLOUD_QUALITY == 2
    float skysize = 0.75;
#endif

#if CLOUD_QUALITY == 1
    float skysize = 0.5;
#endif
    vec3 viewVector = (gl_ModelViewMatrix * gl_Vertex).xyz;

    np3 = (viewToWorld(viewVector));

    texcoord = gl_MultiTexCoord0.xy;
    color = gl_Color;
    vec3 position = mat3(gl_ModelViewMatrix) * gl_Vertex.xyz + gl_ModelViewMatrix[3].xyz;

    gl_Position = taaFunc(toClipSpace3(position), frameCounter, viewResolution);
    gl_Position.xy = gl_Position.xy * skysize + skysize * gl_Position.w - gl_Position.w;
}
