经典的“没有任何内容被渲染” OpenGL问题

发布于 2024-11-26 20:57:00 字数 1730 浏览 5 评论 0原文

我知道,这很令人沮丧。我无法在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奈何桥上唱咆哮 2024-12-03 20:57:00

仅仅调用 gl*Pointer 函数是不够的;还需要调用 gl*Pointer 函数。你需要告诉 OpenGL 它应该从那些特定的数组中提取。对于内置数组(glVertexPointer、glColorPointer 等),您可以使用 glEnableClientState() 来处理相关的特定数组。

例如:

glBindBuffer(GL_ARRAY_BUFFER, vertices);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, NULL);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_BYTE, 0, (const GLvoid *)(sizeof(GLfloat) * 2));

这应该会提供更好的结果。

完成渲染后,您还应该对这些数组使用 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:

glBindBuffer(GL_ARRAY_BUFFER, vertices);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, NULL);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_BYTE, 0, (const GLvoid *)(sizeof(GLfloat) * 2));

That should provide better results.

You should also use glDisableClientState() on those arrays after you are finished rendering with them.

不一样的天空 2024-12-03 20:57:00

您似乎缺少几个步骤:

  • VAO 绑定
  • 启用客户端状态(VBO),例如 glEnableClientState(GL_VERTEX_ARRAY)

You seem to be missing a few steps:

  • VAO binding
  • enabling client state (VBO), e.g. glEnableClientState(GL_VERTEX_ARRAY)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文