#ifdef VERTEX

out vec2 vLmcoord;
out vec4 vColor;
out vec2 vTexcoord;

#ifdef GBUFFERS_LINE

uniform float viewWidth;
uniform float viewHeight;

vec4 vpLineTransform()
{
    const float LINE_WIDTH  = 2.0;
    const float VIEW_SHRINK = 1.0 - (1.0 / 256.0);

    mat4 VIEW_SCALE = mat4(mat3(VIEW_SHRINK));

    vec2 screenSize = vec2(viewWidth, viewHeight);

    vec4 linePosStart =gl_ProjectionMatrix *VIEW_SCALE *gl_ModelViewMatrix *vec4(gl_Vertex.xyz, 1.0);

    vec4 linePosEnd =gl_ProjectionMatrix *VIEW_SCALE *gl_ModelViewMatrix *vec4(gl_Vertex.xyz + gl_Normal.xyz, 1.0);

    vec3 ndcStart = linePosStart.xyz / linePosStart.w;
    vec3 ndcEnd   = linePosEnd.xyz   / linePosEnd.w;

    vec2 lineDir = normalize((ndcEnd.xy - ndcStart.xy) * screenSize);

    vec2 lineOffset =vec2(-lineDir.y, lineDir.x) *(LINE_WIDTH / screenSize);

    if (lineOffset.x < 0.0)lineOffset = -lineOffset;

    if ((gl_VertexID % 2) == 0)
    {
        return vec4((ndcStart + vec3(lineOffset, 0.0)) * linePosStart.w,linePosStart.w);
    }
    else
    {
        return vec4((ndcStart - vec3(lineOffset, 0.0)) * linePosStart.w, linePosStart.w);
    }

    ////ALL THIS CODE FOR A LINES???? I CAN'T BELIVE IT
}

#endif

void main()
{
#ifndef GBUFFERS_LINE

    gl_Position = ftransform();

#else

    gl_Position = vpLineTransform();

#endif

    vLmcoord = (gl_TextureMatrix[1] * gl_MultiTexCoord1).xy;
    vColor   = gl_Color;
    vTexcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;
}

#endif


#ifdef FRAGMENT

#ifndef GBUFFERS_LINE
uniform sampler2D lightmap;
uniform sampler2D gtexture;
uniform int renderStage;
#endif

uniform float frameTimeCounter;

in vec2 vLmcoord;
in vec4 vColor;
in vec2 vTexcoord;

#include "/lib/Conf/RGB_LINE_AND_ORB_CONF.glsl"
#include "/lib/Animations/GamingSelection.glsl"

void main()
{
#ifdef GBUFFERS_LINE

    vec4 FinalColor = vec4(1.0);

#else

    vec4 FinalColor = vec4(1.0);
#endif

#ifdef RAINBOW_LINES

    float vpHue = fract(frameTimeCounter * RGB_VELOCITY +
                        gl_FragCoord.x * 0.0005);

    vec3 vpRainbow;

    #if STYLE == RGB_LINE
        vpRainbow = vpHSVtoRGB(vec3(vpHue, 0.85, 1.0));
    #elif STYLE == GAMING
        vpRainbow = Gaming_Line(vec3(vpHue, 0.85, 1.0));
    #endif

    #ifdef GBUFFERS_LINE

        // Las líneas de selección sí se reemplazan completamente.
        FinalColor.rgb = vpRainbow;

    #else

        // Conserva la textura y el color original.
        FinalColor = vColor;
    #endif

#endif

/* DRAWBUFFERS:0 */
gl_FragData[0] = FinalColor;
}

#endif