diff options
Diffstat (limited to 'shader')
-rw-r--r-- | shader/light.frag | 30 | ||||
-rw-r--r-- | shader/light.vert | 2 |
2 files changed, 15 insertions, 17 deletions
diff --git a/shader/light.frag b/shader/light.frag index c3cdbfb..340b459 100644 --- a/shader/light.frag +++ b/shader/light.frag @@ -7,33 +7,31 @@ varying vec3 normal, pos; void main() { vec3 n, l, refl, eye; float NdotL, RdotE; - vec4 color, specularColor; - float dist, att, specularFactor; + vec4 specularColor; + float dist, distSq, att, specularFactor; n = normalize(normal); l = gl_LightSource[0].position.xyz - pos; - dist = length(l); - l /= dist; + distSq = dot(l, l); - /* compute the dot product between normal and normalized lightdir */ - NdotL = max(dot(n, l), 0.0); + NdotL = dot(n, l); if (NdotL > 0.0) { - att = 1.0 / (gl_LightSource[0].constantAttenuation + - gl_LightSource[0].linearAttenuation * dist + - gl_LightSource[0].quadraticAttenuation * dist * dist); - color = att * (diffuse * NdotL + ambient); + dist = sqrt(distSq); - refl = normalize(reflect(-l, n)); - eye = normalize(-pos); + att = 1.0 / (gl_LightSource[0].quadraticAttenuation * distSq); + gl_FragColor = att * (diffuse * NdotL / dist + ambient) * texture2D(tex, gl_TexCoord[0].st); - RdotE = max(dot(refl, eye), 0.0); - specularFactor = att * pow(RdotE, gl_FrontMaterial.shininess); - specularColor = specularFactor * gl_FrontMaterial.specular * gl_LightSource[0].specular; + refl = reflect(-l, n); - gl_FragColor = color * texture2D(tex, gl_TexCoord[0].st) + specularColor; + RdotE = -dot(refl, pos) / sqrt(dot(refl, refl) * dot(pos, pos)); + + if(RdotE > 0.0) { + specularFactor = att * pow(RdotE, gl_FrontMaterial.shininess); + gl_FragColor += specularFactor * gl_FrontMaterial.specular * gl_LightSource[0].specular; + } } else { gl_FragColor = vec4(0, 0, 0, 1); diff --git a/shader/light.vert b/shader/light.vert index b97da78..2faa1c0 100644 --- a/shader/light.vert +++ b/shader/light.vert @@ -3,7 +3,7 @@ varying vec3 normal, pos; void main() { - normal = normalize(gl_NormalMatrix * gl_Normal); + normal = gl_NormalMatrix * gl_Normal; pos = vec3(gl_ModelViewMatrix * gl_Vertex); |