Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
4.2 kB
2
Indexable
Never
#version 330 core
out vec4 FragColor;

#define NUM_POINT_LIGHTS 4

in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoords;

struct Material {
    sampler2D diffuse;
    sampler2D specular;
    float shininess;
};

struct DirLight{
    vec3 direction;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;

};

struct PointLight{
    vec3 position;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;

    float constant;
    float linear;
    float quadratic;

};

struct SpotLight{

    vec3 position;
    vec3 direction;
    float cutOff;
    float outerCutOff;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;

    float constant;
    float linear;
    float quadratic;

};

uniform vec3 viewPos;
uniform Material material;
uniform DirLight dirlight;
uniform PointLight pointlights[4];
uniform SpotLight spotlight;

vec3 calcDirLight(DirLight dirlight,vec3 normal,vec3 viewDir);
vec3 calcPointLight(PointLight pointlight, vec3 normal,vec3 viewDir,vec3 FragPos);
vec3 calcSpotLight(SpotLight spotlight,vec3 normal,vec3 viewDir,vec3 FragPos);

void main(){

    vec3 norm = normalize(Normal);
    vec3 viewDir = normalize(viewPos - FragPos);

    vec3 result = calcDirLight(dirlight,norm,viewDir);

    for(int i = 0; i < 4;i++){
        result += calcPointLight(pointlights[i],norm,viewDir,FragPos);
    }

    result += calcSpotLight(spotlight,norm,viewDir,FragPos);

    FragColor = vec4(result,1.0);
}

vec3 calcDirLight(DirLight dirlight,vec3 normal,vec3 viewDir){

    vec3 lightDir = normalize(-dirlight.direction);

    //diffuse
    float diff = max(dot(normal,lightDir),0.0f);
    //specular
    vec3 reflectDIr = reflect(-lightDir,normal);
    float spec = pow(max(dot(viewDir,reflectDIr),0.0f),material.shininess);

    //combine
    vec3 ambient = dirlight.ambient * texture(material.diffuse,TexCoords).rgb;
    vec3 diffuse = dirlight.diffuse * diff * texture(material.diffuse,TexCoords).rgb;
    vec3 specular = dirlight.specular * spec * texture(material.specular,TexCoords).rgb;

    return (ambient + diffuse + specular);
}

vec3 calcPointLight(PointLight pointlight, vec3 normal,vec3 viewDir,vec3 FragPos){

    vec3 lightDir = normalize(pointlight.position - FragPos);
    //diff
    float diff = max(dot(normal,lightDir),0.0f);
    //spec
    vec3 reflectDir = reflect(-lightDir,normal);
    float spec = pow(max(dot(viewDir,reflectDir),0.0f),material.shininess);
    //attenuation
    float distance = length(pointlight.position - FragPos);
    float attenuation = 1.0/(pointlight.constant + pointlight.linear * distance + pointlight.quadratic * distance * distance);
    //combine
    vec3 ambient = dirlight.ambient * texture(material.diffuse,TexCoords).rgb;
    vec3 diffuse = dirlight.diffuse * diff * texture(material.diffuse,TexCoords).rgb;
    vec3 specular = dirlight.specular * spec * texture(material.specular,TexCoords).rgb;

    ambient *= attenuation;
    diffuse *= attenuation;
    specular*= attenuation;

    return (ambient + diffuse + specular);

}

vec3 calcSpotLight(SpotLight spotlight,vec3 normal,vec3 viewDir,vec3 FragPos){

    //diff
    vec3 lightDir = normalize(spotlight.position - FragPos);
    float diff = max(dot(normal,lightDir),0.0);
    //spec
    vec3 reflectDir = reflect(-lightDir,normal);
    float spec = pow(max(dot(viewDir,reflectDir),0.0),material.shininess);
    //attenuation
    float distance = length(spotlight.position - FragPos);
    float attenuation = 1.0/(spotlight.constant + spotlight.linear * distance + spotlight.quadratic * distance * distance);
    //intensity
    float theta = dot(lightDir,normalize(-spotlight.direction));
    float epsilon = spotlight.cutOff - spotlight.outerCutOff;
    float intensity = clamp((theta - spotlight.outerCutOff)/epsilon,0.0f,1.0f);
    //combine
    vec3 ambient =  spotlight.ambient * texture(material.diffuse,TexCoords).rgb;
    vec3 diffuse =  spotlight.diffuse * diff * texture(material.diffuse,TexCoords).rgb;
    vec3 specular = spotlight.specular * spec * texture(material.specular,TexCoords).rgb;

    ambient *= attenuation;
    diffuse *= attenuation;
    specular*= attenuation;

    return (ambient + diffuse + specular);

}