summaryrefslogtreecommitdiff
path: root/data/shaders/phong-frag.glsl
diff options
context:
space:
mode:
Diffstat (limited to 'data/shaders/phong-frag.glsl')
-rw-r--r--data/shaders/phong-frag.glsl45
1 files changed, 20 insertions, 25 deletions
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