Vertex Attributes and Layouts in OpenGL - hls333555/OpenGL GitHub Wiki
A vertex is just more than a position, it contains many other data including texture coordinates, normals, colors, tangents etc., those data are called attributes. In order to tell OpenGL what the layout of our buffer actually is, we can call glVertexAttribPointer()
, you can go to glVertexAttribPointer for its detail usage.
Parameter notes:
-
Stride - It is the amount of bytes between each vertex. Imagine that we have a pointer at the beginning to the buffer, and we go 32 bytes into it and we should be at the very start of the next vertex.
-
Pointer - It is the offset of current attribute in bytes in a vertex. In the case illustrated above, the offset of Position attribute would be 0, the offset of TextureCoordinate attribute would 12, while the offset of Normal attribute would be 20.
Below are the code to enable and define a vertex attribute data:
// Enable the position vertex attribute data
glEnableVertexAttribArray(0); // Do not forget this!
// Define a position vertex attribute data
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (const void*)0);