/**
 *
 * Diplomova prace
 * Stinove techniky na dnesnim hardware a jejich porovnani
 *
 * Michal Toth
 *
 * Fakulta informacnich technologii
 * Vysoke uceni technicke v Brne
 *
 * 2013/2014
 *
 */

/*
 * Ploche stinovani
 */

#version 420

in vec3 pos;
in vec3 norm;
in vec2 tex;

uniform mat4 mvp;
uniform mat4 m;
uniform mat4 mNorm;


uniform vec3 La;
uniform vec3 Ld;
uniform vec3 Ls;

uniform vec3 cameraPos;
uniform vec3 lightPos;

uniform float shininess=32;
uniform bool blinn;

out vec2 tc;
flat out vec3 AD;
flat out vec3 S;

void main() {
    vec4 p = m * vec4(pos,1);
    gl_Position = mvp * m * vec4(pos,1);
    vec3 N = normalize((mNorm*vec4(norm,1)).xyz);
    vec3 L = normalize(lightPos-p.xyz);

    vec3 A = La;
    vec3 D = Ld*max(dot(L,N),0);


    S = vec3(0,0,0);
    if(shininess>0){
	vec3 V = normalize(cameraPos-p.xyz);
	if(blinn){
	    float spec = pow(clamp(dot(N,normalize(V+L)),0,1),shininess);
	    S = spec*Ls;
	}else{
	    vec3 R = reflect(-L,N);
	    float spec = max(0,pow(dot(R,V),shininess));
	    S = spec*Ls;
	}
    }

    AD=A+D;

    tc=tex;
}
