Search code examples
openglgraphicsopengl-3

Per Pixel lighting in modern GLSL?


I'm looking for a proper simple example.

Right now I had something based on a tutorial but I don't see variations in the middle of the triangles. It appears each triangle changes color but only in whole.

out vec3 vertex_normal;         
out vec3 vertex_light_position; 

.. on the vertex shader.

with

vertex_normal = normalize(NormalMatrix * in_Normal);

// old gl_NormalMatrix: "transpose of the inverse of the upper
// leftmost 3x3 of gl_ModelViewMatrix"

mat3 NormalMatrix = transpose(
                        inverse(
                            mat3(

                                ModelViewMatrix

                            )
                        )
                    );

and on fragment shader:

float diffuse_value = MAX(dot(vertex_normal, vertex_light_position), 0.0);

gl_FragColor =  out_Color * diffuse_value

But as I said, each triangle appears to be a solid color (that changes only in whole).

I heard that normalization may play a role but if I normalize() vertex_normal it's same result.


Solution

  • Check your values for vertex_normal and vertex_light_position. You can do this by making the fragment shader do:

    gl_FragColor = vertex_normal

    I'm not sure what vertex_light_position is, but it sounds like it should be the normal from the vertex to the light, not the absolute position of the light.

    EDIT: See http://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/lighting.php