Hello, I want to convert vertices and colors variables to Vertex Buffer Objects (VBO). I managed to write the below code, though when program runs after a while it jumps out. Moreover, how can I make sure the VBO is created and vertices & colors are transfered to GPU? I also need to update the colors values. How can I do that when it is transfered to GPU? (but vertices is constant).
void PlanPositionIndicator::render(
QGLShaderProgram* shaderProgram)
{
qDebug() << vertices.size() << " " << sizeof(QVector3D);
if(!initialized)
{
GLuint vertexId = shaderProgram->attributeLocation("vertex");
GLuint colorId = shaderProgram->attributeLocation("color");
glGenBuffers(2, VBO_ID);
glGenVertexArrays(1, &VAO_ID);
glBindVertexArray(VAO_ID);
glBindBuffer(GL_ARRAY_BUFFER, VBO_ID[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(QVector3D)*vertices.size(), vertices.constData(), GL_STATIC_DRAW);
glVertexAttribPointer(vertexId, 3, GL_FLOAT, GL_FALSE, 0, 0);// param - 1 ->0
glEnableVertexAttribArray(vertexId);
glBindBuffer(GL_ARRAY_BUFFER, VBO_ID[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*vertices.size(), colors, GL_DYNAMIC_DRAW);
glVertexAttribPointer(colorId, 1, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(colorId);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
glBindVertexArray(VAO_ID);
int vertexLocation = shaderProgram->attributeLocation("vertex");
int colorLocation = shaderProgram->attributeLocation("color");
shaderProgram->enableAttributeArray(vertexLocation);
shaderProgram->enableAttributeArray(colorLocation);
// shaderProgram->setAttributeBuffer(vertexLocation, GL_FLOAT , 0, 3, 0);
// shaderProgram->setAttributeBuffer(colorLocation , GL_FLOAT , 0, 1, 0);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDrawArrays(GL_TRIANGLES, 0, 6*numVerts);
// glDrawElements(GL_TRIANGLES, vertices.size(), GL_FLOAT, vertices.constData());
}