2016-11-30 3 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);」とコメントしてくださいすべて正常に動作します。しかし、私のシェイダーは起動しません。

そして

#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 

と最も単純例QOpenGLWidgetサブクラス。

#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/)に取り組んでいます。だからおそらく次のリリースが可能になるでしょう。

現在、この問題を回避する方法はQImageのに描き、その後、OGLでテクスチャとしてそのイメージを描画するためにQPainterのを使用することです。

ここに私がまとめた概念の証明があります。 生産コードでこれをしないでください。クワッドにシェーダ+ 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); 
関連する問題