使用坐标数组在QOpenGLWidget中绘制GL_LINE

问题描述

我有使用QtCreator完成的应用程序。我的渲染类是

    Form {
        ForEach(nameArray,id: \.self) { name in
            Button(action: {
                selectedName = name
                showLink = true
            }) {
                Text(name)
            }
        }
    }
    .background(Group{
        if showLink {
            NavigationLink(destination: NameView(selectedName: selectedName),isActive: $linkIsActive) {
                EmptyView()
            }
        }
    })

现在,我想使用具有顶点坐标的数组来绘制对象。
我在此页面上找到了一些绘制三角形的教程: OpenGL Window Example

我试图重用它来绘制简单的线条

GLRendWindow::GLRendWindow(QWidget *parent): QOpenGLWidget(parent)

也是我的初始化方法

void GLRendWindow::drawLine()
{
    GLfloat line[] =
    {
        0.5f,1.0f,0.5f,-1.0f
    };
    GLfloat line_colors[] =
    {
        1.0f,0.0f,// red
        0.0f,// blue
    };
    shaderProgram->bind();
    glVertexAttribPointer(posAttr,2,GL_FLOAT,GL_FALSE,line);
    glVertexAttribPointer(colAttr,3,line_colors);
    glEnableVertexAttribArray(colAttr);
    glEnableVertexAttribArray(posAttr);
    glDrawArrays(GL_LINE,2);
    glDisableVertexAttribArray(posAttr);
    glDisableVertexAttribArray(colAttr);
    shaderProgram->release();
}

和绘制方法

void GLRendWindow::initializeGL()
{
    initializeOpenGLFunctions();
    glClearColor( 0.0f,0.0f );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );

    shaderProgram = new QOpenGLShaderProgram(this);
    shaderProgram->addShaderFromSourceCode(QOpenGLShader::Vertex,vertexShaderSource);
    shaderProgram->addShaderFromSourceCode(QOpenGLShader::Fragment,fragmentShaderSource);
    shaderProgram->link();
    posAttr = shaderProgram->attributeLocation("posAttr");
    colAttr = shaderProgram->attributeLocation("colAttr");
}

和调整大小方法

void GLRendWindow::paintGL()
{
    glClear( GL_COLOR_BUFFER_BIT );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    drawLine();

    if (isUpdated)
    {
       //some other stuff

        isUpdated = false;
    }
    glFlush();
}

但是当我运行应用程序时,什么也没画。没有错误,没有例外,什么都没有。 在本教程中,我看到了

void GLRendWindow::resizeGL(int w,int h)
{
    glViewport( 0,w,h );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();

    if ( w < h && w > 0 )
    {
        glFrustum( - 1.0,1.0,- 1.0 * h / w,1.0 * h / w,5.0 );
    }
    else
    {
        if ( w >= h && h > 0 )
        {
            glFrustum( - 0.3 * w / h,0.3 * w / h,- 0.3,0.3,0.5,5.0 );
        }
    }
}

可能将解决方案隐藏在这里,但是我完全不明白它的用途以及正在发生什么。我什至不确定画简单线条是否必要。
如何使用坐标数组查看直线?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)