LOG750-LAB2/src/shaders/basicShader.vert
2016-11-10 18:51:45 -05:00

91 lines
2.4 KiB
GLSL

#version 400 core
uniform mat4 mvMatrix;
uniform mat4 projMatrix;
uniform mat3 normalMatrix;
uniform vec3 lDirection;
uniform bool isLightSource;
uniform vec4 color;
uniform bool isSky;
uniform bool isPhong;
uniform vec3 pointLight[3];
in vec4 vPosition;
in vec3 vNormal;
in vec2 vUv;
out vec2 texCoords;
out vec4 ifColor;
out vec3 fPosition;
out vec3 fNormal;
vec4 calcDirLight(vec4 eye, vec3 fPos, vec3 fNorm) {
// Get lighting vectors
vec3 LightDirection = normalize(lDirection);
vec3 nfNormal = normalize(fNorm);
vec3 nviewDirection = normalize(fPos);
// Compute diffuse component
float diff = 0.65 * max(0.0, dot(nfNormal, LightDirection));
// Compute specular component
vec3 Rl = normalize(-LightDirection+2.0*nfNormal*dot(nfNormal,LightDirection));
float spec = 0.1*pow(max(0.0, dot(Rl, nviewDirection)), 16);
// Compute ambient component
vec3 amb = vec3(0.1);
// Vertex color (currently static, to be replaced with texture)
vec4 color = ifColor;
return color * vec4(amb + diff + spec, 1);
}
vec4 calcPointLight(vec4 eye, vec3 fPos, vec3 fNorm, int i) {
// Get lighting vectors
vec3 LightDirection = normalize(pointLight[i] - fPos);
vec3 nfNormal = normalize(fNorm);
vec3 nviewDirection = normalize(fPos);
// Attenuation
float distance = length(pointLight[i] - nviewDirection);
float attenuation = 1.0f / (distance * distance);
// Compute diffuse component
float diff = attenuation * 0.65 * max(0.0, dot(nfNormal, LightDirection));
// Compute specular component
vec3 Rl = normalize(-LightDirection+2.0*nfNormal*dot(nfNormal,LightDirection));
float spec = attenuation * 0.1*pow(max(0.0, dot(Rl, nviewDirection)), 16);
// Compute ambient component
vec3 amb = attenuation * vec3(0.1);
// Vertex color (currently static, to be replaced with texture)
vec4 color = ifColor;
return 0.3 * color * vec4(amb + diff + spec, 1);
}
void
main()
{
vec4 vEyeCoord = mvMatrix * vPosition;
gl_Position = projMatrix * vEyeCoord;
fPosition = -vEyeCoord.xyz;
fNormal = normalMatrix * vNormal;
texCoords = vUv;
ifColor = color;
if(!isPhong && !isSky && !isLightSource) {
ifColor = calcDirLight(vEyeCoord, fPosition, fNormal)
+ calcPointLight(vEyeCoord, fPosition, fNormal, 0)
+ calcPointLight(vEyeCoord, fPosition, fNormal, 1)
+ calcPointLight(vEyeCoord, fPosition, fNormal, 2);
}
}