我正在学习opengl,目前我正在与VAO合作。 我想使用VAO绘制一个立方体和一个三角形但不幸的是,只绘制了我后来创建的对象。这就是我在主循环中所做的:
<div class="body">
<div class="why-not-twenty-px">
containing div has height <span class="smaller">•</span> of 21px, not 20px
</div>
<div class="why-not-sixty-px">
containing div has height of 61 px, not 60px multiline multiline multiline multiline multiline multiline <span class="smaller">•</span> multiline multiline multiline multiline multiline multiline multiline multiline multiline multiline multiline multiline multiline multiline multiline multiline multiline multiline multiline multiline multiline
</div>
</div>
void main()
{
//loading shader, generate window, etc ...
//generate a cube:
GLuint cube_vao = generateCube();
//next, generate a triangle:
GLuint triangle_vao = generateTriangle();
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
do
{
//draw:
glBindVertexArray(triangle_vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(cube_vao);
glDrawArrays(GL_TRIANGLES, 0, 12*3);
glfwPollEvents();
glfwSwapBuffers(window);
} while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0);
}
和generateCube()
两者基本相同:创建顶点,创建vbo,创建vao并设置属性。然后他们返回vao id。
这是generateTriangle()
,例如:
generateTriangle()
使用此代码,只会绘制多维数据集。
此外,如果我注释掉这些线条:
主要是generateTriangle()
{
//generate the vertex positions:
GLfloat triangle_pos[] = //not part of the snippet -> too long
//generate vbo for the positions:
GLuint pos_vbo;
glGenBuffers(1, &pos_vbo);
glBindBuffer(GL_ARRAY_BUFFER, pos_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_pos), triangle_pos, GL_STATIC_DRAW);
//next, generate the vertex colors:
GLfloat triangle_color[] = //not part of the snippet -> too long
//generate vbo for the colors:
GLuint col_vbo;
glGenBuffers(1, &col_vbo);
glBindBuffer(GL_ARRAY_BUFFER, col_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_color), triangle_color, GL_STATIC_DRAW);
//generate VAO:
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLint pos_attrib_id = glGetAttribLocation(programID, "line_pos");
glEnableVertexAttribArray(pos_attrib_id);
glBindBuffer(GL_ARRAY_BUFFER, pos_vbo);
glVertexAttribPointer(pos_attrib_id, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
GLint col_attrib_id = glGetAttribLocation(programID, "color");
glEnableVertexAttribArray(col_attrib_id);
glBindBuffer(GL_ARRAY_BUFFER, col_vbo);
glVertexAttribPointer(col_attrib_id, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
//function to set the perspective (argument is the model matrix)
setPerspective(glm::mat4(1.0f));
return vao;
}
和glBindVertexArray(cube_vao);
,三角形被绘制但是具有立方体的颜色和位置,这让我发疯。
如果有帮助,我正在使用带着色器版本120的OSX。