On pixel lighting in modern GLSL?

I am looking for a suitable simple example.

Now I had something based on a textbook, but I do not see variations in the middle of the triangles. Each triangle seems to change color, but only in general.

out vec3 vertex_normal;         
out vec3 vertex_light_position; 

.. in 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 the 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 seems to be a solid color (which only changes as a whole).

I heard that normalization can play a role, but if I normalize () vertex_normal, then the same result.

+3
source share
1 answer

Check the values vertex_normaland vertex_light_position. You can do this by making a fragment shader:

gl_FragColor = vertex_normal

, vertex_light_position, , , .

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

+4

Source: https://habr.com/ru/post/1765374/


All Articles