C++ 如何将QRect转换为OpenGL坐标?
我有一个应用程序,其中一个多个C++ 如何将QRect转换为OpenGL坐标?,c++,qt,opengl,C++,Qt,Opengl,我有一个应用程序,其中一个多个QOpenGLFramebufferObject需要被blit到一个更大的QOpenGLFramebufferObject上。为了做到这一点,我想我可以简单地使用Qt中的标准坐标将fbo“粘贴”到大型fbo上。需要一个“源”rect和一个“目标”rect。例如,如果我的大型固定基地运营商是1000x1000,而我的小型固定基地运营商是500x500。然后,我只需要执行以下操作: auto sourceRect = QRect(0,0,500,500); // i h
QOpenGLFramebufferObject
需要被blit到一个更大的QOpenGLFramebufferObject
上。为了做到这一点,我想我可以简单地使用Qt中的标准坐标将fbo“粘贴”到大型fbo上。需要一个“源”rect和一个“目标”rect。例如,如果我的大型固定基地运营商是1000x1000,而我的小型固定基地运营商是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)
}
}
autosourcerect=QRect(0,0500500);
//我这里是我的FBO网格中的一行
//j将是专栏
对于(int i=0;i<2;i++){
对于(int j=0;j<2;j++){
auto targetRect=QRect(i*500,j*500500);
//tileFbo是小型FBO
QOpenGLFramebufferObject::blitFramebuffer(大型FBO、targetRect、tileFbo、sourceRect)
}
}
但是,这不起作用,因为QOpenGLFramebufferObject似乎使用OpenGL坐标。那么,如何将QRect转换为使用OpenGL坐标呢
下面是一个示例,我在一个小fbo(500x500)上画一条线,然后在一个大fbo(1000x1000)上blit小fbo。大型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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
QGUI应用程序a(argc、argv);
QSurfaceFormat格式;
格式。设置样本(8);
QOffscreenSurface*表面=新的QOffscreenSurface;
曲面->设置格式(格式);
曲面->创建();
QOpenGLContext*上下文=新QOpenGLContext;
如果(!context->create()){
qDebug()使电流(表面);
auto smallFbo=新QOpenGLFramebufferObject(QSize(500500));
auto largeFbo=新QOPENGLFRAMEFBUFFEROBJECT(QSize(10001000));
smallFbo->bind();
QOpenGLPaintDevice paintDevice(smallFbo->size());
QPainter painter(油漆工和油漆设备);
绘制线(QLine(0,0400));
smallFbo->release();
//将小型fbo保存到磁盘
smallFbo->toImage().save(“/home/Desktop/smallFbo.png”);
//blit帧缓冲区
QOpenGLFramebufferObject::blitFramebuffer(大FBO,QRect(0,0500500),小FBO,QRect(0,0500500));
//将大型fbo保存到磁盘
largeFbo->toImage().save(“/home/Desktop/largeFbo.png”);
返回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));
结果,
据我所知,此功能是您所需要的一切
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));
结果,
在循环的第一次迭代中,targetRect是
QRect(0,0,0,0)
是否正常?为了理解该函数的机制,最好尝试不使用循环,只需尝试将一个fbo与另一个具有特定QRect的fbo进行blit。实际上,第一个rect将具有坐标(0,0500)因此,在qt标准坐标中,它将位于左上角,大小为500。我也尝试过不使用循环,但我无法让它工作。你所说的“…QOpenGLFramebufferObject似乎使用OpenGL坐标”是什么意思
?即使这是真的,源和目标也需要由QRectF
而不是QRect
指定。因为根据我的测试,fbo被绘制在看似“随机”的位置(根据我的测试,(0,0)坐标似乎是大型fbo的左下角)。为什么QRectF
会有任何不同?在循环的第一次迭代中,targetRect是QRect(0,0,0,0)
是正常的吗?为了理解该函数的机制,最好尝试不使用循环,只需尝试用特定的QRect将一个fbo blit到另一个fbo。实际上,第一个矩形将具有坐标(0,0500500)因此,在qt标准坐标中,它将位于左上角,大小为500。我也尝试过不使用循环,但我无法让它工作。你所说的“…QOpenGLFramebufferObject似乎使用OpenGL坐标”是什么意思
?即使这是真的,源和目标也需要由QRectF
而不是QRect
指定。因为根据我的测试,fbo被绘制在看似“随机”的位置(根据我的测试,(0,0)坐标似乎是大型fbo的左下角).为什么QRectF
会有什么不同?