Implemented point lights
This commit is contained in:
parent
345ae95835
commit
e1ceb4d8ee
23
shader.fs
23
shader.fs
@ -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 ;
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user