OpenGL based QQuickItem can't rendering with other QML elements at the same time
Unsolved
QML and Qt Quick
-
I'm new in Qt/OpenGL stuff and I've created an OpenGL based QQuickItem class that should render 3D.
If I use it without any QML elements in scene it works, but when I set together something likeRectangle
, I only can see black rectangle instead of 3D render. What's wrong?
For example, this is how render works:
GLWindow::GLWindow(QQuickItem *parent) : QQuickItem(parent) { setFlag(QQuickItem::ItemHasContents, true); frameTimer = new QTimer(this); connect(this, &GLWindow::windowChanged, this, &GLWindow::handleWindowChanged); connect(frameTimer, &QTimer::timeout, this, &GLWindow::updateScene); frameTimer->start(FRAMERATE_MS); }
void GLWindow::updateScene() { if (window()) window()->update(); }
void GLWindow::render() { initializeOpenGLFunctions(); glClearColor(0.f, 0.f, 0.f, 1.f); QPoint cursorPos = QCursor::pos(); // VIEWPORT qreal ratio = window()->devicePixelRatio(); GLint x = static_cast<GLint>(ratio * parentItem()->x()); GLint y = static_cast<GLint>(ratio * parentItem()->y()); GLsizei w = static_cast<GLsizei>(ratio * parentItem()->width()); GLsizei h = static_cast<GLsizei>(ratio * parentItem()->height()); glViewport(x, y, w, h); glEnable(GL_DEPTH_TEST); glEnable(GL_SCISSOR_TEST); glScissor(x, y, w, h); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDisable(GL_SCISSOR_TEST); glDisable(GL_DEPTH_TEST); // Rendering stuff... }
void GLWindow::handleWindowChanged(QQuickWindow *win) { if (win) { connect(win, &QQuickWindow::afterRendering, this, &GLWindow::render, Qt::DirectConnection); win->setClearBeforeRendering(true); } }