'What values are passed to the fragment shaders for inner fragments of a triangle when only having defined values per point

I have the following very basic shaders.

Vertex shader:

attribute vec4 a_foo;

varying vec4 v_foo;

void main()
{
    v_foo = a_foo;
    gl_Position =  a_foo;
}

Fragment shader:

varying vec4 v_foo;

uniform sampler2D u_texture;

void main()
{
    gl_FragColor = texture2D(u_texture, v_foo.xy);
}

The attribute a_foo I provide as a vector for each point. It is passed to the fragment shader as v_foo.

When my mesh consists of only single points (GL_POINTS) it is clear that I can expect v_foo to match a_foo. But what happens in case of having a triangle (GL_TRIANGLES) consisting of three points, when I have much more fragments (texels?) than points?

Will a_foo get interpolated for fragments between the fragments of the points? Does this happen for all types of varying that I can pass between vertex and fragment shader?



Solution 1:[1]

Each primitive is rasterized so every "visible" (not clipped) pixel of your triangle will invoke a fragment shader and pass values interpolated (because you used varying) from the 3 triangle control points based on fragment relative position to the 3 control points.

The interpolation might be linear or perspective corrected linear depends on your OpenGL pipeline settings.

For more info about rasterization of convex polygons see:

However gfx cards use barycentric coordinates and test all pixels inside BBOX if inside polygon or not by simple winding rule test to take advantage of parallelism...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Spektre