I am using OpenGL to render chunks of blocks in a Voxel game. Each chunk has its own vertex buffer, and the existing render function (simplified) is as follows:
foreach (Chunk c in chunks)
{
glBindBuffer(BufferID.Array, c.handle);
if (c.dirty)
{
glBufferData(BufferID.Array, ... , BufferUsage.StaticDraw);
c.dirty = false;
}
// For position, normal, colour and UV
glEnableVertexAttribArray(...);
glVertexAttribPointer(...);
glUseProgram(shader.Handle);
glDrawArrays(DrawMode.TriangleStrip, 0, c.vertexBuffer.Length);
}
The code above works, however there is high CPU usage on glEnableVertexAttribArray, glVertexAttribPointer and glUseProgram.
Can I call these functions before the loop starts, or must it be called after every glBindBuffer?