Something wrong whit QOpenGLWidget and QOpenGLFunctions.



  • Hello All:
    I use QOpenGLShaderProgram, QOpenGLShader, and so on. QT's class can render very successful. But when I try to use opengl's API directly, get nothing on screen. Is there something wrong? Please help me. Thanks! Code as follow:

    class Widget : public QOpenGLWidget, protected QOpenGLFunctions
    {
        Q_OBJECT
    public:
        Widget(QWidget *parent = 0);
        ~Widget();
    protected:
        virtual void initializeGL();
        virtual void paintGL();
        virtual void resizeGL(int w, int h);
    private:
        QMatrix4x4  m_projectionMat;
        GLint       m_pMatrixUniform;
        GLuint vertexAttributeBuffer;
    };
    
    Widget::Widget(QWidget *parent) : QOpenGLWidget(parent)
    {
    }
    
    Widget::~Widget()
    {
    }
    void Widget::initializeGL()
    {
        initializeOpenGLFunctions();
        glViewport(0, 0, width()*devicePixelRatio(), height()*devicePixelRatio());
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_CULL_FACE);
        glCullFace(GL_BACK);
        glClearColor(0, 0, 0, 1);
    
        GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
        const char * vertexShaderCode =
                "attribute highp vec3 aVertexPosition;  \
                 uniform mat4 uPMatrix;                 \
                 void main(void) {                      \
                     gl_Position = uPMatrix * vec4(aVertexPosition, 1.0); \
                 }";
        glShaderSource(vertexShader,1,&vertexShaderCode,NULL);
        glCompileShader(vertexShader);
    
        GLint success;
        GLchar infoLog[512];
        glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
        if(!success)
        {
            glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
            qWarning() << "ERROR::SHADER::VERTEX::COMPILATION_FAILED" << infoLog;
        }
    
        GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
        const char * fragmentShaderCode =
                "void main(void) {                  \
                     gl_FragColor = vec4(1.0, 1.0, 0.0,1.0);    \
                 }";
        glShaderSource(fragmentShader, 1, &fragmentShaderCode, NULL);
        glCompileShader(fragmentShader);
        glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
        if(!success)
        {
            glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
            qWarning() << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED" << infoLog;
        }
    
        GLuint shaderProgram = glCreateProgram();
        glAttachShader(shaderProgram, vertexShader);
        glAttachShader(shaderProgram, fragmentShader);
        glLinkProgram(shaderProgram);
        glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
        if(!success)
        {
            glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
            qWarning() << "ERROR::SHADER_PROGRAM::LINK_FAILED" << infoLog;
        }
        glUseProgram(shaderProgram);
    
        GLint positionLocation = glGetAttribLocation(shaderProgram,"aVertexPosition");
        glEnableVertexAttribArray(positionLocation);
        glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
        m_pMatrixUniform = glGetUniformLocation(shaderProgram, "uPMatrix");
    
        glGenBuffers(1, &vertexAttributeBuffer);
        glBindBuffer(GL_ARRAY_BUFFER, vertexAttributeBuffer);   //缓存对象此刻被真正创建
        GLfloat data[] = {
            0.0, 2.0, -3.0,
            0.0, 0.0, -3.0,
            2.0, 0.0, -3.0
        };
        glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
    }
    
    void Widget::paintGL()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glUniformMatrix4fv(m_pMatrixUniform, 1, GL_FALSE, m_projectionMat.constData());
        glBindBuffer(GL_ARRAY_BUFFER, vertexAttributeBuffer);
        glDrawArrays(GL_TRIANGLES, 0, 3);
    }
    
    void Widget::resizeGL(int w, int h)
    {
        m_projectionMat.setToIdentity();
        m_projectionMat.perspective(45.0, w/h, 1.0, 6.0);
    }
    

    Thanks again.


Log in to reply
 

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