Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL Render Function Order

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?


1 Answers

Before there were Vertex Array Objects (VAOs) you had indeed to redo the bindings whenever you wanted to change the memory layout and/or pointer. Then Vertex Array Objects got introduced, and for a time people where sad because with the drivers back then performance still left a lot to be desired (as of writing this, this was about 10 years ago – time flies). Ever since then drivers improved a lot.

Essentially a VAOs stores which buffers are bound to what attribute, and the memory layout of those bindings. https://www.khronos.org/opengl/wiki/Vertex_Specification#Vertex_Array_Object

like image 123
datenwolf Avatar answered Dec 07 '25 21:12

datenwolf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!