
[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 = vec4(1.0, 1.0, 1.0, in_color.w);//;

    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 float u_damage;
uniform mat4 u_view;
uniform mat4 u_inv_view;

uniform vec4 u_lights[8 * 2];

void main(void)
{
    /* 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;

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

        float sdotn = max(dot(s, pass_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);
    }

    vec3 light = ambient + diffuse + specular;

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

