From a4fa46a4fda967348ea18961c177330491bdb953 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 15 Dec 2009 02:25:40 +0100 Subject: Added nice lighting shaders --- shader/default.frag | 44 ++++++++++++++++++++++++++++++++++++++++++++ shader/default.vert | 25 +++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 shader/default.frag create mode 100644 shader/default.vert (limited to 'shader') diff --git a/shader/default.frag b/shader/default.frag new file mode 100644 index 0000000..b39b46d --- /dev/null +++ b/shader/default.frag @@ -0,0 +1,44 @@ +uniform sampler2D tex; + +varying vec4 diffuse, ambientGlobal, ambient; +varying vec3 normal, pos; +/*varying vec3 normal, lightVector, reflectVector, eyeVector;*/ + + +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 new file mode 100644 index 0000000..133c871 --- /dev/null +++ b/shader/default.vert @@ -0,0 +1,25 @@ +varying vec4 diffuse, ambientGlobal, ambient; +varying vec3 normal, pos; +/*varying vec3 normal, lightVector, reflectVector, eyeVector;*/ + + +void main() { + normal = normalize(gl_NormalMatrix * gl_Normal); + + pos = vec3(gl_ModelViewMatrix * gl_Vertex); + /*lightVector = vec3(gl_LightSource[0].position - ecPos); + + reflectVector = normalize(reflect(-lightVector, normal)); + eyeVector = vec3(-ecPos);*/ + + /* Compute the diffuse, ambient and globalAmbient terms */ + diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse; + + /* The ambient terms have been separated since one of them */ + /* suffers attenuation */ + ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient; + ambientGlobal = gl_LightModel.ambient * gl_FrontMaterial.ambient; + + gl_TexCoord[0] = gl_MultiTexCoord0; + gl_Position = ftransform(); +} -- cgit v1.2.3