Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QOpenGLWidget and QPainter ,Can't render 2D and 3D at the same time

QOpenGLWidget and QPainter ,Can't render 2D and 3D at the same time

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 826 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sdaujiaojiao
    wrote on last edited by
    #1

    I don't know why .I do as Qt Assistant:put opengl code in @QPainter::beginNativePainting ();/QPainter::endNativePainting ();@ .But I only get the 3D.
    my code:
    @
    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);

    };
    @

    @
    #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!");
    

    }
    @

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved