我尝试使用VBO上的顶点数组渲染立方体,使用位置和颜色以及指定的索引,但是当我尝试绘制它时,屏幕上什么也没显示。
这是我的数据:
Vertex VertexData[8];
GLfloat CubeVertexData[] =
{
// front
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
// back
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
-1.0, 1.0, -1.0 };
GLubyte CubeColors[] = {
// Front
0, 255, 0, 255,
255, 255, 0, 255,
0, 255, 0, 255,
0, 0, 255, 255,
// Back
255, 0, 0, 255,
255, 255, 0, 255,
0, 255, 0, 255,
0, 255, 0, 255
};
GLushort CubeElementData[] = {
// front
0, 1, 2,
2, 3, 0,
// top
3, 2, 6,
6, 7, 3,
// back
7, 6, 5,
5, 4, 7,
// bottom
4, 5, 1,
1, 0, 4,
// left
4, 0, 3,
3, 7, 4,
// right
1, 5, 6,
6, 2, 1
};
所以我使用自己的Vertex结构将位置和颜色存储在一起。
glGenBuffers(1, &myVboID);
glBindBuffer(GL_ARRAY_BUFFER, myVboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(VertexData), &VertexData[0], GL_STATIC_DRAW);
glGenBuffers(1, &vertElemID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertElemID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(CubeElementData), &CubeElementData[0], GL_STATIC_DRAW);
`
当我试图画画时,它只是在画面上没有显示任何内容。我真的很困惑,因为我有另一个类似的程序完全相同,但使用过剩,它工作正常。
//Bind vbo and indices
glBindBuffer(GL_ARRAY_BUFFER, myVboID);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(Vertex, position));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_UNSIGNED_INT, GL_TRUE, sizeof(Vertex), (void*) offsetof(Vertex, color));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertElemID);
int size;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
这就是我在每个数组工作时为其创建VBO的方法
// Vertex data
glGenBuffers(1, &vertBuffID);
glBindBuffer(GL_ARRAY_BUFFER, vertBuffID);
glBufferData(GL_ARRAY_BUFFER, sizeof(CubeVertexData), CubeVertexData, GL_STATIC_DRAW);
// Color data
glGenBuffers(1, &vertColorID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertColorID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(CubeColors), CubeColors, GL_STATIC_DRAW);
// Index array of vertex / color data
glGenBuffers(1, &vertElemID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertElemID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(CubeElementData), &CubeElementData[0], GL_STATIC_DRAW);
这就是我绘制的方式,这种方法非常好,但由于某种原因我无法使用顶点数组。
glBindBuffer(GL_ARRAY_BUFFER, vertBuffID);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertColorID);
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertElemID);
int size;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);