aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-08-05 13:16:59 +0000
committertaw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5>2008-08-05 13:16:59 +0000
commitd9acb7a1cf5684f58145f652e787ac5e1a73e1a1 (patch)
treee2b6d2bfa4b6cc7be49160c9c0629e05b2f4ae47
parent61e7ac209eef22045823551b423289b4c237d8cd (diff)
Phong model for lighting
git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@208 84d2e878-0bd5-11dd-ad15-13eda11d74c5
-rw-r--r--data/shaders/lighting.frag34
-rw-r--r--data/shaders/lighting.vert16
2 files changed, 26 insertions, 24 deletions
diff --git a/data/shaders/lighting.frag b/data/shaders/lighting.frag
index 1bc0afd..5545b03 100644
--- a/data/shaders/lighting.frag
+++ b/data/shaders/lighting.frag
@@ -9,13 +9,14 @@
*
*/
+varying vec3 pos;
varying vec3 normal;
varying vec3 light0vc;
-varying vec3 light0hvc;
+varying float light0dist;
+
varying vec3 light1vc;
varying vec3 light2vc;
-varying vec3 light2hvc;
uniform sampler2D texture;
uniform sampler2D normalmap;
@@ -52,31 +53,32 @@ void main() {
/* Fill-in light (light 2) */
if ( fill_light_enabled ) {
- float ndothv;
+ float diff_fac, spec_fac;
+
+ diff_fac = max(dot(norm, normalize(light2vc)), 0.0);
+ diff += col_ambi_diff * gl_LightSource[2].diffuse.rgb * diff_fac;
- ndothv = max(dot(norm, normalize(light2hvc)), 0.0);
- diff += col_ambi_diff * gl_LightSource[2].diffuse.rgb * max(dot(vec3(light1vc), norm), 0.0);
- spec += gl_LightSource[2].specular.rgb * pow(ndothv, 80.0);
-
} else {
/* Spotlight (light 0) - positional, spotlight */
- float falloff;
- float spot;
- float ndothv;
+ float falloff, spot;
+ float diff_fac, spec_fac;
falloff = 1/ ( gl_LightSource[0].constantAttenuation
- + gl_LightSource[0].linearAttenuation * length(light0vc)
- + gl_LightSource[0].quadraticAttenuation * pow(length(light0vc), 2.0) );
+ + gl_LightSource[0].linearAttenuation * light0dist
+ + gl_LightSource[0].quadraticAttenuation * pow(light0dist, 2.0) );
+
spot = max(dot(normalize(-light0vc), gl_LightSource[0].spotDirection), 0.0);
spot = pow(spot, gl_LightSource[0].spotExponent);
- ndothv = max(dot(norm, normalize(light0hvc)), 0.0);
+ diff_fac = max(dot(norm, normalize(light0vc)), 0.0);
+ diff += col_ambi_diff * gl_LightSource[0].diffuse.rgb * spot * falloff * diff_fac;
- diff += col_ambi_diff * gl_LightSource[0].diffuse.rgb * spot * falloff *
- max(dot(normalize(light0vc).xyz, norm), 0.0);
+ vec3 E = normalize(-pos);
+ vec3 R = normalize(-reflect(light0vc, norm));
- spec += vec3(1.0, 1.0, 1.0) * gl_LightSource[0].specular.rgb * spot * falloff * pow(ndothv, 80.0);
+ spec_fac = pow(max(0.0, dot(R, E)), 80.0);
+ spec += vec3(1.0, 1.0, 1.0) * gl_LightSource[0].specular.rgb * spot * falloff * spec_fac;
/* Background glow (light 1) - diffuse only, directional */
diff += col_ambi_diff * gl_LightSource[1].diffuse.rgb * max(dot(vec3(light1vc), norm), 0.0);
diff --git a/data/shaders/lighting.vert b/data/shaders/lighting.vert
index 7f9d606..59d026d 100644
--- a/data/shaders/lighting.vert
+++ b/data/shaders/lighting.vert
@@ -9,13 +9,14 @@
*
*/
+varying vec3 pos;
varying vec3 normal;
varying vec3 light0vc;
-varying vec3 light0hvc;
+varying float light0dist;
+
varying vec3 light1vc;
varying vec3 light2vc;
-varying vec3 light2hvc;
varying vec3 col_ambi_diff;
varying vec3 col_emit;
@@ -23,16 +24,16 @@ varying vec3 col_emit;
void main() {
vec4 vert;
- vec4 E;
+ vec3 vert_to_light;
vert = gl_ModelViewMatrix * gl_Vertex;
+ pos = vec3(vert);
normal = gl_NormalMatrix * gl_Normal;
- E = vec4(0.0, 0.0, 1.0, 0.0) - vert;
/* Spotlight - positional light */
- light0vc = vec3(gl_LightSource[0].position - vert);
- light0hvc = normalize(vec3(E) + light0hvc);
- light0vc = normalize(light0vc);
+ vert_to_light = vec3(gl_LightSource[0].position - vert);
+ light0dist = length(vert_to_light);
+ light0vc = normalize(vert_to_light);
/* Diffuse "background glow" - this can be normalised only once, here, since 'position'
* is really 'direction' and is the same for all vertices. */
@@ -40,7 +41,6 @@ void main() {
/* Fill-in light */
light2vc = normalize(vec3(gl_LightSource[2].position));
- light2hvc = normalize(vec3(E) + light2vc);
/* Material properties */
col_ambi_diff = gl_Color.rgb;