Advice on VBO/VAO and how best to use them.

Ok, so let’s see.
So I have the object below and it crashes after 31 vertices are added, works fine up until that point. Not sure if I hit the buffer limit for my video card or I am missing something else.

So to cover my bases and be able to move on. Should the GPU buffer be used to create cube boxes then adjust them using the geometry shader or is there some thing I can adjust to fix my issue and add more vertices/objects.

Basically, I don’t know when to use the GPU buffer and when to stick to cpu processing.

Side note, let’s say you are using the buffer for every object - creating multiple indices and such(and my issue isn’t buffer size). Should you also use the gpu buffer for simple flat surfaces or should more simple items be done cpu side then sent to the gpu?


GLfloat vertices[] = {
//pos                     //color             //texture
//back top
0.0f, 1.4f, 0.0f,        0.0f, 0.8f, 0.8f, 0.0f, 0.0f,//0
-0.1f, 1.4f, 0.005f,   0.8f, 0.0f, 0.8f, 0.0f, 0.0f,
-0.5f, 0.03f, 0.38f,   0.8f, 0.8f, 0.0f, 0.0f, 0.0f,
-0.45f, 0.0f, 0.35f,   0.8f, 0.8f, 0.0f, 0.0f, 0.0f,
-0.25f, 0.03f, 0.75f, 0.8f, 0.0f, 0.8f, 0.0f, 0.0f,
-0.25f, 0.03f, 0.7f,   0.0f, 0.8f, 0.8f, 0.0f, 0.0f,//5
-0.25f, 0.02f, 0.7f,   0.0f, 0.8f, 0.8f, 0.0f, 0.0f,
-0.25f, 0.02f, 0.75f, 0.8f, 0.0f, 0.8f, 0.0f, 0.0f,
0.25f, 0.02f, 0.7f,    0.8f, 0.8f, 0.0f, 0.0f, 0.0f,
0.25f, 0.02f, 0.75f,  0.8f, 0.8f, 0.0f, 0.0f, 0.0f,
0.25f, 0.03f, 0.75f,  0.8f, 0.0f, 0.8f, 0.0f, 0.0f,//10
0.25f, 0.03f, 0.7f,    0.0f, 0.8f, 0.8f, 0.0f, 0.0f,
0.5f, 0.03f, 0.38f,    0.0f, 0.8f, 0.8f, 0.0f, 0.0f,
0.45f, 0.0f, 0.35f,    0.8f, 0.0f, 0.8f, 0.0f, 0.0f,
0.1f, 1.4f, 0.005f,    0.8f, 0.8f, 0.0f, 0.0f, 0.0f,
//back bottom
-0.52f, 0.0f, 0.365f, 0.8f, 0.8f, 0.0f, 0.0f, 0.0f,//15
-0.25f, 0.00f, 0.75f, 0.8f, 0.0f, 0.8f, 0.0f, 0.0f,
-0.25f, 0.0f, 0.7f,     0.0f, 0.8f, 0.8f, 0.0f, 0.0f,
-0.25f, 0.01f, 0.75f, 0.0f, 0.8f, 0.8f, 0.0f, 0.0f,
-0.25f, 0.01f, 0.70f, 0.8f, 0.0f, 0.8f, 0.0f, 0.0f,
0.25f, 0.01f, 0.70f,  0.8f, 0.8f, 0.0f, 0.0f, 0.0f,//20
0.25f, 0.01f, 0.75f,  0.8f, 0.8f, 0.0f, 0.0f, 0.0f,
0.25f, 0.0f, 0.7f,      0.0f, 0.8f, 0.8f, 0.0f, 0.0f,
0.25f, 0.00f, 0.75f,  0.8f, 0.0f, 0.8f, 0.0f, 0.0f,
0.52f, 0.0f, 0.365f,  0.8f, 0.8f, 0.0f, 0.0f, 0.0f,
//middle right bottom base
-0.1f, 1.4f, 0.0025f, 0.8f, 0.0f, 0.8f, 0.0f, 0.0f,//25
-0.51f, 0.03f, 0.37f, 0.8f, 0.8f, 0.0f, 0.0f, 0.0f,
-0.68f, 0.03f, 0.00125f, 0.8f, 0.8f, 0.8f, 0.0f, 0.0f,
-0.63f, 0.0f, 0.0f,    0.8f, 0.8f, 0.8f, 0.0f, 0.0f,
-0.1f, 1.4f, 0.001f,  0.8f, 0.0f, 0.8f, 0.0f, 0.0f,
-0.7f, 0.0f, 0.0f,      0.8f, 0.8f, 0.8f, 0.0f, 0.0f,//30
//middle left bottom base
0.1f, 1.4f, 0.0025f,  0.8f, 0.8f, 0.0f, 0.0f, 0.0f,
/*
0.51f, 0.03f, 0.37f,  0.8f, 0.8f, 0.0f, 0.0f, 0.0f,
.68f, 0.03f, 0.00125f, 0.8f, 0.8f, 0.8f, 0.0f, 0.0f,
.63f, 0.0f, 0.0f, 0.8f, 0.8f, 0.8f, 0.0f, 0.0f,
0.1f, 1.4f, .001f,     0.8f, 0.0f, 0.8f, 0.0f, 1.0f,//35
0.7f, 0.0f, 0.0f,       0.8f, 0.8f, 0.8f, 0.0f, 0.0f,
*/
};
GLuint indices[] = {
0, 1, 2,
2, 3, 0,
3, 2, 4,
4, 5, 3,
5, 4, 6,
6, 7, 5,
7, 6, 8,
8, 9, 7,
9, 8, 10,
10, 11, 9,
11, 10, 12,
12, 13, 11,
13, 12, 14,
14, 0, 13,

4, 2, 15,
15, 16, 4,
16, 15, 3,
3, 17, 16,
3, 17, 5,
17, 16, 18,
18, 19, 17,
19, 18, 20,
20, 21, 19,
21, 20, 22,
22, 23, 21,
23, 22, 24,
24, 22, 13,
13, 22, 11,
24, 23, 10,
23, 24, 12,

1, 0, 25,
25, 0, 26,
2, 1, 25,
25, 26, 2,
3, 26, 15,
26, 3, 27,
27, 28, 26,
28, 27, 29,
28, 29, 0,
30, 27, 26,
26, 30, 15,
30, 15, 28,
28, 15, 3,

//0, 31, 14,
//0, 31, 32,
};


