View Full Version : qt opengl coloring cube example-vbo edited
saman_artorious
30th June 2013, 12:12
To write a vbo sample I edited the qt opengl coloring cube example. What I did is to create a vao and two vbos for vertices and colors variables. Unfortunately, when program runs, it doesn't render anything. Attached file is the program, as I am only using Linux, you may not worry about any virus detections.
I would appreciate if you run this example on your system and try if you can make it give the desired output.
wysota
30th June 2013, 15:58
You are not linking the buffers to the shader program you are using anywhere. Are you sure this is just a modified version of an example?
saman_artorious
30th June 2013, 17:52
You are not linking the buffers to the shader program you are using anywhere.
I changed the coloring example and added vbo to it. I do not understand your statement. I already linked vertices and colors variables to vertex and color respectively in the shader program. I do not understand where I am doing wrong.
could get more into the details please.
wysota
30th June 2013, 18:02
I already linked vertices and colors variables to vertex and color respectively in the shader program.
Where exactly did you do that? Which file, which line?
saman_artorious
30th June 2013, 18:14
Please check the file
glwidget.cpp.
The linking of shader programs is happening inside
initializeGL()
Rendering and initialization(if not initialized)is done in the
paintGL() function.
if(!initialized)
{
GLuint vertexId = shaderProgram.attributeLocation("vertex");
GLuint colorId = shaderProgram.attributeLocation("color");
glGenVertexArrays(1, &VAO_ID);
glBindVertexArray(VAO_ID);
glBindBuffer(GL_ARRAY_BUFFER, VBO_ID[0]);
glVertexAttribPointer(vertexId, 3, GL_FLOAT, GL_FALSE, sizeof(vertices), 0);
glBufferData(GL_ARRAY_BUFFER, sizeof(QVector3D)*vertices.size(), vertices.constData(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, VBO_ID[1]);
glVertexAttribPointer(colorId, 3, GL_FLOAT, GL_FALSE, sizeof(colors), 0);
glBufferData(GL_ARRAY_BUFFER, sizeof(QVector3D)*colors.size(), colors.constData(), GL_STATIC_DRAW);
initialized = true;
}
wysota
30th June 2013, 22:13
Again, which line in your opinion binds the VBO to the shader? Because none of what you posted in this post does. This code only fills the buffer.
saman_artorious
1st July 2013, 09:45
Again, which line in your opinion binds the VBO to the shader? Because none of what you posted in this post does. This code only fills the buffer.
Here:
int vertexLocation = shaderProgram->attributeLocation("vertex");
shaderProgram->enableAttributeArray(vertexLocation);
int colorLocation = shaderProgram->attributeLocation("color");
shaderProgram->enableAttributeArray(colorLocation);
wysota
1st July 2013, 10:03
I think you forgot to read the docs for "enableAttributeArray". This only tells the shader to expect an array instead of a single value. It does not tell it what data to assign to this array. I already told you in the other thread a couple of days ago which function you need to use. Now RTFM for enableAttributeArray and read the other thread again.
saman_artorious
1st July 2013, 12:20
I added the two lines concerning setAttributeBuffer() to my code. However, it gives me segmentation fault when after glDrawArray() is called. Though it executes fine with
glDrawElements(GL_TRIANGLES, vertices.size(), GL_FLOAT, vertices.constData()); but does not render anything on the screen.
shaderProgram->setAttributeBuffer(vertexLocation, GL_FLOAT , 0, 3, 0);
shaderProgram->setAttributeBuffer(colorLocation , GL_FLOAT , 0, 1, 0);
whole render part:
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, numVerts);
could you please run this code on your system, I don't know exactly what I am missing. Although, I added setArrtibuteBuffers.
wysota
1st July 2013, 15:48
Make sure you are setting up the buffers correctly. I would also advise to use Qt classes for VBO manipulation.
saman_artorious
4th July 2013, 10:49
Make sure you are setting up the buffers correctly. I would also advise to use Qt classes for VBO manipulation.
Thanks for your guidance. I solved the problem. The correct code for what I aimed for is:
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)*verticesSize, 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)*verticesSize, colors, GL_STATIC_DRAW);
glVertexAttribPointer(colorId, 1, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(colorId);
glBindBuffer(GL_ARRAY_BUFFER, 0);
vertices.clear();
free(colors);
initialized = true;
}
glBindVertexArray(VAO_ID);
int vertexLocation = shaderProgram->attributeLocation("vertex");
int colorLocation = shaderProgram->attributeLocation("color");
shaderProgram->enableAttributeArray(vertexLocation);
shaderProgram->enableAttributeArray(colorLocation);
glEnableClientState(GL_VERTEX_ARRAY);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDrawArrays(GL_TRIANGLES, 0, 6*numVerts);
glFlush();
glBindVertexArray(0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.