我正在关注 opengl-tutorial.org 系列,教程 3 在屏幕上绘制了一个三角形。该代码使用顶点着色器通过为其提供 ModelViewProjection (MVP) 矩阵来进行顶点变换。
我可以让三角形在每个循环中围绕它的原点旋转。我无法解决的问题是,当我添加第二个三角形时,我怎样才能让其中一个三角形旋转而另一个三角形保持静止。着色器中的整个顶点变换与主循环一起工作让我感到困惑。
下面是教程中的部分代码:
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();
}
要旋转,我使用这个:
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;
我正在通过修改顶部的顶点缓冲区来绘制第二个三角形:
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
};
然后画出我的 2 个三角形:
glDrawArrays(GL_TRIANGLES, 0, 6); // 3 indices starting at 0 -> 1 triangle
顶点着色器显然正在变换所有 6 个顶点。我该如何编写这个程序,以便只有第二个三角形旋转?
最佳答案
您正在通过具有相同参数的相同顶点着色器发送两个三角形。您可以使用不同的参数分别通过着色器发送它们,也可以通过不同的着色器发送它们,具体取决于您要执行的操作。最简单的方法可能是这样的:
// Draw the first triangle like you did above
glDrawArrays (GL_TRIANGLES, 0, 3);
// ... set up the matrix for the second triangle here ...
// Tell the shader about the new matrix
glUniformMatrix (MatrixID, 1, GL_FALSE, &MVP[0][0]);
// And draw starting at the second triangle
glDrawArrays (GL_TRIANGLES, 3, 3);
关于c++ - OpenGL 3.3 - 2 个三角形的不同旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22432696/