Vertex Buffer Objects
- Tags
- opengl
Data (vertices) allocated on the GPU.
We send data from our program to the GPU so that it can be processed without slow transfers between the GPU and the CPU. This is generally the first stage in the drawing something.
Once in the GPU the vertex shader has near instant access to data.
Creating a VBO
We create a buffer and access it through an id.
unsigned int VBO;
glGenBuffers(1, &VBO);
OpenGL also needs to know the kind of buffer this object points to.
glBindBuffer(GL_ARRAY_BUFFER, VBO);
We can then assign data to a VBO.
float vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
Data Management
The final parameter in the call to glBufferData
specifies how the data should be
managed. Possible values include:
Value | Meaning |
---|---|
GL_STREAM_DRAW | Data is set only once and used by the GPU at most a few times. |
GL_STATIC_DRAW | Data is set only once and used many times. |
GL_DYNAMIC_DRAW | Data is changed a lot and used many times. |