OpenGL:使用顶点数组绘制立方体会导致图形故障
当尝试使用顶点数组(而不是使用立即模式渲染)在 OpenGL 中绘制以下四边形时,我得到了图片中显示的图形故障(线段),可以在下面的第二个链接中找到该故障。这条线似乎向上延伸到无限远。
GLdouble vertices[] = {
// back
0.0, 0.0, 0.0,
si, 0.0, 0.0,
si, -si, 0.0,
0.0, -si, 0.0,
// front
0.0, 0.0, si,
0.0, -si, si,
si, -si, si,
si, 0.0, si,
// left
0.0, 0.0, 0.0,
0.0, -si, 0.0,
0.0, -si, si,
0.0, 0.0, si,
// right
si, 0.0, 0.0,
si, 0.0, si,
si, -si, si,
si, -si, 0.0,
// top
0.0, 0.0, 0.0,
0.0, 0.0, si,
si, 0.0, si,
si, 0.0, 0.0,
// bottom
0.0, -si, 0.0,
si, -si, 0.0,
si, -si, si,
0.0, -si, si,
};
立即绘制:
glBegin(GL_QUADS);
for (int i = 0; i < sizeof(vertices)/sizeof(*vertices)/3; i++)
glVertex3d(vertices[i * 3], vertices[i * 3 + 1], vertices[i * 3 + 2]);
glEnd();
使用顶点数组绘制:
glVertexPointer(3, GL_DOUBLE, 0, vertices);
glDrawArrays(GL_QUADS, 0, sizeof(vertices)/sizeof(*vertices));
图像:
我做错了什么?
When trying to draw the following quads in OpenGL using a vertex array (instead of using immediate mode rendering), I get the graphical glitch (line segment) shown in the picture, which can be found in the second link below. The line seems to extend upwards to infinity.
GLdouble vertices[] = {
// back
0.0, 0.0, 0.0,
si, 0.0, 0.0,
si, -si, 0.0,
0.0, -si, 0.0,
// front
0.0, 0.0, si,
0.0, -si, si,
si, -si, si,
si, 0.0, si,
// left
0.0, 0.0, 0.0,
0.0, -si, 0.0,
0.0, -si, si,
0.0, 0.0, si,
// right
si, 0.0, 0.0,
si, 0.0, si,
si, -si, si,
si, -si, 0.0,
// top
0.0, 0.0, 0.0,
0.0, 0.0, si,
si, 0.0, si,
si, 0.0, 0.0,
// bottom
0.0, -si, 0.0,
si, -si, 0.0,
si, -si, si,
0.0, -si, si,
};
Immediate drawing:
glBegin(GL_QUADS);
for (int i = 0; i < sizeof(vertices)/sizeof(*vertices)/3; i++)
glVertex3d(vertices[i * 3], vertices[i * 3 + 1], vertices[i * 3 + 2]);
glEnd();
Drawing with vertex array:
glVertexPointer(3, GL_DOUBLE, 0, vertices);
glDrawArrays(GL_QUADS, 0, sizeof(vertices)/sizeof(*vertices));
Images:
Correct cube drawn in immediate mode
Glitchy cube drawn with vertex array
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将您的绘图更改为
sizeof() 并不像您想象的那样工作。它不返回数组中的元素数量。您需要以某种方式跟踪该数组中的顶点数量,然后在 glDrawArrays 调用中使用该计数器。
编辑 - 更多信息:
glDrawArrays 中的最后一个参数是您正在渲染的顶点数。 sizeof(vertices) 应返回 GLdouble* 中的字节数(4 字节),sizeof(vertices*) 应返回 GLdouble 中的字节数(8 字节)。那么 4 / 8 在技术上应该四舍五入到零,因为它们是整数,所以我实际上不确定为什么它会渲染任何东西。
但是,我非常确定(假设您的顶点正确完成),如果您将该数字更改为 24,它将起作用。
Try changing your draw to
sizeof() doesn't work the way you think it does. It doesn't return the number of elements in an array. You need to somehow keep track of the number of vertices in that array, and then use that counter in your glDrawArrays call.
edit - more information:
The last parameter in glDrawArrays is the number of vertices you are rendering. sizeof(vertices) should return the number of bytes in a GLdouble* (4 bytes), and sizeof(vertices*) should return the number of bytes in a GLdouble (8 bytes). Then 4 / 8 should technically round to zero since they are integers, so I'm not actually sure why it's rendering anything.
However, I'm pretty sure that (assuming your vertices are done correctly), if you change that number to 24, it will work.