From d9acb7a1cf5684f58145f652e787ac5e1a73e1a1 Mon Sep 17 00:00:00 2001 From: taw27 Date: Tue, 5 Aug 2008 13:16:59 +0000 Subject: Phong model for lighting git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@208 84d2e878-0bd5-11dd-ad15-13eda11d74c5 --- data/shaders/lighting.frag | 34 ++++++++++++++++++---------------- data/shaders/lighting.vert | 16 ++++++++-------- 2 files changed, 26 insertions(+), 24 deletions(-) (limited to 'data') 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; -- cgit v1.2.3