# What would you normally do if you have 2 sets of APIs to call upon?

## Recommended Posts

lucky6969b    1330

If I have both OpenGL and DirectX

do you do something like this

#if OPENGL

glMatrix GetViewMatrix()

#else

D3DXMATRIX GetViewMatrix()

#endif

{

}

if I have hundreds of these methods/functions, it would be quite tedious to code.

What is the general practices of this?

Thanks

Jack

##### Share on other sites
kunos    2254

my preferred way to do it is to have 2 static libs implementing the abstractions. The build system will link the appropriate lib for the platform.

##### Share on other sites
Pink Horror    2459
For this example, you would probably want a typedef or a struct containing the matrix. You should be able to have a basic math header that defines inline functions amd typedefs for their arguments and return values.

##### Share on other sites
Hodgman    51220
What he said ^
//write once:
#if OPENGL
typedef glMatrix GfxMatrix;
#else
typedef D3DXMATRIX GfxMatrix;
#endif
...
//many uses:
GfxMatrix GetViewMatrix();

But in this particular case, just use the same matrix structure in both versions of the code. If you need conversion functions internally, then write them.
struct GfxMatrix {};

D3DXMATRIX Cast( const GfxMatrix& ) {...}
glMatrix   Cast( const GfxMatrix& ) {...}

//or: this may be valid if you can ensure your class and the one you're mapping to use the exact same memory layout
const D3DXMATRIX& Cast( const GfxMatrix& mat ) { return *(D3DXMATRIX*)&mat; }
However, neither D3D or GL require you to use some specific matrix type. They both accept float[16] at the lowest level... You're talking about using some "glMatrix" type (not part of OpenGL, what is this library?) and also using D3DX. You  can just pick one of those libraries and use it for both GL and D3D! Edited by Hodgman

##### Share on other sites
pinebanana    479

However, neither D3D or GL require you to use some specific matrix type. They both accept float[16] at the lowest level... You're talking about using some "glMatrix" type (not part of OpenGL, what is this library?) and also using D3DX. You can just pick one of those libraries and use it for both GL and D3D!

Going further with that, you should use a pre-built math library to help you that is cross-platform in order for it to be used with GL and D3D. I recommend glm.