
[vert.glsl]
#version 130

in vec3 in_position;
in vec3 in_Normal;
in vec4 in_color;
in vec2 in_Index;
in vec2 in_Weight;

uniform mat4 u_modelview;
uniform mat4 u_view;
uniform mat4 u_projection;
uniform mat3 u_normalmat;
//10is not a fix idea, should be more.
uniform mat4 u_bone_list[10];

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

uniform vec4 u_lights[8 * 2];

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

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

vec4 in_Light3_Pos = vec4(-10.0, 20.0, 0.0, 1.0);
vec3 in_Light3_Size_Inner = vec3(12.0, 12.0, 12.0);
vec3 in_Light3_Size_Outer = vec3(10.0, 10.0, 10.0);
vec3 in_Light3_diffuse = vec3(0.4, 1.0, 0.4);
#endif

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.7, 0.7, 0.7);
    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);
    }

#if 0
    //Light calculation for cube light
    //const float cos_45      = 0.70710678118;
    //const float inv_cos_45  = 0.29289321881;
    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 proj_local_dir = local_vertex - proj_vertex;
    vec3 inner_dir = proj_vertex / in_Light3_Size_Inner;
    inner_dir.x = (inner_dir.x == 1.0)?(1.0):(0.0);
    inner_dir.y = (inner_dir.y == 1.0)?(1.0):(0.0);
    inner_dir.z = (inner_dir.z == 1.0)?(1.0):(0.0);

    //inside the cube
    if (length(proj_local_dir) == 0.0)
    {
        sdotn = 1.0;
        light_radius_mod = 1.0;
    }
    //spec calculation
    else
    {
        //Distance calculation
        vec3 proj_local_light = proj_local_dir / in_Light3_Size_Outer;
        light_radius_mod = max(0.0, 1.0 - length(proj_local_light));
        //cube orientation
        sdotn = max(0.0, dot(normalize(proj_local_dir), normalize(inner_dir)));


        //if (length(inner_dir) > 1.0)
        //    sdotn = 0.0;
        //else
        //{
        //    //vec3 proj_local_light = max(vec3(0.0,0.0,0.0), vec3(1.0,1.0,1.0) - abs(proj_local_dir / in_Light3_Size_Outer));
        //}
        /*
        proj_local_dir = normalize((u_view * vec4(proj_vertex + in_Light3_Pos.xyz,1.0)).xyz - pass_vertex.xyz);
        sdotn = max(dot(proj_local_dir, pass_tnormal), 0.0);
        r = reflect(-proj_local_dir, pass_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 calculation
    diffuse += in_Light3_diffuse * sdotn; //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);
}

