Learn OpenGLES 3
VAO, VBO and EBO
Source code: https://github.com/chuzcjoe/learnopengles
1. Vertex Buffer Object (VBO)
In our first demo, where we render a triangle in the center of the screen, we define the vertices and store them on the CPU. When glDrawArrays
is called, this data is transferred from the CPU to the GPU for rendering. While this method works well for small data sizes, it can become a performance bottleneck if large amounts of data need to be loaded from the CPU to the GPU. VBO
is designed to address such issue. VBO
manages large batches of data on GPU memory and keep it there if enough space left on GPU. Once the data is in GPU memory, shader programs can have instant access to it.
1 | GLfloat vertices[4 * (3 + 4)] = { |
2. Vertex Array Object (VAO)
A Vertex Array Object (VAO) in OpenGL is a container object that encapsulates the state needed to specify vertex data to the GPU. It essentially keeps track of multiple vertex buffer objects (VBOs) and their associated vertex attribute configurations, allowing you to easily switch between different vertex data sets and attribute setups.
After a VAO is bound, any subsequent vertex attribute calls will be stored in VAO.
1 | // Generate VAO and define how data is stored |
3. Element Buffer Object (EBO)
Element Buffer Objects (EBOs), also known as Index Buffer Objects (IBOs), are an essential feature in OpenGL used for indexing vertex data. They allow you to specify indices that define the order in which vertices are processed, making it possible to reuse vertex data to draw multiple primitives (such as triangles, lines, etc.), which can significantly reduce the amount of memory required and improve performance.
1 | // without EBO |
To use EBO:
1 | GLfloat vertices[4 * (3 + 4)] = { |
4. Shaders
The vertex shader takes two inputs because we defined two attribute pointers in VAO with location 0(position) and 1(color).
1 | // VAO VBO Shader |
5. Demo
Complete code to setup VAO, VBO and EBO.
1 | GLfloat vertices[4 * (3 + 4)] = { |
Drawing code
1 | glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
References
Learn OpenGLES 3