I want to define vertices and colors vectors in my program to VBO. I am confused with the implementation of VBO using QGLBuffer in QT. the program logic seems correct. However, when it runs, it gives me segmentation fault. Could anyone familiar with QT QGLBuffer guide me solve this problem please.
definition:
float* GetColorBuffer()
{
return colors;
}
QGLBuffer* Get_mColorData()
{
return m_colorData;
}
float* Get_mColor()
{
return m_color;
}
GLuint vbods[2];
QVector<QVector3D> vertices;
float* colors;
QGLBuffer* m_bufferData;
QGLBuffer* m_colorData;
float* m_color;
initialization
void PlanPositionIndicator::QtVBO()
{
m_bufferData = new QGLBuffer(QGLBuffer::VertexBuffer);
m_bufferData->create();
m_bufferData->bind();
//m_bufferData->setUsagePattern(QGLBuffer::StaticDraw); //staticDraw
m_bufferData->allocate(6*sizeof(float)* ANGLE_COUNT*RANGE_COUNT);
m_bufferData->release();
//m_data = (QVector<QVector3D>*)m_bufferData->map (QGLBuffer::ReadWrite);
m_colorData = new QGLBuffer(QGLBuffer::VertexBuffer);//static
m_colorData->create();
m_colorData->bind();
//m_colorData->setUsagePattern(QGLBuffer::StaticDraw);
m_colorData->allocate(6*sizeof(float)* ANGLE_COUNT*RANGE_COUNT);
m_color = (float*)m_colorData->map(QGLBuffer::ReadWrite);
m_colorData->unmap();
m_colorData->release();
}
this is how I render to screen:
glEnableClientState( GL_VERTEX_ARRAY);
glEnableClientState( GL_COLOR_ARRAY);
if (m_bufferData->bind ()) {
m_bufferData->write(0,vertices.constData(),sizeof(vertices));
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer( 3, GL_FLOAT, 0, &vertices);
if(m_colorData->bind())
{
shaderProgram->setAttributeArray("vertex", vertices.constData());
shaderProgram->enableAttributeArray("vertex");
shaderProgram->setAttributeArray("color", GL_FLOAT,colors,1);
shaderProgram->enableAttributeArray("color");
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
}
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
m_bufferData->release();
m_colorData->release();
}
Next, I need to change colors values in my program, for this, I need to map m_colorData to a variable, unmap it and modify the pointer array. In this part, I receive segmentation fault.
int k = segmentIndex * RANGE_COUNT * 6;
for(int i = 0; i < RANGE_COUNT; ++i)
{
float rnd = ((rand() + segmentIndex) % 10000) / 10000.0f;
QGLBuffer* glBuffer = ppi->Get_mColorData();
float* m_color; /*= ppi->Get_mColor();*/
m_color[k++] = rnd;
m_color[k++] = rnd;
m_color[k++] = rnd;
m_color[k++] = rnd;
m_color[k++] = rnd;
m_color[k++] = rnd;
glBuffer->unmap ();