c++ - 如何将QRect转换为OpenGL坐标?

标签 c++ qt opengl

我有一个应用程序,其中需要将几个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));
结果是。
enter image description here

关于c++ - 如何将QRect转换为OpenGL坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62545057/

相关文章:

Android - C++ 在 cygwin/android-ndk 中编译错误

java - jtype (JNI) 和 C/C++ 的类型有什么区别?

c++ - 自动包含所需的 C++ header IDE?

c++ - 为什么设计器生成的嵌入为 "Aggregation"的 UI 类无法实现自定义插槽?

c++ - 每像素照明黑点/奇怪的伪影

c++调试断言HTTP请求失败

c++ - 生产者/消费者设计 - 在 Qt 中跨线程共享队列变量

xcode - 是否有任何 opengl 示例以便我可以在 MacOS 上运行?

c++ - 如何使用 QT、OpenGL、C++ 和 Linux 更改屏幕分辨率?

c++ - 平凡与非平凡类型的复制省略差异