void main() {
if (gl_VertexID == 0) {
gl_Position = vec4(1.0, 0.0, 0.0, 1.0);
}
else if (gl_VertexID == 1) {
gl_Position = vec4(-0.5, 0.866, 0.0, 1.0);
}
else { // gl_VertexID == 2
gl_Position = vec4(-0.5, -0.866, 0.0, 1.0);
}
}
Our first vertex shader isn’t particularly elegant, but it certainly gets the job done. Recall that we requested that three vertices were drawn. Even though we didn’t have the application specify the vertex positions (actually, they default to (0, 0, 0, 1) when you don’t specify any data), we can update our positions in the vertex shader explicitly.
Further, since each vertex had its own ID, we can specify the vertex position we want for that ID, and here we do it very simply. Each execution of the vertex shader (of which there will be three, since we specified that number using the count parameter for the gl.drawArrays()
call).
Note that we assign the vertex’s position to a special variable named gl_Position
. This specifies where in the window (well, viewport specifically, but we’ll get to the details in a bit) that vertex’s position maps too.