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

时间:2013-12-20 21:50:59

标签: c++ opengl

我正在寻找一种在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;
}

我在问是否有直接绘制圆柱的方法

1 个答案:

答案 0 :(得分: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

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