////////////////////////////////////////////////
//
// stain shaders
//
////////////////////////////////////////////////

// staintype:
//    o -> overbright
//    t -> transparent
//    f -> fogged

stainopt = [ >= (strstr $staintype $arg1) 0 ]

stainvariantshader = [
  local staintype
  staintype = $arg2
  variantshader 0 $arg1 (? (stainopt "t") 0 -1) (if (! (stainopt "t")) [result [
    @(? (stainopt "f") [
        //:fog
    ])
    in vec4 vvertex, vcolor;
    in vec2 vtexcoord0;
    uniform mat4 camprojmatrix;
    uniform vec4 colorscale;
    out vec4 color;
    out vec2 texcoord0;
    void main(void)
    {
        gl_Position = camprojmatrix * vvertex;
        color = vcolor * colorscale;
        texcoord0 = vtexcoord0; 
    }
  ]]) [
    uniform sampler2D tex0;
    in vec4 color;
    in vec2 texcoord0;
    layout(location = 0) out vec4 fragcolor;
    @(? (stainopt "t") [
        layout(location = 1) out vec4 gglow;
    ])
    void main(void)
    {
        vec4 diffuse = texture(tex0, texcoord0);
        @(if (stainopt "o") [result [
            diffuse.rgb = mix(vec3(0.5), diffuse.rgb, color.rgb);
            fragcolor.rgb = diffuse.rgb;
            fragcolor.a = 0.5;
            @(? (stainopt "t") [
                gglow.rgb = diffuse.rgb;
                gglow.a = 0.5;
            ])
        ]] [result [
            diffuse *= color;
            fragcolor = diffuse;
            @(? (stainopt "t") [
                gglow.rgb = vec3(0.0);
                gglow.a = diffuse.a;    
            ])
        ]])
    }
  ]
]

stainshader = [
    staintype = $arg2
    stainvariantshader $arg1 $arg2
    if (! (stainopt "f")) [
        stainvariantshader $arg1 (concatword $arg2 "t")
    ]
]

stainshader "stain" ""
stainshader "overbrightstain" "o"
stainshader "foggedstain" "f"

