QQuickFramebufferObject flipped on Y axis in QML
I created a new QML project and I am using a custom QML element to integrate raw OpenGL.
If I take a screenshot using framebufferObject()->toImage().save("a.png");
QQuickFramebufferObject :: Renderer, then the surface is a correct renderer, but the QML surface is inverted on the Y axis.
To fix this I need to scale my matrix by -1 on the Y axis, but now my screenshot is flipped. I think flip from QML and OpenGL coordinates. Can anyone give me QML to render correctly instead of scaling my matrix?
source to share
Answer from qt-project forum: http://qt-project.org/forums/viewthread/48124/ . He links to the following bug report: https://bugreports.qt-project.org/browse/QTBUG-41073
source to share
just override the function QSGNode *QQuickFramebufferObject::updatePaintNode(QSGNode *, UpdatePaintNodeData *)
with
QSGNode* QVTKFrameBufferObjectItem::updatePaintNode(
QSGNode *const inOutNode, UpdatePaintNodeData *const inOutData) {
const int width = this->width();
const int height = this->height();
QMatrix4x4 flipY;
flipY.translate(width*0.5, height*0.5);
flipY.scale(1.0, -1.0);
flipY.translate(-width*0.5, -height*0.5);
inOutData->transformNode->setMatrix(flipY);
return QQuickFramebufferObject::updatePaintNode(inOutNode, inOutData);
}
source to share
In your custom element (which is of course a subclass QQuickFramebufferObject
), override the method updatePaintNode()
like this:
QSGNode* updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData* upnData)
{
QSGNode* node = QQuickFramebufferObject::updatePaintNode(oldNode, upnData);
QSGSimpleTextureNode& textureNode = dynamic_cast<QSGSimpleTextureNode&>(*node);
textureNode.setTextureCoordinatesTransform(QSGSimpleTextureNode::MirrorVertically);
return node;
}
source to share