diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-03-14 22:11:43 +1100 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-03-14 22:11:43 +1100 |
commit | 253c90f7ceabff956260b40b69ec7ca55f91e710 (patch) | |
tree | 49e45764acab57368b4914bcb0fd77b6b1a1c862 /assets/shaders/blinn_phong.frag | |
parent | 2af96e3bc19fac5a3dc27f0eedff1b95ef1d473b (diff) | |
parent | 70308798adbaa376da97c9c0739d437fe76b8b36 (diff) |
Merge branch 'master' of github.com:omnisci3nce/celeritas-core
Diffstat (limited to 'assets/shaders/blinn_phong.frag')
-rw-r--r-- | assets/shaders/blinn_phong.frag | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/assets/shaders/blinn_phong.frag b/assets/shaders/blinn_phong.frag new file mode 100644 index 0000000..095b19a --- /dev/null +++ b/assets/shaders/blinn_phong.frag @@ -0,0 +1,95 @@ +#version 410 core +out vec4 FragColor; + +// A Blinn-Phong material with textures for diffuse and specular +// lighting maps and a numeric shininess factor. +struct Material { + sampler2D diffuse; + sampler2D specular; + float shininess; +}; + +struct DirLight { + vec3 direction; + vec3 ambient; + vec3 diffuse; + vec3 specular; +}; + +struct PointLight { + vec3 position; + // fall off properties + float constant; + float linear; + float quadratic; + + vec3 ambient; + vec3 diffuse; + vec3 specular; +}; + +in VS_OUT { + vec3 FragPos; + vec3 Normal; + vec2 TexCoords; + vec4 FragPosLightSpace; +} fs_in; + +// --- Uniforms +#define NUM_POINT_LIGHTS 4 +uniform vec3 viewPos; +uniform Material material; +uniform DirLight dirLight; +uniform PointLight pointLights[NUM_POINT_LIGHTS]; + +// --- Function prototypes +vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir); +vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir); + +void main() { + vec3 norm = normalize(fs_in.Normal); + vec3 viewDir = normalize(viewPos - fs_in.FragPos); + + vec3 result = CalcDirLight(dirLight, norm, viewDir); + for (int i = 0; i < 4; i++) { + result += CalcPointLight(pointLights[i], norm, fs_in.FragPos, viewDir); + } + + FragColor = vec4(result, 1.0); +} + +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 result + vec3 ambient = light.ambient * vec3(texture(material.diffuse, fs_in.TexCoords)); + vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, fs_in.TexCoords)); + vec3 specular = light.specular * spec * vec3(texture(material.specular, fs_in.TexCoords)); + return (ambient + diffuse + specular); +} + +vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir) +{ + vec3 lightDir = normalize(light.position - fragPos); + // diffuse + float diff = max(dot(normal, lightDir), 0.0); + // specular + vec3 reflectDir = reflect(-lightDir, normal); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); + // attentuation + float distance = length(light.position - fragPos); + float attentuation = 1.0 / (light.constant + light.linear * distance * light.quadratic * (distance * distance)); + // result + vec3 ambient = light.ambient * vec3(texture(material.diffuse, fs_in.TexCoords)); + vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, fs_in.TexCoords)); + vec3 specular = light.specular * spec * vec3(texture(material.specular, fs_in.TexCoords)); + ambient *= attentuation; + diffuse *= attentuation; + specular *= attentuation; + return (ambient + diffuse + specular); +}
\ No newline at end of file |