GLuint VBO, EBO;
glGenVertexArrays(0 , &VAO[0]);
glGenBuffers(0 , &VBO);
glGenBuffers(0 , &EBO);
glBindVertexArray(VAO[0]);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
printf("////%i/////", sizeof(indices));
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (GLvoid*)(3*sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (GLvoid*)(6*sizeof(GLfloat)));
glEnableVertexAttribArray(2);
glBindVertexArray(0);


...........


glBindVertexArray(VAO[0]);
int size;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glDrawElements(GL_TRIANGLES, size/sizeof(GLuint), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);

I’ll add in subData later if needed for multiple objects.

Thanks.

So I have the object below and it crashes after 31 vertices are added, works fine up until that point. Not sure if I hit the buffer limit for my video card or I am missing something else.

What is the “it” that crashes? Where is the program when the crash happens?

Should the GPU buffer be used to create cube boxes then adjust them using the geometry shader or is there some thing I can adjust to fix my issue and add more vertices/objects.

Basically, I don’t know when to use the GPU buffer and when to stick to cpu processing.

It’s not clear what you mean by “the GPU buffer”. You should be using buffer objects no matter what. Nor is it clear what you’re talking about with “create cube boxes”.


0.0f, 1.4f, 0.0f,        0.0f, 0.8f, 0.8f, 0.0f, 0.0f,//0
-0.1f, 1.4f, 0.005f,   0.8f, 0.0f, 0.8f, 0.0f, 0.0f,
-0.5f, 0.03f, 0.38f,   0.8f, 0.8f, 0.0f, 0.0f, 0.0f,
-0.45f, 0.0f, 0.35f,   0.8f, 0.8f, 0.0f, 0.0f, 0.0f,
-0.25f, 0.03f, 0.75f, 0.8f, 0.0f, 0.8f, 0.0f, 0.0f,
-0.25f, 0.03f, 0.7f,   0.0f, 0.8f, 0.8f, 0.0f, 0.0f,//5
-0.25f, 0.02f, 0.7f,   0.0f, 0.8f, 0.8f, 0.0f, 0.0f,
-0.25f, 0.02f, 0.75f, 0.8f, 0.0f, 0.8f, 0.0f, 0.0f,
0.25f, 0.02f, 0.7f,    0.8f, 0.8f, 0.0f, 0.0f, 0.0f,
0.25f, 0.02f, 0.75f,  0.8f, 0.8f, 0.0f, 0.0f, 0.0f,
0.25f, 0.03f, 0.75f,  0.8f, 0.0f, 0.8f, 0.0f, 0.0f,//10
0.25f, 0.03f, 0.7f,    0.0f, 0.8f, 0.8f, 0.0f, 0.0f,
0.5f, 0.03f, 0.38f,    0.0f, 0.8f, 0.8f, 0.0f, 0.0f,
0.45f, 0.0f, 0.35f,    0.8f, 0.0f, 0.8f, 0.0f, 0.0f,
0.1f, 1.4f, 0.005f,    0.8f, 0.8f, 0.0f, 0.0f, 0.0f,
//back bottom
-0.52f, 0.0f, 0.365f, 0.8f, 0.8f, 0.0f, 0.0f, 0.0f,//15
-0.25f, 0.00f, 0.75f, 0.8f, 0.0f, 0.8f, 0.0f, 0.0f,
-0.25f, 0.0f, 0.7f,     0.0f, 0.8f, 0.8f, 0.0f, 0.0f,
-0.25f, 0.01f, 0.75f, 0.0f, 0.8f, 0.8f, 0.0f, 0.0f,
-0.25f, 0.01f, 0.70f, 0.8f, 0.0f, 0.8f, 0.0f, 0.0f,
0.25f, 0.01f, 0.70f,  0.8f, 0.8f, 0.0f, 0.0f, 0.0f,//20
0.25f, 0.01f, 0.75f,  0.8f, 0.8f, 0.0f, 0.0f, 0.0f,
0.25f, 0.0f, 0.7f,      0.0f, 0.8f, 0.8f, 0.0f, 0.0f,
0.25f, 0.00f, 0.75f,  0.8f, 0.0f, 0.8f, 0.0f, 0.0f,
0.52f, 0.0f, 0.365f,  0.8f, 0.8f, 0.0f, 0.0f, 0.0f,
//middle right bottom base
-0.1f, 1.4f, 0.0025f, 0.8f, 0.0f, 0.8f, 0.0f, 0.0f,//25
-0.51f, 0.03f, 0.37f, 0.8f, 0.8f, 0.0f, 0.0f, 0.0f,
-0.68f, 0.03f, 0.00125f, 0.8f, 0.8f, 0.8f, 0.0f, 0.0f,
-0.63f, 0.0f, 0.0f,    0.8f, 0.8f, 0.8f, 0.0f, 0.0f,
-0.1f, 1.4f, 0.001f,  0.8f, 0.0f, 0.8f, 0.0f, 0.0f,
-0.7f, 0.0f, 0.0f,      0.8f, 0.8f, 0.8f, 0.0f, 0.0f,//30

