Step 2 - Night Lighting
- For night lighting the title of the movie "Rang De Basanti" on the billboard is cycled between two textures
GLSL vertex/fragment code for controlling the cycling of the two textures
- Two texture maps are created one without glow and the other with the title glowing
- They are passed on to the fragment shader and each fragment lookup is stored as a color value.
- A control variable is also passed. It is setup in the application as below:
[Vertex_Shader]
varying vec2 TexCoord0, TexCoord1;
void main()
{
TexCoord0 = gl_MultiTexCoord0.st; // Pass in the two textures to the fragment shader
TexCoord1 = gl_MultiTexCoord1.st; // as varying variables.
gl_Position = ftransform();
}
[Fragment_Shader]
varying vec2 TexCoord0, TexCoord1;
uniform sampler2D bbMap0, bbMap1; // uniforms for textures
uniform float cvar; // uniform control variable
void main()
{
float cv = max(sin(cvar), 0.0); // a '+' sine function of the control variable is stored.
vec3 lightcolor0 = vec3(texture2D(bbMap0, TexCoord0)); //lookup the textures
vec3 lightcolor1 = vec3(texture2D(bbMap1, TexCoord1));
//the color is varied over the control variable for each of the textures
vec3 color = lightcolor0 * cv + lightcolor1 * (1 - cv); // multiply with ctrl variable
gl_FragColor = vec4(color, 1.0);
}
In Application:
glUseProgramObjectARB(ntProg);
if(controlvar>=360)
controlvar=0;
controlvar+=0.05;
glActiveTextureARB(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, billboard_texture);
glActiveTextureARB(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, billboardmod_texture);
glUniform1iARB(getUniLoc(ntProg, "bbMap0"), 0);
glUniform1iARB(getUniLoc(ntProg, "bbMap1"), 1);
glUniform1fARB(getUniLoc(ntProg, "cvar"), controlvar);
glUniform3fARB(getUniLoc(ntProg, "lightpos"), light1pos[0], light1pos[1], light1pos[2]);
- The quad is multitextured with the two billboard textures.
- A sine function is used to govern the rate at which the sign glows.
Step 3 - Lighting calculation for a point light
This step just calculates light for the point light that is setup in the program for night lighting. The only difference from the previous lighting calculation is that, the normal, light and eye directions are passed as varying variables to the fragment shader which then calculates the view/reflect vectors and diffuse/specular values.
[Vertex_Shader]
varying vec2 TexCoord0, TexCoord1;
varying vec3 eyeVec, normal, lightVec;
uniform vec3 lightpos;
void main()
{
/*eye position, normal, light vector, reflection vector and view vectors are
calculated for finding the specularity which is the dot product of the
reflection vector and the view vector clamped between 0 and 1 and raised
to the power of 16. */
eyeVec = normalize(vec3(gl_ModelViewMatrix * gl_Vertex));
normal = normalize(gl_NormalMatrix * gl_Normal);
lightVec = normalize(lightpos - eyeVec);
TexCoord0 = gl_MultiTexCoord0.st;
TexCoord1 = gl_MultiTexCoord1.st;
gl_Position = ftransform();
}