QOpenGL fragment shader



  • Being new to it, I'm trying to run a very simple example of QOpenGL functionalities. I can build a program that runs, but appears to completely ignore the fragment shader even though its compilation completes successfully.

    MainWindow.h, defining the custom widget.

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QtOpenGL>
    
    class MainWindow : public QOpenGLWidget, protected QOpenGLFunctions
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
    
    private:
        QOpenGLVertexArrayObject m_vao;
        QOpenGLBuffer m_vbo;
        QOpenGLShaderProgram *m_program;
    
    protected:
        void initializeGL();
        void paintGL();
    };
    
    #endif // MAINWINDOW_H
    

    MainWindow.ccp

    // Shader sources
    const GLchar* vertexSource =
        "in vec2 position;"
        "void main()"
        "{"
        "    gl_Position = vec4(position, 0.0, 1.0);"
        "}";
    
    const GLchar* fragmentSource =
        "void main()"
        "{"
        "   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);"
        "}";
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QOpenGLWidget(parent),
        m_program(0)
    {
        ;
    }
    
    
    void MainWindow::initializeGL()
    {
        // Expose GL functions
        initializeOpenGLFunctions();
    
        // Set clear color
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    
        // Instantiate program, link shaders, bind program
        m_program = new QOpenGLShaderProgram();
        m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexSource);
        m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentSource);
        m_program->link();
        m_program->bind();
    
        // Vertex array object
        m_vao.create();
        m_vao.bind();
    
        // Vertex buffer object
        m_vbo.create();
        m_vbo.bind();
    
        GLfloat vertices[] = {
             0.0f,  0.5f,
             0.5f, -0.5f,
            -0.5f, -0.5f
        };
    
        m_vbo.allocate(vertices, sizeof(vertices));
    
        // Specify the layout of the vertex data
        int posAttrib = m_program->attributeLocation("position");
        glEnableVertexAttribArray(posAttrib);
        glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
    
        m_program->release();
    }
    
    
    void MainWindow::paintGL()
    {
        // Clear the screen to black
        glClear(GL_COLOR_BUFFER_BIT);
    
        // Draw a triangle from the 3 vertices
        glDrawArrays(GL_TRIANGLES, 0, 3);
    }
    
    

    It draws the triangle as it should, but it's white instead of red. If I comment out the line where I compile the fragment shader, I get the same result.
    Main loop just instantiates a QApplication and a MainWindow.
    I've run the hellogl2 example project, which works regularly. Thanks in advance.



  • Start your debugging by checking the return values of link() and bind(). The log() call will return information on errors and warnings. Reference QT Assistant for further detail on the QOpenGLShaderProgram. If all is well after checking those, then the problem is in the assignment of the shader and we'll need to start debugging there.

    -Orby



  • @Orby Forgot to mention it, addShader, link and bind all return true and the log is accordingly empty. Can't really tell if anything's wrong from the docs, it looks in order to me.



  • Does removing the line: m_program->release(); fix the program? It looks like you are setting up the shader, and then destroying it before its actually used.


  • Moderators

    Hi! Releasing the program in the init function is correct but you have to bind it again in the rendering function. Same for the buffers.



  • Sorry I might be sending you on a wild goose chase, I haven't done OpenGL in QT C++ since 4.x, I use QML these days.

    Check out: http://doc.qt.io/qt-5/qtopengl-hellogl2-example.html

    Get that example or one of the many others running, and then work backwards to simply drawing vertex's in a triangle with your shader. Thats going to be your fast way to solve the problem at this point.



  • @Wieland That did it! Thanks everyone :)


  • Moderators

    @Aanok I always find OpenGL code very hard to debug. Glad this solved it! :-)


Log in to reply
 

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