步幅在OpenGL | ES中意味着什么

时间:2014-03-10 09:36:12

标签: opengl-es stride

我正在查看方法glVertexPointer的签名

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

任何人都可以帮助我理解第三个参数stride的作用。

我在谷歌搜索后得到了stride的这个定义

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).

我无法得到这是什么意思?如果有人在一个例子的帮助下解释它,那将会非常有用。

感谢。

1 个答案:

答案 0 :(得分:14)

简单情况是您的数组仅包含顶点坐标数据。假设我们只有两个坐标,因此有6个浮点数:

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

指针(第4个参数)指向数组中第一个顶点的开头(即零,指向x1)。步幅应为12,意味着要从一个顶点移动到下一个顶点,OpenGL需要移动12个字节(因为每个顶点中的3个坐标中的每个占用4个字节)。因此,通过移动12个字节,我们到达x2的位置,即第二个顶点的开头。

这是一个紧密包装的例子,因为数组只包含一种类型的数据 - 你可以将步幅设置为零,OpenGL将很乐意一次一个地浮动数组。但是,数组不必仅包含坐标数据 - 您可以将正常数据和颜色数据存储在数组中的坐标旁边:

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

不再紧密打包 - 坐标数据在数组中不再是连续的(它们由法线和颜色值分开)。在这种情况下,步幅为40.要从一个顶点的开头到下一个顶点,OpenGL需要移动40个字节:(3个坐标数据浮点数+ 3个正常数据浮点数+ 4个颜色数据浮点数)x每个浮点数4个字节。