Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QSurfaceFormat does not allow to draw with OpenGL



  • It works nice (I see a square) without QSurfaceFormat when I comment it:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
    //    QSurfaceFormat format;
    //    format.setSamples(8);
    
        Widget w;
    //    w.setFormat(format);
        w.show();
        return a.exec();
    }
    

    But when I uncomment it the code does not draw the square:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QSurfaceFormat format;
    //    format.setSamples(8);
    
        Widget w;
        w.setFormat(format);
        w.show();
        return a.exec();
    }
    

    main.cpp

    // Add this line to .pro:
    // win32: LIBS += -lopengl32
    
    #ifdef _WIN32
    #include <windows.h>
    extern "C" __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
    extern "C" __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x00000001;
    #endif
    
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QOpenGLWidget>
    #include <QtGui/QOpenGLShaderProgram>
    #include <QtGui/QOpenGLBuffer>
    #include <QtGui/QMatrix4x4>
    #include <QtGui/QSurfaceFormat>
    
    class Widget : public QOpenGLWidget
    {
    public:
        Widget()
        {
            setWindowTitle("Square. Qt, OpenGL 3.3, C++");
            setFixedSize(QSize(400, 400));
        }
    
    private:
        QOpenGLShaderProgram m_program;
        QOpenGLBuffer m_vertPosBuffer;
        QOpenGLBuffer m_texCoordBuffer;
        QMatrix4x4 m_mvpMatrix;
        QMatrix4x4 m_projMatrix;
        QMatrix4x4 m_viewMatrix;
        QMatrix4x4 m_modelMatrix;
        int m_uMvpMatrixLocation;
    
        void initializeGL() override
        {
            glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
            const char *vertShaderSrc =
                    "#version 330\n"
                    "in vec3 aPosition;"
                    "uniform mat4 uMvpMatrix;"
                    "void main()"
                    "{"
                    "    gl_Position = uMvpMatrix * vec4(aPosition, 1.0);"
                    "}";
            const char *fragShaderSrc =
                    "#version 330\n"
                    "out vec4 fragColor;"
                    "void main()"
                    "{"
                    "    fragColor = vec4(0.9, 0.9, 1.0, 1.0);"
                    "}";
    
            m_program.addShaderFromSourceCode(QOpenGLShader::Vertex, vertShaderSrc);
            m_program.addShaderFromSourceCode(QOpenGLShader::Fragment, fragShaderSrc);
            m_program.link();
            m_program.bind();
    
            float vertPositions[] = {
                -0.5f, -0.5f, 0.f,
                0.5f, -0.5f, 0.f,
                -0.5f, 0.5f, 0.f,
                0.5f, 0.5f, 0.f
            };
            m_vertPosBuffer.create();
            m_vertPosBuffer.bind();
            m_vertPosBuffer.allocate(vertPositions, sizeof(vertPositions));
            m_program.bindAttributeLocation("aPosition", 0);
            m_program.setAttributeBuffer(0, GL_FLOAT, 0, 3);
            m_program.enableAttributeArray(0);
    
            m_viewMatrix.lookAt(QVector3D(0.f, 0.f, 50.f),
                                QVector3D(0.f, 0.f, 0.f),
                                QVector3D(0.f, 1.f, 0.f));
            m_modelMatrix.scale(100.f, 100.f, 100.f);
        }
    
        void paintGL() override
        {
            glClear(GL_COLOR_BUFFER_BIT);
            m_mvpMatrix = m_projMatrix * m_viewMatrix * m_modelMatrix;
            m_program.bind();
            m_program.setUniformValue(m_uMvpMatrixLocation, m_mvpMatrix);
            glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
        }
    
        void resizeGL(int w, int h) override
        {
            glViewport(0, 0, w, h);
            m_projMatrix.setToIdentity();
            m_projMatrix.ortho(-100.f, 100.f, -100.f, 100.f, 100.f, -100.f);
        }
    };
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QSurfaceFormat format;
    //    format.setSamples(8);
    
        Widget w;
        w.setFormat(format);
        w.show();
        return a.exec();
    }
    


  • I found my stupid mistake. I just forgot to get a uniform location for uMvpMatrix: m_uMvpMatrixLocation = m_program.uniformLocation("uMvpMatrix");

    main.cpp

    // Add this line to .pro:
    // win32: LIBS += -lopengl32
    
    #ifdef _WIN32
    #include <windows.h>
    extern "C" __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
    extern "C" __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x00000001;
    #endif
    
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QOpenGLWidget>
    #include <QtGui/QOpenGLShaderProgram>
    #include <QtGui/QOpenGLBuffer>
    #include <QtGui/QMatrix4x4>
    #include <QtGui/QSurfaceFormat>
    
    class Widget : public QOpenGLWidget
    {
        Q_OBJECT
    public:
        Widget()
        {
            setWindowTitle("Rectangle. Qt, OpenGL 3.3, C++");
            setFixedSize(QSize(400, 400));
            resize(400, 400);
        }
    
    private:
        QOpenGLShaderProgram m_program;
        QOpenGLBuffer m_vertPosBuffer;
        QOpenGLBuffer m_texCoordBuffer;
        QMatrix4x4 m_mvpMatrix;
        QMatrix4x4 m_projMatrix;
        QMatrix4x4 m_viewMatrix;
        QMatrix4x4 m_modelMatrix;
        int m_uMvpMatrixLocation;
    
        void initializeGL() override
        {
            glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
            glEnable(GL_DEPTH_TEST);
            const char *vertShaderSrc =
                    "#version 330\n"
                    "in vec3 aPosition;"
                    "uniform mat4 uMvpMatrix;"
                    "void main()"
                    "{"
                    "    gl_Position = uMvpMatrix * vec4(aPosition, 1.0);"
                    "}";
            const char *fragShaderSrc =
                    "#version 330\n"
                    "out vec4 fragColor;"
                    "void main()"
                    "{"
                    "    fragColor = vec4(0.9, 0.9, 1.0, 1.0);"
                    "}";
    
            m_program.addShaderFromSourceCode(QOpenGLShader::Vertex, vertShaderSrc);
            m_program.addShaderFromSourceCode(QOpenGLShader::Fragment, fragShaderSrc);
            m_program.link();
            m_program.bind();
    
            m_uMvpMatrixLocation = m_program.uniformLocation("uMvpMatrix");
    
            float vertPositions[] = {
                -0.5f, -0.5f, 0.f,
                0.5f, -0.5f, 0.f,
                -0.5f, 0.5f, 0.f,
                0.5f, 0.5f, 0.f
            };
            m_vertPosBuffer.create();
            m_vertPosBuffer.bind();
            m_vertPosBuffer.allocate(vertPositions, sizeof(vertPositions));
            m_program.bindAttributeLocation("aPosition", 0);
            m_program.setAttributeBuffer(0, GL_FLOAT, 0, 3);
            m_program.enableAttributeArray(0);
    
            m_viewMatrix.lookAt(QVector3D(0.f, 0.f, 50.f),
                                QVector3D(0.f, 0.f, 0.f),
                                QVector3D(0.f, 1.f, 0.f));
            m_modelMatrix.translate(QVector3D(0.f, 0.f, 0.f));
            m_modelMatrix.rotate(20.f, QVector3D(0.f, 0.f, 1.f));
            m_modelMatrix.scale(50.f, 70.f, 1.f);
        }
    
        void paintGL() override
        {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            m_mvpMatrix = m_projMatrix * m_viewMatrix * m_modelMatrix;
            m_program.bind();
            m_program.setUniformValue(m_uMvpMatrixLocation, m_mvpMatrix);
            glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
        }
    
        void resizeGL(int w, int h) override
        {
            glViewport(0, 0, w, h);
            m_projMatrix.setToIdentity();
            m_projMatrix.ortho(-100.f, 100.f, -100.f, 100.f, 100.f, -100.f);
        }
    };
    
    #include "main.moc"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QSurfaceFormat format;
        format.setSamples(8);
    
        Widget w;
        w.setFormat(format);
        QSurfaceFormat::setDefaultFormat(format);
        w.show();
        return a.exec();
    }
    

  • Lifetime Qt Champion

    Hi,

    What if you call QSurfaceFormat::setDefaultFormat(format) ?



  • @SGaist said in QSurfaceFormat does not allow to draw with OpenGL:

    What if you call QSurfaceFormat::setDefaultFormat(format) ?

    The same:

    65239627-2b4b-45e3-b2b5-1f27dc96e75e-image.png



  • But I decided to make a new example from scratch (see a code below) with the same task (drawing a rectanle) with format.setSamples(8);. But I made the separated files (Widget.h/Widget.cpp, default.vert/default.frag). It works without that problem. I do not understand why it does not work in one file. I worked before.

    main.cpp

    // Add this line to .pro:
    // win32: LIBS += -lopengl32
    
    #ifdef _WIN32
    #include <windows.h>
    extern "C" __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
    extern "C" __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x00000001;
    #endif
    
    #include "Widget.h"
    
    #include <QtGui/QSurfaceFormat>
    #include <QtWidgets/QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QSurfaceFormat format;
        format.setSamples(8);
        Widget w;
        w.setFormat(format);
        w.show();
        return a.exec();
    }
    

    Widget.h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QtGui/QOpenGLBuffer>
    #include <QtGui/QOpenGLShaderProgram>
    #include <QtWidgets/QOpenGLWidget>
    
    class Widget : public QOpenGLWidget
    {
        Q_OBJECT
    
    public:
        Widget();
        ~Widget();
    
    private:
        void initializeGL() override;
        void paintGL() override;
        void resizeGL(int w, int h) override;
    
        QOpenGLShaderProgram m_program;
        QOpenGLBuffer m_vertPosBuffer;
    };
    
    #endif // WIDGET_H
    

    Widget.cpp

    #include "Widget.h"
    
    Widget::Widget()
    {
        setWindowTitle("OpenGL 3.3, Qt5, C++");
        setFixedSize(QSize(450, 600));
    }
    
    Widget::~Widget()
    {
    }
    
    void Widget::initializeGL()
    {
        glClearColor(0.2f, 0.2f, 0.2f, 1.f);
        glEnable(GL_DEPTH_TEST);
    
        m_program.addCacheableShaderFromSourceFile(QOpenGLShader::Vertex, ":/Assets/Shaders/default.vert");
        m_program.addCacheableShaderFromSourceFile(QOpenGLShader::Fragment, ":/Assets/Shaders/default.frag");
        m_program.link();
        m_program.bind();
    
        float vertPositions[] = {
            -0.5f, -0.5f, 0.f,
            0.5f, -0.5f, 0.f,
            -0.5f, 0.5f, 0.f,
            0.5f, 0.5f, 0.f
        };
        m_vertPosBuffer.create();
        m_vertPosBuffer.bind();
        m_vertPosBuffer.allocate(vertPositions, sizeof(vertPositions));
    
        m_program.bindAttributeLocation("aPosition", 0);
        m_program.setAttributeBuffer(0, GL_FLOAT, 0, 3);
        m_program.enableAttributeArray(0);
    }
    
    void Widget::paintGL()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    }
    
    void Widget::resizeGL(int w, int h)
    {
        glViewport(0, 0, w, h);
    }
    

    default.vert

    #version 330
    
    in vec3 aPosition;
    
    void main()
    {
        gl_Position = vec4(aPosition, 1.0);
    }
    

    default.frag

    #version 330
    
    out vec4 fragColor;
    
    void main()
    {
        fragColor = vec4(1.0, 1.0, 0.0, 1.0);
    }
    


  • I found my stupid mistake. I just forgot to get a uniform location for uMvpMatrix: m_uMvpMatrixLocation = m_program.uniformLocation("uMvpMatrix");

    main.cpp

    // Add this line to .pro:
    // win32: LIBS += -lopengl32
    
    #ifdef _WIN32
    #include <windows.h>
    extern "C" __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
    extern "C" __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x00000001;
    #endif
    
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QOpenGLWidget>
    #include <QtGui/QOpenGLShaderProgram>
    #include <QtGui/QOpenGLBuffer>
    #include <QtGui/QMatrix4x4>
    #include <QtGui/QSurfaceFormat>
    
    class Widget : public QOpenGLWidget
    {
        Q_OBJECT
    public:
        Widget()
        {
            setWindowTitle("Rectangle. Qt, OpenGL 3.3, C++");
            setFixedSize(QSize(400, 400));
            resize(400, 400);
        }
    
    private:
        QOpenGLShaderProgram m_program;
        QOpenGLBuffer m_vertPosBuffer;
        QOpenGLBuffer m_texCoordBuffer;
        QMatrix4x4 m_mvpMatrix;
        QMatrix4x4 m_projMatrix;
        QMatrix4x4 m_viewMatrix;
        QMatrix4x4 m_modelMatrix;
        int m_uMvpMatrixLocation;
    
        void initializeGL() override
        {
            glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
            glEnable(GL_DEPTH_TEST);
            const char *vertShaderSrc =
                    "#version 330\n"
                    "in vec3 aPosition;"
                    "uniform mat4 uMvpMatrix;"
                    "void main()"
                    "{"
                    "    gl_Position = uMvpMatrix * vec4(aPosition, 1.0);"
                    "}";
            const char *fragShaderSrc =
                    "#version 330\n"
                    "out vec4 fragColor;"
                    "void main()"
                    "{"
                    "    fragColor = vec4(0.9, 0.9, 1.0, 1.0);"
                    "}";
    
            m_program.addShaderFromSourceCode(QOpenGLShader::Vertex, vertShaderSrc);
            m_program.addShaderFromSourceCode(QOpenGLShader::Fragment, fragShaderSrc);
            m_program.link();
            m_program.bind();
    
            m_uMvpMatrixLocation = m_program.uniformLocation("uMvpMatrix");
    
            float vertPositions[] = {
                -0.5f, -0.5f, 0.f,
                0.5f, -0.5f, 0.f,
                -0.5f, 0.5f, 0.f,
                0.5f, 0.5f, 0.f
            };
            m_vertPosBuffer.create();
            m_vertPosBuffer.bind();
            m_vertPosBuffer.allocate(vertPositions, sizeof(vertPositions));
            m_program.bindAttributeLocation("aPosition", 0);
            m_program.setAttributeBuffer(0, GL_FLOAT, 0, 3);
            m_program.enableAttributeArray(0);
    
            m_viewMatrix.lookAt(QVector3D(0.f, 0.f, 50.f),
                                QVector3D(0.f, 0.f, 0.f),
                                QVector3D(0.f, 1.f, 0.f));
            m_modelMatrix.translate(QVector3D(0.f, 0.f, 0.f));
            m_modelMatrix.rotate(20.f, QVector3D(0.f, 0.f, 1.f));
            m_modelMatrix.scale(50.f, 70.f, 1.f);
        }
    
        void paintGL() override
        {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            m_mvpMatrix = m_projMatrix * m_viewMatrix * m_modelMatrix;
            m_program.bind();
            m_program.setUniformValue(m_uMvpMatrixLocation, m_mvpMatrix);
            glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
        }
    
        void resizeGL(int w, int h) override
        {
            glViewport(0, 0, w, h);
            m_projMatrix.setToIdentity();
            m_projMatrix.ortho(-100.f, 100.f, -100.f, 100.f, 100.f, -100.f);
        }
    };
    
    #include "main.moc"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QSurfaceFormat format;
        format.setSamples(8);
    
        Widget w;
        w.setFormat(format);
        QSurfaceFormat::setDefaultFormat(format);
        w.show();
        return a.exec();
    }
    

Log in to reply