Can't see lights working openGL ES 2.0

Tag: opengl-es , opengl-es-2.0 Author: lntlctww Date: 2013-04-09

I have set up my lighting in my code and in shader but cannot see anything apart from the quad I have at the moment. The lighting doesn't work and I can't really see where I have gone wrong.

I have a quad with a texture on it in which the quad is rotated by 90 degrees so its lying flat and in my code I have done my lighting like this...

    // Set light direction in model space
PVRTVec4 vLightDirModel;
vLightDirModel =  modelView.inverse() * PVRTVec4(0.57735f, 0.57735f, 0.57735f, 0);

glUniform3fv(m_ShaderProgram.auiLoc[eLight], 1, &vLightDirModel.x);

// Set eye position in model space
PVRTVec4 vEyePosModel;
vEyePosModel = modelView.inverse() * PVRTVec4(0, 0, 0, 1);

glUniform3fv(m_ShaderProgram.auiLoc[eEyePos], 1, &vEyePosModel.x);

and here are my shaders


attribute highp   vec3  inVertex;
attribute mediump vec2  inTexCoord;

uniform highp   mat4  MVPMatrix;
uniform mediump vec3  LightDir;
uniform mediump vec3  EyePos;

varying mediump vec3  EyeDir;
varying lowp    float  specIntensity;
varying mediump vec2  TexCoord;

const mediump float  cShininess = 10.0;

void main()
    // Transform position
    gl_Position = MVPMatrix * vec4(inVertex,1.0);

    // Calculate direction from eye position in model space
    mediump vec3 EyeDir = normalize(EyePos - inVertex);

        // Specular lighting
    // We ignore that N dot L could be negative (light coming 
    // from behind the surface)
    mediump vec3 halfVector = normalize(LightDir + EyeDir);
    lowp float NdotH = max(dot(inNormal, halfVector), 0.0);     
    specIntensity = pow(NdotH, cShininess);

    TexCoord = inTexCoord;

and here is my frag shader

uniform sampler2D reflectionTex;

varying mediump vec2  TexCoord;
varying lowp    float specIntensity;

//This gets updated within the main code
uniform highp float Time;

void main()
    lowp vec3 refColor = texture2D(reflectionTex, TexCoord).rgb;
    gl_FragColor =  vec4(refColor + specIntensity, 1.0);

Other Answer1

You are mixing up your lighting calculations. In your comments you say you're calculating the specular lighting term. So for your flat surface, the most you would hope to see with your code would be a small white area somewhere on the quad, IF the light was angled just-so. Even so the calculation is wrong as you are not calculating the reflected light vector.

The basic phong lighting model includes two other terms, Diffuse (which varies colour with reference to angle between normal and light vec, i.e. the greater the angle, the darker the colour) and Ambient (which adds uniform light to every fragment to simulate ambient light).

I suggest you read this excellent tutorial and adapt it for ES: