Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to implement Phong-Shading using GLSL. I also want to calulate all values using uniforms in the shaders.

Nearly everything works fine, but there is an error with the specular term of the equation.

Here is my vertex shader:

#version 330
layout(location = 0) in vec3 vertex;
layout(location = 1) in vec3 vertex_normal;

uniform vec3 light_ambient_color;
uniform vec3 light_diffuse_color;
uniform vec3 light_specular_color;
uniform vec3 light_position;

uniform vec3 material_ambient_color;
uniform vec3 material_diffuse_color;
uniform vec3 material_specular_color;
uniform float material_specular_shininess;

out vec3 vNormal;
out vec3 camVec;
out vec3 lightVec;

out vec3 diffuse;
out vec3 ambient;
out vec3 specular;
out float shininess; 

uniform mat4 view;
uniform mat4 modelview;
uniform mat4 projection;

void main() {

    diffuse = material_diffuse_color.rgb * light_diffuse_color.rgb;
    ambient = material_ambient_color.rgb * light_ambient_color.rgb;
    specular = material_specular_color.rgb * light_specular_color.rgb;
    shininess = material_specular_shininess;

    mat4 normalMat = transpose(inverse(modelview));

    vNormal = vec3(normalMat * vec4(vertex_normal,0.0));
    vec4 pos = projection  * modelview  * vec4(vertex,1.0);

    camVec =; 
    lightVec = (1) *( -;

    gl_Position = pos; 

And this is my fragment shader:

#version 330

in vec3 vNormal;
in vec3 camVec;
in vec3 lightVec;

in vec3 diffuse;
in vec3 ambient;
in vec3 specular;
in float shininess;

// this defines the fragment output 
out vec4 color;

void main() {

  vec3 normal = normalize(vNormal);
  vec3 viewDir = normalize(camVec);
  vec3 lightDir = normalize(lightVec);

  vec3 myDiffuse = normalize(diffuse);
  vec3 myAmbient = normalize(ambient);
  vec3 mySpecular = normalize(specular);

    vec3 finalColor = ambient;
    float lambert = dot(normal,lightDir);
    vec3 specColor; 

    if(lambert < 0.0){
        specColor = vec3(0.0,0.0,0.0);
        finalColor += myDiffuse * lambert; 

        vec3 h = (viewDir + lightDir ) / length(viewDir + lightDir);
        float beta = dot(h, normal);

        finalColor += mySpecular * pow(beta, shininess);

    color = vec4(finalColor,0);

All uniforms are set correctly. Here is what it looks like without the specular term: Armadillio without specular term

And this happens when I add the specular term (which is not working): Armadillo with specular term

Edit: Another screenshot with changing to in the vertex-shader Changend direction from vertex to camera

share|improve this question
What's shininess set to? It kind of looks like it might be coming out zero or near-zero. If you hard-code a particular shininess value, say 32, what result do you get? – Nathan Reed May 11 '13 at 21:01
Also, you've set camVec = which sounds like it's the vector from the camera to the shaded point (if the camera is at the origin). It should be the negative of that - the vector from the shaded point to the camera, just as lightVec is the vector from the shaded point to the light source. – Nathan Reed May 11 '13 at 21:06
shininess is hardcoded in OpenGL with a value of 15 and the results shown above are with this value. – glethien May 11 '13 at 21:15
I've added another screenshot with the changes you've recommended. Now it looks much more better, but still not what I've expected... – glethien May 11 '13 at 21:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.