From 4e3efa239c9bcb5dd26b4ad9a4d43d4d6a266e6f Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 18 Dec 2009 13:31:07 +0100 Subject: Use multi-pass rendering with seperate ambient and light shaders --- shader/ambient.frag | 8 ++++++++ shader/ambient.vert | 8 ++++++++ shader/default.frag | 43 ------------------------------------------- shader/default.vert | 17 ----------------- shader/light.frag | 41 +++++++++++++++++++++++++++++++++++++++++ shader/light.vert | 16 ++++++++++++++++ 6 files changed, 73 insertions(+), 60 deletions(-) create mode 100644 shader/ambient.frag create mode 100644 shader/ambient.vert delete mode 100644 shader/default.frag delete mode 100644 shader/default.vert create mode 100644 shader/light.frag create mode 100644 shader/light.vert (limited to 'shader') diff --git a/shader/ambient.frag b/shader/ambient.frag new file mode 100644 index 0000000..cef0a3b --- /dev/null +++ b/shader/ambient.frag @@ -0,0 +1,8 @@ +uniform sampler2D tex; + +varying vec4 ambient; + + +void main() { + gl_FragColor = ambient * texture2D(tex, gl_TexCoord[0].st); +} diff --git a/shader/ambient.vert b/shader/ambient.vert new file mode 100644 index 0000000..41a36e7 --- /dev/null +++ b/shader/ambient.vert @@ -0,0 +1,8 @@ +varying vec4 ambient; + +void main() { + ambient = gl_LightModel.ambient * gl_FrontMaterial.ambient; + + gl_TexCoord[0] = gl_MultiTexCoord0; + gl_Position = ftransform(); +} diff --git a/shader/default.frag b/shader/default.frag deleted file mode 100644 index e2acad0..0000000 --- a/shader/default.frag +++ /dev/null @@ -1,43 +0,0 @@ -uniform sampler2D tex; - -varying vec4 diffuse, ambientGlobal, ambient; -varying vec3 normal, pos; - - -void main() { - vec3 n, l, refl, eye; - float NdotL, RdotE; - vec4 color, specularColor; - float dist, att, specularFactor; - - color = ambientGlobal; - - n = normalize(normal); - - l = gl_LightSource[0].position.xyz - pos; - - dist = length(l); - l /= dist; - - /* compute the dot product between normal and normalized lightdir */ - NdotL = max(dot(n, l), 0.0); - - 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); - - refl = normalize(reflect(-l, n)); - eye = normalize(-pos); - - RdotE = max(dot(refl, eye), 0.0); - specularFactor = att * pow(RdotE, gl_FrontMaterial.shininess); - specularColor = specularFactor * gl_FrontMaterial.specular * gl_LightSource[0].specular; - } - else { - specularColor = vec4(0, 0, 0, 1); - } - - gl_FragColor = color * texture2D(tex, gl_TexCoord[0].st) + specularColor; -} diff --git a/shader/default.vert b/shader/default.vert deleted file mode 100644 index 00b8118..0000000 --- a/shader/default.vert +++ /dev/null @@ -1,17 +0,0 @@ -varying vec4 diffuse, ambientGlobal, ambient; -varying vec3 normal, pos; - - -void main() { - normal = normalize(gl_NormalMatrix * gl_Normal); - - pos = vec3(gl_ModelViewMatrix * gl_Vertex); - - diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse; - - ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient; - ambientGlobal = gl_LightModel.ambient * gl_FrontMaterial.ambient; - - gl_TexCoord[0] = gl_MultiTexCoord0; - gl_Position = ftransform(); -} diff --git a/shader/light.frag b/shader/light.frag new file mode 100644 index 0000000..c3cdbfb --- /dev/null +++ b/shader/light.frag @@ -0,0 +1,41 @@ +uniform sampler2D tex; + +varying vec4 diffuse, ambient; +varying vec3 normal, pos; + + +void main() { + vec3 n, l, refl, eye; + float NdotL, RdotE; + vec4 color, specularColor; + float dist, att, specularFactor; + + n = normalize(normal); + + l = gl_LightSource[0].position.xyz - pos; + + dist = length(l); + l /= dist; + + /* compute the dot product between normal and normalized lightdir */ + NdotL = max(dot(n, l), 0.0); + + 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); + + refl = normalize(reflect(-l, n)); + eye = normalize(-pos); + + RdotE = max(dot(refl, eye), 0.0); + specularFactor = att * pow(RdotE, gl_FrontMaterial.shininess); + specularColor = specularFactor * gl_FrontMaterial.specular * gl_LightSource[0].specular; + + gl_FragColor = color * texture2D(tex, gl_TexCoord[0].st) + specularColor; + } + else { + gl_FragColor = vec4(0, 0, 0, 1); + } +} diff --git a/shader/light.vert b/shader/light.vert new file mode 100644 index 0000000..b97da78 --- /dev/null +++ b/shader/light.vert @@ -0,0 +1,16 @@ +varying vec4 diffuse, ambient; +varying vec3 normal, pos; + + +void main() { + normal = normalize(gl_NormalMatrix * gl_Normal); + + pos = vec3(gl_ModelViewMatrix * gl_Vertex); + + diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse; + + ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient; + + gl_TexCoord[0] = gl_MultiTexCoord0; + gl_Position = ftransform(); +} -- cgit v1.2.3