A usual way to pass data from a vertex to a fragment shader is to use “varying” qualifier. However it works only in the case when the data is common for all vertices in a primitive.
Let’s say I am rendering a line and want to paint ends in different colors. So I declare
varying vec4 color1;
varying vec4 color2;
In the vertex shader I would:
if( vertex in the left corner )
color = red
else color = blue;
The idea is that each vertex would write only one value and by the time the processing comes to the fragment shader both vertices are processed and both color1 and color2 are initialized.
However it does not work that way. Even though the vertex shader only writes one of the variables the other variable gets overwritten anyway and usually contains garbage.
So how would I pass data from different vertices to the fragment shader under conditions that each vertex knows nothing about the other one.