经典的“没有任何内容被渲染” OpenGL问题
我知道,这很令人沮丧。我无法在 OpenGL 应用程序中显示任何内容 - 我看到的只是一个空视口。
当我第一次开始编写应用程序时,我手动绘制顶点(使用 GL_QUADS )并且一切正常。然后我决定切换到 VBO (Vertex B提供O对象)。现在什么都不起作用了。
下面是顶点的结构:
struct SimpleVertex
{
GLfloat x, y;
GLbyte r, g, b, a;
};
如您所见,它非常简单 - 顶点的 x 和 y 坐标以及 RGBA 颜色数据。这是填充顶点和索引缓冲区的代码:
const SimpleVertex rect_vertices[] = {
{ -0.8, 0.8, 0, 255, 0, 128 },
{ 0.8, 0.8, 0, 255, 0, 128 },
{ 0.8, -0.8, 0, 255, 0, 128 },
{ -0.8, -0.8, 0, 255, 0, 128 }
};
const GLuint rect_indices[] = {
0, 1, 2, 3
};
GLuint vertices;
GLuint indices;
glGenBuffers(1, &vertices);
glBindBuffer(GL_ARRAY_BUFFER, vertices);
glBufferData(GL_ARRAY_BUFFER,
sizeof(rect_vertices),
rect_vertices,
GL_STATIC_DRAW);
glGenBuffers(1, &indices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
sizeof(rect_indices),
rect_indices,
GL_STATIC_DRAW);
最后但同样重要的是,这是应该绘制矩形的代码:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindBuffer(GL_ARRAY_BUFFER, vertices);
glVertexPointer(2, GL_FLOAT, 0, NULL);
glColorPointer(4, GL_BYTE, 0,
(const GLvoid *)(sizeof(GLfloat) * 2));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, NULL);
glDisable(GL_BLEND);
我不明白为什么没有渲染任何内容。顶点和颜色数据与使用 glVertex2f()
的先前版本相比基本没有变化。
I know, it's quite frustrating. I can't get anything to show up in my OpenGL application - all I see is an empty viewport.
When I first started writing the application, I was manually drawing the vertices (using GL_QUADS
) and everything worked fine. Then I decided to switch to VBOs (Vertex Buffer Objects). Now nothing works.
Here is the structure for vertices:
struct SimpleVertex
{
GLfloat x, y;
GLbyte r, g, b, a;
};
As you can see, it is very simple - x and y coords for the vertices and RGBA color data. Here is the code that fills the vertex and index buffer:
const SimpleVertex rect_vertices[] = {
{ -0.8, 0.8, 0, 255, 0, 128 },
{ 0.8, 0.8, 0, 255, 0, 128 },
{ 0.8, -0.8, 0, 255, 0, 128 },
{ -0.8, -0.8, 0, 255, 0, 128 }
};
const GLuint rect_indices[] = {
0, 1, 2, 3
};
GLuint vertices;
GLuint indices;
glGenBuffers(1, &vertices);
glBindBuffer(GL_ARRAY_BUFFER, vertices);
glBufferData(GL_ARRAY_BUFFER,
sizeof(rect_vertices),
rect_vertices,
GL_STATIC_DRAW);
glGenBuffers(1, &indices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
sizeof(rect_indices),
rect_indices,
GL_STATIC_DRAW);
And last but certainly not least, here is the code that is supposed to draw the rectangle:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindBuffer(GL_ARRAY_BUFFER, vertices);
glVertexPointer(2, GL_FLOAT, 0, NULL);
glColorPointer(4, GL_BYTE, 0,
(const GLvoid *)(sizeof(GLfloat) * 2));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, NULL);
glDisable(GL_BLEND);
I can't figure out why nothing is being rendered. The vertex and color data is essentially unchanged from the previous version that used glVertex2f()
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅仅调用 gl*Pointer 函数是不够的;还需要调用 gl*Pointer 函数。你需要告诉 OpenGL 它应该从那些特定的数组中提取。对于内置数组(glVertexPointer、glColorPointer 等),您可以使用
glEnableClientState()
来处理相关的特定数组。例如:
这应该会提供更好的结果。
完成渲染后,您还应该对这些数组使用
glDisableClientState()
。Simply calling the gl*Pointer functions is not enough; you need to tell OpenGL that it should pull from those particular arrays. For the built-in arrays (glVertexPointer, glColorPointer, etc), you use
glEnableClientState()
, for the particular array in question.For example:
That should provide better results.
You should also use
glDisableClientState()
on those arrays after you are finished rendering with them.您似乎缺少几个步骤:
glEnableClientState(GL_VERTEX_ARRAY)
You seem to be missing a few steps:
glEnableClientState(GL_VERTEX_ARRAY)