Home > Enterprise >  QSurfaceFormat does not allow to draw with OpenGL
QSurfaceFormat does not allow to draw with OpenGL

Time:12-07

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();
}

CodePudding user response:

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();
}
  • Related