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. Update OpenGL buffer data using memcpy
Forum Update on Monday, May 27th 2025

Update OpenGL buffer data using memcpy

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.4k 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.
  • D Offline
    D Offline
    dalishi
    wrote on 11 Jun 2018, 05:04 last edited by dalishi 6 Nov 2018, 05:09
    #1

    Hi,

    I'm using QOpenGLWidget in Qt5.10.1 drawing objects only when the vertices change. I have a clean() function connected to a button that can be used to clean the scene. Please note I only memcpy the vertices data when data comes (see the code below: if (data_comes) {...}), Problem is: even when there is no data comes, my clean button still works, i.e. the scene has been cleaned. I have tested, when I click clean button, the program does not go into if(data_comes) loop, meaning no vertices data is memcpied. Then why the scene is still cleaned?

    In my initializeGL():

        m_vao.create();
        if (m_vao.isCreated())
            m_vao.bind();
    
        m_vboPos.create();
        if (m_vboPos.isCreated()) {
            m_vboPos.setUsagePattern(QOpenGLBuffer::DynamicDraw);
            m_vboPos.bind();
            m_vboPos.allocate(NUM_SAMPLING * sizeof(QVector3D));
            
            glVertexAttribPointer(m_posAttr, 3, GL_FLOAT, GL_FALSE, 0, 0);
            glEnableVertexAttribArray(m_posAttr);
    
            m_vboPos.release();
    
        }
    
    

    In my paintGL():

        if (data_comes) {
            m_vboPos.bind();
            GLfloat* PosBuffer = (GLfloat*) (m_vboPos.map(QOpenGLBuffer::WriteOnly));
            if (PosBuffer != (GLfloat*) NULL) {
                memcpy(PosBuffer, m_Vertices.constData(), m_Vertices.size() * sizeof(QVector3D));
            m_vboPos.unmap();
            m_vboPos.release();
        } else {
          // Handle not being able to map the buffer
        }
    
        // Draw
        m_vao.bind();
        glDrawArrays(GL_LINE_STRIP, 0, m_Vertices.size());
    
    

    In my clean():

        m_Vertices.clear();
    
    J 1 Reply Last reply 11 Jun 2018, 05:35
    0
    • D dalishi
      11 Jun 2018, 05:04

      Hi,

      I'm using QOpenGLWidget in Qt5.10.1 drawing objects only when the vertices change. I have a clean() function connected to a button that can be used to clean the scene. Please note I only memcpy the vertices data when data comes (see the code below: if (data_comes) {...}), Problem is: even when there is no data comes, my clean button still works, i.e. the scene has been cleaned. I have tested, when I click clean button, the program does not go into if(data_comes) loop, meaning no vertices data is memcpied. Then why the scene is still cleaned?

      In my initializeGL():

          m_vao.create();
          if (m_vao.isCreated())
              m_vao.bind();
      
          m_vboPos.create();
          if (m_vboPos.isCreated()) {
              m_vboPos.setUsagePattern(QOpenGLBuffer::DynamicDraw);
              m_vboPos.bind();
              m_vboPos.allocate(NUM_SAMPLING * sizeof(QVector3D));
              
              glVertexAttribPointer(m_posAttr, 3, GL_FLOAT, GL_FALSE, 0, 0);
              glEnableVertexAttribArray(m_posAttr);
      
              m_vboPos.release();
      
          }
      
      

      In my paintGL():

          if (data_comes) {
              m_vboPos.bind();
              GLfloat* PosBuffer = (GLfloat*) (m_vboPos.map(QOpenGLBuffer::WriteOnly));
              if (PosBuffer != (GLfloat*) NULL) {
                  memcpy(PosBuffer, m_Vertices.constData(), m_Vertices.size() * sizeof(QVector3D));
              m_vboPos.unmap();
              m_vboPos.release();
          } else {
            // Handle not being able to map the buffer
          }
      
          // Draw
          m_vao.bind();
          glDrawArrays(GL_LINE_STRIP, 0, m_Vertices.size());
      
      

      In my clean():

          m_Vertices.clear();
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 11 Jun 2018, 05:35 last edited by
      #2

      @dalishi said in Update OpenGL buffer data using memcpy:

      data_comes

      Where do you set it to true and where do you set it to false?

      D 1 Reply Last reply 11 Jun 2018, 06:48
      0
      • D Offline
        D Offline
        dalishi
        wrote on 11 Jun 2018, 06:17 last edited by dalishi 6 Nov 2018, 06:18
        #3

        Hi Jsulm,
        In my paintGL(), I have a function to update the vertices data, which listens to another thread. If data comes, this function updates the vertices and returns true. If no data comes, it returns false.

        void MyOpenGL::paintGL() {
            ....
            bool data_comes = updateVertices();
            ....
        }
        
        J 1 Reply Last reply 11 Jun 2018, 06:49
        0
        • P Offline
          P Offline
          Prince_0912
          wrote on 11 Jun 2018, 06:31 last edited by
          #4

          In if condition you have to disable the button when no data comes that might be helpful to you.

          D 1 Reply Last reply 11 Jun 2018, 06:49
          0
          • J jsulm
            11 Jun 2018, 05:35

            @dalishi said in Update OpenGL buffer data using memcpy:

            data_comes

            Where do you set it to true and where do you set it to false?

            D Offline
            D Offline
            dalishi
            wrote on 11 Jun 2018, 06:48 last edited by
            #5

            @jsulm I think i found why. In my clean(), the m_Vertices.size() becomes 0 and then glDrawArrays(GL_LINE_STRIP, 0, m_Vertices.size()); will draw 0 points.

            1 Reply Last reply
            0
            • D dalishi
              11 Jun 2018, 06:17

              Hi Jsulm,
              In my paintGL(), I have a function to update the vertices data, which listens to another thread. If data comes, this function updates the vertices and returns true. If no data comes, it returns false.

              void MyOpenGL::paintGL() {
                  ....
                  bool data_comes = updateVertices();
                  ....
              }
              
              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 11 Jun 2018, 06:49 last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • P Prince_0912
                11 Jun 2018, 06:31

                In if condition you have to disable the button when no data comes that might be helpful to you.

                D Offline
                D Offline
                dalishi
                wrote on 11 Jun 2018, 06:49 last edited by
                #7

                @Prince_0912 Thanks. I should say "no new data comes". With the old data, i still want to have clean() function.

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  Prince_0912
                  wrote on 11 Jun 2018, 06:51 last edited by
                  #8

                  @dalishi Ok i glad that you track down fault in your code.

                  1 Reply Last reply
                  0

                  1/8

                  11 Jun 2018, 05:04

                  • Login

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