I'm not sure if this has been posted before.
I'm just not sure how I should abstract OpenGL or DirectX specific things from classes specific to rendering. Such as shaders, texture objects, vertex buffer objects, etc.
For example:
Let's say there is a Texture2D class, that is used to refer to textures stored on the GPU. The problem is: storing the specific GLuint (or whatever the alternative for DX is) within the Texture2D object directly, which would make my code very dependent to OpenGL. I've thought of a couple alternatives:
Option 1:
Store an unsigned integer inside the Texture object, (not actually the GLuint), which will resemble an index of the actual OpenGL/DX specific data. Which will most likely be present inside a TextureLoader class. Where the Renderer will have a TextureLoader, and may be accessed through a getter.
Here is what I mean:
Texture2d (this is a basic data structure)
- width/height
- index
Texture2dLoader (abstract)
- loadTexture(const Image&) : Texture2d
- destroyTexture(Texture& texture)
OglTexture2dLoader : TextureLoader
- loadTexture(const Image&) : Texture2d // override
- destroyTexture(Texture& texture) // override
- vector<GLuint> textures // dynamic array, appended to when loading texture
Here are the pros/cons that I've thought of from this design.
Pros:
- Simple, non dependant on OpenGL or DirectX
- Easy to make another implementation
Cons:
- Requires manual deletion, therefore:
- If Texture object goes out of scope, there will be no way of deleting texture manually
Option 2:
Make an abstract TextureHandler class, which will have specific OpenGL/DX in the derived class
Texture2dHandler (abstract)
- virtual destructor
Texture2d (this is a basic data structure)
- width/height
- handler : unique_ptr<Texture2dHandler>
OglTexture2dHandler : TextureHandler
- GLuint id
- (destructor)
Texture2dLoader (abstract)
- loadTexture(const Image&) : Texture2d
- destroyTexture(Texture& texture)
OglTexture2dLoader : TextureLoader
- loadTexture(const Image&) : Texture2d // override
- destroyTexture(Texture& texture) // override
Pros:
- Allows for RAII
Cons:
- Handler requires to be allocated on the heap
The same issue is also applied to shaders, buffer objects (such as VBOs), and whatever else, and therefore this will be present all over my code. Does anyone have personal experience with this? What option should I go for?
This is really bugging my mind. Thanks for reading.






