How to use QOpenGLWidget to draw a triangle correctly?



  • Update 2018.01.18:
    I solve the question and u can go to here to see the source code.


    Origin Post:
    I am trying to write a simple program——to use QOpenGLWidget to draw a colorful triangle but I still can't see the right result. My program zip was here and I put the core codes below ,could anyone tell me what's wrong with my code...? Thx anyway.

    glwidget.h:

    #ifndef GLWIDGET_H
    #define GLWIDGET_H
    
    #include <QOpenGLWidget>
    #include <QOpenGLFunctions>
    #include <QOpenGLShaderProgram>
    #include <QOpenGLContext>
    #include <QOpenGLBuffer>
    
    class GLWidget : public QOpenGLWidget , protected QOpenGLFunctions
    {
    public:
        GLWidget();
        ~GLWidget();
    
    private:
        void paintGL() Q_DECL_OVERRIDE;
        void initializeGL() Q_DECL_OVERRIDE;
        void resizeGL(int w, int h) Q_DECL_OVERRIDE;
    
        uchar *fragmentShader;
        uchar *vertexShader;
    
        QOpenGLShaderProgram *mShaderProgram;
        QOpenGLBuffer vbo;
    
        GLint a_colorPos;
        GLint a_positionPos;
        GLint u_mvpMatrixPos;
    
    public:
    
    };
    
    #endif // GLWIDGET_H
    

    glwidget.cpp:

    #include "glwidget.h"
    
    GLWidget::GLWidget()
    {
        mShaderProgram = Q_NULLPTR;
    }
    
    GLWidget::~GLWidget()
    {
    
    }
    
    void GLWidget::paintGL()
    {
        glViewport(0,0,this->width(),this->height());
        glClear(GL_COLOR_BUFFER_BIT);
    
        mShaderProgram->bind();
    
        QMatrix4x4 matrix;
        matrix.ortho(-10.0f,10.0f,-10.0f,10.0f,-100.0f,100.0f);
        matrix.translate(0,0,-2);
        mShaderProgram->setUniformValue(u_mvpMatrixPos, matrix);
    
        glDrawArrays(GL_TRIANGLES,0,3);
    
    //    glDisableVertexAttribArray(a_colorPos);
    //    glDisableVertexAttribArray(a_positionPos);
    
        mShaderProgram->release();
    }
    
    void GLWidget::initializeGL()
    {
    static const char shader1[] =
                "uniform highp mat4 u_mvpMatrix;        \n"
                "attribute highp vec4 a_position;       \n"
                "attribute lowp vec4 a_color;           \n"
                "varying lowp vec4 v_color;             \n"
                "void main()                            \n"
                "{                                      \n"
                "   v_color = a_color;                     \n"
                "   gl_Position = u_mvpMatrix * a_position;\n"
                "}                                      \n";
    static const char shader2[] =
                "varying lowp vec4 v_color;             \n"
                "void main()                            \n"
                "{                                      \n"
                "   gl_FragColor = v_color;                \n"
                "}                                      \n";
    
        initializeOpenGLFunctions();
    
        if ( !mShaderProgram )
        {
            mShaderProgram = new QOpenGLShaderProgram(this);
            mShaderProgram->addShaderFromSourceCode(QOpenGLShader::Vertex,shader1);
            mShaderProgram->addShaderFromSourceCode(QOpenGLShader::Fragment,shader2);
            mShaderProgram->link();
            a_positionPos  = mShaderProgram->attributeLocation("a_positionPos");
            a_colorPos     = mShaderProgram->attributeLocation("a_color");
            u_mvpMatrixPos = mShaderProgram->uniformLocation("u_mvpMatrix");
    
            vbo.create();
            vbo.bind();
    
            GLfloat vVertices[] = {
                0.0f, 0.5f, 0.0f,
               -0.5f,-0.5f, 0.0f,
                0.5f,-0.5f, 0.0f
            } ;
            GLfloat vColors[]   = {
                1.0f, 0.0f, 0.0f,
                0.0f, 1.0f, 0.0f,
                0.0f, 0.0f, 1.0f
            };
    
            vbo.allocate(vVertices,18*sizeof(GLfloat));
            mShaderProgram->setAttributeBuffer(a_positionPos,GL_FLOAT,0,3,0);
            glEnableVertexAttribArray(a_positionPos);
    
            vbo.write(9*sizeof(GLfloat),vColors,9*sizeof(GLfloat));
            mShaderProgram->setAttributeBuffer(a_colorPos,GL_FLOAT,9*sizeof(GLfloat),3,0);
            glEnableVertexAttribArray(a_colorPos);
    
    //        glVertexAttribPointer(a_positionPos,3,GL_FLOAT,GL_FALSE,0,vVertices);
    //        glVertexAttribPointer(a_colorPos   ,3,GL_FLOAT,GL_FALSE,0,vColors  );
        }
    
        glClearColor(0.0,0.0,0.0,1.0);
    
    }
    
    void GLWidget::resizeGL(int w, int h)
    {
    
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.