Thomas
Thomas

Reputation: 6196

Interfacing an existing API with new classes

Consider the OpenTK library which wraps opengl for c#. It comes with built in matrix classes that the API expects you to use. For example, the overloads in this method.

    public static void UniformMatrix4(int location, bool transpose, ref Matrix4 matrix);
    public static void UniformMatrix4(int location, bool transpose, ref Matrix4d matrix);
    public static void UniformMatrix4(int location, int count, bool transpose, double* value);
    public static void UniformMatrix4(int location, int count, bool transpose, double[] value);
    public static void UniformMatrix4(int location, int count, bool transpose, float* value);
    public static void UniformMatrix4(int location, int count, bool transpose, float[] value);
    public static void UniformMatrix4(int location, int count, bool transpose, ref double value);
    public static void UniformMatrix4(int location, int count, bool transpose, ref float value);

All of the API follows the same form. As an alternative to using the built in matrix class, I can use use a float pointer or float array, etc.

Now, I don't want to use OpenTK's matrix class or any other of their math structs, but I do want to use their API. What options do I have with interfacing my own Matrix class to the api, that is both clean and efficient.

The internal storage of my matrix class does not use a float[] but rather float fields. I don't think there is a way to extract a float array for passing to the API without inefficiently copying the fields into a new array.

Upvotes: 1

Views: 68

Answers (1)

Mark Shevchenko
Mark Shevchenko

Reputation: 8207

There is an unsafe possibility to use an low-level pointer's operations in C# (see details), so you can get the address of the first float element, and you can pass it to OpenTK's functions.

I think, you could use "fixed arrays" (see same link), if you can re-implement your own Matrix class.

Upvotes: 1

Related Questions