octopus
octopus

Reputation: 43

What is the glVertexAttribPointer "stride" argument?

just started learning openGL and, as usual, trying to understand what every bit of code i write does and why it is there, but i could not understand the stride argument is.
according to the documentation it means (crude copy and paste ahead)

Specifies the byte offset between consecutive generic vertex attributes. If stride is 0, the generic vertex attributes are understood to be tightly packed in the array. The initial value is 0.

ok, the last part i can understand. when i did the array1 like this (position tightly packed in the array)

    GLfloat vertices[] = {
      //x,     y,     z
        0.0f,  0.5f,  0.0f,  // middle top
       -0.5f, -0.5f,  0.0f,  // bottom left
        0.5f, -0.5f,  0.0f   // bottom right
    };

and stride has 0, it works :)

glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0);

but if i add more attributes to it1

    GLfloat vertices[] = {
      //x,     y,     z,     r,     g,     b
        0.0f,  0.5f,  0.0f,  1.0f,  0.0f,  0.0f,  // middle top
       -0.5f, -0.5f,  0.0f,  1.0f,  0.0f,  0.0f,  // bottom left
        0.5f, -0.5f,  0.0f,  1.0f,  0.0f,  0.0f   // bottom right
    };

now stride is the size of each vertex, WHY?

glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 6*sizeof(GLfloat), (void*)0);

if i try to use logic and put stride as 3, because it is the distance between one position and the next position on it1 and it just does this for some reason (here)

Upvotes: 2

Views: 217

Answers (1)

Rabbid76
Rabbid76

Reputation: 210908

It is the byte offset between consecutive general vertex attributes. In your second example, each of your attribute tuples has 6 components (x, y, z, r, g, b) and each component has 4 bytes (sizeof(float)). The correct "stride" is therefore 6*sizeof(GLfloat).
If "stride" is set to 0, the distance is automatically calculated from "size" and "type" arguments, assuming that you have only one attribute in a consecutive array. In your first case the "size" is 3 and the "type" is GL_FLOAT, so the automatically calculated "stride" is 3*sizeof(GLfloat).

Upvotes: 3

Related Questions