1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#version 330 core
/**
* The Material structure stores the details about the material
*/
struct Material
{
vec4 ambientColor;
vec4 diffuseColor;
vec4 specularColor;
float dissolve;
float specularPower;
float illumination;
};
// The light structure
struct Light
{
vec3 direction;
vec4 color;
float intensity;
};
uniform mat4 mTransform;
uniform mat4 camProj;
uniform mat4 camView;
uniform sampler2D textureID;
uniform Material material;
uniform Light light;
in vec4 vColor;
in vec4 vNormal;
in vec4 vPosition;
in vec2 vTexCoords;
layout(location = 0) out vec4 fragColor;
vec4 getBaseColor()
{
// Create the texture color
vec4 texColor = texture(textureID, vTexCoords);
return vec4(min(texColor.rgb + vColor.rgb, vec3(1.0)), texColor.a * vColor.a);
}
vec4 getDirectionalLight()
{
// The matrices for transforming into different spaces
mat4 modelMatrix = mTransform;
mat3 normalMatrix = transpose(inverse(mat3(modelMatrix)));
// The transformed normal and position
vec3 normal = normalMatrix * vNormal.xyz;
vec3 position = vec3(modelMatrix * vPosition);
// The individual components of light
vec4 ambientLight = vec4(0.0);
vec4 diffuseLight = vec4(0.0);
vec4 specularLight = vec4(0.0);
// Calculate the ambient light
ambientLight = light.color * material.ambientColor;
// Calculate the diffuse light
float diffuseFactor = dot(normalize(normal), -light.direction);
if (diffuseFactor > 0.0)
{
diffuseLight = light.color * material.diffuseColor * diffuseFactor;
// Calculate the specular light
vec3 vertexToEye = normalize(vPosition.xyz - position);
vec3 lightReflect = normalize(reflect(light.direction, normal));
float specularFactor = dot(vertexToEye, lightReflect);
specularFactor = pow(specularFactor, material.specularPower);
if (specularFactor > 0)
specularLight = light.color * specularFactor;
}
// Calculate the final directional light
return (ambientLight + diffuseLight + specularLight) * light.intensity;
}
void main()
{
fragColor = getBaseColor() * getDirectionalLight();
}
|