#version 430
layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexNormal;
layout (location = 2) in vec2 VertexTexCoord;
out vec3 LightIntensity;
out vec2 OutVertexTexCoord;
struct LightInfo {
vec3 Position; // Light position in eye coords.
vec3 La;
// Ambient light intensity
vec3 Ld;
// Diffuse light intensity
vec3 Ls;
// Specular light intensity
};
uniform LightInfo Light;
struct MaterialInfo {
vec3 Ka;
// Ambient reflectivity
vec3 Kd;
// Diffuse reflectivity
vec3 Ks;
// Specular reflectivity
float Shininess;
// Specular shininess factor
};
uniform MaterialInfo Material;
uniform mat4 ModelViewMatrix;
uniform mat3 NormalMatrix;
uniform mat4 ProjectionMatrix;
uniform mat4 MVP;
void getEyeSpace( out vec3 norm, out vec4 position )
{
norm = normalize( NormalMatrix * VertexNormal);
position = ModelViewMatrix * vec4(VertexPosition,1.0);
}
vec3 phongModel( vec4 position, vec3 norm )
{
vec3 s = normalize(Light.Position - vec3(position));
vec3 v = normalize(-position.xyz);
vec3 r = reflect( -s, norm );
vec3 ambient = Light.La * Material.Ka;
float sDotN = max( dot(s,norm), 0.0 );
vec3 diffuse = Light.Ld * Material.Kd * sDotN;
vec3 spec = vec3(0.0);
if( sDotN > 0.0 )
spec = Light.Ls * Material.Ks *
pow( max( dot(r,v), 0.0 ), Material.Shininess );
return ambient + diffuse + spec;
// return min( max( ambient + diffuse + spec, vec3(0,0,0) ), vec3(0.99,0.99,0.99) );
}
void main()
{
vec3 eyeNorm;
vec4 eyePosition;
// Get the position and normal in eye space
getEyeSpace(eyeNorm, eyePosition);
// Evaluate the lighting equation.
LightIntensity = phongModel( eyePosition, eyeNorm );
// LightIntensity = vec3(0.7,0.7,0.7);
gl_Position = MVP * vec4(VertexPosition,1.0);
OutVertexTexCoord = VertexTexCoord;
}