views:

68

answers:

1

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.

+1  A: 

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

Justicle
light position is based on old gl_LightSource[0].position.xyz. I was told that was a static light position. [it was used that way on a tutorial I based it on]
Lela Dax
Ok, so if both `vertex_normal` and `vertex_light_position` are constant per pixel, then `diffuse_value` will be constant per pixel and you have flat shading.
Justicle
Makes sense. So what is changing? Here's the simple tutorial http://www.swiftless.com/tutorials/glsl/5_lighting_perpixel.html
Lela Dax
That tutorial is wrong. Line 8 of the VS is wrong, which makes line 7 of the FS wrong. See the tutorial added to my answer. What's missing is you need to calculate L per-pixel, which is the normal from the light position to the interpolated vertex position.
Justicle
You are star. That did it.
Lela Dax