Implemented point lights

This commit is contained in:
Nigel Barink 2022-02-16 20:12:07 +01:00
parent 345ae95835
commit e1ceb4d8ee
2 changed files with 23 additions and 4 deletions

View File

@ -12,15 +12,23 @@ struct Material {
};
struct Light{
// vec3 position; // Not neccessary when using directional lights.
vec3 position;
vec3 direction;
// vec3 direction; // neccessary when using directional lights.
vec3 ambient;
vec3 diffuse;
vec3 specular;
// lets create point lights
float constant;
float linear;
float quadratic;
};
uniform Light light;
uniform Material material;
@ -36,7 +44,7 @@ void main()
// diffuse lighting calculation
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(-light.direction); // simplified with directional lights normalize(light.position - FragPos);
vec3 lightDir = normalize(light.position - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
@ -48,6 +56,15 @@ void main()
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = light.specular * spec * vec3( texture(material.specular, TexCoords));
// calculate the attenuation
float distance = length(light.position - FragPos);
float attenuation = 1.0/ (light.constant + light.linear * distance + light.quadratic * (distance*distance));
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
// Calculate the result;
vec3 result = ambient + diffuse + specular ;

View File

@ -265,7 +265,9 @@ while(!glfwWindowShouldClose(window))
shader.setVec3("light.diffuse", glm::vec3(0.5f, 0.5f, 0.5f));
shader.setVec3("light.specular", glm::vec3(1.0f, 1.0f, 1.0f));
shader.setFloat("light.constant", 1.0f);
shader.setFloat("light.linear", 0.09f);
shader.setFloat("light.quadratic", 0.032f);
shader.setVec3("viewPos", camera.Position);