VBOs and std::vector

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

Recommended Posts

Hi I'm trying to compile a Vertex Buffer Object from vertex data stored in a std::vector<>. It's causing me a lot of problems, and no matter what I pass into glBufferDataARB() I cannot get it to work:
void compile_mesh_vbo(amf_mesh_data* mesh)
{
glGenBuffersARB(1, &mesh->id);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, mesh->id);
glBufferDataARB(/* Don't know what I'm doing here */);
return;
}


The mesh object contains an unsigned int 'id' and also has a std::vector<> of vertices which are instances of a vertex struct. Thanks for the help!

Share on other sites
glBufferData just needs an array of floats (your vertex positions) and the number of elements in the array. You can store the data in a vector no problem, but it needs to be a type that OpenGL can understand such as float, not a user defined type such as "vertex struct". For example, in my code I call glBufferData like this:

glBufferDataARB( GL_ARRAY_BUFFER_ARB, vertices.size()*sizeof(float), &(vertices[0]), GL_STATIC_DRAW_ARB);

where "vertices" is a std::vector of floats. So you either need to contruct an array of floats from your vector of vertex structs and then pass that in to glBufferData (you can immediately delete it after the call), or change your mesh structure so that it just keeps a vector of floats instead of vertex structs.

Share on other sites
Cheers buddy, I'll give it a shot!

Share on other sites
It occurs to me also that (barring packing) you could use the same trick with your structure; you'd just have to set up stride information.

Share on other sites
infact, what Rav said is closer to the truth; glBufferData doesn't expect data in any format, you give it a void pointer and tell it how many bytes to copy, the data it copies however could be anything.

It's only when it comes to drawing said data that the types need to be defined.

So, you could have the following vertex structure;
[souce]
struct vertex
{
float x, y, z;
unsigned char r, g, b, a;
float u, v;
}
[/source]

if this is stored in a vector then you can still pass it via glBufferData as before, with one minor change;

glBufferDataARB( GL_ARRAY_BUFFER_ARB, vertices.size()*sizeof(vertex), &(vertices[0]), GL_STATIC_DRAW_ARB);

When you come to draw it you just make sure your offset and pointers point to the correct locations.