gpt4 book ai didi

c++ - 使用 GL_LINE_LOOP 绘制多个圆

转载 作者:行者123 更新时间:2023-11-27 23:23:00 26 4
gpt4 key购买 nike

我在使用这些代码在屏幕上渲染许多圆圈时遇到问题。

float degree = 0;
unsigned int ctr = 0;
for(int xi = -3200; xi < 3200; xi+= 2*r)
{
for(int yi = 4800; yi > -4800; yi-= 2*r)
{
for(int i = 0; i < 360; ++i)
{
vertices.push_back(xi + r * cos(float(degree)));
vertices.push_back(yi + r * sin(float(degree)));
vertices.push_back(-8);

indices.push_back(i+ctr);
++degree;
}
ctr += 360;
degree = 0;
}
}

unsigned int i = 0;
for(i = 0; i < indices.size()/360; ++i)
{
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, &vertices[i*360]);
glLineWidth(1);
glDrawElements(GL_LINE_LOOP, 360, GL_UNSIGNED_INT, &indices[i*360]);
glDisableClientState(GL_VERTEX_ARRAY);
}

这是结果 circles

此外,当我将 xi 值更改为 [-6400, 6400] 时程序崩溃

最佳答案

撇开这种技术的可疑性质不谈,您看起来是在错误地访问索引。

glVertexPointer(3, GL_FLOAT, 0, &vertices[i*360]);
glDrawElements(GL_LINE_LOOP, 360, GL_UNSIGNED_INT, &indices[i*360]);

glDrawElements 的索引指定了距 glVertexPointer 顶点的偏移量。您已经将索引定义为相对于顶点缓冲区的开始:

indices.push_back(i+ctr);   

但是您要为绘制的每个圆移动缓冲区偏移量。因此,在您的索引缓冲区中,第二个圆从索引 360 开始。但是当您绘制第二个圆时,您还移动了顶点指针,使索引 360 成为指针的第 0 个元素。

然后,当您尝试访问索引 360 时,您实际上访问的是元素 720(360 + 缓冲区起始 @360)。

关于c++ - 使用 GL_LINE_LOOP 绘制多个圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11509399/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com