Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QOpenGLWindow QPainter mixed rendering



  • I just do like this http://blog.qt.digia.com/blog/2014/11/20/qt-weekly-20-completing-the-offering-qopenglwindow-and-qrasterwindow/

    But I can't get the right result. This is my code:

    .h
    @
    class Render : public QOpenGLWindow
    {
    public:
    Render();
    ~Render();
    protected:
    void initializeGL();
    void paintGL();
    void resizeGL(int w ,int h);
    QOpenGLFunctions * f;
    QOpenGLBuffer * triangle;
    QOpenGLVertexArrayObject * vao;
    QOpenGLShaderProgram * program;
    QMatrix4x4 mv,p;
    QTimer * time;
    @
    [ Chris Kawa ]: Added markers around code.


  • Lifetime Qt Champion

    Hi,

    You should also share the implementation of Render, what you expect to get and what you are currently getting.



  • @
    class Render : public QOpenGLWindow
    {
    public:
    Render();
    ~Render();
    protected:
    void initializeGL();
    void paintGL();
    void resizeGL(int w ,int h);
    QOpenGLFunctions * f;
    QOpenGLBuffer * triangle;
    QOpenGLVertexArrayObject * vao;
    QOpenGLShaderProgram * program;
    QMatrix4x4 mv,p;
    QTimer * time;
    float rota;
    //QPainter * painter;

    };
    @



  • @
    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);
    time = new QTimer;
    connect(time,SIGNAL(timeout()),this,SLOT(update()));
    time->start(50);
    rota = 0;

    }

    Render::~Render()
    {

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

    f = context()->functions();
    
    
    program =  new QOpenGLShaderProgram;
    program->addShaderFromSourceCode(QOpenGLShader::Vertex,
                                     "#version 330 core \n\
                                     layout(location = 0) in vec4 vertex;\
            uniform mat4 mvp;\
                                     void main() \
            {\
                gl_Position = mvp * vertex;\
             }");
    
    program->addShaderFromSourceCode(QOpenGLShader::Fragment,
                                     "#version 330 core \n\
                                     out vec4 fragColor;\
            void main() \
    { \
        fragColor = vec4(0.0f,1.0f,0.0f,1.0f);\
    }");
    program->link();
    
    vao = new QOpenGLVertexArrayObject;
    vao->create();
    vao->bind();
    triangle = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
    triangle->create();
    triangle->bind();
    
    triangle->setUsagePattern(QOpenGLBuffer::StaticDraw);
    triangle->allocate(tri,sizeof(tri));
    program->enableAttributeArray(0);
    program->setAttributeBuffer(0,GL_FLOAT,0,4,0);
    f->glEnable(GL_DEPTH_TEST);
    

    }
    void Render::resizeGL(int w ,int h)
    {
    p.setToIdentity();
    p.perspective(35.0f,float(w)/float(h),1.0f,30.0f);
    }
    void Render::paintGL()
    {
    program->bind();
    f->glClearColor(0.5,0.5f,0.5f,1.0f);
    f->glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    f->glViewport(0,0,width(),height());

    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);
    program->setUniformValue("mvp",p*mv);
    vao->bind();
    f->glDrawArrays(GL_TRIANGLES,0,3);
    rota=rota+1.5;
    rota=rota>360.0?0:rota;
    
    
    QPainter pp(this);
    
    pp.drawText(10,20,"dsdsds");
    update();
    

    }
    @



  • [quote author="SGaist" date="1418256231"]Hi,

    You should also share the implementation of Render, what you expect to get and what you are currently getting.[/quote]

    my code have been posted.I don't know why i can't post it completely....

    I do as the blog(link i have posted) write.But I can't get the right result.Only have the background color.What's Wrong whith my code?


Log in to reply