The confusion comes from the fact that the shader controls several arrays of vertices immediately when it should be considered as a universal entity. The vertex array is passed to the shader, then the object is drawn. And the process is repeated.
For example, suppose we assign the variable matrixID to uniform MVP:
// get handle for our "MVP" uniform GLuint matrixID = glGetUniformLocation(programID, "MVP");
When we are ready to draw an object, we set matrixID to an MVP object:
glUniformMatrix4fv(matrixID, 1, GL_FALSE, &cubeMVP[0][0]);
Then bind the vertex buffer, set the pointer to the attribute and draw it:
glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, cubeVerteciesBuffer); glVertexAttribPointer( 0, // shader layout location 3, GL_FLOAT, GL_FALSE, 0, (void *)0 ); glDrawArrays(GL_TRIANGLES, 0, 12*3); // draw cube
Now we go to the triangle and repeat the process - set the matrixID to the MVP object, bind the vertex buffer, set the attribute pointer and draw it:
glUniformMatrix4fv(matrixID, 1, GL_FALSE, &triMVP[0][0]); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, triangleVerteciesBuffer); glVertexAttribPointer( 0, // shader layout location 3, GL_FLOAT, GL_FALSE, 0, (void *)0 ); glDrawArrays(GL_TRIANGLES, 0, 3); // draw triangle
Corresponding vertex shader code:
#version 330 core // Input vertex data, different for all executions of this shader. layout(location = 0) in vec3 vertecies_modelspace; uniform mat4 MVP; void main(){ gl_Position = MVP * vec4(vertecies_modelspace, 1); }