The approach suggested in @ Pondwater's answer is well founded and reasonable. Since alternatives are always valuable, this is a slightly different approach.
The idea here is that you have two shapes with two different colors. To indicate which vertex one of the two colors uses, you enter an additional vertex attribute. This additional attribute is a single float, where a value of 0.0 means you want to use the first color, and 1.0 for which you want to use the second color.
For your example of a triangle, let's say you want to color the 1st and 3rd vertices blue, and the second red. Extend the vertex data as follows:
vertices = [0.0, 0.5, 0.0, 0.5, -0.5, 1.0, -0.5, -0.5, 0.0]
Then you set the second vertex attribute (named colorWeight in the code below), following the same pattern that you used for position . You will have a second set of glEnableVertexAttribArray() , glVertexAttribPointer() , etc. For this new attribute.
In your vertex shader, you add a new colorWeight attribute and pass it to the fragment shader:
in vec2 position; in float colorWeight; out float fragColorWeight; void main() { gl_Position = vec4(position, 0.0, 1.0); fragColorWeight = colorWeight; }
Then in the fragment shader you now have two identical colors and mix them based on the relative weight:
uniform vec3 triangleColor; uniform vec3 secondaryColor; in float fragColorWeight; out vec4 outColor; void main() { vec3 mixedColor = mix(triangleColor, secondaryColor, fragColorWeight); outColor = vec4(mixedColor, 1.0); }
Now you can get the location of the single secondaryColor variable and set it independently of triangleColor to change the color of only the second vertex of the triangle.