Sign in to follow this  
FunLogic

VBOs and std::vector

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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this