From caf243c60f84850a9a70c94db577dd2322a782fa Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 12 Aug 2021 07:42:22 -0400 Subject: graphics: Refactor model/phong/pbr modules to use new mesh module. --- data/shaders/pbr-frag.glsl | 4 ++-- data/shaders/phong-frag.glsl | 45 ++++++++++++++++++++------------------------ 2 files changed, 22 insertions(+), 27 deletions(-) (limited to 'data') diff --git a/data/shaders/pbr-frag.glsl b/data/shaders/pbr-frag.glsl index a941a29..478c40a 100644 --- a/data/shaders/pbr-frag.glsl +++ b/data/shaders/pbr-frag.glsl @@ -53,7 +53,7 @@ out vec4 fragColor; uniform Material material; uniform Light lights[MAX_LIGHTS]; -uniform vec4 ambientLightColor; +uniform vec4 ambientLight; uniform bool vertexColored; uniform vec3 cameraPosition; uniform sampler2D baseColorTexture; @@ -386,7 +386,7 @@ void main(void) { // is dampened by the ambient occlusion factor. // // TODO: Use image based lighting. - color += ambientLightColor.rgb * albedo * ambientOcclusion; + color += ambientLight.rgb * albedo * ambientOcclusion; // Apply Reinhard tone mapping to convert our high dynamic range // color value to low dynamic range. All of the lighting // calculations stacked on top of each other is likely to create diff --git a/data/shaders/phong-frag.glsl b/data/shaders/phong-frag.glsl index 7cab3f8..363c4e7 100644 --- a/data/shaders/phong-frag.glsl +++ b/data/shaders/phong-frag.glsl @@ -2,17 +2,13 @@ struct Material { vec3 ambient; - sampler2D ambientMap; bool useAmbientMap; vec3 diffuse; - sampler2D diffuseMap; bool useDiffuseMap; vec3 specular; - sampler2D specularMap; bool useSpecularMap; float shininess; - sampler2D bumpMap; - bool useBumpMap; + bool useNormalMap; }; struct Light { @@ -40,10 +36,14 @@ in vec2 fragTex; out vec4 fragColor; #endif +uniform sampler2D ambientMap; +uniform sampler2D diffuseMap; +uniform sampler2D specularMap; +uniform sampler2D normalMap; uniform Material material; uniform Light lights[MAX_LIGHTS]; uniform vec3 cameraPosition; -uniform vec4 ambientLightColor; +uniform vec4 ambientLight; const float GAMMA = 2.2; @@ -54,10 +54,6 @@ vec2 texture(sampler2D tex, vec2 coord) { } #endif -float posDot(vec3 v1, vec3 v2) { - return max(dot(v1, v2), 0.0); -} - vec3 gammaCorrect(vec3 color) { return pow(color, vec3(1.0 / GAMMA)); } @@ -105,7 +101,7 @@ vec3 lightRadiance(Light light, vec3 direction) { vec3 materialAmbient() { if(material.useAmbientMap) { - return texture(material.ambientMap, fragTex).rgb; + return texture(ambientMap, fragTex).rgb; } else { return material.ambient; } @@ -113,7 +109,7 @@ vec3 materialAmbient() { vec3 materialDiffuse() { if(material.useDiffuseMap) { - vec4 color = texture(material.diffuseMap, fragTex); + vec4 color = texture(diffuseMap, fragTex); // discard transparent fragments. if(color.a == 0.0) { discard; @@ -126,18 +122,18 @@ vec3 materialDiffuse() { vec3 materialSpecular() { if(material.useSpecularMap) { - return texture(material.specularMap, fragTex).rgb; + return texture(specularMap, fragTex).rgb; } else { return material.specular; } } vec3 materialNormal() { - if(material.useBumpMap) { + if(material.useNormalMap) { // Compute tangent space using fragment data rather than relying // on tangent attributes. See: // http://www.thetenthplanet.de/archives/1180 - vec3 tangentNormal = normalize(texture(material.bumpMap, fragTex).xyz * 2.0 - 1.0); + vec3 tangentNormal = normalize(texture(normalMap, fragTex).xyz * 2.0 - 1.0); vec3 q1 = dFdx(fragWorldPos); vec3 q2 = dFdy(fragWorldPos); vec2 st1 = dFdx(fragTex); @@ -156,8 +152,8 @@ vec3 materialNormal() { void main() { vec3 viewDir = normalize(cameraPosition - fragWorldPos); vec3 ambientOcclusion = materialAmbient(); - vec3 baseDiffuseColor = materialDiffuse(); - vec3 baseSpecularColor = materialSpecular(); + vec3 diffuseColor = materialDiffuse(); + vec3 specularColor = materialSpecular(); vec3 normal = materialNormal(); vec3 color = vec3(0.0); @@ -170,18 +166,17 @@ void main() { } vec3 lightDir = lightDirection(light); - vec3 radiance = lightRadiance(light, lightDir); - float diffuseFactor = posDot(lightDir, normal); - vec3 reflectDir = reflect(-lightDir, normal); - vec3 diffuseColor = baseDiffuseColor * diffuseFactor; vec3 halfVector = normalize(lightDir + viewDir); - float specularFactor = pow(posDot(halfVector, normal), material.shininess); - vec3 specularColor = baseSpecularColor * specularFactor; - color += (diffuseColor + specularColor) * radiance; + vec3 radiance = lightRadiance(light, lightDir); + float lambert = clamp(dot(normal, lightDir), 0.0, 1.0); + vec3 diffuseLight = radiance * lambert; + float specularFactor = clamp(dot(halfVector, normal), 0.0, 1.0) * float(lambert > 0.0); + vec3 specularLight = radiance * pow(specularFactor, material.shininess); + color += diffuseLight * diffuseColor + specularLight * specularColor; } // Apply ambient lighting. - vec3 ambientColor = baseDiffuseColor * ambientLightColor.rgb * ambientOcclusion; + vec3 ambientColor = diffuseColor * ambientLight.rgb * ambientOcclusion; color += ambientColor; // Apply gamma correction and HDR tone mapping to get the final -- cgit v1.2.3