#version 120

/*
 Copyright (C) 2010-2017 Kristian Duske

 This file is part of TrenchBroom.

 TrenchBroom is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 TrenchBroom is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
 */

uniform vec3 CameraPosition;
uniform vec3 LightDirection;
uniform vec4 LightDiffuse;
uniform vec4 LightSpecular;
uniform vec4 MaterialDiffuse;
uniform vec4 MaterialAmbient;
uniform vec4 MaterialSpecular;
uniform float MaterialShininess;
uniform vec4 GlobalAmbient;

void main(void) {
	gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
    
    vec3 normal = normalize(gl_NormalMatrix * gl_Normal);
    float NdotL = max(dot(normal, LightDirection), 0.0); // cosine of normal and light direction

    vec4 diffuse = NdotL * LightDiffuse * MaterialDiffuse;
    vec4 ambient = GlobalAmbient * MaterialAmbient;

    vec3 eyeVector = normalize(CameraPosition - gl_Vertex.xyz);
    vec3 halfVector = normalize(eyeVector - LightDirection);
    float NdotHV = max(-dot(normal, halfVector), 0.0);
    vec4 specular = MaterialSpecular * LightSpecular * pow(NdotHV, MaterialShininess);

	gl_FrontColor = vec4((diffuse + ambient + specular).xyz, 1.0);
}
