Because we're working with triangles all the information the vertex shader has can be interpolated along the surface of the triangle. A useful way of doing this is by using Barycentric coordinates.
OpenGL does this interpolation for us automatically, for example consider the following vertex shader:
#version 130
attribute vec4 position; // vertex position from VBO
attribute vec2 uv; // texture coordinates from VBO
uniform vec3 camera; // position of the camera
uniform mat4 projection; // camera projection matrix
smooth out vec2 texel; // smoothed texture coordinate for fragment shader
void main()
{
gl_Position = projection * (position - vec4(camera.x, camera.y, camera.z, 0.0));
texel = uv;
}
This is an extremely standard vertex shader. The vertex has a position which is transformed using the camera and projection matrices.
As you can see from the comment we also output a 'smoothed texture coordinate for the fragment shader'. Using the OpenGL keyword smooth
we tell OpenGL to interpolate this variable over the surface of the triangle of which this is one vertex. Conversely there is also the flat
keyword which doesn't do this.
Now in the frament shader we consume this data:
#version 130
uniform sampler2D diffuse; // diffuse texture
smooth in vec2 texel; // where we should sample the texture
out vec4 outputColor;
void main()
{
outputColor = texture(diffuse, texel);
}
Et voila, we have our nicely textured triangle instead of just a triangle made up of the three colours made from whatever sample(texture,uv)
would've been in each vertex.
Edit the following quote is directly take from the file teodron
linked. I thought it would be a good idea to add this to the answer to make it more complete and hopefully answer Jeffrey's question in the comments.
The provoking vertex of a primitive is the vertex that determines the
constant primary and secondary colors when flat shading is enabled.
In OpenGL, the provoking vertex for triangle, quad, line, and
(trivially) point primitives is the last vertex used to assemble the
primitive. The polygon primitive is an exception in OpenGL where the
first vertex of a polygon primitive determines the color of the
polygon, even if actually broken into triangles and/or quads.