Sign in to follow this  
lucky6969b

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this