Draw an OpenGL scene behind a Qt Quick view
-
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
-
@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!