Drawing alternating VBOs with glDrawArrays

I am currently using glDrawElements to render using multiple VBOs (vertex, color, texture and index). I found that very few vertices are separated, so I would like to switch to glDrawArrays and one alternating VBO.

I could not find a clear example: 1) create an alternating VBO and add a square or three (vert, color, texture) to it and 2) do whatever is necessary for drawing using glDrawArrays. What is the code for these two steps?

+3
source share
1 answer

Above my head:

//init
glBindBuffer(GL_ARRAY_BUFFER, new_array);
GLfloat data[] = { 
    0.f, 0.f, 0.f, 0.f, 0.f,
    0.f, 0.f, 100.f, 0.f, 1.f,
    0.f, 100.f, 100.f, 1.f, 1.f,
    0.f, 100.f, 100.f, 1.f, 1.f,
    0.f, 100.f, 0.f, 1.f, 0.f,
    0.f, 0.f, 0.f, 0.f, 0.f,
};
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);

// draw
glBindBuffer(GL_ARRAY_BUFFER, new_array);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 5*sizeof(GLfloat), NULL);
glClientActiveTexture(GL_TEXTURE0);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 5*sizeof(GLfloat), ((char*)NULL)+3*sizeof(GLfloat) );
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

There is nothing special about this code. Just see how:

  • Data from the array is dataloaded: as is, all adjacent
  • 5 * sizeof (GLfloat), , : 3 2 texcoord. , , 2, .
  • . , , 0. texcoord 3 , 3 * sizeof (GLfloat).

- : UNORMs, . GLfloat, GLubyte . , , , .

+7

All Articles