| Bugs |
GLSL Tutorial |
||||||||||||||||||||||||
| GLSL Tutorial
Index OpenGLTutorials @ Lighthouse3d.com
Led Shader
|
|
||||||||||||||||||||||||
![]() |
![]() |
![]() |
| Texture Unit 0 | Texture Unit 1 | Textured Cube |
And now for something a little different: a glow in the dark effect. We want the second texture to glow in the dark, i.e. it will be fully bright when the light doesn't hit, and it will be dimmed as it gets more light.
![]() |
![]() |
| Additive Multi-Texture | Glowing Multi-Texture |
If intensity is zero then we want the second texture in its full strength. When the intensity is 1 we only want a 10% contribution of the second texture unit. For all the other values of intensity we want to interpolate. We can achieve this with the smoothstep function. This function has the following signature:
genType smoothStep(genType edge0, genType edge1, genType x);The result will be zero if x <= edge0, 1 if x >= edge1 and performs smooth Hermite interpolation between 0 and 1 when edge0 < x < edge1. In our case we want to call the function as follows:
coef = smoothStep(1.0, 0.2, intensity);The following fragment shader does the trick:
varying vec3 lightDir,normal;
uniform sampler2D tex,l3d;
void main()
{
vec3 ct,cf,c;
vec4 texel;
float intensity,at,af,a;
intensity = max(dot(lightDir,normalize(normal)),0.0);
cf = intensity * (gl_FrontMaterial.diffuse).rgb +
gl_FrontMaterial.ambient.rgb;
af = gl_FrontMaterial.diffuse.a;
texel = texture2D(tex,gl_TexCoord[0].st);
ct = texel.rgb;
at = texel.a;
c = cf * ct;
a = af * at;
float coef = smoothstep(1.0,0.2,intensity);
c += coef * vec3(texture2D(l3d,gl_TexCoord[0].st));
gl_FragColor = vec4(c, a);
}
A Shader Designer project is available in here.
| [Previous: Combine Texture + Fragment] | [Next: The gl_NormalMatrix] |