QVectors2D 的 QT OpenGL QList 绘制不正确

QT OpenGL QList of QVectors2D not Drawing correctly

我正在尝试使用二维向量的 QList 绘制一系列非连接 lines/arcs 并淡化列表中较旧的颜色。

例如:

void drawArcs(QList<QVector2D>& points,
               float centerX, float centerY,
               float red, float green, float blue)
{
glBegin(GL_LINE_STRIP);
float colorGain;
int INC;
INC=0;
colorGain=float(INC)/float(TotalArcPoints);
foreach (const QVector2D& vec, points)
{

    glColor3f(colorGain*red, colorGain*green, colorGain*blue);

    glVertex3f( vec.x() + centerX,
                    - vec.y() + centerY,
                    0.0);

    INC++;
    colorGain=float(INC)/float(TotalArcPoints);
}
glEnd();
}

然而,这将我所有的弧线连接在一起,我希望 QList 中的每组 2D 向量都是它自己的弧线,但是当我将代码更改为此时。它什么也没画,屏幕是空白的。

void drawArcs(QList<QVector2D>& points,
               float centerX, float centerY,
               float red, float green, float blue)
{

float colorGain;
int INC;
INC=0;
colorGain=float(INC)/float(TotalArcPoints);
foreach (const QVector2D& vec, points)
{
    glBegin(GL_LINE_STRIP);
    glColor3f(colorGain*red, colorGain*green, colorGain*blue);

    glVertex3f( vec.x() + centerX,
                    - vec.y() + centerY,
                    0.0);

    INC++;
    colorGain=float(INC)/float(TotalArcPoints);
    glEnd();
}

}

颜色映射在上面的代码中工作正常,所以我认为这不是问题所在。我更困惑的是为什么在 for each 循环中移动 glBegin/glEnd 会导致不绘制任何内容。

有什么想法吗?

在你的函数中,只有一个顶点,

所以在第一个函数(您的代码)中: 所有顶点都在 glBeginglEnd 之间连接。

在(您的代码的)第二个函数中:glBeginglEnd 之间,只有一个顶点。所以你没有看到任何一行。

现在用伪代码来解决你的问题:

这里有两个案例

案例一:

假设您需要输入点之间的线。 我的意思是,如果你的点向量有 4 个点 p1、p2、p3、p4。 第一行在 p1 和 p2 之间。 第二行在 p3 和 p4 之间。

for(int i = 0; i<points.size(); i++)
{

    glBegin(GL_LINE_STRIP);

    //FIRST POINT OF THE LINE
    glVertex3f( points.at(i).x() + center.x, 
                    - points.at(i).y() + center.y,
                    0.0);

    i = i + 1;

    //SECOND POINT OF THE LINE
    glVertex3f( points.at(i).x() + center.x, 
                    - points.at(i).y() + center.y,
                    0.0);
    glEnd();

}

案例二:

假设您想要中心和点之间的线。 我的意思是,如果你的点向量有 4 个点 p1、p2、p3、p4。 第一行在 c 和 p1 之间。 第二行在 c 和 p2 之间。 第三行在 c 和 p3 之间。 第四行在c和p4之间。

for(int i = 0; i<points.size(); i++)
{

    glBegin(GL_LINE_STRIP);

    //FIRST POINT OF THE LINE (CENTER)
    glVertex3f( center.x, center.y,0.0);

    //SECOND POINT OF THE LINE
    glVertex3f( points.at(i).x() + center.x, 
                    - points.at(i).y() + center.y,
                    0.0);
    glEnd();
}