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

QQuickWidget With QOpenGLFunctions is displaying white sceen



  • I am using the following code but i am getting nothing error but displaying white screen instad of white screen.

    #include "squircle.h"

    #include <QtQuick/qquickwindow.h>
    #include <QtGui/QOpenGLShaderProgram>
    #include <QtGui/QOpenGLContext>
    #include <QOpenGLFramebufferObjectFormat>
    #include <QVector2D>
    #include <QVector3D>

    struct VertexData
    {
    QVector3D position;
    QVector2D texCoord;
    };
    Squircle::Squircle()
    : m_t(0)
    , m_renderer(nullptr)
    {
    this->setAcceptedMouseButtons(Qt::AllButtons);
    connect(this, &QQuickItem::windowChanged, this, &Squircle::handleWindowChanged);
    }

    void Squircle::setT(qreal t)
    {
    if (t == m_t)
    return;
    m_t = t;
    emit tChanged();
    if (window())
    window()->update();
    }

    void Squircle::handleWindowChanged(QQuickWindow *win)
    {
    if (win) {
    connect(win, &QQuickWindow::beforeSynchronizing, this, &Squircle::sync, Qt::DirectConnection);
    connect(win, &QQuickWindow::sceneGraphInvalidated, this, &Squircle::cleanup, Qt::DirectConnection);
    // If we allow QML to do the clearing, they would clear what we paint
    // and nothing would show.
    win->setClearBeforeRendering(false);
    }
    }

    void Squircle::cleanup()
    {
    if (m_renderer) {
    delete m_renderer;
    m_renderer = nullptr;
    }
    }
    void Squircle::mousePressEvent(QMouseEvent *event)
    {
    // m_color = Qt::black;
    //update(); //changing an attribute of the qquickitem and updating the scenegraph
    qDebug() << "mouse press";
    }
    void Squircle::mouseReleaseEvent(QMouseEvent *event)
    {
    // m_color = Qt::black;
    //update(); //changing an attribute of the qquickitem and updating the scenegraph
    qDebug() << "mouse release";
    }
    SquircleRenderer::~SquircleRenderer()
    {
    delete m_program;
    }

    void Squircle::sync()
    {
    if (!m_renderer) {
    m_renderer = new SquircleRenderer();
    connect(window(), &QQuickWindow::beforeRendering, m_renderer, &SquircleRenderer::paint, Qt::DirectConnection);
    }
    m_renderer->setViewportSize(window()->size() * window()->devicePixelRatio());
    m_renderer->setT(m_t);
    m_renderer->setWindow(window());
    }

    void SquircleRenderer::paint()
    {
    initializeOpenGLFunctions();

    arrayBuf.create();
      indexBuf.create();
    if (!m_program) {
    
    
           m_program = new QOpenGLShaderProgram();
           m_program->addCacheableShaderFromSourceCode(QOpenGLShader::Vertex,
                                            "#ifdef GL_ES\n"
                                            "precision mediump int;"
                                            "precision mediump float;"
                                            "#endif\n"
    
                                            "uniform mat4 mvp_matrix;"
                                            "uniform mediump vec4 color;"
                                            "attribute vec4 a_position;"
                                            "attribute vec2 a_texcoord;"
    
                                            "varying vec2 v_texcoord;"
                                                       "void main() {"
                                                       "    gl_Position = mvp_matrix * a_position;"
                                                       "    v_texcoord = a_texcoord;"
                                                       "}");
           m_program->addCacheableShaderFromSourceCode(QOpenGLShader::Fragment,
                                            "#ifdef GL_ES\n"
                                            "precision mediump int;"
                                            "precision mediump float;"
                                            "#endif\n"
    
                                            "uniform sampler2D texture;"
                                            "uniform mediump vec4 color;"
                                            "varying vec2 v_texcoord;"
                                                       "void main() {"
                                                        "gl_FragColor = color;"
                                                       "}");
    
           //m_program->bindAttributeLocation("vertices", 0);
           m_program->link();
             glEnable(GL_DEPTH_TEST);
             glEnable(GL_CULL_FACE);
       }
       m_program->bind();
    

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    QMatrix4x4 matrix;
    matrix.translate(0.0, 0.0, -5.0);
    m_program->setUniformValue("mvp_matrix", matrix);

      // m_program->enableAttributeArray(0);
    
       VertexData values[] ={
           // Vertex data for face 0
           {QVector3D(-1.0f, -1.0f,  1.0f), QVector2D(0.0f, 0.0f)},  // v0
           {QVector3D( 1.0f, -1.0f,  1.0f), QVector2D(0.33f, 0.0f)}, // v1
           {QVector3D(-1.0f,  1.0f,  1.0f), QVector2D(0.0f, 0.5f)},  // v2
           {QVector3D( 1.0f,  1.0f,  1.0f), QVector2D(0.33f, 0.5f)}, // v3
    
           // Vertex data for face 1
           {QVector3D( 1.0f, -1.0f,  1.0f), QVector2D( 0.0f, 0.5f)}, // v4
           {QVector3D( 1.0f, -1.0f, -1.0f), QVector2D(0.33f, 0.5f)}, // v5
           {QVector3D( 1.0f,  1.0f,  1.0f), QVector2D(0.0f, 1.0f)},  // v6
           {QVector3D( 1.0f,  1.0f, -1.0f), QVector2D(0.33f, 1.0f)}, // v7
    
           // Vertex data for face 2
           {QVector3D( 1.0f, -1.0f, -1.0f), QVector2D(0.66f, 0.5f)}, // v8
           {QVector3D(-1.0f, -1.0f, -1.0f), QVector2D(1.0f, 0.5f)},  // v9
           {QVector3D( 1.0f,  1.0f, -1.0f), QVector2D(0.66f, 1.0f)}, // v10
           {QVector3D(-1.0f,  1.0f, -1.0f), QVector2D(1.0f, 1.0f)},  // v11
    
           // Vertex data for face 3
           {QVector3D(-1.0f, -1.0f, -1.0f), QVector2D(0.66f, 0.0f)}, // v12
           {QVector3D(-1.0f, -1.0f,  1.0f), QVector2D(1.0f, 0.0f)},  // v13
           {QVector3D(-1.0f,  1.0f, -1.0f), QVector2D(0.66f, 0.5f)}, // v14
           {QVector3D(-1.0f,  1.0f,  1.0f), QVector2D(1.0f, 0.5f)},  // v15
    
           // Vertex data for face 4
           {QVector3D(-1.0f, -1.0f, -1.0f), QVector2D(0.33f, 0.0f)}, // v16
           {QVector3D( 1.0f, -1.0f, -1.0f), QVector2D(0.66f, 0.0f)}, // v17
           {QVector3D(-1.0f, -1.0f,  1.0f), QVector2D(0.33f, 0.5f)}, // v18
           {QVector3D( 1.0f, -1.0f,  1.0f), QVector2D(0.66f, 0.5f)}, // v19
    
           // Vertex data for face 5
           {QVector3D(-1.0f,  1.0f,  1.0f), QVector2D(0.33f, 0.5f)}, // v20
           {QVector3D( 1.0f,  1.0f,  1.0f), QVector2D(0.66f, 0.5f)}, // v21
           {QVector3D(-1.0f,  1.0f, -1.0f), QVector2D(0.33f, 1.0f)}, // v22
           {QVector3D( 1.0f,  1.0f, -1.0f), QVector2D(0.66f, 1.0f)}  // v23
       };
       GLushort indices[] = {
               0,  1,  2,  3,  3,     // Face 0 - triangle strip ( v0,  v1,  v2,  v3)
               4,  4,  5,  6,  7,  7, // Face 1 - triangle strip ( v4,  v5,  v6,  v7)
               8,  8,  9, 10, 11, 11, // Face 2 - triangle strip ( v8,  v9, v10, v11)
              12, 12, 13, 14, 15, 15, // Face 3 - triangle strip (v12, v13, v14, v15)
              16, 16, 17, 18, 19, 19, // Face 4 - triangle strip (v16, v17, v18, v19)
              20, 20, 21, 22, 23      // Face 5 - triangle strip (v20, v21, v22, v23)
          };
       arrayBuf.bind();
       arrayBuf.allocate(values, 24 * sizeof(VertexData));
       indexBuf.bind();
           indexBuf.allocate(indices, 34 * sizeof(GLushort));
    
           arrayBuf.bind();
            indexBuf.bind();
    
       quintptr offset = 0;
        // Tell OpenGL programmable pipeline how to locate vertex position data
        int vertexLocation = m_program->attributeLocation("a_position");
        m_program->enableAttributeArray(vertexLocation);
        m_program->setAttributeBuffer(vertexLocation, GL_FLOAT, offset, 3, sizeof(VertexData));
    
        // Offset for texture coordinate
        offset += sizeof(QVector3D);
    
        // Tell OpenGL programmable pipeline how to locate vertex texture coordinate data
        int texcoordLocation = m_program->attributeLocation("a_texcoord");
        m_program->enableAttributeArray(texcoordLocation);
        m_program->setAttributeBuffer(texcoordLocation, GL_FLOAT, offset, 2, sizeof(VertexData));
    
        int colorLocation = m_program->uniformLocation("color");
            QColor color(0, 255, 0, 255);
            m_program->setUniformValue(colorLocation, color);
            glDrawElements(GL_TRIANGLE_STRIP, 34, GL_UNSIGNED_SHORT, 0);
       //m_program->setAttributeArray(0, GL_FLOAT, values, 2);
       //m_program->setUniformValue("t", (float) m_t);
    
       //glViewport(0, 0, m_viewportSize.width(), m_viewportSize.height());
    
    
    
    
       //m_program->disableAttributeArray(0);
       //m_program->release();
    
       // Not strictly needed for this example, but generally useful for when
       // mixing with raw OpenGL.
       //m_window->resetOpenGLState();
    
    // Restore the matrix state
    //glPopMatrix();
    

    }

    can anyone help me out in these



  • I think you should:

    1. write a minimal test program to show the problem in the title - nobody should be expected to read, understand and debug your long code
    2. describe your mobile or embedded system and its configuration and your Qt version and its configuration - someone might notice something in either

Log in to reply