1

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?

Sebastian Barth
  • 4,079
  • 7
  • 40
  • 59

1 Answers1

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...

Spektre
  • 49,595
  • 11
  • 110
  • 380