luca-piccioni / OpenGL.Net

Modern OpenGL bindings for C#.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Gl.Get return IntPtr?

ForeverZer0 opened this issue · comments

This is an enhancement request, not a bug report.

Was wondering if it would be possible to expose a public version of the the Get method to simply return an IntPtr (and/or even an unsafe pointer variation). This would eliminate the need for casting to custom data types.

For example, I commonly use System.Numerics, and find using the Matrix4x4 structure more intuitive than working with an array of floats. As it is (as far as I can see), I am forced to get it as an array first, then if I would like it as a Matrix4x4, either create a whole new structure and use the array values (bad), or pin the float and Marshal a Matrix4x4 from its address (still bad).

I think it would be far more intuitive simply to get a pointer and Marshal to it directly, skipping the middle conversions and copies.

Ideally, I would like to see someday the inclusion of System.Numerics directly in the API (I think it would break support for older framework versions at this point in time), but would be very happy to at least see the inclusion of simply getting a pointer, and be able to Marshal data in any way I see fit.

Was wondering if it would be possible to expose a public version of the Get method to simply return an IntPtr (and/or even an unsafe pointer variation).

Yes, it is possible; the code generator supports unsafe and generic overloads.

	public static unsafe void Get(GetPName pname, [Out] float* data);
           
	public static void GetFloat<T>(GetPName pname, out T data) where T : struct;

So I can write:

Matrix4x4f mv = Matrix4x4f.Identity * Matrix4x4f.Scaled(2.0f, 2.0f, 2.0f);
		
Gl.LoadMatrixf(mv);
Gl.GetFloat(GetPName.ModelviewMatrix, out mv);

Or, if you prefer Numerics:

System.Numerics.Matrix4x4 mv = System.Numerics.Matrix4x4.CreateFromYawPitchRoll(_Angle, 0.45f, 2.0f);

Gl.LoadMatrixf(mv);
Gl.GetFloat(GetPName.ModelviewMatrix, out mv);

However, I cannot find a good reason to support glGet commands, except when using deprecated enumerations (you setup program uniforms with glUniformMatrix*, isn't?). Am I missing something? However, since other deprecated function (i.e. glLoadMatrixf) already have generic overloads, I'll add unsafe and generic overloads even to glGet commands.

Be aware of #98. I cannot reproduce the issue -.-, everything is working correctly on my machine (TM).

Ideally, I would like to see someday the inclusion of System.Numerics directly in the API

This will not happen. OpenGL.Net is decoupled by all data structures not directly related to the OpenGL & Co. API. Personally, I use OpenGL.Net.Math and support Numerics from there; instead, you can prefer anything you like.

Ah, great, thanks for the reply. Excuse my naivety, haven't fully explored the API and somehow missed the generic overload.

As for the System.Numerics, have not yet checked out OpenGL.Net.Math, am guessing I probably should instead of using System.Numerics. I really don't care have no reason to use System.Numerics if the math library supports vectors, matrices, etc. That was the bottom-line request I suppose, not necessarily actual "System.Numerics", but the integrated types for vectors, etc, as opposed to arrays of floats, which are not as intuitive, and didn't want to have to reinvent the wheel just to use them.

Sorry about the noobiness, recently started learning OpenGL, and just graduated from old-school fixed pipeline to vertex buffers, etc, to give you clue of my inexperience, lol.

Anyways, thanks again, greatly appreciated!

Don't worry. The wiki should cover the essential for starting with this library.