C++ 如何将QPaint与OpenGL 3.3格式一起使用?
我正在尝试在QOpenGLWidget中使用QPainter。但它只适用于3.0 OpenGL版本。有什么问题 这是我的密码C++ 如何将QPaint与OpenGL 3.3格式一起使用?,c++,qt,opengl,C++,Qt,Opengl,我正在尝试在QOpenGLWidget中使用QPainter。但它只适用于3.0 OpenGL版本。有什么问题 这是我的密码 #include <QApplication> #include <QMainWindow> #include "window.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); QSurfaceFormat format; format.setR
#include <QApplication>
#include <QMainWindow>
#include "window.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSurfaceFormat format;
format.setRenderableType(QSurfaceFormat::OpenGL);
format.setProfile(QSurfaceFormat::CoreProfile);
format.setVersion(3,3);
// Set widget up
Window *widget = new Window;
widget->setFormat(format);
// Set the window up
QMainWindow window;
window.setCentralWidget(widget);
window.resize(QSize(800, 600));
window.show();
return app.exec();
}
#包括
#包括
#包括“window.h”
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
QSurfaceFormat格式;
setRenderableType(QSurfaceFormat::OpenGL);
format.setProfile(QSurfaceFormat::CoreProfile);
格式.设置版本(3,3);
//设置小部件
窗口*小部件=新窗口;
小部件->设置格式(格式);
//把窗户关上
qmain窗口;
setCentralWidget(小部件);
调整窗口大小(QSize(800600));
window.show();
返回app.exec();
}
如果我评论“format.setVersion(3,3);”的话,一切都会很好。但我的着色器不会启动
和QOpenGLWidget子类
#ifndef WINDOW_H
#define WINDOW_H
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
class QOpenGLShaderProgram;
class Window : public QOpenGLWidget,
protected QOpenGLFunctions
{
Q_OBJECT
// OpenGL Events
public:
void initializeGL();
void resizeGL(int width, int height);
void paintGL();
};
#endif // WINDOW_H
\ifndef窗口
#定义窗口
#包括
#包括
QOpenGLShaderProgram类;
类窗口:公共QOpenGLWidget,
受保护的QoPenglFunction
{
Q_对象
//OpenGL事件
公众:
void initializeGL();
空隙大小(内部宽度、内部高度);
void paintGL();
};
#endif//WINDOW\u H
最简单的例子
#include "window.h"
#include <QDebug>
#include <QString>
#include <QOpenGLShaderProgram>
#include "vertex.h"
#include <QPainter>
void Window::initializeGL()
{}
void Window::resizeGL(int width, int height)
{}
void Window::paintGL()
{
QPainter p(this);
p.setPen(Qt::red);
p.drawLine(rect().topLeft(), rect().bottomRight());
}
#包括“window.h”
#包括
#包括
#包括
#包括“vertex.h”
#包括
void Window::initializeGL()
{}
void Window::resizeGL(整数宽度、整数高度)
{}
空窗口::paintGL()
{
油漆工p(本);
p、 设置笔(Qt::红色);
p、 抽绳(rect().topLeft(),rect().bottomRight());
}
我也有同样的问题。基本上,QPaint设计用于OpenGL ES 2.0。3.3核心桌面OpenGL配置文件与ES 2.0不兼容。如果您使用3.3核心OpenGL配置文件,则不能将QPaint与之配合使用(至少在OS X上不能)
然而,这个问题显然正在解决()。因此,也许下一个版本将有可能实现这一点
目前,解决这一问题的方法是使用QPaint绘制QImage,然后在OGL中将该图像绘制为纹理
这是我刚刚整理的概念证明。在生产代码中,不要这样做。为四边形设置着色器+VAO(已连接VBO)。然后使用带有适当变换的GLDrawArray将纹理放到屏幕上
glClearColor(1.f,1.f,1.f,1.f);
glClear(GL_COLOR_BUFFER_BIT);
// Use QPainter to draw to a QImage and then display the QImage
QFont f{"Helvetica",18};
QStaticText txt{msg};
txt.prepare({},f);
auto dims = txt.size().toSize();
QImage buffer(dims,QImage::Format_ARGB32);
{
QPainter p(&buffer);
p.fillRect(QRect{{0,0},dims},QColor::fromRgb(255,255,255));
p.setPen(QColor::fromRgb(0,0,0));
p.setFont(f);
p.drawStaticText(0,0,txt);
}
// Blit texture to screen
// If you at all care about performance, don't do this!
// Create the texture once, store it and draw a quad on screen.
QOpenGLTexture texture(buffer,QOpenGLTexture::DontGenerateMipMaps);
GLuint fbo;
glGenFramebuffers(1,&fbo);
glBindFramebuffer(GL_READ_FRAMEBUFFER,fbo);
texture.bind();
glFramebufferTexture2D(
GL_READ_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D,
texture.textureId(),
0);
glBlitFramebuffer(
0,
dims.height(),
dims.width(),
0,
width() / 2 - dims.width() / 2,
height() / 2 - dims.height() / 2,
width() / 2 + dims.width() / 2,
height() / 2 + dims.height() / 2,
GL_COLOR_BUFFER_BIT,
GL_LINEAR);
glDeleteFramebuffers(1,&fbo);
你有什么硬件和驱动程序?他们支持OpenGL 3.3吗?