Update OpenGL buffer data using memcpy
-
wrote on 11 Jun 2018, 05:04 last edited by dalishi 6 Nov 2018, 05:09
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();
-
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();
@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?
-
wrote on 11 Jun 2018, 06:17 last edited by dalishi 6 Nov 2018, 06:18
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(); .... }
-
wrote on 11 Jun 2018, 06:31 last edited by
In if condition you have to disable the button when no data comes that might be helpful to you.
-
@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?
-
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(); .... }
This post is deleted! -
In if condition you have to disable the button when no data comes that might be helpful to you.
wrote on 11 Jun 2018, 06:49 last edited by@Prince_0912 Thanks. I should say "no new data comes". With the old data, i still want to have clean() function.
-
wrote on 11 Jun 2018, 06:51 last edited by
@dalishi Ok i glad that you track down fault in your code.
1/8