I am following the opengl-tutorial.org series and the tutorial 3 draws a triangle on the screen.The code uses a vertex shader to make the vertex transformations by feeding it a ModelViewProjection (MVP) matrix.
I can make the triangle rotate around it's origin on every loop. What I can't come around is when I add a second triangle, how can I make only one of them rotate while the other one remains static. This whole vertex transformations in the shader working together with the main loop is confusing me.
Here's part of the code directly from the tutorial:
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
static const GLushort g_element_buffer_data[] = { 0, 1, 2 };
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
glm::mat4 View = glm::lookAt(
glm::vec3(0,0,3),
glm::vec3(0,0,0),
glm::vec3(0,1,0));
glm::mat4 Model = glm::mat4(1.0f);
glm::mat4 MVP = Projection * View * Model;
do{
// Clear the screen
glClear( GL_COLOR_BUFFER_BIT );
// Use our shader
glUseProgram(programID);
// Send our transformation to the currently bound shader,
// in the "MVP" uniform
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0,
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3); // 3 indices starting at 0 -> 1 triangle
glDisableVertexAttribArray(0);
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
}
To rotate, I'm using this:
glm::mat4 Model = glm::mat4(1.0f);
rot=rot+0.01f;
glm::vec3 myRotationAxis( 0, 0, 1);
Model = glm::rotate( Model, rot, myRotationAxis );
glm::mat4 MVP = Projection * View * Model;
I'm drawing a second triangle by modifying the vertex buffer on top:
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
1.5f, 0.0f, -5.0f,
2.5f, 0.0f, -5.0f,
2.0f, 1.0f, -5.0f
};
And then draw my 2 triangles:
glDrawArrays(GL_TRIANGLES, 0, 6); // 3 indices starting at 0 -> 1 triangle
The vertex shader is clearly transforming all 6 vertices. How would I make this program so that only the second triangle rotates?