Menu

OpenGL object encapsulation

Milan Davidovic

GLSlayer classes that encapsulate OpenGL objects do not expose a Bind() function. I think it is much better to hide bind-to-edit semantics. What these classes do is bind objects if it is needed, by checking if the object that is about to be used is already bound to required target. For example, setting data for a buffer object is implemented something like this:

//
void GLBuffer::BufferData(size_t size, const void* data, BufferUsage usage)
{
    if(_id != *_currentId)
    {
        glBindBuffer(_target, _id);
        *_currentId = _id;
    }
    glBufferData(_target, size, data, GetGLEnum(usage));
}

where _currentId points to a GLuint of last bound buffer for _target. So if not already bound it will bind the buffer and update the GLuint variable that tracks current ID, and then proceed to update buffer's data. Direct state access extension would be much better, but it is not used because it's not in the core yet and it's incomplete.


Related

Wiki: Home