
[vert.glsl]
#version 130

in vec3 in_Position;
in vec3 in_Normal;
in vec4 in_Color;

uniform mat4 u_modelview;
uniform mat4 u_view;
uniform mat4 u_projection;
uniform mat3 u_normalmat;

out vec4 pass_vertex; /* View space */
out vec3 pass_tnormal;
out vec4 pass_color;

void main(void)
{
    vec4 vertex = u_modelview * vec4(in_Position, 1.0);
    vec3 tnorm = normalize(u_normalmat * in_Normal);

    pass_vertex = vertex;
    pass_tnormal = tnorm;
    pass_color = in_Color;

    gl_Position = u_projection * vertex;
}

[frag.glsl]
#version 130

#if defined GL_ES
precision highp float;
#endif

in vec4 pass_vertex; /* View space */
in vec3 pass_tnormal;
in vec4 pass_color;

uniform mat4 u_view;
uniform mat4 u_inv_view;

uniform vec4 u_lights[8 * 2];

uniform float u_damage; /* FIXME: remove this */

#if 0
//Cos(45) = 0.70710678118
//1.0 - Cos(45) = 0.29289321881

const float cos_45      = 0.70710678118;
const float inv_cos_45  = 0.29289321881;

//Cube Light
vec4 in_Light3_Pos = vec4(-10.0, 10.0, 5.0, 1.0);
vec3 in_Light3_Size_Inner = vec3(3.0, 1.0, 3.0);
vec3 in_Light3_Size_Outer = vec3(15.0, 15.0, 15.0);
vec3 in_Light3_diffuse = vec3(0.4, 1.0, 0.4);
#endif

void main(void)
{
    vec3 tnormal = pass_tnormal;

    /* Material properties */
    vec3 specular_reflect = vec3(0.8, 0.75, 0.4);
    float specular_power = 60.0;

    /* World properties */
    vec3 ambient = vec3(0.1, 0.1, 0.1);
    vec3 specular = vec3(0.0, 0.0, 0.0);
    vec3 diffuse = vec3(0.0, 0.0, 0.0);

    /* Light precalculations */
    vec3 v = normalize(-pass_vertex.xyz);

    /* Apply lighting */
    for (int i = 0; i < 8; i++)
    {
        vec4 pos = u_lights[i * 2];
        vec4 color = u_lights[i * 2 + 1];
        vec3 s, r, p;

        p = (u_view * pos).xyz;
        if (pos.w > 0.0)
        {
            /* Point light -- no attenuation yet */
            s = normalize(p - pass_vertex.xyz);
        }
        else
        {
            /* Directional light */
            s = normalize(-p);
        }
        r = reflect(-s, tnormal);

        float sdotn = max(dot(s, tnormal), 0.0);
        diffuse += color.xyz * sdotn;
        if (sdotn > 0.0)
            specular += color.xyz * specular_reflect
                         * pow(max(dot(r, v), 0.0), specular_power);
    }

#if 0
    //Light calculation for cube light
    vec3 specular_color = vec3(1.0, 1.0, 0.6);
    vec3 Local_Vertex = (u_inv_view * pass_vertex).xyz - (in_Light3_Pos).xyz;
    vec3 Proj_Vertex = clamp(Local_Vertex.xyz, -in_Light3_Size_Inner, in_Light3_Size_Inner);
    vec3 new_LightDir = Local_Vertex - Proj_Vertex;

    vec3 light_radius = max(vec3(0.0,0.0,0.0), vec3(1.0,1.0,1.0) - abs(new_LightDir / in_Light3_Size_Outer));
    float light_radius_mod = min(light_radius.x, min(light_radius.y, light_radius.z));

    if (length(new_LightDir) == 0.0)
        sdotn = 1.0;
    else
    {
        new_LightDir = normalize((u_view * vec4(Proj_Vertex + in_Light3_Pos.xyz,1.0)).xyz - pass_vertex.xyz);
        sdotn = max(dot(new_LightDir, tnormal), 0.0);
        r = reflect(-new_LightDir, tnormal);
        if (sdotn > 0.0 && light_radius_mod > 0.0)
            specular += specular_color * min(specular_reflect, light_radius_mod)
                     * pow(max(dot(r, v), 0.0), specular_power);
    }
    diffuse += in_Light3_diffuse * min(sdotn, light_radius_mod);
    //----------
#endif

    vec3 light = ambient + diffuse + specular;

    vec4 real_color = mix(pass_color, vec4(1.2, 1.2, 1.2, 1.0), u_damage);
    gl_FragColor = real_color * vec4(light, 1.0);
}

