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. QOpenGLWindow QPainter mixed rendering
QtWS25 Last Chance

QOpenGLWindow QPainter mixed rendering

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.9k Views
  • 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 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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sdaujiaojiao
        wrote on last edited by
        #3

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

        };
        @

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sdaujiaojiao
          wrote on last edited by
          #4

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

          }
          @

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sdaujiaojiao
            wrote on last edited by
            #5

            [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?

            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