saman_artorious
23rd June 2013, 12: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.
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.