From be186c70322fc3e00b9e79c5a0c0f504fb7f3f17 Mon Sep 17 00:00:00 2001 From: taw27 Date: Wed, 6 Aug 2008 11:57:50 +0000 Subject: Simplify shaders by moving fill-in light to a separate program git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@218 84d2e878-0bd5-11dd-ad15-13eda11d74c5 --- data/Makefile.am | 3 +- data/shaders/fill-light.frag | 84 ++++++++++++++++++++++++++++++++++++++++++++ data/shaders/fill-light.vert | 40 +++++++++++++++++++++ data/shaders/lighting.frag | 71 +++++++++++++------------------------ 4 files changed, 151 insertions(+), 47 deletions(-) create mode 100644 data/shaders/fill-light.frag create mode 100644 data/shaders/fill-light.vert (limited to 'data') diff --git a/data/Makefile.am b/data/Makefile.am index f4ec188..b818d11 100644 --- a/data/Makefile.am +++ b/data/Makefile.am @@ -4,7 +4,8 @@ models_DATA = models/floor models/lander models/randombox models/walle models/wa models/pipe-horiz shadersdir = $(datadir)/thrust3d/shaders -shaders_DATA = shaders/lighting.vert shaders/lighting.frag shaders/swirlytron.vert shaders/swirlytron.frag +shaders_DATA = shaders/lighting.vert shaders/lighting.frag shaders/swirlytron.vert shaders/swirlytron.frag \ + shaders/fill-light.vert shaders/fill-light.frag texturesdir = $(datadir)/thrust3d/textures textures_DATA = textures/floor1.png textures/tiledwall.png textures/radioactive.png textures/fuel.png \ diff --git a/data/shaders/fill-light.frag b/data/shaders/fill-light.frag new file mode 100644 index 0000000..b9aba4c --- /dev/null +++ b/data/shaders/fill-light.frag @@ -0,0 +1,84 @@ +/* + * lighting.frag + * + * Lighting calculations + * + * (c) 2007-2008 Thomas White + * + * thrust3d - a silly game + * + */ + +varying vec3 pos; +varying vec3 normal; + +varying vec3 light2vc; + +uniform sampler2D texture; +uniform sampler2D normalmap; + +uniform bool has_normals; +uniform bool texture_enabled; +uniform bool texture_emits; +uniform float alpha; + +varying vec3 col_ambi_diff; +varying vec3 col_emit; + +void main() { + + vec3 ambi; + vec3 emit; + vec3 diff = vec3(0.0, 0.0, 0.0); + vec3 spec = vec3(0.0, 0.0, 0.0); + vec3 norm = normal; + float diff_fac, spec_fac; + vec3 L, E, R; + + if ( has_normals ) { + norm += (texture2D(normalmap, gl_TexCoord[0].st).rgb - vec3(0.5, 0.5, 0.5)) / 2.0; + } + + norm = normalize(norm); + + /* Ambient */ + ambi = col_ambi_diff * gl_LightModel.ambient.rgb; + + /* Emission */ + emit = col_emit; + + /* Light 2: Fill-in for lander craft */ + L = normalize(gl_LightSource[2].position.xyz - pos); + E = normalize(-pos); + R = normalize(-reflect(L, norm)); + diff_fac = max(0.0, dot(normalize(light2vc).xyz, norm)); + spec_fac = max(0.0, dot(R, E)); + spec_fac = pow(spec_fac, 80.0); + + diff += col_ambi_diff * gl_LightSource[2].diffuse.rgb * diff_fac; + spec += gl_LightSource[2].specular.rgb * spec_fac; + + if ( texture_enabled ) { + + vec3 tex = texture2D(texture, gl_TexCoord[0].st).rgb; + + if ( texture_emits ) { + gl_FragColor = vec4(tex.r, tex.g, tex.b, alpha); + } else { + gl_FragColor = vec4(min(tex.r * (ambi.r + diff.r) + spec.r, 1.0), + min(tex.g * (ambi.g + diff.g) + spec.g, 1.0), + min(tex.b * (ambi.b + diff.b) + spec.b, 1.0), + alpha); + } + + } else { + + 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), + alpha); + + } + +} + diff --git a/data/shaders/fill-light.vert b/data/shaders/fill-light.vert new file mode 100644 index 0000000..38dcfc9 --- /dev/null +++ b/data/shaders/fill-light.vert @@ -0,0 +1,40 @@ +/* + * fill-light.vert + * + * Lighting calculations for the lander craft + * + * (c) 2007-2008 Thomas White + * + * thrust3d - a silly game + * + */ + +varying vec3 pos; +varying vec3 normal; + +varying vec3 light2vc; + +varying vec3 col_ambi_diff; +varying vec3 col_emit; + +void main() { + + vec4 vert; + + vert = gl_ModelViewMatrix * gl_Vertex; + pos = vert.xyz; + normal = gl_NormalMatrix * gl_Normal; + + /* Light 2: Fill-in light for lander */ + light2vc = normalize(vec3(gl_LightSource[2].position)); + + /* Material properties */ + col_ambi_diff = gl_Color.rgb; + col_emit = gl_FrontMaterial.emission.rgb; + + /* Coordinates */ + gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; + gl_Position = ftransform(); + +} + diff --git a/data/shaders/lighting.frag b/data/shaders/lighting.frag index 7ec0d3e..39f39fc 100644 --- a/data/shaders/lighting.frag +++ b/data/shaders/lighting.frag @@ -37,6 +37,9 @@ void main() { vec3 diff = vec3(0.0, 0.0, 0.0); vec3 spec = vec3(0.0, 0.0, 0.0); vec3 norm = normal; + float falloff, spot; + float diff_fac, spec_fac; + vec3 L, E, R; if ( has_normals ) { norm += (texture2D(normalmap, gl_TexCoord[0].st).rgb - vec3(0.5, 0.5, 0.5)) / 2.0; @@ -49,53 +52,29 @@ void main() { /* Emission */ emit = col_emit; - - if ( fill_light_enabled ) { - - float diff_fac, spec_fac; - vec3 L, E, R; - - /* Light 2: Fill-in for lander craft */ - L = normalize(gl_LightSource[2].position.xyz - pos); - E = normalize(-pos); - R = normalize(-reflect(L, norm)); - diff_fac = max(0.0, dot(normalize(light2vc).xyz, norm)); - spec_fac = max(0.0, dot(R, E)); - spec_fac = pow(spec_fac, 80.0); - - diff += col_ambi_diff * gl_LightSource[2].diffuse.rgb * diff_fac; - spec += gl_LightSource[2].specular.rgb * spec_fac; - - } else { - float falloff, spot; - float diff_fac, spec_fac; - vec3 L, E, R; - - /* Light 0: Lander craft's spotlight */ - falloff = 1.0 / ( gl_LightSource[0].constantAttenuation - + gl_LightSource[0].linearAttenuation * length(light0vc) - + gl_LightSource[0].quadraticAttenuation * pow(length(light0vc), 2.0) ); - - spot = max(dot(normalize(-light0vc), gl_LightSource[0].spotDirection), 0.0); - spot = pow(spot, gl_LightSource[0].spotExponent); - - diff_fac = max(0.0, dot(normalize(light0vc).xyz, norm)); - - L = normalize(gl_LightSource[0].position.xyz - pos); - E = normalize(-pos); - R = normalize(-reflect(L, norm)); - spec_fac = max(0.0, dot(R, E)); - spec_fac = pow(spec_fac, 80.0); - - diff += col_ambi_diff * gl_LightSource[0].diffuse.rgb * spot * falloff * diff_fac; - - spec += vec3(1.0, 1.0, 1.0) * gl_LightSource[0].specular.rgb * spot * falloff * spec_fac; - - /* Light 1: Diffuse background glow */ - diff += col_ambi_diff * gl_LightSource[1].diffuse.rgb * max(0.0, dot(vec3(light1vc), norm)); - - } + /* Light 0: Lander craft's spotlight */ + falloff = 1.0 / ( gl_LightSource[0].constantAttenuation + + gl_LightSource[0].linearAttenuation * length(light0vc) + + gl_LightSource[0].quadraticAttenuation * pow(length(light0vc), 2.0) ); + + spot = max(dot(normalize(-light0vc), gl_LightSource[0].spotDirection), 0.0); + spot = pow(spot, gl_LightSource[0].spotExponent); + + diff_fac = max(0.0, dot(normalize(light0vc).xyz, norm)); + + L = normalize(gl_LightSource[0].position.xyz - pos); + E = normalize(-pos); + R = normalize(-reflect(L, norm)); + spec_fac = max(0.0, dot(R, E)); + spec_fac = pow(spec_fac, 80.0); + + diff += col_ambi_diff * gl_LightSource[0].diffuse.rgb * spot * falloff * diff_fac; + + spec += vec3(1.0, 1.0, 1.0) * gl_LightSource[0].specular.rgb * spot * falloff * spec_fac; + + /* Light 1: Diffuse background glow */ + diff += col_ambi_diff * gl_LightSource[1].diffuse.rgb * max(0.0, dot(vec3(light1vc), norm)); if ( texture_enabled ) { -- cgit v1.2.3