diff options
author | taw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5> | 2008-05-24 21:30:42 +0000 |
---|---|---|
committer | taw27 <taw27@84d2e878-0bd5-11dd-ad15-13eda11d74c5> | 2008-05-24 21:30:42 +0000 |
commit | 8684d5a1a6d18e32eb15324f19da18ae88472707 (patch) | |
tree | a14fd840242c533ec1cf2d949d43a526555efec5 /data | |
parent | 3e3436f1a84a3f418e9894356a6262118b30708f (diff) |
Loads of shader and lighting stuff
git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@39 84d2e878-0bd5-11dd-ad15-13eda11d74c5
Diffstat (limited to 'data')
-rw-r--r-- | data/shaders/lighting.frag | 75 | ||||
-rw-r--r-- | data/shaders/lighting.vert | 39 |
2 files changed, 68 insertions, 46 deletions
diff --git a/data/shaders/lighting.frag b/data/shaders/lighting.frag index ee1df5b..77102e9 100644 --- a/data/shaders/lighting.frag +++ b/data/shaders/lighting.frag @@ -9,43 +9,56 @@ * */ -varying vec4 col_ambi; -varying vec4 col_diff; -varying vec4 col_spec; -varying vec4 col_emit; -varying float shininess; - varying vec3 normal; -varying vec3 halfvc; + +varying vec3 light0vc; +varying vec3 light1vc; +varying vec3 light2vc; +varying vec3 light2hvc; uniform sampler2D texture; +uniform int fill_light_enabled; + +varying vec3 col_ambi_diff; +varying vec3 col_emit; void main() { - vec4 ambi; - vec4 diff; - vec4 spec; - vec3 light; - vec3 norml; - - /* Ambient contribution */ - ambi = col_ambi * gl_LightModel.ambient; - ambi += col_ambi * gl_LightSource[0].ambient; - ambi = vec4(0.0, 0.0, 0.0, 1.0); - - /* Diffuse contribution */ - light = vec3(normalize(gl_LightSource[0].position)); - norml = normalize(normal); - diff = col_diff * clamp(dot(light, normal), 0.0, 1.0); - - /* Specular contribution */ - spec = col_spec * clamp(pow(dot( vec3(normal), halfvc ), shininess), 0.0, 1.0); - spec = vec4(0.0, 0.0, 0.0, 1.0); - - gl_FragColor = vec4(min(col_emit.r + ambi.r + diff.r + spec.r, 1.0), - min(col_emit.g + ambi.g + diff.g + spec.g, 1.0), - min(col_emit.b + ambi.b + diff.b + spec.b, 1.0), - min(col_emit.a + ambi.a + diff.a + spec.a, 1.0)); + vec3 ambi; + vec3 emit; + vec3 diff = vec3(0.0, 0.0, 0.0); + vec3 spec = vec3(0.0, 0.0, 0.0); + + vec3 norm = normalize(normal); + + /* Ambient */ + ambi = col_ambi_diff * gl_LightModel.ambient.rgb; + + /* Emission */ + emit = col_emit; + + /* Spotlight (light 0) - diffuse only, positional, spotlight */ + float falloff = 1 - length(light0vc) * gl_LightSource[0].linearAttenuation; + float spot = max(dot(normalize(-light0vc), gl_LightSource[0].spotDirection), 0.0); + spot = pow(spot, gl_LightSource[0].spotExponent); + diff += col_ambi_diff * gl_LightSource[0].diffuse.rgb * spot * falloff * max(dot(normalize(light0vc).xyz, norm), 0.0); + + /* Background glow (light 1) - diffuse only, directional */ + diff += col_ambi_diff * gl_LightSource[1].diffuse.rgb * max(dot(vec3(light1vc), norm), 0.0); + + /* Fill-in light (light 2) - this is the only one which has a specular component */ + if ( fill_light_enabled == 1 ) { + /* Diffuse */ + diff += col_ambi_diff * gl_LightSource[2].diffuse.rgb * max(dot(vec3(light1vc), norm), 0.0); + /* Specular */ + float ndothv = max(dot(norm, normalize(light2hvc)), 0.0); + spec = vec3(1.0, 1.0, 1.0) * gl_LightSource[2].specular.rgb * pow(ndothv, 80.0); + } + + gl_FragColor = vec4(min(emit.r + ambi.r + diff.r + spec.r, 1.0), + min(emit.g + ambi.g + diff.g + spec.g, 1.0), + min(emit.b + ambi.b + diff.b + spec.b, 1.0), + 1.0); gl_FragColor *= texture2D(texture, gl_TexCoord[0].st); diff --git a/data/shaders/lighting.vert b/data/shaders/lighting.vert index eaa51c2..1817e9f 100644 --- a/data/shaders/lighting.vert +++ b/data/shaders/lighting.vert @@ -9,27 +9,36 @@ * */ -varying vec4 col_ambi; -varying vec4 col_diff; -varying vec4 col_spec; -varying vec4 col_emit; -varying float shininess; - varying vec3 normal; -varying vec3 halfvc; + +varying vec3 light0vc; +varying vec3 light1vc; +varying vec3 light2vc; +varying vec3 light2hvc; + +uniform int fill_light_enabled; + +varying vec3 col_ambi_diff; +varying vec3 col_emit; void main() { - /* Directions */ - normal = normalize(gl_NormalMatrix * gl_Normal); - halfvc = vec3(gl_LightSource[0].halfVector); + /* Spotlight - positional light */ + vec4 vert = gl_ModelViewMatrix * gl_Vertex; + light0vc = gl_LightSource[0].position.xyz - vert.xyz; + + /* Diffuse "background glow" - this can be normalised only once, here, since 'position' + * is really 'direction' and is the same for all vertices. */ + light1vc = normalize(vec3(gl_LightSource[1].position)); + + /* Fill-in light */ + light2vc = normalize(vec3(gl_LightSource[2].position)); + light2hvc = normalize(gl_LightSource[2].halfVector.xyz); /* Material properties */ - col_ambi = gl_FrontMaterial.ambient; - col_diff = gl_FrontMaterial.diffuse; - col_spec = gl_FrontMaterial.specular; - col_emit = gl_FrontMaterial.emission; - shininess = gl_FrontMaterial.shininess; + normal = gl_NormalMatrix * gl_Normal; + col_ambi_diff = gl_Color.rgb; + col_emit = gl_FrontMaterial.emission.rgb; /* Coordinates */ gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; |