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 Updated to NodeBB v4.3 + New Features

Update OpenGL buffer data using memcpy

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.4k Views 2 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.
  • D Offline
    D Offline
    dalishi
    wrote on last edited by dalishi
    #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();
    
    jsulmJ 1 Reply Last reply
    0
    • D dalishi

      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();
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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
      0
      • D Offline
        D Offline
        dalishi
        wrote on last edited by dalishi
        #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();
            ....
        }
        
        jsulmJ 1 Reply Last reply
        0
        • Prince_0912P Offline
          Prince_0912P Offline
          Prince_0912
          wrote on 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
          0
          • jsulmJ jsulm

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

              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();
                  ....
              }
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • Prince_0912P Prince_0912

                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 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
                • Prince_0912P Offline
                  Prince_0912P Offline
                  Prince_0912
                  wrote on last edited by
                  #8

                  @dalishi Ok i glad that you track down fault in your 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