大家好我试图绘制一个索引的VAO,但它并没有真正起作用。 它只绘制第一个元素(第一行)。 我用它绘制两条线,只有三个顶点,但只有第一个工作。 VAO创建代码:
GLushort indices[] = {
0, 1, 1, 2
};
GLfloat pos[] = {
50, 50,
200, 100,
500, 200
};
GLfloat colors[] = {
1, 1, 1,
1, 1, 1,
1, 1, 1
};
GLuint vaoID;
GLuint vboID[2];
GLuint ibo;
trackShader.use();
glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);
glGenBuffers(2, vboID);
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * sizeof(GLushort), indices, GL_STATIC_DRAW);
trackShader.addAttribute("vertex");
glBindBuffer(GL_ARRAY_BUFFER, vboID[0]);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(GLfloat), pos, GL_STATIC_DRAW);
glVertexAttribPointer(trackShader.attribute("vertex"), 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(trackShader.attribute("vertex"));
trackShader.addAttribute("color");
glBindBuffer(GL_ARRAY_BUFFER, vboID[1]);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(GLfloat), colors, GL_STATIC_DRAW);
glVertexAttribPointer(trackShader.attribute("color"), 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(trackShader.attribute("color"));
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
trackShader.disable();
绘图代码:
glBindVertexArray(vaoID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
trackShader.use();
glDrawElements(GL_LINES, 4, GL_UNSIGNED_SHORT, 0);
trackShader.disable();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);