无法在 OpenGL 中显示 VBO
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看到与 rodrigo 相同的问题 - 您的数据类型不匹配,正如您在这里看到的:
sizeof(int) - 使用整数类型
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 个调用更改为:
如果这不起作用,请向我们展示如何声明 glVertex 和 VBOInd 并填充数据。也许您正在使用 std::vector ?您需要这样称呼这些数据容器:
如果有不清楚的地方,只需在评论中询问..
I see the same problem as rodrigo - you have data type mismatch, as you can see here:
sizeof(int) - using integer type
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:
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:
If something's unclear, just ask in comments..
尝试将最后一行更改为:
除非 IndexVBOID 中的数据非常短,否则上面的 sizeof(int) 将是错误的。
Try changing the last line to:
Unless your data in the IndexVBOID are really short, but then, the sizeof(int) above would be wrong.