无法在 OpenGL 中显示 VBO

发布于 2024-11-29 23:18:37 字数 952 浏览 3 评论 0原文

我在 OpenGL 中有一个 VBO 和一个 IBO,但无法正确绘制它们。您能否告诉我在框架显示功能中我可能忘记了什么? - struct Point3D 是一个内部有 3 个浮点的结构体 (x,y,z)。 - nbVertex 是 glVertex 数组中的顶点数量。 - nbVBOInd 是 VBOInd 数组中的索引数量。

    glGenBuffers(1, &VertexVBOID);
    glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(struct Point3D)*nbVertex, glVertex, GL_STATIC_DRAW);

    glGenBuffers(1, &IndexVBOID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*nbVBOInd, VBOInd, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, sizeof(glVertex), BUFFER_OFFSET(0));   //The starting point of the VBO, for the vertices

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
    glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));   //The starting point of the IBO

谢谢 !

I have a VBO and an IBO in OpenGL, but am unable to draw them properly. Could you please let me know what I could have forgotten in the frame display function ?
- struct Point3D is a struct with 3 floats inside (x,y,z).
- nbVertex is the amount of vertexes in the glVertex array.
- nbVBOInd is the amount of indices in the VBOInd array.

    glGenBuffers(1, &VertexVBOID);
    glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(struct Point3D)*nbVertex, glVertex, GL_STATIC_DRAW);

    glGenBuffers(1, &IndexVBOID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*nbVBOInd, VBOInd, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, sizeof(glVertex), BUFFER_OFFSET(0));   //The starting point of the VBO, for the vertices

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
    glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));   //The starting point of the IBO

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

一世旳自豪 2024-12-06 23:18:37

我看到与 rodrigo 相同的问题 - 您的数据类型不匹配,正如您在这里看到的:

glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*nbVBOInd, VBOInd, GL_STATIC_DRAW);

sizeof(int) - 使用整数类型

glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));   

GL_UNSIGNED_SHORT - 根据 openGL 规范使用短类型

,glDrawElements 只能使用无符号数据类型。要解决此问题,您需要:

  • 在声明中将 VBOInd 更改为无符号类型,例如:

    unsigned int* VBOInd = new unsigned int[nbVBOInd]

  • 将第 6 个调用替换为

    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*nbVBOInd, VBOInd, GL_STATIC_DRAW);

  • 将第 11 个(最后一次)调用替换为

    glDrawElements(GL_TRIANGLES, nbVBOInd, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

无论如何我相信问题隐藏在指针设置中,将第 9 个调用更改为

glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));

如果这不起作用,请向我们展示如何声明 glVertex 和 VBOInd 并填充数据。也许您正在使用 std::vector ?您需要这样称呼这些数据容器:

glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*nbVBOInd, &VBOInd[0], GL_STATIC_DRAW);

如果有不清楚的地方,只需在评论中询问..

I see the same problem as rodrigo - you have data type mismatch, as you can see here:

glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*nbVBOInd, VBOInd, GL_STATIC_DRAW);

sizeof(int) - using integer type

glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));   

GL_UNSIGNED_SHORT - using short type

according to openGL specification, there are only unsigned data types possible for glDrawElements. To fix this you need:

  • change VBOInd to unsigned type in declaration like:

    unsigned int* VBOInd = new unsigned int[nbVBOInd]

  • replace 6th call with

    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*nbVBOInd, VBOInd, GL_STATIC_DRAW);

  • replace 11th (last) call with

    glDrawElements(GL_TRIANGLES, nbVBOInd, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

Anyway I believe that the problem is hidden in pointer setup, change 9th call to:

glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));

If that doesn't work, please show us how glVertex and VBOInd is declared and filled with data. Maybe you're using std::vector? You need to call these data containers like:

glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*nbVBOInd, &VBOInd[0], GL_STATIC_DRAW);

If something's unclear, just ask in comments..

橘寄 2024-12-06 23:18:37

尝试将最后一行更改为:

glDrawElements(GL_TRIANGLES, nbVBOInd, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

除非 IndexVBOID 中的数据非常短,否则上面的 sizeof(int) 将是错误的。

Try changing the last line to:

glDrawElements(GL_TRIANGLES, nbVBOInd, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

Unless your data in the IndexVBOID are really short, but then, the sizeof(int) above would be wrong.

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