Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does stride mean in OpenGL|ES

I was going through the signature of the method glVertexPointer which is

void glVertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid * pointer)

Can anyone help me understand what does the third argument stride do.

I got this definition for stride after googling about it

Amount of bytes from the beginning of one element to the beginning of the following element. If you pass a zero as stride, it means they are tightly packed. If you have an array of floats, which contains vertices like this, x1,y1,z1,x2,y2,z2... and so on, you can set stride to either zero (as tightly packed), or 12 (3 floats*4 bytes each from the beginning of vertex one to the beginning of vertex two).

I am not able to get what does this mean ? it would be really helpful if someone explains it with the help of an example.

thanks.

like image 225
Mayank Avatar asked Mar 10 '14 09:03

Mayank


1 Answers

The simple case is when your array contains only vertex coordinate data. Say we have just two coordinates, hence 6 floats:

{x1,y1,z1, x2,y2,y2}

The pointer (4th parameter) points to the beginning of the first vertex in the array (i.e. zero, pointing to x1). The stride should be 12, meaning that to move from one vertex to the next, OpenGL needs to move along by 12 bytes (since each of the 3 coordinates in each vertex takes up 4 bytes). So by moving by 12 bytes, we get to where x2 is, the beginning of the second vertex.

This is an example of tightly packed since the array only contains one type of data - you can set the stride to zero and OpenGL will happily work its way through the array one float at a time. However the array doesn't have to contain only coordinate data - you could store normal and color data alongside the coordinates in the array as well:

{x1,y1,z1,nx1,ny1,nz1,r1,g1,b1,a1, x2,y2,z2,nx2,ny2,nz2,r2,g2,b2,a2}

This is no longer tightly packed - the coordinate data are no longer contiguous in the array (they are separated by normal and colour values). In this case the stride would be 40. To get from the beginning of one vertex to the next, OpenGL needs to move along by 40 bytes: (3 floats of coordinate data + 3 floats of normal data + 4 floats of colour data) x 4 bytes per float.

like image 119
NigelK Avatar answered Oct 05 '22 23:10

NigelK