So, my question today is about opengl's vertex buffer objects. I've read every tutorial on the internet for every different platform that exists, but I couldn't resolve this issue.
I have a custom vertex class which include some (but not much) extra garbage in it, in addition to the VNT data:
class Vertex { int stuffA; float pos[3]; float tc[2]; float n[3]; int stuffB;};class Tri { unsigned short v[3];};
Since there isn't too much extra data in the 'stuffs' and since i am not loading up huge models, i don't mind just dumping all of the data into my VBO:
Vertex verts[3]; Tri tris[1]; /* initialize both with data.. */ GLuint vertexBuffer = 0; GLuint indexBuffer = 0; glGenBuffersARB(1, &vertexBuffer); glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexBuffer); glBufferDataARB(GL_ARRAY_BUFFER_ARB, 3*sizeof(Vertex), verts[0].pos, GL_STATIC_DRAW_ARB); glGenBuffersARB(1, &indexBuffer); glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, indexBuffer); glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(unsigned short)*3, tris[0].v, GL_STATIC_DRAW);
Finally, in my main loop, I render everything. Now, here is the issue -- the structure of my Vertex class may (and most likely will) be changing in the near future. In order to save me much headache, I've been using the offsetof() function defined in stddef.h to get my byte offsets. I assumed this would work, but my project keeps crashing. Now here is there interesting part --- if I move member variable 'stuffA' to the end of the class, the program doesn't crash! What am I doing wrong??
glBindBufferARB(GL_ARRAY_BUFFER, vertexBuffer);glEnableVertexAttribArrayARB(0);glVertexAttribPointerARB(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex,pos) );glEnableVertexAttribArrayARB(1);glVertexAttribPointerARB(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex,n) );glEnableVertexAttribArrayARB(2);glVertexAttribPointerARB(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex,tc) );glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, (void*)offsetof(Vertex,pos) );
As always, thank you for any assistance provided ahead of time, and your help is greatly appreciated =)