Skip to main content
1 of 2
KaiserJohaan
  • 1.5k
  • 2
  • 23
  • 42

glBufferSubData and offset alignment?

I'm having trouble rendering some of my meshes; some display just fine, some are drawn wrong or not at all.

While troubleshooting I was looking at the openGL docs (http://www.opengl.org/sdk/docs/man3/) and on the topic of glBufferSubData the following line caught my eye:

Clients must align data elements consistent with the requirements of the client platform, with an additional base-level requirement that an offset within a buffer to a datum comprising N bytes be a multiple of N.

I am buffering vertex + normal and index data like this:

    glGenBuffers(1, &mVBO);
    glGenBuffers(1, &mIndexBuffer);
    glGenVertexArrays(1, &mVAO);
 
    // buffer vertex, normals and index data
    glBindVertexArray(mVAO);
    glBindBuffer(GL_ARRAY_BUFFER, mVBO);
    glBufferData(GL_ARRAY_BUFFER, (vertexData.size() + normalData.size()) * sizeof(float), NULL, GL_STATIC_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, NULL, vertexData.size() * sizeof(float), &vertexData[0]);
    glBufferSubData(GL_ARRAY_BUFFER, vertexData.size() * sizeof(float), normalData.size() * sizeof(float), &normalData[0]);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexData.size() * sizeof(uint32_t), &indexData[0], GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(vertexData.size() * sizeof(float)));

   // unbind array buffer and VAO
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

So for the vertex and normals I first allocate the complete buffer using glBufferData and then fill it in using glBufferSubData. My question then is, according to the docs, do I need to align the offset my normal data, and if so with what? Is this what couldve caused some meshes to draw incorrectly?

KaiserJohaan
  • 1.5k
  • 2
  • 23
  • 42