Can you explain these shaders?

I’m currently trying to learn how to add lights in LWJGL through shaders. Google search bought me these two but I don’t understand them. Can you explain them to me please?

[icode]shader.vert[/icode]


varying vec3 normal;
varying vec3 vertex;

void main(void)  
{     
   vertex = vec3(gl_ModelViewMatrix * gl_Vertex);
   normal = normalize(gl_NormalMatrix * gl_Normal);
   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;  
}

[icode]shader.frag[/icode]


varying vec3 normal;
varying vec3 vertex;

void main (void)  
{  
   vec3 light = normalize(gl_LightSource[0].position.xyz - vertex);   
   vec3 eyeCoords = normalize(-vertex);
   vec3 reflection = normalize(-reflect(light, normal));  
 
   //calculate Ambient Term:  
   vec4 ambientTerm = gl_FrontLightProduct[0].ambient;    

   //calculate Diffuse Term:  
   vec4 diffuseTerm = gl_FrontLightProduct[0].diffuse * max(dot(normal, light), 0.0);
   diffuseTerm = clamp(diffuseTerm, 0.0, 1.0);     
   
   // calculate Specular Term:
   vec4 specularTerm = gl_FrontLightProduct[0].specular * pow(max(dot(reflection, eyeCoords),0.0), 0.3 * gl_FrontMaterial.shininess);
   specularTerm = clamp(specularTerm, 0.0, 1.0); 
   
   // write Total Color:  
   gl_FragColor = gl_FrontLightModelProduct.sceneColor + ambientTerm + diffuseTerm + specularTerm;     
}

The main problem in them is the terms and how they works.

[icode]light[/icode] is the direction of the light source from the vertex.
[icode]eyeCoords[/icode] is the direction of the camera from the vertex.
[icode]reflection[/icode] is the direction that the light would be reflected by the face.


   //calculate Ambient Term:  
   vec4 ambientTerm = gl_FrontLightProduct[0].ambient;  

The ambient term is always the same.


   //calculate Diffuse Term:  
   vec4 diffuseTerm = gl_FrontLightProduct[0].diffuse * max(dot(normal, light), 0.0);
   diffuseTerm = clamp(diffuseTerm, 0.0, 1.0);

The diffuse term depends on how aligned the face is with the light source; if the face’s normal vector is in the same direction as the light source, it is fully lit. If it is perpendicular then is is not lit at all. The dot product essentially gives a measure of how aligned [icode]normal[/icode] and [icode]light[/icode] are.


   // calculate Specular Term:
   vec4 specularTerm = gl_FrontLightProduct[0].specular * pow(max(dot(reflection, eyeCoords),0.0), 0.3 * gl_FrontMaterial.shininess);
   specularTerm = clamp(specularTerm, 0.0, 1.0); 

The specular term depends on how aligned the camera is with the reflected light. The calculation is similar to above except the effect is transformed with a power to adjust the size of the spot you see, so that a matt sphere would have a larger bright spot than a shiny one.

Hopefully that makes it a bit clearer, check out this wikipedia page too:

It’s a standard lambertian illumination model. More info here:

I also list some 3D-specific links toward the bottom.

If you’re working with shaders I’d suggest using custom attributes instead of relying on gl_Normal, gl_Vertex, gl_Light and so forth. These are deprecated in the programmable pipeline.

Doing lighting correct is quite a difficult undertaking. The old way of just adding some phong specular to the dot product of the normal and light vector has proven to be not really correct and difficult for artists to control.
Already some years now something called physical correct lighting is the standard, but no so easy to set up. There are quite a lot of papers and presentations out there for the ones interested.

Here is my code I use which works quite well for me: github gist

Here some really good blog posts explain everything:


If you’re looking for an introduction to shaders, then I recommend Lighthouse3D: http://www.lighthouse3d.com/ It’s under tutorials and then either GLSL or GLSL core.