I am trying to find any tutorial or sample code which explains and shows the usage of lights in OpenGL ES 2.0. Most of the material I have seen online refers to Opengl ES 1.1.
Can anyone provide any links or documentation on how to do lighting in OpenGL ES 2.0?
The difficulty with OpenGL ES 2.0 is that since you can write your own shaders pretty much how you want, there's no one set way to provide lighting to an object. It will depend on how you want to present your content.
That said, you can replicate OpenGL ES 1.1's fixed function lighting capabilties by doing something like providing a light direction and calculating the light intensity at each vertex. You can see an example of this in this sample application I slapped together for one of my classes.
The vertex shader used here is the following:
attribute vec4 position;
attribute vec4 normal;
varying float lightIntensity;
uniform mat4 modelViewProjMatrix;
uniform vec3 lightDirection;
void main()
{
vec4 newNormal = modelViewProjMatrix * normal;
vec4 newPosition = modelViewProjMatrix * position;
gl_Position = newPosition;
lightIntensity = max(0.0, dot(newNormal.xyz, lightDirection));
}
with the matching fragment shader:
varying highp float lightIntensity;
void main()
{
lowp vec4 yellow = vec4(1.0, 1.0, 0.0, 1.0);
gl_FragColor = vec4((yellow * lightIntensity * 0.2).rgb, 1.0);
}
The dot product of the light direction and the normal at that vertex is used to calculate the light intensity hitting the eye of the viewer from that vertex. This light intensity is then interpolated between the vertices in each triangle to give you the intensity at each fragment.
I do some more advanced per-pixel lighting in my open source Molecules application, where each sphere and cylinder is really a raytraced impostor residing within a rectangle. For each pixel, I calculate the specular and ambient lighting contribution at that point on the virtual object, and then combine that with an ambient occlusion factor calculated at the loading of the model. That's far on the other extreme of what you can do with lighting, but you can dig into the code there or read my little writeup on how this works.