Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 330
- in vec2 outTexCoord;
- out vec4 fragColor;
- uniform vec3 colour;
- uniform int useColour;
- in vec3 mvVertexNormal;
- in vec3 mvVertexPos;
- struct Attenuation
- {
- float constant;
- float linear;
- float exponent;
- };
- struct PointLight
- {
- vec3 colour;
- // Light position is assumed to be in view coordinates
- vec3 position;
- float intensity;
- Attenuation att;
- };
- uniform sampler2D texture_sampler;
- uniform vec3 ambientLight;
- uniform float specularPower;
- uniform PointLight pointLight;
- vec4 calcPointLight(PointLight light, vec3 position, vec3 normal)
- {
- vec4 diffuseColour = vec4(0, 0, 0, 0);
- vec4 specColour = vec4(0, 0, 0, 0);
- // Diffuse Light
- vec3 light_direction = light.position - position;
- vec3 to_light_source = normalize(light_direction);
- float diffuseFactor = max(dot(normal, to_light_source ), 0.0);
- diffuseColour = vec4(light.colour, 1.0) * light.intensity * diffuseFactor;
- // Specular Light
- vec3 camera_direction = normalize(-position);
- vec3 from_light_source = -to_light_source;
- vec3 reflected_light = normalize(reflect(from_light_source, normal));
- float specularFactor = max( dot(camera_direction, reflected_light), 0.0);
- specularFactor = pow(specularFactor, specularPower);
- specColour = specularFactor * vec4(light.colour, 1.0);
- // Attenuation
- float distance = length(light_direction);
- float attenuationInv = light.att.constant + light.att.linear * distance +
- light.att.exponent * distance * distance;
- return (diffuseColour + specColour) / attenuationInv;
- }
- void main()
- {
- if ( useColour == 1 )
- {
- fragColor = vec4(colour, 1);
- }
- else
- {
- fragColor = texture(texture_sampler, outTexCoord);
- }
- vec4 diffuseSpecularComp = calcPointLight(pointLight, mvVertexPos, mvVertexNormal);
- fragColor = vec4(ambientLight, 1) + diffuseSpecularComp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement