Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 330 core
- struct Material {
- sampler2D diffuse;
- sampler2D specular;
- sampler2D emission;
- 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 4
- out vec4 FragColor;
- in vec3 Normal;
- in vec3 FragPos;
- in vec2 TexCoords;
- uniform vec3 objectColor;
- uniform vec3 viewPos;
- uniform Material material;
- uniform DirLight dirLight;
- uniform PointLight pointLights[NR_POINT_LIGHTS];
- uniform int number_of_point_lights;
- vec3 directional_light_calculation(DirLight light, vec3 normal, vec3 viewDir);
- vec3 point_light_calculation(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
- void main()
- {
- vec3 output = vec3(0.0f);
- vec3 norm = normalize(Normal);
- vec3 viewDir = normalize(viewPos - FragPos);
- output += directional_light_calculation(dirLight, norm, viewDir);
- for (int i = 0; i < NR_POINT_LIGHTS; i++)
- {
- output += point_light_calculation(pointLights[i], norm, FragPos, viewDir);
- }
- FragColor = vec4(texture(material.emission, TexCoords) + output, 1.0f);
- }
- vec3 directional_light_calculation(DirLight light, vec3 normal, vec3 viewDir){
- vec3 lightDir = normalize(-light.direction);
- float diff = max(dot(normal, lightDir), 0.0);
- vec3 reflectDir = reflect(-lightDir, normal);
- float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
- vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
- vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
- vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
- return ambient + diffuse + specular;
- }
- vec3 point_light_calculation(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir){
- vec3 lightDir = normalize(light.position - fragPos);
- float diff = max(dot(normal, lightDir), 0.0);
- vec3 reflectDir = reflect(-lightDir, normal);
- float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
- float distance = length(light.position - fragPos);
- float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
- vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
- vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
- vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
- ambient *= attenuation;
- diffuse *= attenuation;
- specular *= attenuation;
- return ambient + diffuse + specular;
- }
Add Comment
Please, Sign In to add comment