diff options
Diffstat (limited to 'data/shaders')
-rw-r--r-- | data/shaders/phong-frag.glsl | 31 | ||||
-rw-r--r-- | data/shaders/phong-vert.glsl | 2 |
2 files changed, 22 insertions, 11 deletions
diff --git a/data/shaders/phong-frag.glsl b/data/shaders/phong-frag.glsl index 84e9feb..9d8e40c 100644 --- a/data/shaders/phong-frag.glsl +++ b/data/shaders/phong-frag.glsl @@ -105,7 +105,7 @@ vec3 lightRadiance(Light light, vec3 direction) { vec3 materialAmbient() { if(material.useAmbientMap) { - return texture(material.ambientMap, fragTex).rgb * material.ambient; + return texture(material.ambientMap, fragTex).rgb; } else { return material.ambient; } @@ -118,7 +118,7 @@ vec3 materialDiffuse() { if(color.a == 0.0) { discard; } - return color.rgb * material.diffuse; + return color.rgb; } else { return material.diffuse; } @@ -126,7 +126,7 @@ vec3 materialDiffuse() { vec3 materialSpecular() { if(material.useSpecularMap) { - return texture(material.specularMap, fragTex).rgb * material.specular; + return texture(material.specularMap, fragTex).rgb; } else { return material.specular; } @@ -134,13 +134,27 @@ vec3 materialSpecular() { vec3 materialNormal() { if(material.useBumpMap) { - return normalize(texture(material.bumpMap, fragTex).xyz * 2.0 - 1.0); + // 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 q1 = dFdx(fragWorldPos); + vec3 q2 = dFdy(fragWorldPos); + vec2 st1 = dFdx(fragTex); + vec2 st2 = dFdy(fragTex); + vec3 N = normalize(fragNorm); + vec3 T = normalize(q1 * st2.t - q2 * st1.t); + vec3 B = -normalize(cross(N, T)); + mat3 TBN = mat3(T, B, N); + + return normalize(TBN * tangentNormal); } else { - return fragNorm; + return normalize(fragNorm); } } void main() { + vec3 viewDir = normalize(cameraPosition - fragWorldPos); vec3 ambientOcclusion = materialAmbient(); vec3 baseDiffuseColor = materialDiffuse(); vec3 baseSpecularColor = materialSpecular(); @@ -160,16 +174,13 @@ void main() { float diffuseFactor = posDot(lightDir, normal); vec3 reflectDir = reflect(-lightDir, normal); vec3 diffuseColor = baseDiffuseColor * diffuseFactor; - float specularFactor = 0; - if(material.shininess > 0) { - specularFactor = pow(posDot(lightDir, reflectDir), material.shininess); - } + float specularFactor = pow(posDot(viewDir, reflectDir), material.shininess); vec3 specularColor = baseSpecularColor * specularFactor; color += (diffuseColor + specularColor) * radiance; } // Apply ambient lighting. - vec3 ambientColor = baseDiffuseColor * ambientOcclusion * ambientLightColor.rgb; + vec3 ambientColor = baseDiffuseColor * ambientLightColor.rgb * ambientOcclusion; color += ambientColor; // Apply gamma correction and HDR tone mapping to get the final diff --git a/data/shaders/phong-vert.glsl b/data/shaders/phong-vert.glsl index 9b72eb3..f7fd363 100644 --- a/data/shaders/phong-vert.glsl +++ b/data/shaders/phong-vert.glsl @@ -30,7 +30,7 @@ uniform mat4 projection; void main() { fragWorldPos = vec3(model * vec4(position, 1.0)); - fragNorm = normalize(model * vec4(normal, 1.0)).xyz; + fragNorm = mat3(model) * normal; fragTex = texcoord; gl_Position = projection * view * vec4(fragWorldPos, 1.0); } |