Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Draw an OpenGL scene behind a Qt Quick view
Qt 6.11 is out! See what's new in the release blog

Draw an OpenGL scene behind a Qt Quick view

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 0 Offline
    0 Offline
    02JanDal
    wrote on last edited by
    #1

    Hello,

    I'm trying to draw an OpenGL scene behind a Qt Quick view (the QQ view is supposed to be used for menu overlays etc.), but it seems like it isn't working.

    I'm using QQuickWindow::beforeRendering to do my OpenGL stuff, but I only see my triangle flash quickly at the beginning, and then it doesn't show up anymore.

    A compilable project is "here":https://www.dropbox.com/s/xobi7htbg7oaee5/QQGLTest.tar.gz, relevant code snippets:
    In my constructor:
    @
    setClearBeforeRendering(false);
    @
    The slot connected to QQuickWindow::sceneGraphInitialized:
    @
    m_context = openglContext();
    m_funcs = m_context->versionFunctions<QOpenGLFunctions_3_3_Core>();
    if (!m_funcs)
    {
    qFatal("Could not obtain OpenGL version object");
    exit(1);
    }
    m_funcs->initializeOpenGLFunctions();

    m_funcs->glEnable(GL_DEPTH_TEST);
    m_funcs->glDepthFunc(GL_LESS);

    m_shaderProgram = new QOpenGLShaderProgram;
    // setup shaders
    {
    static const char *vertexShaderSource =
    "#version 330 core\n"
    "layout(location = 0) in vec3 vertexPosition;\n"
    "void main() {\n"
    " gl_Position.xyz = vertexPosition;\n"
    " gl_Position.w = 1.0;\n"
    "}";
    static const char *fragmentShaderSource =
    "#version 330 core\n"
    "out vec3 color;\n"
    "void main() {\n"
    " color = vec3(1, 0, 0);\n"
    "}";

    m_shaderProgram->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
    m_shaderProgram->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
    m_shaderProgram->link();
    }

    m_triangleVao = new QOpenGLVertexArrayObject;
    m_triangleVao->create();
    m_triangleVao->bind();

    static const GLfloat g_vertex_buffer_data[] = {
    -1.0f, -1.0f, 0.0f,
    1.0f, -1.0f, 0.0f,
    0.0f, 1.0f, 0.0f,
    };

    m_vertexBuffer = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
    m_vertexBuffer->create();
    m_vertexBuffer->bind();
    m_vertexBuffer->setUsagePattern(QOpenGLBuffer::StaticDraw);
    m_vertexBuffer->allocate(g_vertex_buffer_data, sizeof(GLfloat) * 3 * 3);
    @
    The slot connected to beforeRendering:
    @
    if (!isExposed())
    {
    return;
    }

    m_funcs->glViewport(0, 0, width(), height());
    m_funcs->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    // render
    {
    m_shaderProgram->bind();

    m_funcs->glEnableVertexAttribArray(0);
    m_funcs->glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer->bufferId());
    m_funcs->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
    m_funcs->glDrawArrays(GL_TRIANGLES, 0, 3);
    m_funcs->glDisableVertexAttribArray(0);

    m_shaderProgram->release();
    }
    @

    Both slots are connected with Qt::DirectConnection.

    I'm rather new to OpenGL, so it's very possible that I might be doing something wrong.

    Thanks in advance,

    Jan

    1 Reply Last reply
    0
    • S Offline
      S Offline
      steventaitinger
      wrote on last edited by steventaitinger
      #2

      @02JanDal said:

      QOpenGLBuffer

      I think you are mixing QOpenGLBuffer with the regular OpenGl buffers. I am new too though so I can't help fix it yet!

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved