#version 330 core out vec4 FragColor; struct Material { sampler2D texture_diffuse1; sampler2D texture_diffuse2; sampler2D texture_diffuse3; sampler2D texture_specular1; sampler2D texture_specular2; float shininess; }; struct DirLight { vec3 direction; vec3 ambient; vec3 diffuse; vec3 specular; }; struct PointLight { vec3 position; float constant; float linear; float quadratic; vec3 ambient; vec3 diffuse; vec3 specular; }; #define NR_POINT_LIGHTS 10 in vec3 FragPos; in vec3 Normal; in vec2 TexCoords; uniform int PointLightNum; uniform vec3 viewPos; uniform DirLight dirLight; uniform PointLight pointLights[NR_POINT_LIGHTS]; uniform Material material; // function prototypes vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir); vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir); void main() { // properties vec3 norm = normalize(Normal); vec3 viewDir = normalize(viewPos - FragPos); // == ===================================================== // Our lighting is set up in 3 phases: directional, point lights and an optional flashlight // For each phase, a calculate function is defined that calculates the corresponding color // per lamp. In the main() function we take all the calculated colors and sum them up for // this fragment's final color. // == ===================================================== // phase 1: directional lighting vec3 result = CalcDirLight(dirLight, norm, viewDir); // phase 2: point lights for(int i = 0; i < PointLightNum; i++) { result += CalcPointLight(pointLights[i], norm, FragPos, viewDir); } // phase 3: spot light FragColor = vec4(result, 1.0); } // calculates the color when using a directional light. vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir) { vec3 lightDir = normalize(-light.direction); // diffuse shading float diff = max(dot(normal, lightDir), 0.0); // specular shading vec3 reflectDir = reflect(-lightDir, normal); float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); // combine results vec3 ambient = light.ambient * vec3(texture(material.texture_diffuse1, TexCoords)); vec3 diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse1, TexCoords)); vec3 specular = light.specular * spec * vec3(texture(material.texture_specular1, TexCoords)); return (ambient + diffuse + specular); } // calculates the color when using a point light. vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir) { vec3 lightDir = normalize(light.position - fragPos); // diffuse shading float diff = max(dot(normal, lightDir), 0.0); // specular shading vec3 reflectDir = reflect(-lightDir, normal); float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); // attenuation float distance = length(light.position - fragPos); float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance)); attenuation = min(attenuation, 1.0f); // combine results vec3 ambient = light.ambient * vec3(texture(material.texture_diffuse1, TexCoords)); vec3 diffuse = light.diffuse * diff * vec3(texture(material.texture_diffuse1, TexCoords)); vec3 specular = light.specular * spec * vec3(texture(material.texture_specular1, TexCoords)); ambient *= attenuation; diffuse *= attenuation; specular *= attenuation; return (ambient + diffuse + specular); }