diff --git a/shader.fs b/shader.fs index 27f0fea..52fd39c 100644 --- a/shader.fs +++ b/shader.fs @@ -14,7 +14,9 @@ struct Material { struct Light{ vec3 position; - // vec3 direction; // neccessary when using directional lights. + vec3 direction; // neccessary when using directional lights or spotlights. + float cutoff; + float outerCutOff; vec3 ambient; vec3 diffuse; @@ -39,6 +41,8 @@ uniform vec3 viewPos; void main() { + + // ambient lighting calculation vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords)); @@ -56,17 +60,29 @@ void main() float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); vec3 specular = light.specular * spec * vec3( texture(material.specular, TexCoords)); + + + float theta = dot(lightDir, normalize(-light.direction)); + float epsilon = light.cutoff - light.outerCutOff; + float intensity = clamp((theta - light.outerCutOff)/ epsilon, 0.0, 1.0); + + + diffuse *= intensity; + specular *= intensity; + + // 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 ; FragColor = vec4(result, 1.0); diff --git a/src/main.cpp b/src/main.cpp index 23cb8bb..1727ebd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -258,13 +258,17 @@ while(!glfwWindowShouldClose(window)) - - shader.setVec3("light.direction", lightpos); + shader.setVec3("light.position", camera.Position); + shader.setVec3("light.direction", camera.Front); + shader.setFloat("light.cutoff", glm::cos(glm::radians(12.5f))); + shader.setFloat("light.outerCutOff", glm::cos(glm::radians(17.5f))); shader.setVec3("light.ambient", glm::vec3(0.2f, 0.2f, 0.2f)); 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);