user1117065
user1117065

Reputation: 15

OpenGL - Not able to copy data past a certain size to bound VBO

I'm still trying to grasp the quirks of OpenGL but it doesn't help trying to learn on OSX which uses OpenGL 4.2 Core, which could be relevant.

All I'm simply trying to do is copy an array of vertex data parsed from an object file, which shows up fine if I iterate through that data, into a bound VBO, however when I use glGetBufferSubData to return the contents of the currently bound VBO it shows the data fine for a few dozen lines then starts display what only I can see as corrupted data like so...

Vertex: (-0.437500, 0.328125, 0.765625)
Vertex: (0.500000, 0.390625, 0.687500)
Vertex: (-0.500000, 0.390625, 0.687500)
Vertex: (0.546875, 0.437500, 0.578125)
Vertex: (-0.546875, 0.437500, 0.578125)
-------------------vvvvv corrupts here
Vertex: (0.625000, 19188110749038498652920741888.000000, 12125095608195490978463744.000000)
Vertex: (68291490374736750313472.000000, 70795556751816250086766057357312.000000, 0.000000)
Vertex: (4360831549674110915425861632.000000, 4544202249129758853702852018176.000000, 50850084445497733730842814971904.000000)

This happens even if I try initializing a buffer of the same or similar size with all zeroes, it gets weird after some arbitrary amount. Here's a snippet to see what I'm doing, but I can't imagine what's going on.

// Parse an OBJ file
compositeWavefrontObj com;
parseObjFileVerticies("suzanne.obj", &com);

// Bind a vertex array object
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

// copy vertex buffer data
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, com.attrib->num_vertices*sizeof(GLfloat), 0, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, com.attrib->num_vertices*sizeof(GLfloat), com.attrib->vertices);

// Read back data from bound buffer object
GLfloat *vertBuffer = malloc(sizeof(GLfloat) * com.attrib->num_vertices);
glGetBufferSubData(GL_ARRAY_BUFFER, 0, com.attrib->num_vertices, vertBuffer);
for (int i = 0; i < com.attrib->num_vertices; ++i) {
    printf("Vertex: (%f,", *(vertBuffer+i));
    ++i;
    printf(" %f,", *(vertBuffer+i));
    ++i;
    printf(" %f)\n", *(vertBuffer+i));
}
printf("\n\n\n");

Upvotes: 0

Views: 55

Answers (1)

Ripi2
Ripi2

Reputation: 7198

I see two problems:

glBufferData(GL_ARRAY_BUFFER, com.attrib->num_vertices*sizeof(GLfloat), 0, GL_STATIC_DRAW); needs a size in bytes for the whole data.
Because you have X,Y,Z for each vertex, it should be

glBufferData(GL_ARRAY_BUFFER, com.attrib->num_vertices*3*sizeof(GLfloat), 0, GL_STATIC_DRAW);

note the '3'.

Same when you allocate room and read the buffer.

GLfloat *vertBuffer = malloc(sizeof(GLfloat) * com.attrib->num_vertices * 3);
glGetBufferSubData(GL_ARRAY_BUFFER, 0, com.attrib->num_vertices * 3 * sizeof(GLfloat), vertBuffer);

Upvotes: 1

Related Questions