vbo stride question
Why is the stride for glVertexPointer, glTexCoordPointer and glNormalPointer set to 0 in the SGI and Nehe examples ?
I am packing all 3 into my data and I set the stride to (sizeof(GLfloat)*8) (3 for vertex, 3 for normal and 2 for texcoords). This (I think) is what causes it to crash once in a while. When my level loads everything is drawn fine. If I set the stride to zero, my geometry looks completely screwed up.
What should the stride be set to and why ?
Thanks,
Nitzan
It depends on how is packed your data.
If you have all your vertex data in one array (array : read as malloc''ed pointer), all your normals in another array, etc. Then your stride is 0. For instance if vertex, normals, etc are stored like that :
[vertex0][vertex1][vertex2]...
[normal0][normal1][normal2]...
[texcoord0][texcoord1][texcoord2]...
If your vertex, normal, etc is packed like that :
[vertex0][normal0][texcoord0][vertex1][normal1][texcoord1][vertex2][normal2][texcoord2]...
Then you should set a non-null stride, which corresponds to the offset needed to switch from one element to the next. (this stride is counted as bytes btw)
If you have all your vertex data in one array (array : read as malloc''ed pointer), all your normals in another array, etc. Then your stride is 0. For instance if vertex, normals, etc are stored like that :
[vertex0][vertex1][vertex2]...
[normal0][normal1][normal2]...
[texcoord0][texcoord1][texcoord2]...
If your vertex, normal, etc is packed like that :
[vertex0][normal0][texcoord0][vertex1][normal1][texcoord1][vertex2][normal2][texcoord2]...
Then you should set a non-null stride, which corresponds to the offset needed to switch from one element to the next. (this stride is counted as bytes btw)
So if my data is packed as follows:
[vertex][vertex][vertex][normal][normal][normal[tex][tex]
Is my stride 8 for all of them ? Or is it 5 for glVertexPointer, 5 for glNormalPointer and 6 for glTexCoordPointer ?
My other (new) questions are...
#1 is the stride given in bytes or bits ?
#2 when calling glBufferDataARB(), do I pass the size of the data in bytes or bits ?
#3 for glDrawElements, do I pass the size of the data in bytes or bits ?
Thanks!
Nitzan Wilnai
[vertex][vertex][vertex][normal][normal][normal[tex][tex]
Is my stride 8 for all of them ? Or is it 5 for glVertexPointer, 5 for glNormalPointer and 6 for glTexCoordPointer ?
My other (new) questions are...
#1 is the stride given in bytes or bits ?
#2 when calling glBufferDataARB(), do I pass the size of the data in bytes or bits ?
#3 for glDrawElements, do I pass the size of the data in bytes or bits ?
Thanks!
Nitzan Wilnai
class vec3 { float x,y,z; }class vec2 { float x,y; }class vertex{ vec3 coord, normal; vec2 texcoord;};vertex vertexarray[1000]; // stride is sizeof(vertex)=8*4=32;ORvec3 coord_array[1000]; // stride is sizeof(vec3) or 0vec3 normal_array[1000]; // stride is sizeof(vec3) or 0vec2 texcoord_array[1000]; // stride is sizeof(vec2) or 0
There tends to be a bit of confusion regarding VBO stride, mostly because of its special meaning for 0.
"Stride" in this context means the distance between the beginning of a value in memory, and the beginning of the next value in memory. It is not the distance between the end of one and the beginning of the next. So in a VBO that is an array of a structure, the stride for each element of that structure will be the sizeof the structure as a whole. Keep in mind that struct padding can affect this.
How appropriate. You fight like a cow.
"Stride" in this context means the distance between the beginning of a value in memory, and the beginning of the next value in memory. It is not the distance between the end of one and the beginning of the next. So in a VBO that is an array of a structure, the stride for each element of that structure will be the sizeof the structure as a whole. Keep in mind that struct padding can affect this.
How appropriate. You fight like a cow.
Ok great,
And stride is in bytes right ?
So now is the size for glBufferDataARB() and glDrawElements in bytes or bits ?
Thanks,
Nitzan
And stride is in bytes right ?
So now is the size for glBufferDataARB() and glDrawElements in bytes or bits ?
Thanks,
Nitzan
Wait, by bytes you mean bits right ?
Just kidding! Sorry Stefu, I didnt notice your earlier post.
Thanks guys,
It works great on one machine, I will try on a couple of other ones soon.
Nitzan
Just kidding! Sorry Stefu, I didnt notice your earlier post.

Thanks guys,
It works great on one machine, I will try on a couple of other ones soon.
Nitzan
Well, it still doesnt work on my main machine (GF3 TI200 and 52.16 drivers).
Can anyone tell me if anything is wrong with this code ?
I edited and cleaned up and fixed the code a bit so its easier to read. I am guessing that one of my sizes is incorrect as the code works fine in debug mode but crashes in release. It actually displays the level in the first frame but crashes on the second one.
Thanks,
Nitzan
Edited: I mistakenly wrote the wrong number of indices for the model. Its 36, not 12 (yes I know I can reduce it to 8 but I am doing this for simplicity's sake). Also added the glUnmapBufferARB() calls.
[edited by - nitzan on November 5, 2003 2:44:36 PM]
Can anyone tell me if anything is wrong with this code ?
I edited and cleaned up and fixed the code a bit so its easier to read. I am guessing that one of my sizes is incorrect as the code works fine in debug mode but crashes in release. It actually displays the level in the first frame but crashes on the second one.
// create model// 12 triangles// verticesglGenBuffersARB(1, &vertexObject);glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexObject);// 432 = sizeof(GLfloat) * 12 triangles * 3 vertices * 3 valuesglBufferDataARB(GL_ARRAY_BUFFER_ARB, 432, NULL, GL_STATIC_DRAW_ARB);vertex_data=(float *)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB);// pack vertex dataglUnmapBufferARB(GL_ARRAY_BUFFER_ARB)// normalsglGenBuffersARB(1, &normalObject);glBindBufferARB(GL_ARRAY_BUFFER_ARB, normalObject);// 432 = sizeof(GLfloat) * 12 triangles * 3 vertices * 3 valuesglBufferDataARB(GL_ARRAY_BUFFER_ARB, 432, NULL, GL_STATIC_DRAW_ARB);normal_data=(float *)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB);// pack normal dataglUnmapBufferARB(GL_ARRAY_BUFFER_ARB)// texturesglGenBuffersARB(1, &textureObject);glBindBufferARB(GL_ARRAY_BUFFER_ARB, textureObject);// 288 = sizeof(GLfloat) * 12 triangles * 3 vertices * 2 valuesglBufferDataARB(GL_ARRAY_BUFFER_ARB, 288, NULL, GL_STATIC_DRAW_ARB);texcoord_data=(float *)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB);// pack texture dataglUnmapBufferARB(GL_ARRAY_BUFFER_ARB)// draw modelglBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexObject);glVertexPointer(3, GL_FLOAT, 0, 0);glBindBufferARB(GL_ARRAY_BUFFER_ARB, normalObject);glNormalPointer(GL_FLOAT, 0, 0);glBindBufferARB(GL_ARRAY_BUFFER_ARB, textureObject);glTexCoordPointer(2, GL_FLOAT, 0, 0);// 36 indices, one for each verticesglDrawRangeElements(GL_TRIANGLES, 0, 36, 36, GL_UNSIGNED_SHORT, indices);// draw shadows// draw shadow (carmack's reverse, closed part)// 4 visible triangles, 8 drawn (4 on top and 4 on bottom)glGenBuffersARB(1, &dynamic_tightshadow_vertexObject);glBindBufferARB(GL_ARRAY_BUFFER_ARB, dynamic_tightshadow_vertexObject);// 288 = sizeof(GLfloat) * 8 triangles * 3 vertices * 3 valuesglBufferDataARB(GL_ARRAY_BUFFER_ARB, 288, NULL, GL_DYNAMIC_DRAW_ARB);data=(float *)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB);// pack shadow dataglUnmapBufferARB(GL_ARRAY_BUFFER_ARB)glBindBufferARB(GL_ARRAY_BUFFER_ARB, dynamic_tightshadow_vertexObject[which]);// Vertex positions are at offset 0glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));// 24 indicesglDrawRangeElements(GL_TRIANGLES, 0, 24, 24, GL_UNSIGNED_SHORT, dynamic_tightshadow_indices);// draw shadow (carmack's reverse, side part)// 4 visible triangles, 12 drawn (12 triangles to completely wrap the 4)glGenBuffersARB(1, &dynamic_sideshadow_vertexObject);glBindBufferARB(GL_ARRAY_BUFFER_ARB, dynamic_sideshadow_vertexObject);// 432 = sizeof(GLfloat) * 12 triangles * 3 vertices * 3 valuesglBufferDataARB(GL_ARRAY_BUFFER_ARB, 432, NULL, GL_DYNAMIC_DRAW_ARB);data=(float *)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB);// pack shadow dataglUnmapBufferARB(GL_ARRAY_BUFFER_ARB)glBindBufferARB(GL_ARRAY_BUFFER_ARB, dynamic_sideshadow_vertexObject);// Vertex positions are at offset 0glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));// 36 indicesglDrawRangeElements(GL_TRIANGLES, 0, 36, 36, GL_UNSIGNED_SHORT, dynamic_sideshadow_indices);
Thanks,
Nitzan
Edited: I mistakenly wrote the wrong number of indices for the model. Its 36, not 12 (yes I know I can reduce it to 8 but I am doing this for simplicity's sake). Also added the glUnmapBufferARB() calls.
[edited by - nitzan on November 5, 2003 2:44:36 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement