c++ - 画一个连续的螺旋?

标签 c++ opengl

这是我编写的一个绘制螺旋的函数:

void drawSpiral(GLfloat x1, GLfloat y1, GLfloat radius)
{
    int lineAmount = 500;
    GLfloat piMultiplier = 10.0f * 3.141592654;

    glBegin(GL_LINES);

    for (int i = 0; i <= lineAmount; i++) 
    {
        GLfloat theta = i/lineAmount*piMultiplier;
        GLfloat x = x1 + (radius*cos(i*piMultiplier/lineAmount));
        GLfloat y = y1 + (radius*sin(i*piMultiplier/lineAmount));

        glVertex2f(x, y);

        radius += 1;
    }
    glEnd();
}

问题是螺旋被绘制为虚线而不是一条连续的线。

spiral-lines

我可以使用 GL_LINE_LOOP 而不是 GL_LINES 得到一条连续的线,但是(可以理解的)从螺旋的中间到外部绘制一条线。所以我知道我的逻辑是好的,我只是不确定这样做的 opengl 方式是什么。

最佳答案

你可以这样做:

  • GL_LINES 带有一个顶点数组,如:[start, end, start, end...]。在这种情况下,您将需要一个非常冗余的数组,例如:[A, B, B, C, C, D]

  • GL_LINE_STRIP 带有像 [A, B, C, D] 这样的顶点数组

  • GL_LINE_LOOP 将在最终顶点和第一个顶点之间创建一条线,我认为这不是您想要的。

简而言之,使用 GL_LINE_STRIP 而不是 GL_LINES

关于c++ - 画一个连续的螺旋?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43815319/

相关文章:

c++ Opengl 处理绘制的元素

python - 如何在opengl和pygame中用键盘按键移动立方体?

opengl - OpenGL 着色器可以访问渲染前设置的哪些值?

c++ - 通过未知大小的外部普通数组进行循环

c++ - 放置新的和非默认的构造函数

c++ - 使用 fstream 和 ifstream 的文本文件 I/O

ruby - 带有 Ruby 的 OpenGL 3.1+

c++ - Cuda + OpenGL 互操作性,glDrawArrays() 访问冲突

c++ - 在 Windows 中使用 cpp 的窗口函数

c++ - 在 Eigen (C++) 中求解欠定方程组