I'm developing a simple educational project to study OpenGL ES. I'm try to render some simple OBJ models using Blinn-Phong. The first version of the of my implementation calculate all the lighting equation in this way:

    #version 300 es

struct material {
    vec4 ka;
    vec4 kd;
    vec4 ks;
    float sh;

struct pointLight {
    vec3 position;
    vec4 color;

uniform mat4 mvMatrix;
uniform mat4 mvpMatrix;
uniform mat4 normalMatrix;
uniform pointLight light;
uniform material surfaceMaterial;

layout(location = 0) in vec3 inputPosition;
layout(location = 1) in vec3 inputNormal;

out vec4 v_color;

void main() {

    //Convert vertex and normal to camera space.
    vec3 normal = vec3(normalize(normalMatrix * vec4(inputNormal, 0.0)));
    vec4 vertexPositionHomogeneous = mvMatrix * vec4(inputPosition, 1.0);
    vec3 vertexPosition = vec3(vertexPositionHomogeneous) / vertexPositionHomogeneous.w;

    //Calculate light direction and view direction.
    vec3 lightDirection = normalize(light.position - vertexPosition);
    vec3 viewPosition = vec3(0.0, 0.0, 1.0);
    vec3 viewDirection = normalize(-1.0 *  vertexPosition);

    //Cosine theta diffuse lambertian component.
    float cosTheta = max(0.0, dot(normal, normalize(lightDirection)));

    //H vector for specular component.
    vec3 h = normalize(lightDirection + viewDirection);

    vec4 ambient = surfaceMaterial.ka * light.color;
    vec4 diffuse = surfaceMaterial.kd * light.color * cosTheta;
    vec4 specular = surfaceMaterial.ks * light.color * pow(max(0.0, dot(h, normal)), surfaceMaterial.sh);

    //Output position and color for fragment shader.
    v_color = specular;
    gl_Position = mvpMatrix * vec4(inputPosition, 1.0);

This version doesn't work. This is what I obtain:

enter image description here

If I move the calculation to the fragment shader it works. The is the result:

enter image description here

This image is obtained using a fragment shader that contains the same lighting calculation that I used in the vertex shader above. So my question is: why the vertex shader only version is not working? Am I missing something about how vertex and fragment shaders works?

  • $\begingroup$ I can't test this until I get home so can't fix your main problem right now, but there's a fair bit of unnecessary maths in your shader : $\endgroup$ – russ Jun 6 '16 at 6:15
  • 2
    $\begingroup$ the W divide of vertexPositionHomogenous is pointless as only the projection matrix alters W ; you're normalizing lightDirection twice, once in the declaration and once in the calculation of cosTheta ; and the halfway vector calculation is cheaper as (lightDirection + viewDirection) * 0.5 .... could you post the fragment shader you're using with this vertex shader as well? Because other than that the maths look right. $\endgroup$ – russ Jun 6 '16 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.