To create a colored square, we need to make some changes to
vertexArray, as we will need to pass six vertices instead of three. We have to pass six vertices because, as you saw earlier, we can draw only in triangles. So, we need three points for the triangle to form the top part of the square and three more points to form its bottom part:
let vertexArray:[Float] = [ -1.0, 1.0, 0, 1, //a -1.0, -1.0, 0, 1, //b 1.0, -1.0, 0, 1, //c -1.0, 1.0, 0, 1, //a 1.0, -1.0, 0, 1, //c 1.0, 1.0, 0, 1, //d ]
You will see that the
c points are repeated to form the second triangle, because the diagonal of the square are the same points for the first triangle.
Notice that in the vertex array, we are now passing four values per vertex instead ...