PDA

View Full Version : QGLBuffer VBO implementation



saman_artorious
23rd June 2013, 13:08
I want to transfer vertices & colors variables in my drawing GPU. Unfortunately, it cannot render when the program runs.

definitions is as follow:


QVector<QVector3D> vertices;
float* colors;

QGLBuffer* m_bufferData;
QGLBuffer* m_colorData;
float* m_color;

Here is what I do iniside initializeGL(), As vertices is built once and used forever, I do not allocate and map it. However, colors change regularly in the code, so I mapped colors to m_color in the code.


void PlanPositionIndicator::QtVBO()
{
m_bufferData = new QGLBuffer(QGLBuffer::VertexBuffer);
m_bufferData->create();
m_bufferData->bind();
m_bufferData->setUsagePattern(QGLBuffer::DynamicDraw);
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);
m_colorData->create();
m_colorData->bind();
m_colorData->setUsagePattern(QGLBuffer::DynamicDraw);
m_colorData->allocate(6*sizeof(float)* ANGLE_COUNT*RANGE_COUNT);
m_colorData->release();
m_color = (float*)m_colorData->map(QGLBuffer::ReadWrite);
}


So far so good, next, in the paintGL(), after setting up matrices and binging the shader program, I call the render function to render my drawing as following:


void PlanPositionIndicator::render(QGLShaderProgram* shaderProgram)
{
glEnableClientState(GL_VERTEX_ARRAY);
if (m_bufferData->bind ()) {

m_bufferData->write(0,vertices.constData(),sizeof(vertices));

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer( 3, GL_FLOAT, 0, &vertices);

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);
m_bufferData->release();
}
}


Finally, I attempt to change values of colors array. Firstly I did it with map() & unmap() but it gave me segmentation fault. Next, I used write(), it runs well, but does not render anything.



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 = (float*)glBuffer->map (QGLBuffer::ReadWrite);

float a[5];
glBuffer->write(0,m_color,100);

// 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 ();
}


Any suggestions would be appreciable.

saman_artorious
25th June 2013, 12:26
I still do not understand why the allocated mapped m_color buffer above gives me a segmentation. I am dubious if my approach is correct. Has anyone done this before?

wysota
25th June 2013, 18:02
Your drawing code doesn't use the buffers anywhere.

saman_artorious
25th June 2013, 20:15
what you mean!?

wysota
25th June 2013, 20:58
I mean you create the buffers but don't use them anywhere. You are using the original QVector:


shaderProgram->setAttributeArray("vertex", vertices.constData());

saman_artorious
26th June 2013, 07:50
I mean you create the buffers but don't use them anywhere.


Here, I set up their relation:

m_bufferData->write(0,vertices.constData(),sizeof(vertices));

And pass the original variable in the function you mentioned above.

wysota
26th June 2013, 07:53
No, here you copy data from the vector to the buffer. But then you use the vector again instead of the buffer. You should use setAttributeBuffer and not setAttributeArray.