MonoTouch OpenTK and UniformMatrix4

2019-04-30 09:46发布

I'm trying to pass an OpenTK Matrix4 to a shader uniform, but there doesn't seem to be a suitable overload for GL.UniformMatrix4. The overloads accept either float or float[] or ref float. Similarly I can't find a way to convert a Matrix4 instance to a float array - I've seen one sample that uses a ToArray method on the Matrix4, but that doesn't seem to be present in the distribution I'm using.

Sure I'm missing something simple as this is pretty fundamental to being able to pass a model/view/projection matrix to a shader.

I'm using the version of OpenTK shipping with the lastest version of MonoTouch.

2条回答
放我归山
2楼-- · 2019-04-30 10:16

Based on the iphone tag, I'm assuming you're using OpenTK.Graphics.ES20.GL and not the regular desktop OpenTK.Graphics.OpenGL.GL. The OpenGL ES bindings are not as thorough as the standard desktop bindings, which contain overloads for vectors and matrices.

The method you wrote is almost exactly the same as the method the actual OpenGL bindings, only that one passes the Matrix4 as ref, because passing a Matrix4 by value is slow.

And just FYI, it is possible to guarantee the order of fields in a struct in C# using StructLayoutAttribute with a LayoutKind of Sequential, and the OpenTK math structs do just that.

查看更多
Root(大扎)
3楼-- · 2019-04-30 10:33

This helper function works, but seems like a hack.

Basically it's just passing the address of Row0,Col0. Since C# makes no guarantees about the order of fields in a structure though, theoretically it's working by luck more than anything.

public static void UniformMatrix4(int location, Matrix4 value)
{
    GL.UniformMatrix4(location, 1, false, ref value.Row0.X);
}

Surely OpenTK should have bindings to allowing passing a Matrix4 directly.

查看更多
登录 后发表回答