我有两个向量, std :: vector col和 std :: vector pos,
我的着色器如下:
const GLchar* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec2 position;\n"
"layout (location = 1) in vec3 PColor;\n"
"out vec3 color;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(position.x, position.y, 0.0, 1.0);\n"
"color = PColor;\n"
"}\0";
const GLchar* fragmentShaderSource = "#version 330 core\n"
"out vec4 color;\n"
"in vec4 PColor;\n"
"void main()\n"
"{\n"
"color = PColor;\n"
"}\n\0";
我可以执行以下操作将pos和col绑定到同一个VAO
void CreateVertexBuffer()
{
glGenBuffers(1, &posbuffer);
glGenVertexArrays(1, &VAO);
glBindBuffer(GL_ARRAY_BUFFER, posbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(int) * pos.size(), &pos.front(), GL_STATIC_DRAW);
glBindVertexArray(VAO);
glVertexAttribPointer(0, 2, GL_INT, GL_FALSE, 2 * sizeof(GL_INT), (GLvoid*)0);
glEnableVertexAttribArray(0);
//glBindVertexArray(0); delete
glGenBuffers(1, &colbuffer);
glBindBuffer(GL_ARRAY_BUFFER, colbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GL_FLOAT) * col.size(), &col.front(), GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GL_FLOAT), (GLvoid*)0);
glEnableVertexAttribArray(1);
glBindVertexArray(0);
}
我想让我的程序画一个圆圈,但核心被抛弃,所以我认为我的VAO绑定可能有些问题。谢谢!
答案 0 :(得分:0)
您应该在生成VBO之前生成VAO
glGenVertexArrays(1, &vao[object_id]);
glBindVertexArray(vao[object_id]);
glGenBuffers(1, &vbo[0]);
glBindBuffer(target, vbo[0]);
glBufferData(target, size, data1, dataType);
glEnableVertexAttribArray(h_Attributes[UV]);
glVertexAttribPointer(h_Attributes[UV], 2, GL_FLOAT, 0, 0, 0);
glGenBuffers(1, &vbo[1]);
glBindBuffer(target, vbo[1]);
glBufferData(target, size, data2, dataType);
glEnableVertexAttribArray(h_Attributes[POSITION]);
glVertexAttribPointer(h_Attributes[POSITION], 3, GL_FLOAT, 0, 0, 0);
位置向量必须是float而不是int,因为它将在着色器中为vec2
<强>更新强> 绘制对象时,必须绑定生成的vao
glBindVertexArray(vao[object_id]);
如果您没有更新阵列的任何值,则不需要在渲染循环中绑定任何vbos。但是,如果需要更新颜色或位置的顶点,则必须调用这些函数。
glBindVertexArray(vao[object_id]);
glBindBuffer(target, vbo[object_id]);
glBufferData(GL_ARRAY_BUFFER, sizeof(the_array), &the_array[0], GL_DYNAMIC_DRAW);