【问题标题】:QT OpenGL QList of QVectors2D not Drawing correctlyQVectors2D 的 QT OpenGL QList 未正确绘制
【发布时间】:2017-04-24 19:31:52
【问题描述】:

我正在尝试使用 2d 矢量的 QList 来绘制一系列非连接线/弧,并在列表中越旧越淡化颜色。

例如:

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 不会导致任何内容被绘制。

有什么想法吗?

【问题讨论】:

    标签: c++ qt opengl qlist qvector


    【解决方案1】:

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

    所以在(您的代码的)第一个函数中:所有顶点都在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();
    
    }
    

    案例 2:

    假设您想要中心和点之间的线。 我的意思是,如果您的点向量有 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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-26
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 2016-09-26
      相关资源
      最近更新 更多