我正在尝试使用顶点数组对象在OpenGL 4.1中绘制一些顶点和索引:
float vertices[] = {-0.8, -0.8, 0, 0.8, -0.8, 0, 0, 0.8, 0};
unsigned int indices[] = { 0, 1, 2 };
设置VAO并使用glDrawElements()绘制后,我得到一个空屏幕。我还尝试使用一对简单的着色器,只返回原始顶点位置和硬编码颜色,但也没有运气。
以下是设置VBO和VAO的代码:
unsigned int vertexArray;
glGenVertexArrays(1, &vertexArray);
glBindVertexArray(vertexArray);
unsigned int vertexBufferID;
unsigned int indexBufferID;
glGenBuffers(1, &vertexBufferID);
glGenBuffers(1, &indexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, 4 * vertexCount, vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 4 * vertexCount, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * indexCount, indices, GL_STATIC_DRAW);
我使用以下方式绘制VAO:
glBindVertexArray(vertexArray);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (void*)0);
这是所有OpenGL调用的痕迹:
// Setup code
glViewport(0,0,1024,768)
glGetString(GL_RENDERER)="GeForce GT 540M/PCIe/SSE2"
glGetString(GL_VENDOR)="NVIDIA Corporation"
glGetString(GL_VERSION)="4.1.0 NVIDIA 359.46"
glGetString(GL_SHADING_LANGUAGE_VERSION)="4.10 NVIDIA via Cg compil..."
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LESS)
glGenVertexArrays(1,000000000015F9B4)
glBindVertexArray(1)
glGenBuffers(1,000000000015F9D4)
glGenBuffers(1,000000000015F9F4)
glBindBuffer(GL_ARRAY_BUFFER,1)
glBufferData(GL_ARRAY_BUFFER,36,000000000015FA48,GL_STATIC_DRAW)
glEnableVertexAttribArray(0)
glVertexAttribPointer(0,3,GL_FLOAT,false,36,0000000000000000)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,2)
glBufferData(GL_ELEMENT_ARRAY_BUFFER,12,000000000015FA88,GL_STATIC_DRAW)
glClearColor(0.300000,0.300000,0.400000,1.000000)
// End of setup, here's the code rendering two frames (they just repeat):
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT)
glBindVertexArray(1)
glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_INT,0000000000000000)
glGetError()=GL_NO_ERROR
wglSwapBuffers(0000000069011A82)=true
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT)
glBindVertexArray(1)
glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_INT,0000000000000000)
glGetError()=GL_NO_ERROR
wglSwapBuffers(0000000069011A82)=true
据我所知,我正在做的每个教程都说我应该做的事情,但出于某种原因,我的屏幕上什么都没有出现。我错过了什么?
答案 0 :(得分:1)
您的数据紧密打包在数组中,因此glVertexAttribPointer
中的stride参数应为0.顺便说一句,如果声明静态数组,则应使用sizeof
来传递大小数组而不是依赖于实现的事物,如4 * vertexCount
:
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);