VertexArray help

Hi,

I’m using a vertex array with a FloatBuffer created by BufferUtils. Here is the code:


this.vbuf = BufferUtil.newFloatBuffer(this.width * this.height * 3);
this.vbuf.put(new float[] { x, y, z });
...
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL.GL_FLOAT, 4, this.vbuf);
...
gl.glArrayElement(someindex);

I have to set the stride to 4 in the glVertexPointer call but in all examples I see the stride is set to 0. Can anyone explain this to me? the vbuf is just a simple FloatBuffer holding vertices with 3 elements.

I believe that stride is the number of elements in the buffer to skip over between subsequent vertices. So if stride is 0, the buffer is completely filled with vertices. You could however create a massive buffer that alternates vertices, normals, and texture coordinates, in which case the space between each vertex needs to be adjusted.

Why do you need the stride to be 4 if you’re not sure what it means?

No, a stride of 0 is a special value, that causes the driver to calculate the actual stride (maybe 4…).

@lhkbob: You got me wrong. I do understand what the stride means. But I have to set the stride to 4 to get a correct calculation.

@Riven: Thanks. Then I’ll guess if I set the stride to 0 the calculation is kind of wrong for a FloatBuffer.

Where did you get this?
In the gl spec, it says:

[quote]The one, two, three, or four values in an array that correspond to a single vertex
comprise an array element. The values within each array element are stored sequentially
in memory. If stride is specified as zero, then array elements are stored
sequentially as well. The error INVALID VALUE is generated if stride is negative.
Otherwise pointers to the ith and (i + 1)st elements of an array differ by stride
basic machine units (typically unsigned bytes), the pointer to the (i + 1)st element
being greater.
[/quote]

Basicly because a stride of 0 is not possible if you have data.

Stride: interval of data offsets => when stride is 0, all data is in the same memory-location

that’s why 0 is a convenience value, it is in BYTES, so when using 3d floats, a stride of 0 becomes 12 (3*4).