uniform sampler2D u_texture;

varying vec3  v_position;
varying vec3  v_normal;
varying float v_textured;

void main() {
    vec4 color_ambient = vec4(0.1, 0.1, 0.0, 1.0);
    vec4 color_diffuse = v_textured > 0.0 ? texture2D(u_texture, gl_TexCoord[0]) : gl_Color;
    vec4 color_specular = vec4(0.25);
    
    vec3 normal   = normalize(v_normal);
    vec3 lightVec = normalize(vec3(5.0, 5.0, 10.0) - v_position);
    float diffuse = dot(lightVec, normal);
    
    gl_FragColor = color_ambient;       // ambient
    if (diffuse > 0.0) {
        vec3 viewVec = normalize(-v_position);
        vec3 reflectVec = reflect(-lightVec, normal);
        float specular = pow(max(dot(viewVec, reflectVec), 0.0), 5.0);
        gl_FragColor += color_diffuse * diffuse + color_specular * specular;
        gl_FragColor = min(gl_FragColor, vec4(1.0));
    }
}
