• Advertisement
Sign in to follow this  

[Solved] Having problems drawing vertex buffer objects

This topic is 2677 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys,

I've successfully set up vertex arrays and have everything rendering using glDrawElements. The problem that I'm having now is in getting vertex buffer objects to draw. I think I have it loading into GPU memory just fine, but nothing is drawing....(the entire screen goes white). I've searched around and haven't a clue as to why my implementation isn't working...could someone please help?


Here's the loading:


if (glewGetExtension("GL_ARB_vertex_buffer_object"))
{
cout << "Vertex buffer objects supported!" << endl;

//Load vertices
glGenBuffers(1, &this->vbo_vertices); //Get name/ID
glBindBuffer(GL_ARRAY_BUFFER, this->vbo_vertices); //Bind the buffer to the name
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*this->numFaces*3*3, this->vertexArray, GL_STATIC_DRAW);

//Load normals
glGenBuffers(1, &this->vbo_normals); //Get name/ID
glBindBuffer(GL_ARRAY_BUFFER, this->vbo_normals); //Bind the buffer to the name
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*this->numFaces*3*3, this->normalsArray, GL_STATIC_DRAW);

//Load UVs
glGenBuffers(1, &this->vbo_uvs); //Get name/ID
glBindBuffer(GL_ARRAY_BUFFER, this->vbo_uvs); //Bind the buffer to the name
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*this->numFaces*3*2, this->uvArray, GL_STATIC_DRAW);

//Load index
glGenBuffers(1, &this->vbo_index); //Get name/ID
glBindBuffer(GL_ARRAY_BUFFER, this->vbo_index); //Bind the buffer to the name
glBufferData(GL_ARRAY_BUFFER, sizeof(GLuint)*this->numFaces*3, this->indexArray, GL_STATIC_DRAW_ARB);

cout << "Loaded model into GPU memory" << endl;

}



Now, when it comes to drawing, the following renders nothing (renders totally white screen)...


if (glewGetExtension("GL_ARB_vertex_buffer_object"))
{
//Load vertices
glBindBuffer(GL_ARRAY_BUFFER, this->mesh->vbo_vertices);
glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
glEnableClientState(GL_VERTEX_ARRAY);

//Load normals
glBindBuffer(GL_ARRAY_BUFFER, this->mesh->vbo_normals);
glNormalPointer(GL_FLOAT, 0, BUFFER_OFFSET(0));
glEnableClientState(GL_NORMAL_ARRAY);

//Load UVs
glBindBuffer(GL_ARRAY_BUFFER, this->mesh->vbo_uvs);
glTexCoordPointer(2, GL_FLOAT, 0, BUFFER_OFFSET(0));
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glBindBuffer(GL_ARRAY_BUFFER, this->mesh->vbo_index);

glDrawElements(GL_TRIANGLES, 3*this->mesh->numFaces, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

} else {


//Drawing with vertex arrays
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glVertexPointer(3, GL_FLOAT, 0, this->mesh->vertexArray);
glNormalPointer(GL_FLOAT, 0, this->mesh->normalsArray);
glTexCoordPointer(2, GL_FLOAT, 0, this->mesh->uvArray);

glDrawElements(GL_TRIANGLES, 3*this->mesh->numFaces, GL_UNSIGNED_INT, this->mesh->indexArray);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}



Thanks so much in advance!

Leonard

[Edited by - leonardteo on October 21, 2010 1:22:40 PM]

Share this post


Link to post
Share on other sites
Advertisement
Your index buffer needs to be bound to target GL_ELEMENT_ARRAY_BUFFER, not GL_ARRAY_BUFFER.

Share this post


Link to post
Share on other sites
if you checked the man pages for glBindBuffer, you will see that the previous binding for a target in broken whenever another one is specified. With that said, why are you using separate VBO for vertex data of the same mesh? Create an interleaved VBO and use the offset to address vertex, uv, normals and so on. Also the buffer target for index data is different from that of vertex data. See OpenGL man pages on glBindBuffer and glBufferData. I could have given you the answer but that would have been too easy.

Share this post


Link to post
Share on other sites
Quote:

With that said, why are you using separate VBO for vertex data of the same mesh? Create an interleaved VBO and use the offset to address vertex, uv, normals and so on.


There's nothing wrong with his setup for data, it is just as common to use separate buffers for vertex/normal/texcoord as it is to use interleaved format. Going one way or the other may have minor performance differences depending on how the data is used, but one way is not generally better than another.

Share this post


Link to post
Share on other sites
Quote:
Original post by cgrant
if you checked the man pages for glBindBuffer, you will see that the previous binding for a target in broken whenever another one is specified. With that said, why are you using separate VBO for vertex data of the same mesh? Create an interleaved VBO and use the offset to address vertex, uv, normals and so on. Also the buffer target for index data is different from that of vertex data. See OpenGL man pages on glBindBuffer and glBufferData. I could have given you the answer but that would have been too easy.


Yes, that's a good point. I'll move it all into a single array buffer object. Thanks.

Also, I do have the Red Book and sometimes it's just missing a single word, like the GL_ELEMENT_ARRAY_BUFFER instead of the GL_ARRAY_BUFFER. Sorry for coming across so n00bish, but I really am reading the fine manual.. :)

L.

Share this post


Link to post
Share on other sites
You should always have all your vertex attributs in a single Buffer in a interleaved order. Also it is recommended to padd the overall attributs size to a multiply of 32bit;

Only when u'r going to change single attributs often pack them in single buffers.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement