From cb7385bc1b6bcf6800c4af5c50f4f8652b3d233c Mon Sep 17 00:00:00 2001 From: s473621 Date: Sat, 3 Feb 2024 21:03:12 +0100 Subject: [PATCH] adding pbr shader for plants.......?...?..? --- PlanetCreator/cw 6/shaders/shader_pbr.frag | 67 ++++++++++++++-------- PlanetCreator/cw 6/src/ex_6_1.hpp | 3 +- 2 files changed, 46 insertions(+), 24 deletions(-) diff --git a/PlanetCreator/cw 6/shaders/shader_pbr.frag b/PlanetCreator/cw 6/shaders/shader_pbr.frag index f071342..9f2a55b 100644 --- a/PlanetCreator/cw 6/shaders/shader_pbr.frag +++ b/PlanetCreator/cw 6/shaders/shader_pbr.frag @@ -1,9 +1,15 @@ #version 430 core +#define PI 3.14159265359 float AMBIENT = 0.1; +uniform vec3 albedo; +uniform float metallic; +uniform float roughness; uniform vec3 color; uniform vec3 lightPos; +uniform vec3 viewPos; +uniform vec3 ambientColor; uniform sampler2D colorTexture; in vec3 fragNormal; @@ -12,40 +18,55 @@ in vec2 texCoords; out vec4 outColor; -uniform float shininess; -uniform vec3 ambientColor; -uniform vec3 specularColor; -uniform vec3 emissiveColor; -uniform float opticalDensity; -uniform float dissolve; -uniform int illuminationModel; +float specularD(float NdotH, float roughness) +{ + float alpha = roughness * roughness; + float alpha2 = alpha * alpha; + float cos2ThetaH = NdotH * NdotH; + + float expTerm = (cos2ThetaH - 1.0) / (alpha2 * cos2ThetaH); + return exp(expTerm) / (PI * alpha2 * pow(cos2ThetaH + alpha2 - 1.0, 2.0)); +} + +vec3 specularF(float LdotH, vec3 F0) +{ + return F0 + (vec3(1.0) - F0) * pow(1.0 - LdotH, 5.0); +} + +float specularG(float NdotL, float NdotV, float roughness) +{ + float k = (roughness + 1.0) * (roughness + 1.0) / 8.0; + + float GL = NdotL / (NdotL * (1.0 - k) + k); + float GV = NdotV / (NdotV * (1.0 - k) + k); + + return GL * GV; +} void main() { - vec3 lightDir = normalize(lightPos - fragPosition); vec3 normal = normalize(fragNormal); - float diffuse = max(0.0, dot(normal, lightDir)); + vec3 viewDir = normalize(viewPos - fragPosition); + vec3 lightDir = normalize(lightPos - fragPosition); - - vec3 lambertian = texture(colorTexture, texCoords).rgb * diffuse; + vec3 F0 = vec3(0.04); + F0 = mix(F0, albedo, metallic); - - vec3 viewDir = normalize(-fragPosition); - vec3 halfwayDir = normalize(lightDir + viewDir); - float specular = pow(max(0.0, dot(normal, halfwayDir)), shininess); - vec3 blinnPhong = specularColor * specular; + float NdotL = max(dot(normal, lightDir), 0.0); + float NdotV = max(dot(normal, viewDir), 0.0); - - vec3 emissive = emissiveColor; + vec3 H = normalize(lightDir + viewDir); + float NdotH = max(dot(normal, H), 0.0); + float LdotH = max(dot(lightDir, H), 0.0); + vec3 albedo = texture(colorTexture, texCoords).rgb; + albedo *= 2.0; + vec3 specular = F0 * (specularD(NdotH, roughness) * specularF(LdotH, F0) * specularG(NdotL, NdotV, roughness)) / (4.0 * NdotL * NdotV); + vec3 diffuse = (1.0 - F0) * albedo / PI; vec3 ambient = ambientColor * AMBIENT; - - vec3 finalColor = lambertian + blinnPhong + emissive + ambient; - - - finalColor *= (1.0 - dissolve); + vec3 finalColor = ambient + (diffuse + specular) * NdotL; outColor = vec4(finalColor, 1.0); } diff --git a/PlanetCreator/cw 6/src/ex_6_1.hpp b/PlanetCreator/cw 6/src/ex_6_1.hpp index 7f88d68..1e4e03d 100644 --- a/PlanetCreator/cw 6/src/ex_6_1.hpp +++ b/PlanetCreator/cw 6/src/ex_6_1.hpp @@ -399,7 +399,8 @@ void drawObjectTexture_plant(Core::RenderContext& context, glm::mat4 modelMatrix glUniform1f(glGetUniformLocation(program, "opticalDensity"), material.Ni); //glUniform1f(glGetUniformLocation(program, "dissolve"), material.d); glUniform1i(glGetUniformLocation(program, "illuminationModel"), material.illum); - + glUniform1f(glGetUniformLocation(program, "metallic"), 0.05); + glUniform1f(glGetUniformLocation(program, "roughness"), 0.2); Core::DrawContext(context); glUseProgram(0); }