QOpenGLWidget和QPainter,无法同时渲染2D和3D

时间:2014-12-11 12:29:03

标签: c++ qt opengl

我的Qt版本是5.4。我测试了新的类QOpenGWidget,我做了Qt助手:把opengl代码放在QPainter :: beginNativePainting(); / QPainter :: endNativePainting(); ,但2D没有显示。我不知道我的代码有什么问题。这是我的代码:

·H

class Render : public QOpenGLWidget
{
public:
    Render();
    ~Render();
protected:
    void initializeGL();
    void paintGL();
    void resizeGL(int  w ,int h);
    QOpenGLFunctions_3_3_Core * f;
    QOpenGLBuffer * triangle;
    QOpenGLVertexArrayObject * vao;
    QOpenGLShaderProgram * program;
    QMatrix4x4 mv,p;
    QTimer * time;
    float rota;
    QPainter * painter;
    bool needInit;
    //void paintEvent(QPaintEvent *e);

};

的.cpp

#include "render.h"
GLfloat tri[] =
{
    0.0f,1.0f,-1.0f,1.0f,
    1.0f,1.0f,-1.0f,1.0f,
    1.0f,0.0f,-1.0f,1.0f,

};
Render::Render()
{
    QSurfaceFormat format;
    format.setDepthBufferSize(24);
    format.setStencilBufferSize(8);
    // format.setVersion(3,3);
    //format.setProfile(QSurfaceFormat::CoreProfile);

    setFormat( format);
    rota = 1.5;
    needInit = true;
    //setAutoFillBackground(false);
    time =  new QTimer;
    connect(time,SIGNAL(timeout()),this,SLOT(update()));
    time->start(50);
}

Render::~Render()
{

}
void Render::initializeGL()
{
    f = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
    f->initializeOpenGLFunctions();

    //f = context()->functions();

}
void Render::resizeGL(int w ,int h)
{

    p.setToIdentity();
    p.perspective(35.0f,float(w)/float(h),1.0f,30.0f);

}

void Render::paintGL()
{
    QPainter pntr(this);
    // painter->setViewport(50,50,100,100);
    pntr.beginNativePainting();
    QOpenGLShaderProgram  programs ;
    programs.addShaderFromSourceCode(QOpenGLShader::Vertex,
                                     "#version 330 core \n\
                                    layout(location = 0) in vec4 vertex;\
           uniform mat4 mvp;\
   void main() \
   {\
       gl_Position = mvp * vertex;\
   }");

    programs.addShaderFromSourceCode(QOpenGLShader::Fragment,
                                     "#version 330 core \n\
                                    out vec4 fragColor;\
           void main() \
   { \
       fragColor = vec4(0.0f,1.0f,0.0f,1.0f);\
   }");
    programs.link();

    QOpenGLVertexArrayObject vaos;
    vaos.create();
    f->glBindVertexArray(vaos.objectId());
    QOpenGLBuffer triangles(QOpenGLBuffer::VertexBuffer);
    triangles.create();
    f->glBindBuffer(GL_ARRAY_BUFFER,triangles.bufferId());
    triangles.setUsagePattern(QOpenGLBuffer::DynamicDraw);
    triangles.allocate(tri,sizeof(tri));
    programs.enableAttributeArray(0);
    programs.setAttributeBuffer(0,GL_FLOAT,0,4,0);

    f->glEnable(GL_DEPTH_TEST);

    needInit = false;

    // this->makeCurrent();
    f->glViewport(0,0,width(),height());
    f->glClearColor(1.0f,0.5f,0.5f,1.0f);
    f->glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);

    programs.bind();
    mv.setToIdentity();

    mv.lookAt(QVector3D(0.0f,0.0f,5.0f),QVector3D(0.0f,0.0f,0.0f),QVector3D(0.0f,1.0f,0.0f));
    mv.rotate(0.5+rota,0,1,0);
    programs.setUniformValue("mvp",p*mv);
    f->glBindVertexArray(vaos.objectId());
    f->glDrawArrays(GL_TRIANGLES,0,3);
    rota=rota+1.5;

    pntr.endNativePainting();

    pntr.drawText(50,100,"Look This!");

}

2 个答案:

答案 0 :(得分:3)

QPainter仅在QSurfaceFormat format QOpenGLWidget未设置为&#34;核心配置文件&#34;时才有效。

以下是如何从QSurfaceFormat format功能设置main :(请注意,您必须从Render构造函数中删除格式定义)

int main(int argc, char *argv[])
{

    QApplication a(argc, argv);

    QSurfaceFormat  format;

    format.setSamples(4);
    format.setDepthBufferSize(24);
    format.setStencilBufferSize(8);
    //format.setVersion(3,2);
    //format.setProfile(QSurfaceFormat::CoreProfile);
    QSurfaceFormat::setDefaultFormat(format);


    MainWindow w;
    w.show();

    return a.exec();
}

答案 1 :(得分:1)

在开始绘制2D内容之前,尝试禁用深度测试。

...
glDisable(GL_DEPTH_TEST);
pntr.endNativePainting();
...

我遇到了类似的问题,每当我更新场景时,2D内容都会消失。添加这一行解决了它。