In a simple hello-world OpenGL program, which simply draws a static triangle on the window, when I set the 3 vertex of the triangle to red, green and blue color, the triangle is filled with gradient.
But when I use shaders like this:
Vertex Shader:
attribute vec4 aVertex;
attribute vec4 aColor;
varying vec4 vColor;
void main(void) {
gl_Position = gl_ModelViewMatrix * gl_ProjectionMatrix * aVertex;
vColor = aColor;
}
where the attributes aVertex
and aColor
comes from a vertex buffer, passed through a call of glVertexAttribPointer
.
Fragment Shader:
varying vec4 vColor;
void main(void) {
gl_FragColor = vColor;
}
The triangle is still filled with gradient, and here comes the question:
If vertex-shader is calculated per vertex, then each instance of vColor
should be assigned with the color of a vertex. And the vertex color should be either red, green, or blue, as set in the vertex buffer.
So where did the gradient come from?
Or, in another word, when did it happen that in the frag-shader, the vColor
turns out to be the interpolated color instead of the vertex's?