Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MonoTouch OpenTK and UniformMatrix4

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.

like image 713
Brad Robinson Avatar asked Dec 04 '11 11:12

Brad Robinson


2 Answers

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.

like image 86
Brad Robinson Avatar answered Nov 09 '22 16:11

Brad Robinson


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.

like image 29
Robert Rouhani Avatar answered Nov 09 '22 17:11

Robert Rouhani