2016-11-30 48 views
-1

我想在我的QOpenGLWidget中使用QPainter。但它只適用於3.0 OpenGL版本。問題是什麼?如何在OpenGL 3.3格式中使用QPainter?

這是我的代碼。

#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(); 
} 

如果我會評論「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 

和simpliest例子。

#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()); 
} 
+0

你有什麼硬件和驅動程序?他們是否支持OpenGL 3.3? – ybungalobill

回答

0

我剛剛有同樣的問題。基本上,QPainter設計用於OpenGL ES 2.0。 3.3核心桌面OpenGL配置文件與ES 2.0不兼容。如果你使用3.3核心的OpenGL配置文件,那麼你不能在它上面使用QPainter(至少不能在OS X上)。

但問題是顯然正在制定(https://codereview.qt-project.org/#/c/166202/)。所以也許下次發佈這將是可能的。

目前,解決此問題的方法是使用QPainter繪製到QImage,然後將該圖像作爲OGL中的紋理繪製。

這是我剛纔放在一起的一個概念證明。 在生產代碼不要這樣做。設置着色器+ VAO(附帶VBO)爲一個四邊形。然後使用glDrawArrays和適當的變換將紋理放置在屏幕上。

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);