# Need help with VBO

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

## Recommended Posts

Hi, I am experiencing some weird problems with my VBO generation. Here is what I do:
// Declare usable variables
int num_faces = my_mesh->NumFaces;
int num_verts_per_face = 3;
int num_elements_per_vert = 3;

// Generate buffer
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, num_faces * num_verts_per_face * num_elements_per_vert * sizeof(GLfloat), NULL, GL_STATIC_DRAW);

// Generate sub data for vertices
glBufferSubData(GL_ARRAY_BUFFER, 0, num_faces * num_verts_per_face * sizeof(GLfloat), my_mesh->Vertices);


I am reading the number of faces from my mesh (~2000) and making the buffer large enough to hold all faces x 3 vertices per face x 3 values per vertex (X, Y and Z). Now, here comes my problem. When I try to draw my mesh using the VBO, I get a crash.
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexPointer(3, GL_FLOAT, 0, (GLvoid*)NULL);
glEnableClientState(GL_VERTEX_ARRAY);

glDrawArrays(GL_TRIANGLES, 0, my_mesh->NumFaces * 3);

glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);


If I draw a smaller number of elements (i.e. decrease the third parameter for glDrawArrays) it works, but the mesh is not complete since I don't draw all elements. I've followed quite a lot of tutorials on this, but none of them work. I also did try to skip the glBufferSubData() part, but that did not yield any usable result either. Hope someone has some better insight into this. Thanks!

##### Share on other sites
shouldn't this

glBufferSubData(GL_ARRAY_BUFFER, 0, num_faces * num_verts_per_face * sizeof(GLfloat), my_mesh->Vertices);

be?

glBufferSubData(GL_ARRAY_BUFFER, 0, num_faces * num_verts_per_face * num_elements_per_vert * sizeof(GLfloat), my_mesh->Vertices);

Also, I recommend you use indexed arrays and draw with glDrawElements.

##### Share on other sites
Indexed arrays is my next step, but it turns even more complex that way. I load my meshes from the ASE format, so the data is not conformed to what VBO needs. I'll try your suggestion, thank you.

##### Share on other sites
Quote:
 Original post by bzroomshouldn't thisglBufferSubData(GL_ARRAY_BUFFER, 0, num_faces * num_verts_per_face * sizeof(GLfloat), my_mesh->Vertices);be?glBufferSubData(GL_ARRAY_BUFFER, 0, num_faces * num_verts_per_face * num_elements_per_vert * sizeof(GLfloat), my_mesh->Vertices);Also, I recommend you use indexed arrays and draw with glDrawElements.

Hrmm, the change you suggested did not work, still crashes (access violation in the second frame of drawing the mesh when calling glDrawArrays()). I feel VBO's are far too complex, I hope they fix this in OpenGL 3.0 (or better yet, have their own mesh format, but that's just a wish) :)

I'll keep at this, it should not be impossible to solve.

##### Share on other sites
Quote:
Original post by Eldritch
Quote:
 Original post by bzroom...glBufferSubData(GL_ARRAY_BUFFER, 0, num_faces * num_verts_per_face * num_elements_per_vert * sizeof(GLfloat), my_mesh->Vertices);

Hrmm, the change you suggested did not work, still crashes (access violation in the second frame of drawing the mesh when calling glDrawArrays()).

Are you sure that you are allocating and initializing enough memory? Having only the vertices for one frame rather than all frames would fit the symptoms (first frame ok, second frame from unallocated memory).
Quote:
 I feel VBO's are far too complex, I hope they fix this in OpenGL 3.0 (or better yet, have their own mesh format, but that's just a wish) :)

Not bloody likely. Vertex buffers and vertex programs allow for any conceivable mesh format, there is no reason to add complication in order to reduce flexibility.
If you resent using low-level OpenGL programming interfaces, maybe you don't need them and you should use higher level libraries such as scene graph frameworks and game engines.

##### Share on other sites
VBO's are like 10 lines of code to work with. Not that complex. There are no problems to fix. What reference are you using for your code?

The only things I can thing of:
both bufferdata calls should be:
num_faces * num_verts_per_face * num_elements_per_vert * sizeof(GLfloat)

It's crashing because of a pointer obviously. You either don't have the correct data in saying how many faces you have. Assuming all data is a triangle. Are you loading your own mesh in or using a library?

My suggestion would be to start with a smaller model, like 1 triangle. Get that to work, then maybe a cube. In your verison ur saying that one way it doesnt draw all the faces. So my suggestion is to do that with a smaller model that you yourself can debug visually.

##### Share on other sites
another note. Your not providing normals, so maybe they arent all drawing due to back facing.

glDisable(GL_CULLFACE);

try that.

##### Share on other sites
Quote:
 Original post by dpadam450another note. Your not providing normals, so maybe they arent all drawing due to back facing. glDisable(GL_CULLFACE);

That wouldn't cause a crash though.

What exactly is my_mesh->Vertices? does it point to the right thing?

##### Share on other sites
Quote:
 Are you sure that you are allocating and initializing enough memory? Having only the vertices for one frame rather than all frames would fit the symptoms (first frame ok, second frame from unallocated memory).

What exactly do you mean by having the vertices for only one frame? I am sure I am allocating enough memory according to all the tutorials and the book I have read, though all of them have a hardcoded array of vertex positions, normals and tex coords instead of a mesh, but that should not be any different.

Quote:
 That wouldn't cause a crash though.What exactly is my_mesh->Vertices? does it point to the right thing?

It points to the vertices of the mesh, which are stored as (GLfloat*). As soon as I have transferred them to the vertex buffer, I free that memory (in accordance to how most of the tutorials and the article in my book does it).

Quote:
 It's crashing because of a pointer obviously. You either don't have the correct data in saying how many faces you have. Assuming all data is a triangle. Are you loading your own mesh in or using a library?

I am using my own code for loading ASE models. All faces are triangles.

In glDrawArrays(), is it really correct to pass my_mesh->NumFaces * 3 as the third param? Just want to make sure that is correct, since we're speaking about pointers and stuff. If I reduce that param to my_mesh->NumFaces, it works, but it does not draw the whole mesh.

Drawing one single triangle was successful using this code. I simply passed 1 face and 3 vertices for it...

##### Share on other sites
I've always had problems with glDrawArrays() when using an array of just straight up floats. Try making a structure that holds the 3 floats needed for each vertex and make an array of those structures and fill in the corresponding values from your raw float array.

When you get to allocating the size of the buffer just use the total number of vertices x sizeof(myVertexStructurethingy).

Now when you call glDrawArrays(), the third parameter should correspond to the number of elements in that array of vertex structures.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633679
• Total Posts
3013296
×