Hi all,
I'm looking for an elegant way to design a rendering API abstraction layer, but I keeping running into problems because of OpenGL's bind-to-edit model. For example, one thing I want to do is cache/shadow the current render state to avoid redundant state changes, but bind-to-edit makes this especially difficult.
Here's some pseudocode (I'm pretending there is only one texture unit, for the sake of simplicity):
class Texture {
public:
void Create()
{
glBindTexture(GL_TEXTURE_2D, m_name); // the wrong texture is now bound
... // code to create texture
}
GLuint GetGLName() const { return m_name; }
private:
GLuint m_name;
};
class GraphicsDevice {
public:
void SetTexture(Texture* texture)
{
if (texture != m_texture) {
glBindTexture(GL_TEXTURE_2D, texture->GetGLName());
m_texture = texture;
}
}
private:
Texture* m_texture;
};
As you can see, the texture class needs to bind itself in order to create the OpenGL texture object, and so the the GraphicsDevice class now thinks a different texture is bound than the one that actually is.
The obvious way to fix this problem is to add a glGet(GL_TEXTURE_BINDING_2D) call in Texture::Create so that the previous texture can be bound afterwards, but glGet() calls are supposed to be avoided, right?
The only other solutions I can think of involve various ugly techniques such as circular dependencies, global variables, etc.
Is there a better way of solving this problem?






