Last two arguments of glVertexAttribPointer when using VBO

I am confused how glVertexAttribPointer is being used with VBOs. From here it is clear that pre-last argument is the offset (distance between two vertex data) in memory and last is the pointer to memory that vertex data for a specific attribute. I.e. if I have such data:

GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f,
    -0.5f, -0.5f, 0.0f,
    0.5f, -0.5f, 0.0f };

glVertexAttribPointer(index, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
glEnableVertexAttribArray(index);

then we say that use vVertices to set vertex positions. We specify that 3 floats make one vertex and offset is 0.

But how we use it with VBO? We set last two arguments to 0, right? Why? And how about this case:

glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

typedef  struct  {
    float  Position[2];
    float  Color[4];
}  Vertex;

Vertex  data[] =
{
    { { -1, -1 }, { 0, 1, 0, 1 } },
    { { 1, -1 }, { 0, 1, 0, 1 } },
    { { -1, 1 }, { 0, 1, 0, 1 } },
    { { 1, 1 }, { 0, 1, 0, 1 } }
};

GLubyte  indices[] = { 0, 1, 2,   // first triangle index 
    2, 3, 1 };  // second triangle index

glGenBuffers(1, &vertexVBO);
glBindBuffer(GL_ARRAY_BUFFER, vertexVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
GLuint positionLocation = glGetAttribLocation(program->getProgram(), "a_position");
glEnableVertexAttribArray(positionLocation);

glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *)offsetof(Vertex, Color));


glGenBuffers(1, &colorVBO);
glBindBuffer(GL_ARRAY_BUFFER, colorVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);

GLuint colorLocation = glGetAttribLocation(program->getProgram(), "a_color");
glEnableVertexAttribArray(colorLocation);
glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *)offsetof(Vertex, Color));

Why we specify 32 and 0x00008as last two arguments for setting color attribute data? Please explain how to use glVertexAttribPointer with VAO and VBO.


Source: c++

Leave a Reply