gpt4 book ai didi

c++ - 在基于着色器的 opengl 中绘制圆柱体

转载 作者:行者123 更新时间:2023-11-28 07:12:52 24 4
gpt4 key购买 nike

我正在寻找一种在 opengl 上绘制圆柱体的好方法,我尝试绘制多个圆圈

for (GLuint m = 0; m <= segments; ++m) {
for (GLuint n = 0; n <= segments; ++n) {
GLfloat const t = 2 * M_PI * (float) n / (float) segments;
//position
points[num++] = x + sin(t) * r;
points[num++] = .0005 * m;
points[num++] = y + cos(t) * r;
//color
points[num++] = 1;
points[num++] = 1;
points[num++] = 1;
//texture
points[num++] = sin(t) * 0.5 + 0.5;
points[num++] = cos(t) * 0.5 + 0.5;
}
}

和显示功能

GLuint pointer = 0;
for (GLuint i = 0; i <= segments; ++i) {
glDrawArrays(GL_TRIANGLE_FAN, pointer, segments + 1);
pointer += segments + 1;
}

请问有没有直接画这个圆柱体的方法

最佳答案

在另一个上面绘制许多圆盘太慢了(除非你真的想把圆柱体画成圆盘的切片)

您应该只绘制圆柱体的侧面。例如,四边形网格将是

// for (GLuint m = 0; m <= segments; ++m)
float const bottom = .0005f * 0.f;
float const top = .0005f * (segments-1.f);
for(GLuint n = 0; n <= segments; ++n)
{
GLfloat const t0 = 2 * M_PI * (float)n / (float)segments;
GLfloat const t1 = 2 * M_PI * (float)(n+1) / (float)segments;
//quad vertex 0
points[num++] = x + sin(t0) * r;
points[num++] = bottom;
points[num++] = y + cos(t0) * r;
//quad vertex 1
points[num++] = x + sin(t1) * r;
points[num++] = bottom;
points[num++] = y + cos(t1) * r;
//quad vertex 2
points[num++] = x + sin(t1) * r;
points[num++] = top;
points[num++] = y + cos(t1) * r;
//quad vertex 3
points[num++] = x + sin(t0) * r;
points[num++] = top;
points[num++] = y + cos(t0) * r;
}

您可以添加 2 个圆盘(底座)来关闭圆柱体。

您可以使用顶点+索引缓冲区减少从内存中获取顶点的次数。在新版本的 OGL 中,您可以通过使用 gl_VertexID

索引网格来消除顶点内存读取

关于c++ - 在基于着色器的 opengl 中绘制圆柱体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20712492/

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