EBO stands for Element Buffer Objects and is used to store indexes of drawn Objects. An index is a positional marker of an element in a vertex attribute array. With index drawing, you specify data through an array of vertex attributes, and then instruct OpenGL to complete drawing using indexes pointing to the data. The main purpose of index drawing is to solve the problem of high overlap of vertices. The principle of using index plotting is shown below.In the figure on the left, when the rectangle is drawn without an index, overlapping vertex data is specified repeatedly. V1 and V2 are repeated, using six vertex attribute data; In the figure on the right, when using index drawing, you only need to specify the index of the vertex in the attribute array. 0,1,2,3 represent V0,V1,V2,V3 vertices. The drawing rectangle specifies a total of 6 indexes and uses 4 vertex attribute data.

The above example may not make a big difference, but when the object to be drawn contains multiple overlapping vertices, if each vertex attribute contains attributes such as position, color, texture coordinates, normal vectors, etc., then there will be a lot of extra space overhead, affecting GPU performance. At the same time, if you need to change the data at one vertex, you have to change the data at the same vertex, which is not very interesting. Therefore, using index rendering saves storage space and allows flexibility to respond to changes in vertex attributes. Rectangles can be drawn without indexes, we use 6 vertices, and the data is as follows:

GLfloat vertices[] = {
        // The first triangle
        0.5 f.0.0 f.0.0 f.1.0 f.0.0 f.0.0 f.0.5 f.0.0 f.0.0 f.0.0 f.1.0 f.0.0 f.0.0 f.0.5 f.0.0 f.0.0 f.0.0 f.1.0 f.// The second triangle
        0.5 f.0.0 f.0.0 f.1.0 f.0.0 f.0.0 f.0.0 f.0.5 f.0.0 f.1.0 f.1.0 f.0.0 f.0.5 f.0.0 f.0.0 f.0.0 f.1.0 f.0.0 f};Copy the code

Then update the number of vertices as you draw

glDrawArrays(GL_TRIANGLES, 0.6);
Copy the code

Here’s how to draw using indexes. We need to store vertex attribute data in VBO and, on the other hand, use EBO to store index data. We respecify vertex data and index data as follows:

// Specify the vertex position color of the vertex attribute data
GLfloat vertices[] = {
    0.5 f.0.0 f.0.0 f.1.0 f.0.0 f.0.0 f./ / 0
    0.5 f.0.0 f.0.0 f.0.0 f.1.0 f.0.0 f./ / 1
    0.0 f.0.5 f.0.0 f.0.0 f.0.0 f.1.0 f./ / 2
    0.0 f.0.5 f.0.0 f.1.0 f.1.0 f.0.0 f  / / 3
};
// Index data
GLshort indices[] = {
    0.1.2.// The first triangle
    0.3.1   // The second triangle
};
Copy the code

Create the EBO and pass the index data to the EBO as follows:

glGenBuffers(1, &EBOId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
Copy the code

Using index plotting:

Parameter 1: primitive connection parameter 2: number of indexes Parameter 3: data type of the index Parameter 4: index data. You can either pass in the index array directly or use the index buffer, which is used hereglDrawElements(GL_TRIANGLES, sizeof(indices) / sizeof(indices[0]), GL_UNSIGNED_INT, 0);
Copy the code

The running results are as follows: