我正在尝试为我的渲染代码设置 VBO 以获得更多 fps。它适用于顶点位置、颜色和纹理坐标的单独 VBO,但在传输到交错的顶点数据后,没有几何渲染。这是我的设置功能:
const GLsizeiptr data_size = NUMBER_OF_CUBE_VERTICES * 9 *sizeof(float);
// allocate a new buffer
glGenBuffers(1, &cubeVBO);
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
glBufferData(GL_ARRAY_BUFFER, data_size, data, GL_STATIC_DRAW);
float* ptr = (float*)data;
glVertexAttribPointer(ATTRIB_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), (ptr + 0));
glEnableVertexAttribArray(ATTRIB_VERTEX);
glVertexAttribPointer(ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), (ptr + 3));
glEnableVertexAttribArray(ATTRIB_COLOR);
glVertexAttribPointer(ATTRIB_TEXCOORD0, 2, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), (ptr + 7));
glEnableVertexAttribArray(ATTRIB_TEXCOORD0);
glGenBuffers(1, &cubeIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, NUMBER_OF_CUBE_INDICES*sizeof(GLubyte), s_cubeIndices, GL_STATIC_DRAW);
数据数组如下所示:
static float data[] =
{
// position // // color // //UV//
-1.0, +1.0, +1.0, 255,0,0,255, 0,0,
-1.0, -1.0, +1.0, 0,255,0,255, 0,0,
+1.0, +1.0, +1.0, 255,0,255,255, 0,0,
+1.0, -1.0, +1.0, 255,0,0,255, 0,0,
+1.0, +1.0, +1.0, 255,0,0,255, 0,0,
+1.0, -1.0, +1.0, 255,0,0,255, 0,0,
+1.0, +1.0, -1.0, 255,255,0,255, 0,0,
+1.0, -1.0, -1.0, 255,0,0,255, 0,0,
+1.0, +1.0, -1.0, 255,0,255,255, 0,0,
+1.0, -1.0, -1.0, 255,255,0,255, 0,0,
-1.0, +1.0, -1.0, 0,255,0,255, 0,0,
-1.0, -1.0, -1.0, 255,0,0,255, 0,0,
-1.0, +1.0, -1.0, 0,0,255,255, 0,0,
-1.0, -1.0, -1.0, 255,0,0,255, 0,0,
-1.0, +1.0, +1.0, 255,255,0,255, 0,0,
-1.0, -1.0, +1.0, 255,0,0,255, 0,0,
};
这是我的渲染代码:
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIBO);
glDrawElements(GL_TRIANGLE_STRIP, NUMBER_OF_CUBE_INDICES, GL_UNSIGNED_BYTE, s_cubeIndices);
我也尝试使用没有索引缓冲区的 DrawArrays 函数,但结果是相同的 - 没有渲染几何。当我的程序运行时,输出窗口中也有 GLError 1282。感谢您对我的问题的任何帮助,谢谢。