我有一个应用程序,其中需要将几个QOpenGLFramebufferObject
放到较大的QOpenGLFramebufferObject
上。为此,我认为可以使用Qt中的标准坐标将fbo粘贴到大型fbo上。 QOpenGLFramebufferObject::blitFramebuffer需要一个“源” rect和一个“目标” rect。例如,如果我的大型fbo是1000x1000,而较小的fbo是500x500。然后,我只需要执行以下操作:
auto sourceRect = QRect(0,0,500,500);
// i here would be the row of my grid of fbos
// j would be the column
for(int i =0; i < 2; i++){
for(int j= 0; j < 2; j++){
auto targetRect = QRect(i*500,j*500, 500, 500);
// tileFbo are the small fbos
QOpenGLFramebufferObject::blitFramebuffer(largeFbo,targetRect, tileFbo,sourceRect)
}
}
但是,这似乎不起作用,因为QOpenGLFramebufferObject似乎使用OpenGL坐标。那么如何将QRect转换为使用OpenGL坐标呢?这是一个示例,我在其中画了一条小fbo(500x500)的线,然后将小fbo画在大fbo(1000x1000)上。大fbo上的线似乎在左下角而不是左上角。
#include <QGuiApplication>
#include <QOffscreenSurface>
#include <QOpenGLFramebufferObject>
#include <QOpenGLContext>
#include <QPainter>
#include <QOpenGLPaintDevice>
#include <QDebug>
int main(int argc, char *argv[])
{
QGuiApplication a(argc, argv);
QSurfaceFormat format;
format.setSamples(8);
QOffscreenSurface* surface = new QOffscreenSurface;
surface->setFormat(format);
surface->create();
QOpenGLContext* context = new QOpenGLContext;
if(!context->create()){
qDebug() << "Failed to create context";
}
context->makeCurrent(surface);
auto smallFbo = new QOpenGLFramebufferObject(QSize(500,500));
auto largeFbo = new QOpenGLFramebufferObject(QSize(1000,1000));
smallFbo->bind();
QOpenGLPaintDevice paintDevice(smallFbo->size());
QPainter painter(&paintDevice);
painter.drawLine(QLine(0,0,400,400));
smallFbo->release();
// save small fbo to disk
smallFbo->toImage().save("/home/Desktop/smallfbo.png");
// blit the frame buffers
QOpenGLFramebufferObject::blitFramebuffer(largeFbo, QRect(0,0,500,500), smallFbo, QRect(0,0,500,500));
// save large fbo to disk
largeFbo->toImage().save("/home/Desktop/largefbo.png");
return 0;
}
最佳答案
据我了解,此功能是您需要的一切
QRect to_OpenGL_coordinates(const QRect& qt_coordinateRect, const QOpenGLFramebufferObject& fbo){
return QRect(qt_coordinateRect.x(), fbo.height() - qt_coordinateRect.y(),
qt_coordinateRect.width(), qt_coordinateRect.height() * -1);
}
像这样使用// blit the frame buffers
QOpenGLFramebufferObject::blitFramebuffer(largeFbo, to_OpenGL_coordinates(QRect(0,0,500,500), *largeFbo),
smallFbo, to_OpenGL_coordinates(QRect(0,0,500,500), *smallFbo));
结果是。关于c++ - 如何将QRect转换为OpenGL坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62545057/