How to create an OpenGL DirectX Abstraction Layer

I read about creating graphics abstraction layers to make it convenient to switch between graphics platforms. Unfortunately, I could not find many details on this. Is this abstraction functionally achievable with something like this?

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

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

}

How it works? Is there a better way? Can someone point me to some examples of things that this or more example code does?

+5
source share
4 answers

It is usually done in such a way as to have an interface with a “universal” visualization tool:

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.

+7

, . API, , . , GDI , , .

, , HDC. .

, API, . API (DirectX OpenGL) , , .

, . .

, OpenGL , A, B, - , DX , func B, func A. , - :

void functionA(...) {
  if (OpenGL) {
    glFuncA();
  } else {
    //store parameters
  }
}

void functionB(...) {
  if (OpenGL) {
    glFuncB();
  } else {
    dxFuncB();
    dxFuncA( saved params );
  }
}

, . API , .

+4

. , "" . "renderer", OpenGL Direct3D.

- , Ogre3d.

+3

mhutch 100%. , , , , , .

API , , if (this_platform) { do_this(); } else { do_that(); }, .

- , , HOW. , , , (IMHO) API, , , , .

, , , . , , - ( , ..) . , instancing, , API- ", " " " .

+1

All Articles