Search code examples
openglopengl-esdirectxabstraction

How to design an OpenGL DirectX "Abstraction Layer"


I was reading about creating graphics "abstraction layers" to make it convenient to switch between graphics platforms. Unfortunately, I have not been able to find much detail about the subject. Is this abstraction achievable at the function level with something like this?

void pushMatrix(){
  if (directx){
     // do directx function

  }else if (opengl){
     // do opengl function
  }

}

Is this how it works? Is there a better way? Can anyone point me to some examples of things that do this or more example code?


Solution

  • What is usually done is to have an interface to a "generic" renderer :

    class RendererInterface{
        virtual DrawMesh() = 0;
        virtual SwapBuffers() = 0;
        /// etc
    }
    

    with one implementation for each lib :

    class OpenGLRenderer : public RendererInterface{
        virtual DrawMesh(){... }
        ....
    }
    

    But the concept is the same as Alexander's answer.