and


0, 1, 2,
2, 3, 0,
3, 2, 4,
4, 5, 3,
5, 4, 6,
6, 7, 5,
7, 6, 8,
8, 9, 7,
9, 8, 10,
10, 11, 9,
11, 10, 12,
12, 13, 11,
13, 12, 14,
14, 0, 13,
 
4, 2, 15,
15, 16, 4,
16, 15, 3,
3, 17, 16,
3, 17, 5,
17, 16, 18,
18, 19, 17,
19, 18, 20,
20, 21, 19,
21, 20, 22,
22, 23, 21,
23, 22, 24,
24, 22, 13,
13, 22, 11,
24, 23, 10,
23, 24, 12,
 
1, 0, 25,
25, 0, 26,
2, 1, 25,
25, 26, 2,
3, 26, 15,
26, 3, 27,
27, 28, 26,
28, 27, 29,
28, 29, 0,
30, 27, 26,
26, 30, 15,
30, 15, 28,
28, 15, 3,

runs, perfectly fine.
Add
0.1f, 1.4f, 0.0025f, 0.8f, 0.8f, 0.0f, 0.0f, 0.0f,
to vertices and
0, 31, 14, to indices

and it crashes, does not run, no longer processes, and stops functioning. No errors.

I was under the impression that VBO and indexing in modern opengl was to control the GPU buffer directly. You load vertices onto the gpu buffer instead of keeping them indexed on your ram(as with old opengl), then use shaders to control at that point(Want my shader code and matrices code?).
Split the GPU index into multiple sub sections and you can house multiple objects in the single index and use the as needed.

Least that was how I thought it worked 3.3+. Of course, that’s what I am doing - trying to figure out things post 2.0.

Cube box, and adjusting later.
build a cube.
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
-1.0, -1.0, -1.0,
-1.0, -1.0, -1.0,

Index it.
1, 0, 2,
2, 3, 0,
etc.

Send to shaders.
To keep it simple, you could do something like.


uniform vec2 off_sets;
.....
some_var = (off_sets.x - position.y);
position.x = position.x * cos(sqrt(some_var*some_var)*off_sets.y));
position = proj * trans * model * vec4(position.x, position.y, position.z, 1.0f);

not sure that shader code works, but it should give some idea as what i was thinking. Adjusting the dimensions of the box within the shader.

So back to my original question. If best practice is using the VBO/index. Then why is the above failing?
I did all the vertices by hand and it worked until that point. I don’t see any plane over lapping or anything like that, so I assumed it was an issue with the buffer.
If I still have plenty of room in the buffer, why does it crash when putting in the 32nd(point 31 by my markers) point.

it sounds like an off-by-one error … check all your counts and sizes