I'm currently writing a library that abstracts 3D rendering and uses SharpDX and OpenTK as graphics libraries. The problem is that OpenTK and SharpDX have their own implementations of Vector2, Matrix, Quaternion and so on.

I came up with 3 possible ways to deal with this.

  1. My API has to have function overloads for both math types. If I want to extend the basic rendering code with more higher level features (like a Transform class that handles position, rotation and scale) I need to either favor one over the other or provide code for both math types which is not optimal.

  2. Roll my own math type that allows conversions between the SharpDX and OpenTK types. Not optimal either since the math code was already written twice I shouldn't invent the wheel again.

  3. Flat out use only one on the API level and convert only if needed. With this approach I would bind myself to one library which means if I only ever want to use the OpenTK renderer but the API is using the SharpDX math types I still need to have the SharpDX library around without ever using the "graphics" part of it.

I feel like option 2. would result in the nicest usage code but is the most work required. I'm looking for possible solutions to this problem other then the 3 options I have mentioned above.

  • 1
    "Best" is subjective - could you edit your question to describe what you goals are, what you have tried so far, etc? Also, since this is cross-posted, please delete the question at GameDev.SE. – user22815 Oct 27 '15 at 2:00
  • 1
    My first thought is to use Microsoft's implementation System.Numerics.Vectors and convert to the other representations as needed. – CodesInChaos Oct 27 '15 at 8:24

You could implement your "higher level features" in terms of abstract interfaces like IVector2, IMatrix, IQuaternion, and maybe some abstract factories like IVector2Factory. Then you provide two adapters for each of that interfaces one based on SharpDX types, and one on OpenTK types. That way, you do not bind your new classes to the one or the other library.

Of course, on some level you have to make a decision which of the two libs or adapters you are going to use in your final program.


You could use yet another vector library, instead of writing your own. Then convert to the library specific representations where needed.

Microsoft is working on an implementation called System.Numerics.Vectors which can even get SIMD acceleration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.